Sub-Adapters 4

Preview and test each sub adapter.

Hop - Optimism (hop-optimism)

Hop - Arbitrum One (hop-arbitrum-one)

Hop - Polygon (hop-polygon)

Hop - Gnosis Chain (hop-gnosis-chain)

Adapter Code

Check the entire code written for the Adapter.

Source code

Showing TS source.
1export const name = 'Hop Bridge';
2export const version = '0.1.2';
3export const license = 'MIT';
4
5interface Token {
6  stablecoin?: boolean
7  coingecko?: string
8  decimals?: number
9}
10
11const tokens: { [id: string]: Token } = {
12  DAI: {
13    stablecoin: true,
14  },
15  ETH: {
16    coingecko: 'ethereum',
17  },
18  HOP: {
19    coingecko: 'hop-protocol',
20  },
21  MATIC: {
22    coingecko: 'matic-network',
23  },
24  SNX: {
25    coingecko: 'havven',
26  },
27  USDC: {
28    stablecoin: true,
29    decimals: 6,
30  },
31  USDT: {
32    stablecoin: true,
33    decimals: 6,
34  },
35  WBTC: {
36    coingecko: 'bitcoin',
37    decimals: 6,
38  },
39  sUSD: {
40    stablecoin: true,
41  },
42}
43
44const chains = [
45  {
46    id: 'optimism',
47  },
48  {
49    id: 'arbitrum-one',
50    name: 'Arbitrum One',
51    hopChain: 'arbitrum',
52  },
53  {
54    id: 'polygon',
55  },
56  {
57    id: 'gnosis-chain',
58    hopChain: 'gnosis',
59    name: 'Gnosis Chain',
60  },
61]
62
63export function setup(sdk: Context) {
64  let configPromise: Promise<any> | null = null;
65  const getConfig = () => {
66    if (!configPromise) {
67      configPromise = sdk.http.get('https://assets.hop.exchange/mainnet/v1-core-config.json');
68    }
69    return configPromise;
70  }
71
72  const getPrice = async (id: string): Promise<number> => {
73    const token = tokens[id];
74
75    if (!token) {
76      sdk.log.warn(`Token ${id} not found`);
77      return 0;
78    }
79    if (token.stablecoin) {
80      return 1;
81    }
82
83    const price = await sdk.defiLlama.getCurrentPrice('coingecko', token.coingecko);
84    return price;
85  }
86
87  const getHTokenValue = async (chain: string, hopChain?: string) => {
88    const config = await getConfig();
89
90    const amounts = await Promise.all(
91      Object.entries(config.bridges).map(async ([token, chains]) => {
92        if (chains[hopChain || chain]) {
93          const [supply, price] = await Promise.all([
94            sdk.ethers.getERC20Contract(chains[hopChain || chain].l2HopBridgeToken, chain).totalSupply(),
95            getPrice(token),
96          ]);
97          const decimals = tokens[token] ? tokens[token].decimals || 18 : 0;
98          return supply * price / (10 ** decimals);
99        }
100        return 0;
101      })
102    );
103
104    const sum = amounts.reduce((a, b) => a + b, 0);
105    return sum;
106  }
107
108  const metadata = {
109    name: 'Hop',
110    icon: sdk.ipfs.getDataURILoader('Qma6xqAk9BksRvPZvTtJZs1BrPaAxAEyeY7ufWvAmteKso', 'image/svg+xml'),
111    category: 'native',
112  };
113
114  sdk.registerBundle('hop', metadata);
115
116  for (const chain of chains) {
117    sdk.register({
118      id: `hop-${chain.id}`,
119      bundle: 'hop',
120      queries: {
121        currentValueBridgedAToB: () => getHTokenValue(chain.id, chain.hopChain),
122        currentValueBridgedBToA: async () => 0,
123      },
124      metadata: {
125        ...metadata,
126        subtitle: chain.name || `${chain.id.charAt(0).toUpperCase()}${chain.id.substr(1)}`,
127
128        chainA: 'ethereum',
129        chainB: chain.id,
130      },
131    });
132  }
133}
134

It's something off?

Report it to the discussion board on Discord, we will take care of it.

Adapter Info

Version

0.1.2

License

MIT

IPFS CID

QmdsMLC4q9Wyf6jbWXrfhrhUpNqyAAGiT4rHjjtnY3vJvx

CID (source)

QmeugGgzGfsffUjujHCc9zvuq7a7A4oyoXWbHcoMAFzcbP

Collections

Author

mihal.eth