@avalabs/bridge-unified 2.1.1 → 3.0.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.
- package/LICENSE +9 -0
- package/README.md +115 -31
- package/dist/index.cjs +36 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +252 -84
- package/dist/index.d.ts +252 -84
- package/dist/index.js +9 -4
- package/dist/index.js.map +1 -1
- package/package.json +14 -4
- package/.turbo/turbo-build.log +0 -22
- package/.turbo/turbo-lint.log +0 -4
- package/.turbo/turbo-test.log +0 -26
- package/CHANGELOG.md +0 -37
- package/jest.config.js +0 -9
- package/src/bridges/cctp/__mocks__/asset.mock.ts +0 -15
- package/src/bridges/cctp/__mocks__/bridge-transfer.mock.ts +0 -48
- package/src/bridges/cctp/__mocks__/chain.mocks.ts +0 -33
- package/src/bridges/cctp/__mocks__/config.mock.ts +0 -45
- package/src/bridges/cctp/abis/erc20.ts +0 -117
- package/src/bridges/cctp/abis/message-transmitter.ts +0 -318
- package/src/bridges/cctp/abis/token-router.ts +0 -843
- package/src/bridges/cctp/factory.test.ts +0 -73
- package/src/bridges/cctp/factory.ts +0 -36
- package/src/bridges/cctp/handlers/estimate-gas.test.ts +0 -110
- package/src/bridges/cctp/handlers/estimate-gas.ts +0 -58
- package/src/bridges/cctp/handlers/get-assets.test.ts +0 -47
- package/src/bridges/cctp/handlers/get-assets.ts +0 -27
- package/src/bridges/cctp/handlers/get-fees.test.ts +0 -61
- package/src/bridges/cctp/handlers/get-fees.ts +0 -26
- package/src/bridges/cctp/handlers/track-transfer.test.ts +0 -779
- package/src/bridges/cctp/handlers/track-transfer.ts +0 -365
- package/src/bridges/cctp/handlers/transfer-asset.test.ts +0 -429
- package/src/bridges/cctp/handlers/transfer-asset.ts +0 -179
- package/src/bridges/cctp/index.ts +0 -1
- package/src/bridges/cctp/types/chain.ts +0 -9
- package/src/bridges/cctp/types/config.ts +0 -20
- package/src/bridges/cctp/utils/build-tx.ts +0 -30
- package/src/bridges/cctp/utils/config.test.ts +0 -49
- package/src/bridges/cctp/utils/config.ts +0 -36
- package/src/bridges/cctp/utils/transfer-data.test.ts +0 -83
- package/src/bridges/cctp/utils/transfer-data.ts +0 -48
- package/src/errors/bridge-error.ts +0 -11
- package/src/errors/bridge-initialization-error.ts +0 -9
- package/src/errors/bridge-unavailable-error.ts +0 -9
- package/src/errors/index.ts +0 -4
- package/src/errors/invalid-params-error.ts +0 -9
- package/src/index.ts +0 -3
- package/src/types/asset.ts +0 -26
- package/src/types/bridge.ts +0 -64
- package/src/types/chain.ts +0 -10
- package/src/types/config.ts +0 -10
- package/src/types/environment.ts +0 -4
- package/src/types/error.ts +0 -19
- package/src/types/index.ts +0 -9
- package/src/types/provider.ts +0 -12
- package/src/types/signer.ts +0 -18
- package/src/types/transfer.ts +0 -35
- package/src/unified-bridge-service.test.ts +0 -209
- package/src/unified-bridge-service.ts +0 -97
- package/src/utils/bridge-types.test.ts +0 -103
- package/src/utils/bridge-types.ts +0 -32
- package/src/utils/caip2.test.ts +0 -44
- package/src/utils/caip2.ts +0 -41
- package/src/utils/client.test.ts +0 -97
- package/src/utils/client.ts +0 -44
- package/src/utils/ensure-config.test.ts +0 -43
- package/src/utils/ensure-config.ts +0 -12
- package/src/utils/index.ts +0 -2
- package/src/utils/network-fee.test.ts +0 -24
- package/src/utils/network-fee.ts +0 -6
- package/src/utils/retry-promise.test.ts +0 -115
- package/src/utils/retry-promise.ts +0 -72
- package/src/utils/wait.test.ts +0 -33
- package/src/utils/wait.ts +0 -4
- package/tsconfig.jest.json +0 -7
- package/tsconfig.json +0 -9
- package/tsup.config.ts +0 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Copyright (C) 2021, Ava Labs, Inc. All rights reserved.
|
|
2
|
+
|
|
3
|
+
Subject to the limited license below (**”License””), you may not, and you may not permit anyone else to, copy, reproduce, aggregate, republish, download, post, distribute, license, sublicense, reverse engineer, modify, or create derivative works based on this software (collectively, **“Software”**).
|
|
4
|
+
|
|
5
|
+
You are hereby granted a limited, non-exclusive, non-sublicensable and non-transferable license to download and use the Software as-is solely (i) for use in connection with the Avalanche Public Blockchain platform, having a NetworkID of 1 (Mainnet) or 5 (Fuji), and associated blockchains, comprised exclusively of the Avalanche X-Chain, C-Chain, P-Chain and any subnets linked to the P-Chain (**“Avalanche Authorized Platform”**) or (ii) for non-production, testing or research purposes without any commercial application within the Avalanche ecosystem (**“Non-Commercial Use”**); provided that, in each case, you may not use or allow use of the Software (a) in connection with any forks of the Avalanche Authorized Platform, (b) in any manner not operationally connected to the Avalanche Authorized Platform other than for Non-Commercial Use, or (c) to the extent the number of monthly active users or the number of total installs of any software that uses the Software across all versions thereof exceeds 10,000 at any time. You may not modify or alter the Software in any way.
|
|
6
|
+
|
|
7
|
+
You hereby acknowledge and agree to the terms set forth at www.avalabs.org/important-notice.
|
|
8
|
+
|
|
9
|
+
**TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, AND AVA LABS EXPRESSLY DISCLAIMS AND EXCLUDES ALL REPRESENTATIONS, WARRANTIES AND OTHER TERMS AND CONDITIONS, WHETHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION BY OPERATION OF LAW OR BY CUSTOM, STATUTE OR OTHERWISE, AND INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTY, TERM, OR CONDITION OF NON-INFRINGEMENT, MERCHANTABILITY, TITLE, OR FITNESS FOR PARTICULAR PURPOSE. YOU USE THE SOFTWARE AT YOUR OWN RISK. AVA LABS EXPRESSLY DISCLAIMS ALL LIABILITY (INCLUDING FOR ALL DIRECT, CONSEQUENTIAL OR OTHER DAMAGES OR LOSSES) RELATED TO ANY USE OF THE SOFTWARE.**
|
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ The bridging ecosystem is complex. There are often multiple tools that can be us
|
|
|
16
16
|
|
|
17
17
|
These are the bridges we currently support:
|
|
18
18
|
|
|
19
|
-
- **CCTP** - preferred for
|
|
19
|
+
- **CCTP** - preferred for bridging USDC between Ethereum and Avalanche C-Chain. See the `bridges/cctp` folder.
|
|
20
20
|
|
|
21
21
|
Future bridges we plan to support:
|
|
22
22
|
|
|
@@ -37,16 +37,75 @@ Future bridges we plan to support:
|
|
|
37
37
|
## Usage
|
|
38
38
|
|
|
39
39
|
```js
|
|
40
|
-
import { createUnifiedBridgeService, Environment, BridgeTransfer } from '@avalabs/bridge-unified';
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
import { createUnifiedBridgeService, getEnabledBridgeServices, Environment, BridgeTransfer } from '@avalabs/bridge-unified';
|
|
41
|
+
|
|
42
|
+
const environment = Environment.TEST;
|
|
43
|
+
const evmSigner: EvmSigner = {
|
|
44
|
+
sign: async ({ data, from, to, value }) => {
|
|
45
|
+
return await window.ethereum.request({
|
|
46
|
+
method: 'eth_sendTransaction,',
|
|
47
|
+
params:{
|
|
48
|
+
account: from,
|
|
49
|
+
data: data ?? undefined,
|
|
50
|
+
from,
|
|
51
|
+
to: to ?? null,
|
|
52
|
+
chain: undefined,
|
|
53
|
+
value
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const btcSigner: BtcSigner = {
|
|
60
|
+
sign: async ({ inputs, outputs }) => {
|
|
61
|
+
return await window.ethereum.request({
|
|
62
|
+
method: 'bitcoin_signTransaction',
|
|
63
|
+
params: { inputs, outputs },
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const cctpInitializer: EvmBridgeInitializer = {
|
|
69
|
+
type: BridgeType.CCTP,
|
|
70
|
+
signer: evmSigner,
|
|
71
|
+
};
|
|
72
|
+
const icttErc20Initializer: EvmBridgeInitializer = {
|
|
73
|
+
type: BridgeType.ICTT_ERC20_ERC20,
|
|
74
|
+
signer: evmSigner,
|
|
75
|
+
};
|
|
76
|
+
const avalancheEvmInitializer: EvmBridgeInitializer = {
|
|
77
|
+
type: BridgeType.AVALANCHE_EVM,
|
|
78
|
+
signer: evmSigner,
|
|
79
|
+
};
|
|
80
|
+
const avalancheBtcInitializer: AvaToBtcBridgeInitializer = {
|
|
81
|
+
type: BridgeType.AVALANCHE_AVA_BTC,
|
|
82
|
+
signer: evmSigner,
|
|
83
|
+
bitcoinFunctions: bitcoinProvider as BitcoinFunctions,
|
|
84
|
+
};
|
|
85
|
+
const bitcoinAvaInitializer: BtcToAvaBridgeInitializer = {
|
|
86
|
+
type: BridgeType.AVALANCHE_BTC_AVA,
|
|
87
|
+
signer: btcSigner,
|
|
88
|
+
bitcoinFunctions: bitcoinProvider as BitcoinFunctions,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// use all available bridges
|
|
92
|
+
const enabledBridgeInitializers: BridgeInitializer[] = [
|
|
93
|
+
cctpInitializer,
|
|
94
|
+
icttErc20Initializer,
|
|
95
|
+
avalancheEvmInitializer,
|
|
96
|
+
avalancheBtcInitializer,
|
|
97
|
+
bitcoinAvaInitializer,
|
|
98
|
+
]
|
|
99
|
+
|
|
100
|
+
// fetch all available bridge services
|
|
101
|
+
const enabledBridgeServices = await getEnabledBridgeServices(environment, disabledBridgeTypes);
|
|
102
|
+
|
|
103
|
+
// create a new service for a given environment and list of bridge services
|
|
43
104
|
const unifiedService = createUnifiedBridgeService({
|
|
44
|
-
environment
|
|
105
|
+
environment,
|
|
106
|
+
enabledBridgeServices
|
|
45
107
|
});
|
|
46
108
|
|
|
47
|
-
// init the service, fetch and setup its configs
|
|
48
|
-
await unifiedService.init();
|
|
49
|
-
|
|
50
109
|
// get the list of supported assets, grouped by caip2 (https://chainagnostic.org/CAIPs/caip-2) chain IDs
|
|
51
110
|
const assets = await unifiedService.getAssets()
|
|
52
111
|
|
|
@@ -64,7 +123,7 @@ const updateListener = (transfer: BridgeTransfer) => {
|
|
|
64
123
|
// start tracking the transfer's state. whenever the state changes, it will call the provided `updateListener`
|
|
65
124
|
const { cancel, result } = await unifiedService.trackTransfer({bridgeTransfer, updateListener, ...})
|
|
66
125
|
|
|
67
|
-
//
|
|
126
|
+
// immediately stops tracking and rejects the tracker's promise
|
|
68
127
|
// cancel()
|
|
69
128
|
|
|
70
129
|
// wait for the transfer to finish and get its final state
|
|
@@ -73,9 +132,11 @@ const finalizedBridgeTransfer = await result
|
|
|
73
132
|
|
|
74
133
|
## API
|
|
75
134
|
|
|
76
|
-
###
|
|
135
|
+
### getEnabledBridgeServices(environment, enabledBridgeInitializers);
|
|
77
136
|
|
|
78
|
-
|
|
137
|
+
Type: `(environment: Environment, enabledBridgeInitializers: BridgeInitializer[]) => Promise<BridgeServicesMap>`
|
|
138
|
+
|
|
139
|
+
Returns all available bridge services for a given environment (excluding disabledBridgeTypes). Any bridge service which fails to initialize will be absent from this returned value.
|
|
79
140
|
|
|
80
141
|
#### environment
|
|
81
142
|
|
|
@@ -83,11 +144,21 @@ Type: `Environment`
|
|
|
83
144
|
|
|
84
145
|
Defines if the bridge service should use `testnet` or `mainnet`.
|
|
85
146
|
|
|
86
|
-
####
|
|
147
|
+
#### enabledBridgeInitializers
|
|
148
|
+
|
|
149
|
+
Type: `BridgeInitializer[]`
|
|
150
|
+
|
|
151
|
+
Enables the integration of the provided `BridgeType`s based on each `BridgeInitializer`.
|
|
152
|
+
|
|
153
|
+
#### enabledBridgeServices
|
|
154
|
+
|
|
155
|
+
Type: `BridgeServicesMap` => `Map<BridgeType, BridgeService>`
|
|
87
156
|
|
|
88
|
-
|
|
157
|
+
This includes all the bridge services which were initialized successfully, to pass to `createUnifiedBridgeService`.
|
|
89
158
|
|
|
90
|
-
|
|
159
|
+
### createUnifiedBridgeService({ environment, enabledBridgeServices })
|
|
160
|
+
|
|
161
|
+
Returns a new `unifiedBridgeService` for the given `environment` and `enabledBridgeServices` map.
|
|
91
162
|
|
|
92
163
|
### unifiedBridgeService
|
|
93
164
|
|
|
@@ -97,28 +168,15 @@ Automatically picks the right (enabled) bridge integration to use based on the p
|
|
|
97
168
|
```js
|
|
98
169
|
{
|
|
99
170
|
environment, // the provided Environment during initialization
|
|
100
|
-
bridges, // the list of enabled bridge integrations
|
|
101
|
-
init,
|
|
102
|
-
updateConfigs,
|
|
103
171
|
getAssets,
|
|
104
172
|
getFees,
|
|
173
|
+
estimateGas,
|
|
174
|
+
canTransferAsset,
|
|
105
175
|
transferAsset,
|
|
106
176
|
trackTransfer,
|
|
107
177
|
}
|
|
108
178
|
```
|
|
109
179
|
|
|
110
|
-
#### init
|
|
111
|
-
|
|
112
|
-
Type: `() => Promise<void>`
|
|
113
|
-
|
|
114
|
-
Initializes the unified service by attempting to fetch the configurations of the enabled bridges.
|
|
115
|
-
|
|
116
|
-
#### updateConfigs
|
|
117
|
-
|
|
118
|
-
Type: `() => Promise<void>`
|
|
119
|
-
|
|
120
|
-
Attempts to fetch the configurations of the enabled bridges.
|
|
121
|
-
|
|
122
180
|
#### getAssets
|
|
123
181
|
|
|
124
182
|
Type: `() => Promise<ChainAssetMap>`
|
|
@@ -131,14 +189,40 @@ Type: `(params: FeeParams) => Promise<AssetFeeMap>`
|
|
|
131
189
|
|
|
132
190
|
Calculates and returns the bridge fees in `tokenAddress` - `amount` pairs for a given bridge transfer.
|
|
133
191
|
|
|
192
|
+
#### estimateGas
|
|
193
|
+
|
|
194
|
+
Type: `(params: TransferParams) => Promise<bigint>`
|
|
195
|
+
|
|
196
|
+
Estimates the gas cost of a specific transfer.
|
|
197
|
+
|
|
198
|
+
#### getMinimumTransferAmount
|
|
199
|
+
|
|
200
|
+
Type: `(params: FeeParams) => Promise<bigint>`
|
|
201
|
+
|
|
202
|
+
Calculates and returns the minimum transfer amount for a given bridge transfer.
|
|
203
|
+
|
|
134
204
|
#### transferAsset
|
|
135
205
|
|
|
136
206
|
Type: `(params: TransferParams) => Promise<BridgeTransfer>`
|
|
137
207
|
|
|
138
208
|
Starts a new bridge transfer, executing every required step in a single call.
|
|
139
|
-
Transactions signing is done by
|
|
209
|
+
Transactions signing is done by a custom `sign` callback. The custom `sign` implementation may use their own solution or the default `dispatch` callback to submit the transaction to the network.
|
|
140
210
|
Returns a `BridgeTransfer` containing all the (known) initial values such as: environment, addresses, amount, fee, transaction hash, required and actual block confirmation counts, etc.
|
|
141
211
|
|
|
212
|
+
Notes about TransferParams:
|
|
213
|
+
fromAddress: The address where the bridge amount is from.
|
|
214
|
+
|
|
215
|
+
toAddress: The address where the bridge amount is going to end up.
|
|
216
|
+
|
|
217
|
+
For example, A user has an account with AddressC and AddressBtc.
|
|
218
|
+
The user wants to bridge some funds from Ethereum to Avalanche using the same address. FromAddress and toAddress will be both AddressC.
|
|
219
|
+
|
|
220
|
+
The user wants to bridge some funds from Bitcoin to Avalanche.
|
|
221
|
+
FromAddress is AddressBtc and toAddress is AddressC.
|
|
222
|
+
|
|
223
|
+
Some bridges allows you to bridge the tokens to a different address. (CCTP and ICTT ERC20).
|
|
224
|
+
In this case, fromAddress is the address of the token is getting bridged from. And toAddress is the address which is going to receive the bridged funds.
|
|
225
|
+
|
|
142
226
|
#### trackTransfer
|
|
143
227
|
|
|
144
228
|
Type: `(params: TrackingParams) => ({cancel, result})`
|
|
@@ -149,7 +233,7 @@ Tracks the given `BridgeTransfer`'s progress and invokes the provided listener c
|
|
|
149
233
|
|
|
150
234
|
Type: `() => void`
|
|
151
235
|
|
|
152
|
-
If it's still pending, rejects the tracker's promise (`result`)
|
|
236
|
+
If it's still pending, rejects the tracker's promise (`result`) immediately and breaks its loop under the hood.
|
|
153
237
|
|
|
154
238
|
###### result
|
|
155
239
|
|