@gibs/bridge-sdk 1.6.36 → 1.7.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gibs/bridge-sdk",
3
- "version": "1.6.36",
3
+ "version": "1.7.2",
4
4
  "main": "index.js",
5
5
  "access": "public",
6
6
  "type": "module",
@@ -12,7 +12,8 @@
12
12
  "./image-links": "./src/image-links.ts",
13
13
  "./chains": "./src/chains.ts",
14
14
  "./chain-info": "./src/chain-info.ts",
15
- "./fee-type": "./src/fee-type.ts"
15
+ "./fee-type": "./src/fee-type.ts",
16
+ "./settings": "./src/settings.ts"
16
17
  },
17
18
  "scripts": {
18
19
  "build": "tsc"
package/src/abis.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { parseAbi, parseAbiParameters } from 'viem'
2
2
 
3
- /** the abi for the bridge */
3
+ /** the abi for the input bridge */
4
4
  export const inputBridge = parseAbi([
5
5
  'function minPerTx(address token) external view returns(uint256)',
6
6
  'function foreignTokenAddress(address token) external view returns(address)',
@@ -11,47 +11,59 @@ export const inputBridge = parseAbi([
11
11
  'function relayTokensAndCall(address token, address _receiver, uint256 _value, bytes memory _data) external',
12
12
  'function relayTokens(address token, address _receiver, uint256 _value) external',
13
13
  ])
14
+ /** the abi for the input bridge with extra input for the sender origin (tokensex) */
14
15
  export const inputBridgeExtraInput = parseAbi([
15
16
  'function relayTokensAndCall(address token, address _receiver, uint256 _value, bytes memory _data, address _senderOrigin) external',
16
17
  'function relayTokens(address token, address _receiver, uint256 _value, address _senderOrigin) external',
17
18
  ])
19
+ /** the abi for erc677 tokens */
18
20
  export const erc677 = parseAbi([
19
21
  'function transferAndCall(address, uint256, bytes calldata) external',
20
22
  ])
23
+ /** the abi for erc677 tokens bridge with extra input for the sender origin (tokensex) */
21
24
  export const erc677ExtraInput = parseAbi([
22
25
  'function transferAndCall(address, uint256, bytes calldata, address sender) external',
23
26
  ])
27
+ /** the abi to read data from the output router */
24
28
  export const outputRouter = parseAbi([
25
29
  'function onTokenBridged(address _token, uint256 _value, bytes memory _data) external payable',
26
30
  'function WETH() external view returns(address)',
27
31
  'function bridge() external view returns(address)',
28
32
  ])
33
+ /** the abi for the output bridge */
29
34
  export const outputBridge = parseAbi(['function bridgeContract() external view returns(address)'])
35
+ /** the abi for the fee manager */
30
36
  export const feeManager = parseAbi([
31
37
  'function HOME_TO_FOREIGN_FEE() public view returns(bytes32)',
32
38
  'function FOREIGN_TO_HOME_FEE() public view returns(bytes32)',
33
39
  'function getFee(bytes32, address) public view returns(uint256)',
34
40
  ])
41
+ /** the abi for the native router */
35
42
  export const nativeRouter = parseAbi([
36
43
  'function wrapAndRelayTokens(address receiver) external payable',
37
44
  'function relayTokensAndCall(address receiver, bytes calldata data) external payable',
38
45
  ])
46
+ /** the abi for the native router with extra input for the sender origin (tokensex) */
39
47
  export const nativeRouterExtraInput = parseAbi([
40
48
  'function wrapAndRelayTokens(address receiver, address sender) external payable',
41
49
  'function relayTokensAndCall(address receiver, bytes calldata data, address sender) external payable',
42
50
  ])
51
+ /** the abi for the univ2 router */
43
52
  export const univ2Router = parseAbi([
44
53
  'function getAmountsOut(uint256 amountIn, address[] calldata path) external view returns(uint256[] memory)',
45
54
  ])
55
+ /** the abi for the fee delivery struct */
46
56
  export const feeDeliveryStruct = parseAbiParameters('(address, uint256, uint256, uint256)')
47
-
57
+ /** the abi for the relay tokens direct */
48
58
  export const relayTokensDirect = parseAbi([
49
59
  'function safeExecuteSignaturesWithAutoGasLimit(bytes calldata data, bytes calldata signatures) external',
50
60
  ])
61
+ /** the abi for the home amb */
51
62
  export const homeAmb = parseAbi([
52
63
  'function submitSignature(bytes message, bytes signature) external',
53
64
  ])
54
65
 
66
+ /** the abi for the univ2 pair */
55
67
  export const pair = parseAbi([
56
68
  'function getReserves() view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast)',
57
69
  ])
package/src/chain-info.ts CHANGED
@@ -20,6 +20,14 @@ export type TokenBridgeInfo = {
20
20
  }
21
21
  }
22
22
 
23
+ /**
24
+ * Returns the bridged token address and native token address
25
+ * @param chainId - the chain id being targeted
26
+ * @param target - the target
27
+ * @param address - the address
28
+ * @param client - the client
29
+ * @returns the bridged token address and native token address
30
+ */
23
31
  export const links = _.memoize(
24
32
  async ({ chainId, target, address, client }: { chainId: number; target: Hex; address: Hex, client: PublicClient }) => {
25
33
  return multicallRead<Hex[]>({
@@ -36,6 +44,15 @@ export const links = _.memoize(
36
44
  ({ chainId, target, address }) => `${chainId}-${target}-${address}`.toLowerCase(),
37
45
  )
38
46
 
47
+ /**
48
+ * Returns the token bridge info for the given bridge key, asset in, is prod, from chain client, and to chain client
49
+ * @param bridgeKey - the bridge key
50
+ * @param assetIn - the asset in
51
+ * @param isProd - whether to use the production pathway
52
+ * @param fromChainClient - the from chain client
53
+ * @param toChainClient - the to chain client
54
+ * @returns the token bridge info
55
+ */
39
56
  export const tokenBridgeInfo = async ({
40
57
  bridgeKey,
41
58
  assetIn,
@@ -49,7 +66,7 @@ export const tokenBridgeInfo = async ({
49
66
  fromChainClient: PublicClient,
50
67
  toChainClient: PublicClient,
51
68
  }): Promise<null | TokenBridgeInfo> => {
52
- const bridgePathway = pathway(bridgeKey, isProd)
69
+ const bridgePathway = pathway(bridgeKey, isProd, assetIn?.address as Hex | null)
53
70
  if (!assetIn || !bridgePathway) {
54
71
  console.log('missing asset in or bridge pathway')
55
72
  return null
@@ -72,28 +89,23 @@ export const tokenBridgeInfo = async ({
72
89
  }),
73
90
  ])
74
91
  const [toBridged, toNative] = toMappings
75
- // const notGas = !Object.values(nativeAssetOut).find(
76
- // (v) => v.toLowerCase() === assetInAddress.toLowerCase(),
77
- // )
78
92
  if (toBridged !== zeroAddress) {
79
- // if (notGas) console.log('toBridged')
80
93
  return {
81
94
  originationChainId: fromChain,
82
95
  assetInAddress: assetInAddress as Hex,
83
96
  assetOutAddress: toBridged,
84
- toForeign: {
85
- foreign: toBridged,
86
- home: assetInAddress as Hex,
97
+ toHome: {
98
+ home: toBridged,
99
+ foreign: assetInAddress as Hex,
87
100
  },
88
101
  }
89
102
  }
90
103
  if (toNative !== zeroAddress) {
91
- // if (notGas) console.log('toNative')
92
104
  return {
93
105
  originationChainId: toChain,
94
106
  assetInAddress: assetInAddress as Hex,
95
107
  assetOutAddress: toNative,
96
- toHome: {
108
+ toForeign: {
97
109
  home: toNative,
98
110
  foreign: assetInAddress as Hex,
99
111
  },
@@ -101,7 +113,6 @@ export const tokenBridgeInfo = async ({
101
113
  }
102
114
  const [fromBridged, fromNative] = fromMappings
103
115
  if (fromNative !== zeroAddress) {
104
- // if (notGas) console.log('fromNative')
105
116
  return {
106
117
  originationChainId: toChain,
107
118
  assetInAddress: assetInAddress as Hex,
@@ -113,7 +124,6 @@ export const tokenBridgeInfo = async ({
113
124
  }
114
125
  }
115
126
  if (fromBridged !== zeroAddress) {
116
- // if (notGas) console.log('fromBridged')
117
127
  return {
118
128
  originationChainId: fromChain,
119
129
  assetInAddress: assetInAddress as Hex,
@@ -144,9 +154,10 @@ export const tokenBridgeInfo = async ({
144
154
  }),
145
155
  }
146
156
  }
157
+ /** The result of reading the amount out from a router */
147
158
  export type FetchResult = bigint[] | Hex
148
159
  /**
149
- * check the prices that each router offers for the given paths
160
+ * Check the prices that each router offers for the given paths
150
161
  * @param assetInAddress the address of the asset going into the bridge
151
162
  * @param oneTokenInt the number of tokens to push into the bridge (before fees)
152
163
  * @param chain the chain of the asset going into the bridge
@@ -184,10 +195,19 @@ const readAmountOut = ({
184
195
  })
185
196
  }
186
197
 
198
+ /**
199
+ * Fetch the price corrective for the given bridge key, asset out, measurement token, from chain client, to chain client, asset link, amount in, and is prod
200
+ * @param bridgeKeyPartner - the bridge key partner
201
+ * @param assetOut - the asset out
202
+ * @param measurementToken - the measurement token
203
+ * @param fromChainClient - the from chain client
204
+ * @param toChainClient - the to chain client
205
+ * @param assetLink - the asset link
206
+ * @param amountIn - the amount in
207
+ * @param isProd - whether to use the production pathway
208
+ * @returns the price corrective
209
+ */
187
210
  export const fetchPriceCorrective = ({
188
- // bridgeKey,
189
- // assetIn,
190
- // isProd,
191
211
  bridgeKeyPartner,
192
212
  assetOut,
193
213
  measurementToken,
@@ -298,6 +318,14 @@ export const fetchPriceCorrective = ({
298
318
  }
299
319
  }
300
320
 
321
+ /**
322
+ * Fetch the minimum bridge amount in for the given asset in, pathway, from public client, and to public client
323
+ * @param assetIn - the asset in
324
+ * @param pathway - the pathway
325
+ * @param fromPublicClient - the from public client
326
+ * @param toPublicClient - the to public client
327
+ * @returns the minimum bridge amount in
328
+ */
301
329
  export const minBridgeAmountIn = async ({
302
330
  assetIn,
303
331
  pathway,
@@ -318,22 +346,28 @@ export const minBridgeAmountIn = async ({
318
346
  })
319
347
  }
320
348
  /** get the symbol of the native asset, unwrap it if it needs to be */
321
- export const nativeSymbol = (asset: { address: Hex, symbol: string, chainId: number } | null, unwrap = false) => {
349
+ export const nativeSymbol = (asset: { address: string, symbol: string, chainId: number } | null, unwrap = false) => {
322
350
  return asset ? (unwrap && asset.address === nativeAssetOut[toChain(asset.chainId)] ? asset.symbol.slice(1) : asset.symbol) : ''
323
351
  }
324
352
 
353
+ /** The input for loading the bridge fees */
325
354
  export type InputLoadFeeFor = {
326
355
  pathway: Pathway
327
356
  fromChainClient: PublicClient
328
357
  toChainClient: PublicClient
329
358
  }
330
359
 
360
+ /** The fee configuration for a given pathway */
331
361
  export type PathwayExtendableConfig = {
332
362
  feeManager: Hex
333
363
  feeH2F: bigint
334
364
  feeF2H: bigint
335
365
  }
336
-
366
+ /**
367
+ * Returns the chain multicall for the given client
368
+ * @param client - the client
369
+ * @returns the chain multicall
370
+ */
337
371
  export const chainMulticall = (client: PublicClient) => {
338
372
  const chainId = client.chain!.id
339
373
  const metadata = chainsMetadata[toChain(chainId)]
@@ -343,7 +377,13 @@ export const chainMulticall = (client: PublicClient) => {
343
377
  address: metadata.contracts!.multicall3!.address,
344
378
  }) as any
345
379
  }
346
-
380
+ /**
381
+ * Load the bridge fees for the given pathway, from chain client, and to chain client
382
+ * @param pathway - the pathway
383
+ * @param fromChainClient - the from chain client
384
+ * @param toChainClient - the to chain client
385
+ * @returns the bridge fees
386
+ */
347
387
  export const loadBridgeFees = async ({ pathway, fromChainClient, toChainClient }: InputLoadFeeFor) => {
348
388
  const multicall = pathway.feeManager === 'from'
349
389
  ? chainMulticall(fromChainClient)
package/src/config.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { zeroAddress, type Hex } from "viem";
1
+ import { getAddress, zeroAddress, type Hex } from "viem";
2
2
  import _ from 'lodash'
3
3
  import * as imageLinks from '@gibs/bridge-sdk/image-links'
4
4
  import type { BridgeKey, Token, TokenOut } from "./types.js";
@@ -16,21 +16,35 @@ export enum Chains {
16
16
  // TBNB = '0x61',
17
17
  }
18
18
 
19
+ /** The key of a chain - human readable for query params */
19
20
  export type ChainKey = keyof typeof Chains
20
21
 
22
+ /** A map of chain ids to chain keys */
21
23
  export const chainIdToKey = new Map<Chains, ChainKey>(
22
24
  Object.entries(Chains).map(([key, chain]) => [chain, key] as [Chains, ChainKey]),
23
25
  )
24
26
 
27
+ /**
28
+ * Converts a chain id to a chain key
29
+ * @param chainId - the chain id
30
+ * @returns the chain key
31
+ */
25
32
  export const toChain = (chainId: number | string) => `0x${Number(chainId).toString(16)}` as Chains
26
33
 
34
+ /**
35
+ * Converts a chain id to a chain key
36
+ * @param chainId - the chain id
37
+ * @returns the chain key
38
+ */
27
39
  export const toChainKey = (chainId: number | string) => chainIdToKey.get(toChain(chainId))
28
40
 
41
+ /** The provider of an omnibridge */
29
42
  export enum Provider {
30
43
  PULSECHAIN = 'pulsechain',
31
44
  TOKENSEX = 'tokensex',
32
45
  }
33
46
 
47
+ /** A map of chains to native asset out addresses */
34
48
  export const nativeAssetOut = {
35
49
  [Chains.ETH]: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
36
50
  [Chains.BNB]: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
@@ -39,6 +53,12 @@ export const nativeAssetOut = {
39
53
  [Chains.V4PLS]: '0x70499adEBB11Efd915E3b69E700c331778628707',
40
54
  } as Record<Chains, Hex>
41
55
 
56
+ /** A map of chains to deprecated native asset out addresses */
57
+ export const deprecatedNativeAssetOut = {
58
+ [Chains.PLS]: new Set(['0x97Ac4a2439A47c07ad535bb1188c989dae755341']),
59
+ } as Partial<Record<Chains, Set<Hex>>>
60
+
61
+ /** A map of chains to uni v2 router addresses */
42
62
  export const uniV2Routers = {
43
63
  [Chains.PLS]: [
44
64
  '0x98bf93ebf5c380C0e6Ae8e192A7e2AE08edAcc02',
@@ -53,6 +73,7 @@ export const uniV2Routers = {
53
73
  ],
54
74
  } as Record<Chains, Hex[]>
55
75
 
76
+ /** A map of chains to native token symbols */
56
77
  export const nativeTokenSymbol = {
57
78
  [Chains.PLS]: 'PLS',
58
79
  [Chains.V4PLS]: 'V4PLS',
@@ -61,6 +82,7 @@ export const nativeTokenSymbol = {
61
82
  [Chains.BNB]: 'BNB',
62
83
  } as Record<Chains, string>
63
84
 
85
+ /** A map of chains to native token names */
64
86
  export const nativeTokenName = {
65
87
  [Chains.PLS]: 'Pulse',
66
88
  [Chains.V4PLS]: 'V4 Pulse',
@@ -69,24 +91,36 @@ export const nativeTokenName = {
69
91
  [Chains.BNB]: 'BNB',
70
92
  } as Record<Chains, string>
71
93
 
94
+ /** The pathway of a bridge */
72
95
  export type Pathway = {
73
96
  from: Hex
74
97
  to: Hex
75
98
  destinationRouter: Hex | null
76
99
  nativeRouter: Hex
77
100
  validator: Hex
78
- defaultAssetIn: Token
101
+ // defaultAssetIn: Token
79
102
  usesExtraParam: boolean
80
103
  feeManager: 'from' | 'to'
81
104
  toHome: boolean
82
105
  requiresDelivery: boolean
106
+ bridgedNativeAssetOut: Hex
107
+ settingOverrides?: Record<Hex, Partial<Pathway>>
83
108
  }
84
109
 
110
+ /** A map of providers to pathways */
85
111
  export type DeepPathwayConfig = Record<
86
112
  Provider,
87
113
  Partial<Record<Chains, Partial<Record<Chains, Pathway>>>>
88
114
  >
89
115
 
116
+ /** The settings for the pulsechain pls eth pathway */
117
+ const pulsechainPLSETHSettings = {
118
+ from: '0x0e18d0d556b652794EF12Bf68B2dC857EF5f3996',
119
+ to: '0xe20E337DB2a00b1C37139c873B92a0AAd3F468bF',
120
+ destinationRouter: '0x1F0144Ce3BDaf11944Fe0beF6444599a0500695B',
121
+ nativeRouter: '0xE66877Cfe4CEc10ffBbBB3092B7001321AB5809D',
122
+ }
123
+
90
124
  export const pathways = {
91
125
  [Provider.PULSECHAIN]: {
92
126
  [Chains.PLS]: {
@@ -100,20 +134,11 @@ export const pathways = {
100
134
  feeManager: 'from',
101
135
  toHome: false,
102
136
  requiresDelivery: true,
103
- defaultAssetIn: {
104
- symbol: 'WETH',
105
- name: 'Wrapped Ether from Ethereum',
106
- address: '0x02DcdD04e3F455D838cd1249292C58f3B79e3C3C',
107
- decimals: 18,
108
- chainId: 369,
109
- logoURI: imageLinks.network(369),
110
- extensions: {
111
- bridgeInfo: {
112
- '1': {
113
- tokenAddress: nativeAssetOut[Chains.ETH],
114
- },
115
- },
116
- },
137
+ // weth from ethereum on pulsechain
138
+ bridgedNativeAssetOut: '0x02DcdD04e3F455D838cd1249292C58f3B79e3C3C',
139
+ settingOverrides: {
140
+ [zeroAddress]: pulsechainPLSETHSettings,
141
+ [getAddress(nativeAssetOut[Chains.PLS])]: pulsechainPLSETHSettings,
117
142
  },
118
143
  },
119
144
  },
@@ -128,19 +153,14 @@ export const pathways = {
128
153
  usesExtraParam: false,
129
154
  toHome: true,
130
155
  requiresDelivery: false,
131
- defaultAssetIn: {
132
- chainId: 1,
133
- address: nativeAssetOut[Chains.ETH],
134
- name: 'Wrapped Ether',
135
- symbol: 'WETH',
136
- decimals: 18,
137
- logoURI: imageLinks.network(1),
138
- extensions: {
139
- bridgeInfo: {
140
- '369': {
141
- tokenAddress: '0x02DcdD04e3F455D838cd1249292C58f3B79e3C3C',
142
- },
143
- },
156
+ // wpls from pulsechain on ethereum
157
+ bridgedNativeAssetOut: '0xA882606494D86804B5514E07e6Bd2D6a6eE6d68A',
158
+ settingOverrides: {
159
+ [getAddress('0xa882606494d86804b5514e07e6bd2d6a6ee6d68a')]: {
160
+ from: '0xe20E337DB2a00b1C37139c873B92a0AAd3F468bF',
161
+ to: '0x0e18d0d556b652794EF12Bf68B2dC857EF5f3996',
162
+ destinationRouter: '0xE66877Cfe4CEc10ffBbBB3092B7001321AB5809D',
163
+ nativeRouter: '0x1F0144Ce3BDaf11944Fe0beF6444599a0500695B',
144
164
  },
145
165
  },
146
166
  },
@@ -158,21 +178,8 @@ export const pathways = {
158
178
  usesExtraParam: true,
159
179
  toHome: false,
160
180
  requiresDelivery: true,
161
- defaultAssetIn: {
162
- symbol: 'WBNB',
163
- name: 'Wrapped BNB',
164
- address: '0x518076CCE3729eF1a3877EA3647a26e278e764FE',
165
- decimals: 18,
166
- chainId: 369,
167
- logoURI: imageLinks.network(369),
168
- extensions: {
169
- bridgeInfo: {
170
- '56': {
171
- tokenAddress: nativeAssetOut[Chains.BNB],
172
- },
173
- },
174
- },
175
- },
181
+ // wbnb from bsc on pulsechain (tokensex)
182
+ bridgedNativeAssetOut: '0x518076CCE3729eF1a3877EA3647a26e278e764FE',
176
183
  },
177
184
  },
178
185
  [Chains.BNB]: {
@@ -186,21 +193,8 @@ export const pathways = {
186
193
  usesExtraParam: true,
187
194
  toHome: true,
188
195
  requiresDelivery: false,
189
- defaultAssetIn: {
190
- chainId: 56,
191
- address: nativeAssetOut[Chains.BNB],
192
- name: 'Wrapped BNB from BSC',
193
- symbol: 'WBNB',
194
- decimals: 18,
195
- logoURI: imageLinks.network(56),
196
- extensions: {
197
- bridgeInfo: {
198
- '369': {
199
- tokenAddress: '0x518076CCE3729eF1a3877EA3647a26e278e764FE',
200
- },
201
- },
202
- },
203
- },
196
+ // wpls from pulsechain on bsc (tokensex)
197
+ bridgedNativeAssetOut: '0xF6088134D28eeBEF7128BA41FaDb2FCA0666c64C',
204
198
  },
205
199
  },
206
200
  },
@@ -242,21 +236,7 @@ export const testnetPathways = {
242
236
  requiresDelivery: true,
243
237
  toHome: false,
244
238
  feeManager: 'from',
245
- defaultAssetIn: {
246
- chainId: 943,
247
- address: '0x3677bd78CCf4d299328ECFBa61790cf8dBfcF686',
248
- name: 'Wrapped Ether from Sepolia',
249
- symbol: 'WsepETH',
250
- decimals: 18,
251
- logoURI: imageLinks.network(943),
252
- extensions: {
253
- bridgeInfo: {
254
- '11155111': {
255
- tokenAddress: nativeAssetOut[Chains.SEP],
256
- },
257
- },
258
- },
259
- },
239
+ bridgedNativeAssetOut: '0x3677bd78ccf4d299328ecfba61790cf8dbfcf686',
260
240
  },
261
241
  },
262
242
  [Chains.SEP]: {
@@ -270,26 +250,13 @@ export const testnetPathways = {
270
250
  requiresDelivery: false,
271
251
  toHome: true,
272
252
  feeManager: 'to',
273
- defaultAssetIn: {
274
- chainId: 11_155_111,
275
- address: nativeAssetOut[Chains.SEP],
276
- name: 'Wrapped Ether',
277
- symbol: 'sepWETH',
278
- decimals: 18,
279
- logoURI: imageLinks.network(11_155_111),
280
- extensions: {
281
- bridgeInfo: {
282
- '943': {
283
- tokenAddress: '0x3677bd78CCf4d299328ECFBa61790cf8dBfcF686',
284
- },
285
- },
286
- },
287
- },
253
+ bridgedNativeAssetOut: '0x35807560aD0597E23F452cdc82D4Fb0e7E3c6590',
288
254
  },
289
255
  },
290
256
  },
291
257
  } as DeepPathwayConfig
292
258
 
259
+ /** The mainnet bridge keys */
293
260
  const mainnetBridgeKeys = [
294
261
  [Provider.PULSECHAIN, Chains.PLS, Chains.ETH],
295
262
  [Provider.PULSECHAIN, Chains.ETH, Chains.PLS],
@@ -297,16 +264,26 @@ const mainnetBridgeKeys = [
297
264
  [Provider.TOKENSEX, Chains.BNB, Chains.PLS],
298
265
  ] as BridgeKey[]
299
266
 
267
+ /** The testnet bridge keys */
300
268
  const testnetBridgeKeys = [
301
269
  [Provider.PULSECHAIN, Chains.SEP, Chains.V4PLS],
302
270
  [Provider.PULSECHAIN, Chains.V4PLS, Chains.SEP],
303
271
  ] as BridgeKey[]
304
272
 
273
+ /** Returns the valid bridge keys for the given is prod */
305
274
  export const validBridgeKeys = (isProd: boolean) => [
306
275
  ...mainnetBridgeKeys,
307
276
  ...(isProd ? [] : testnetBridgeKeys),
308
277
  ]
309
278
 
279
+ /**
280
+ * Infers the bridge key from the current key, provider, from chain, and to chain
281
+ * @param currentKey - the current bridge key
282
+ * @param provider - the provider
283
+ * @param fromChain - the from chain
284
+ * @param toChain - the to chain
285
+ * @returns the inferred bridge key
286
+ */
310
287
  export const inferBridgeKey = ({
311
288
  currentKey,
312
289
  provider,
@@ -332,38 +309,85 @@ export const inferBridgeKey = ({
332
309
  return [provider, nextFromChain, nextToChain] as BridgeKey
333
310
  }
334
311
 
335
- export const pathway = (bridgeKey: BridgeKey | null, isProd: boolean) => {
312
+ /**
313
+ * Returns the pathway for the given bridge key, is prod, and asset in address
314
+ * @param bridgeKey - the bridge key
315
+ * @param isProd - whether to use the production pathway
316
+ * @param assetInAddress - the asset in address
317
+ * @returns the pathway
318
+ */
319
+ export const pathway = (bridgeKey: BridgeKey | null, isProd: boolean, assetInAddress?: string | Hex | null | undefined) => {
336
320
  if (!bridgeKey) return
337
- return _.get(pathways, bridgeKey) || (!isProd ? _.get(testnetPathways, bridgeKey) : undefined)
321
+ let pathway = _.get(pathways, bridgeKey) || (!isProd ? _.get(testnetPathways, bridgeKey) : undefined)
322
+ if (assetInAddress) {
323
+ const addr = getAddress(assetInAddress)
324
+ if (pathway?.settingOverrides?.[addr]) {
325
+ pathway = {
326
+ ...pathway,
327
+ ...pathway.settingOverrides[addr],
328
+ }
329
+ }
330
+ }
331
+ return pathway
338
332
  }
339
333
 
334
+ /**
335
+ * Returns the default asset in for the given bridge key and is prod
336
+ * @param bridgeKey - the bridge key
337
+ * @param isProd - whether to use the production pathway
338
+ * @returns the default asset in
339
+ */
340
340
  export const defaultAssetIn = ($bridgeKey: BridgeKey | null, isProd: boolean) => {
341
+ if (!$bridgeKey) return null
341
342
  const conf = pathway($bridgeKey, isProd)
342
- const defaultAssetIn = _.get(conf, ['defaultAssetIn']) as Token | undefined
343
+ const defaultAssetIn = _.get(conf, ['bridgedNativeAssetOut']) as Hex | undefined
343
344
  return defaultAssetIn
344
345
  }
345
346
 
347
+ /**
348
+ * Returns whether the given asset is the native asset for the given bridge key
349
+ * @param asset - the asset
350
+ * @param bridgeKey - the bridge key
351
+ * @returns whether the given asset is the native asset for the given bridge key
352
+ */
346
353
  export const isNative = (asset: Token | TokenOut | null, bridgeKey: BridgeKey | null) => {
347
354
  if (!bridgeKey || !asset) {
348
355
  return false
349
356
  }
357
+ const path = pathway(bridgeKey, false)
350
358
  return (
359
+ getAddress(asset.address!) === getAddress(path!.bridgedNativeAssetOut!) ||
351
360
  (asset.address === zeroAddress ||
352
361
  nativeAssetOut[toChain(asset.chainId)]?.toLowerCase() === asset.address?.toLowerCase()) &&
353
362
  !asset.name.includes(' from Pulsechain')
354
363
  )
355
364
  }
356
365
 
366
+ /**
367
+ * Returns whether the given asset is unwrappable for the given bridge key
368
+ * @param bridgeKey - the bridge key
369
+ * @param assetIn - the asset in
370
+ * @returns whether the given asset is unwrappable for the given bridge key
371
+ */
357
372
  export const isUnwrappable = (
358
- asset: Pick<Token, 'extensions'> | null,
359
373
  bridgeKey: BridgeKey | null,
374
+ assetIn: Pick<Token, 'address'> | null,
360
375
  ) => {
361
- if (!bridgeKey || !asset) {
376
+ if (!bridgeKey || !assetIn) {
362
377
  return false
363
378
  }
364
- const [, , toChain] = bridgeKey
365
- return nativeAssetOut[toChain] === asset.extensions?.bridgeInfo?.[Number(toChain)]?.tokenAddress
379
+ const addr = getAddress(assetIn.address)
380
+ const path = pathway(bridgeKey, false)
381
+ if (!path) return false
382
+ return path.bridgedNativeAssetOut === getAddress(addr)
366
383
  }
367
384
 
368
- export const canChangeUnwrap = (bridgeKey: BridgeKey, assetIn: Token | null) =>
369
- !!assetIn && isUnwrappable(assetIn, bridgeKey)
385
+ /**
386
+ * Returns whether the given asset can be unwrapped for the given bridge key
387
+ * @param bridgeKey - the bridge key
388
+ * @param assetIn - the asset in
389
+ * @returns boolean
390
+ */
391
+ export const canChangeUnwrap = (bridgeKey: BridgeKey, assetIn: Token | null) => {
392
+ return !!assetIn && isUnwrappable(bridgeKey, assetIn)
393
+ }
package/src/fee-type.ts CHANGED
@@ -1,12 +1,14 @@
1
-
1
+ /** The type of fee to use for the bridge */
2
2
  export enum FeeType {
3
3
  PERCENT = '%',
4
4
  GAS_TIP = 'gas+%',
5
5
  FIXED = 'fixed',
6
6
  }
7
7
 
8
+ /** The keys of the fee type */
8
9
  export type FeeTypeKeys = keyof typeof FeeType
9
10
 
11
+ /** A map of fee type values to keys */
10
12
  export const feeTypeValToKeyMap = new Map<FeeType, FeeTypeKeys>(
11
13
  (Object.keys(FeeType) as FeeTypeKeys[]).map((key) => [FeeType[key], key]),
12
14
  )