@uniswap/token-list-bridge-utils 1.1.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ import { MappingProvider } from './MappingProvider'
2
+ import { BnbMappedTokenData, GenericMappedTokenData } from '../constants/types'
3
+ import bnbmappings from '../local_mappings/bnb.json'
4
+
5
+ // barring a better external source, we use a local source of mappings for bnb
6
+ export class BnbMappingProvider implements MappingProvider {
7
+ async provide(): Promise<BnbMappedTokenData> {
8
+ const tokens = bnbmappings as BnbMappedTokenData
9
+ return tokens
10
+ }
11
+ }
@@ -1,7 +1,11 @@
1
- import { GenericMappedTokenData, PolygonMappedTokenData } from '../constants/types'
1
+ import {
2
+ BnbMappedTokenData,
3
+ GenericMappedTokenData,
4
+ PolygonMappedTokenData,
5
+ } from '../constants/types'
2
6
 
3
7
  export interface MappingProvider {
4
8
  provide(): Promise<
5
- PolygonMappedTokenData | GenericMappedTokenData
9
+ PolygonMappedTokenData | GenericMappedTokenData | BnbMappedTokenData
6
10
  >
7
11
  }
@@ -3,6 +3,7 @@ import { Contract } from 'web3-eth-contract'
3
3
  import { ArbitrumMappingProvider } from './ArbitrumMappingProvider'
4
4
  import { OptimismMappingProvider } from './OptimismMappingProvider'
5
5
  import { PolygonMappingProvider } from './PolygonMappingProvider'
6
+ import { BnbMappingProvider } from './BnbMappingProvider'
6
7
  import { TokenInfo, TokenList } from '@uniswap/token-lists'
7
8
  import { ethers } from 'ethers'
8
9
  import {
@@ -16,6 +17,8 @@ import Web3 from 'web3'
16
17
  import {
17
18
  GenericMappedTokenData,
18
19
  PolygonMappedTokenData,
20
+ BnbMappedTokenData,
21
+ BnbMappedToken,
19
22
  } from '../constants/types'
20
23
 
21
24
  const web3 = new Web3()
@@ -25,6 +28,7 @@ const SUPPORTED_L2_CHAINS = [
25
28
  ChainId.ARBITRUM_ONE,
26
29
  ChainId.POLYGON,
27
30
  ChainId.OPTIMISM,
31
+ ChainId.BNB,
28
32
  ]
29
33
 
30
34
  export async function buildList(
@@ -42,6 +46,7 @@ export async function buildList(
42
46
  [key: number]: {
43
47
  childTokenValid: boolean
44
48
  childTokenAddress: string | undefined
49
+ decimals?: number
45
50
  }
46
51
  } = {}
47
52
  const l2MappingExtension = {
@@ -86,6 +91,9 @@ export async function buildList(
86
91
  } as unknown as TokenInfo)
87
92
  : ({
88
93
  ...l1Token,
94
+ decimals:
95
+ chainIdToChildTokenDetailsMap[chainId].decimals ??
96
+ l1Token.decimals,
89
97
  chainId: chainId,
90
98
  address:
91
99
  chainIdToChildTokenDetailsMap[chainId].childTokenAddress,
@@ -136,6 +144,8 @@ function getMappingProvider(chainId: ChainId, l1TokenList: TokenList) {
136
144
  return new OptimismMappingProvider()
137
145
  case ChainId.POLYGON:
138
146
  return new PolygonMappingProvider()
147
+ case ChainId.BNB:
148
+ return new BnbMappingProvider()
139
149
  default:
140
150
  throw new Error(`Chain ${chainId} not supported for fetching mappings.`)
141
151
  }
@@ -146,7 +156,10 @@ async function generateTokenMappings(
146
156
  l1TokenList: TokenList
147
157
  ) {
148
158
  const chainIdToMappingsMap: {
149
- [key: number]: PolygonMappedTokenData | GenericMappedTokenData
159
+ [key: number]:
160
+ | PolygonMappedTokenData
161
+ | GenericMappedTokenData
162
+ | BnbMappedTokenData
150
163
  } = {}
151
164
 
152
165
  for (const chainId of chainIds) {
@@ -166,11 +179,15 @@ async function getChildTokenDetails(
166
179
  l1Token: TokenInfo,
167
180
  chainId: ChainId,
168
181
  chainIdToMappingsMap: {
169
- [key: number]: PolygonMappedTokenData | GenericMappedTokenData
182
+ [key: number]:
183
+ | PolygonMappedTokenData
184
+ | GenericMappedTokenData
185
+ | BnbMappedTokenData
170
186
  }
171
187
  ): Promise<{
172
188
  childTokenValid: boolean
173
189
  childTokenAddress: string | undefined
190
+ decimals?: number | undefined
174
191
  }> {
175
192
  const existingMapping: undefined | string =
176
193
  l1Token?.extensions?.bridgeInfo?.[chainId]?.tokenAddress
@@ -189,9 +206,14 @@ async function getChildTokenDetails(
189
206
  childTokenAddress &&
190
207
  (await hasExistingTokenContract(childTokenAddress, chainId))
191
208
  )
209
+ const decimals =
210
+ chainId === ChainId.BNB
211
+ ? (childToken as BnbMappedToken).decimals
212
+ : undefined
192
213
  return {
193
214
  childTokenValid: childTokenValid,
194
215
  childTokenAddress: childTokenAddress,
216
+ decimals: decimals,
195
217
  }
196
218
  }
197
219
  return {
package/src/utils.ts CHANGED
@@ -108,6 +108,8 @@ export function getRpcUrl(chainId: ChainId): string {
108
108
  return 'https://polygon-rpc.com/'
109
109
  case ChainId.POLYGON_MUMBAI:
110
110
  return 'https://rpc-endpoints.superfluid.dev/mumbai'
111
+ case ChainId.BNB:
112
+ return 'https://bsc-dataseed1.binance.org'
111
113
  default:
112
114
  }
113
115
  throw new Error('Unsupported ChainId')