Sub-Adapters 3
Preview and test each sub adapter.
Polkadot - Polkadot Relay Chain-Moonbeam (polkadot-polkadot-moonbeam)
Polkadot - Polkadot Relay Chain-Acala (polkadot-polkadot-acala)
Polkadot - Moonbeam-Acala (polkadot-moonbeam-acala)
Adapter Code
Check the entire code written for the Adapter.
Source code
Showing TS source.
1export const name = 'Polkadot Cross-Chain Bridge';
2export const version = '0.0.2';
3export const license = 'MIT';
4
5interface Asset {
6 id: string
7 nativeChain: string
8 wrappersByChain: { [chain: string]: { address: string, decimals: number } }
9 stablecoin?: boolean
10 coingeckoId?: string
11}
12
13interface Chain {
14 id: string
15 name: string
16}
17
18const assets: Asset[] = [
19 {
20 id: 'dot',
21 nativeChain: 'polkadot',
22 coingeckoId: 'polkadot',
23 wrappersByChain: {
24 moonbeam: {
25 address: '0xffffffff1fcacbd218edc0eba20fc2308c778080',
26 decimals: 10,
27 },
28 acala: {
29 address: '0x0000000000000000000100000000000000000002',
30 decimals: 10,
31 },
32 },
33 },
34 {
35 id: 'aca',
36 nativeChain: 'acala',
37 coingeckoId: 'acala',
38 wrappersByChain: {
39 moonbeam: {
40 address: '0xffffffffa922fef94566104a6e5a35a4fcddaa9f',
41 decimals: 12,
42 },
43 },
44 },
45 {
46 id: 'ausd',
47 nativeChain: 'acala',
48 stablecoin: true,
49 wrappersByChain: {
50 moonbeam: {
51 address: '0xffffffff52c56a9257bb97f4b2b6f7b2d624ecda',
52 decimals: 12,
53 },
54 },
55 },
56];
57
58const chains: Chain[] = [
59 {
60 id: 'polkadot',
61 name: 'Polkadot Relay Chain',
62 },
63 {
64 id: 'moonbeam',
65 name: 'Moonbeam'
66 },
67 {
68 id: 'acala',
69 name: 'Acala',
70 },
71]
72
73export function setup(sdk: Context) {
74 sdk.ethers.addProvider('moonbeam', 'https://rpc.api.moonbeam.network');
75 sdk.ethers.addProvider('acala', 'https://eth-rpc-acala.aca-api.network');
76
77 const getEVMSupply = (chain: string) => async (addr: string) => {
78 const supply = await sdk.ethers.getERC20Contract(addr, chain).totalSupply();
79 return parseInt(supply.toString());
80 }
81
82 const getSupply: { [chain: string]: (addr: string) => Promise<number> } = {
83 moonbeam: getEVMSupply('moonbeam'),
84 acala: getEVMSupply('acala'),
85 }
86
87 const getBridged = async (chainA: string, chainB: string) => {
88 let total = 0;
89 for (const asset of assets) {
90 if (asset.nativeChain === chainA && asset.wrappersByChain[chainB]) {
91 const [wrapperSupply, price] = await Promise.all([
92 getSupply[chainB](asset.wrappersByChain[chainB].address),
93 asset.stablecoin ? Promise.resolve(1) : sdk.coinGecko.getCurrentPrice(asset.coingeckoId),
94 ]);
95 const wrapperSupplyDecimal = wrapperSupply / (10 ** asset.wrappersByChain[chainB].decimals);
96 const bridgedValue = wrapperSupplyDecimal * price;
97 total += bridgedValue;
98 }
99 }
100 return total;
101 }
102
103 sdk.registerBundle('polkadot', {
104 name: 'Polkadot Cross-Chain Bridge',
105 icon: sdk.ipfs.getDataURILoader('Qmc2A56wLVhqcxeKC3ueje88d9YhEBWgiNXAz32to8yu5g', 'image/svg+xml'),
106 category: 'unknown',
107 });
108
109 for (let i = 0; i < chains.length; i += 1) {
110 for (let j = i + 1; j < chains.length; j += 1) {
111 const chainA = chains[i];
112 const chainB = chains[j];
113
114 sdk.register({
115 id: `polkadot-${chainA.id}-${chainB.id}`,
116 bundle: 'polkadot',
117 queries: {
118 currentValueBridgedAToB: () => getBridged(chainA.id, chainB.id),
119 currentValueBridgedBToA: () => getBridged(chainB.id, chainA.id),
120 },
121 metadata: {
122 name: `Polkadot`,
123 icon: sdk.ipfs.getDataURILoader('Qmc2A56wLVhqcxeKC3ueje88d9YhEBWgiNXAz32to8yu5g', 'image/svg+xml'),
124 subtitle: `${chainA.name}-${chainB.name}`,
125 chainA: chainA.id,
126 chainAName: chainA.name,
127 chainB: chainB.id,
128 chainBName: chainB.name,
129 },
130 })
131 }
132 }
133}
134
It's something off?
Report it to the discussion board on Discord, we will take care of it.