@avalabs/bridge-unified 1.0.1 → 2.0.1

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.
Files changed (76) hide show
  1. package/.turbo/turbo-build.log +10 -10
  2. package/.turbo/turbo-lint.log +1 -1
  3. package/.turbo/turbo-test.log +25 -0
  4. package/CHANGELOG.md +18 -0
  5. package/README.md +137 -71
  6. package/dist/index.cjs +11 -3
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.cts +207 -34
  9. package/dist/index.d.ts +207 -34
  10. package/dist/index.js +4 -3
  11. package/dist/index.js.map +1 -1
  12. package/jest.config.js +9 -0
  13. package/package.json +15 -9
  14. package/src/bridges/cctp/__mocks__/asset.mock.ts +15 -0
  15. package/src/bridges/cctp/__mocks__/bridge-transfer.mock.ts +48 -0
  16. package/src/bridges/cctp/__mocks__/chain.mocks.ts +31 -0
  17. package/src/bridges/cctp/__mocks__/config.mock.ts +45 -0
  18. package/src/bridges/cctp/abis/erc20.ts +117 -0
  19. package/src/bridges/cctp/abis/message-transmitter.ts +318 -0
  20. package/src/bridges/cctp/abis/token-router.ts +843 -0
  21. package/src/bridges/cctp/factory.test.ts +73 -0
  22. package/src/bridges/cctp/factory.ts +32 -0
  23. package/src/bridges/cctp/handlers/get-assets.test.ts +47 -0
  24. package/src/bridges/cctp/handlers/get-assets.ts +27 -0
  25. package/src/bridges/cctp/handlers/get-fees.test.ts +61 -0
  26. package/src/bridges/cctp/handlers/get-fees.ts +26 -0
  27. package/src/bridges/cctp/handlers/track-transfer.test.ts +779 -0
  28. package/src/bridges/cctp/handlers/track-transfer.ts +365 -0
  29. package/src/bridges/cctp/handlers/transfer-asset.test.ts +429 -0
  30. package/src/bridges/cctp/handlers/transfer-asset.ts +179 -0
  31. package/src/bridges/cctp/index.ts +1 -0
  32. package/src/bridges/cctp/types/chain.ts +4 -0
  33. package/src/bridges/cctp/types/config.ts +19 -0
  34. package/src/bridges/cctp/utils/config.test.ts +49 -0
  35. package/src/bridges/cctp/utils/config.ts +36 -0
  36. package/src/bridges/cctp/utils/transfer-data.test.ts +83 -0
  37. package/src/bridges/cctp/utils/transfer-data.ts +48 -0
  38. package/src/errors/bridge-error.ts +11 -0
  39. package/src/errors/bridge-initialization-error.ts +9 -0
  40. package/src/errors/bridge-unavailable-error.ts +9 -0
  41. package/src/errors/index.ts +4 -20
  42. package/src/errors/invalid-params-error.ts +9 -0
  43. package/src/index.ts +3 -1
  44. package/src/types/asset.ts +26 -0
  45. package/src/types/bridge.ts +63 -0
  46. package/src/types/chain.ts +10 -0
  47. package/src/types/config.ts +10 -0
  48. package/src/types/environment.ts +4 -0
  49. package/src/types/error.ts +19 -0
  50. package/src/types/index.ts +9 -0
  51. package/src/types/provider.ts +12 -0
  52. package/src/types/signer.ts +18 -0
  53. package/src/types/transfer.ts +35 -0
  54. package/src/unified-bridge-service.test.ts +208 -0
  55. package/src/unified-bridge-service.ts +90 -0
  56. package/src/utils/bridge-types.test.ts +103 -0
  57. package/src/utils/bridge-types.ts +32 -0
  58. package/src/utils/caip2.test.ts +44 -0
  59. package/src/utils/caip2.ts +41 -0
  60. package/src/utils/client.test.ts +97 -0
  61. package/src/utils/client.ts +44 -0
  62. package/src/utils/ensure-config.test.ts +43 -0
  63. package/src/utils/ensure-config.ts +12 -0
  64. package/src/utils/index.ts +2 -0
  65. package/src/utils/network-fee.test.ts +24 -0
  66. package/src/utils/network-fee.ts +6 -0
  67. package/src/utils/retry-promise.test.ts +115 -0
  68. package/src/utils/retry-promise.ts +72 -0
  69. package/src/utils/wait.test.ts +33 -0
  70. package/src/utils/wait.ts +4 -0
  71. package/tsconfig.jest.json +7 -0
  72. package/tsconfig.json +2 -1
  73. package/src/bridge-service.ts +0 -18
  74. package/src/handlers/get-bridge-router.ts +0 -25
  75. package/src/handlers/submit-and-watch-bridge-transaction.ts +0 -1
  76. package/src/handlers/submit-bridge-transaction-step.ts +0 -22
@@ -0,0 +1,72 @@
1
+ import { wait } from './wait';
2
+
3
+ export type Done<T> = (data: T) => void;
4
+
5
+ type Params<T> = {
6
+ promise: (done: Done<T>) => Promise<unknown>;
7
+ delay: number;
8
+ startAfter?: number;
9
+ };
10
+
11
+ export const retryPromise = <T>({ promise, delay, startAfter }: Params<T>) => {
12
+ let isRunning = false;
13
+ let isCancelled = false;
14
+ let errorCount: number = 0;
15
+ let resolve: ((data: T) => void) | undefined = undefined;
16
+ let reject: ((reason?: string) => void) | undefined = undefined;
17
+
18
+ const done = (data: T) => {
19
+ if (resolve && isRunning) {
20
+ isRunning = false;
21
+ resolve(data);
22
+ }
23
+ };
24
+
25
+ const cancel = () => {
26
+ isCancelled = true;
27
+
28
+ if (reject && isRunning) {
29
+ isRunning = false;
30
+ reject('cancelled');
31
+ }
32
+ };
33
+
34
+ const result = new Promise<T>((res, rej) => {
35
+ isRunning = true;
36
+ resolve = res;
37
+ reject = rej;
38
+
39
+ const execute = async (): Promise<void> => {
40
+ if (!isRunning || isCancelled) {
41
+ return;
42
+ }
43
+
44
+ try {
45
+ await promise(done);
46
+
47
+ if (!isRunning || isCancelled) {
48
+ return;
49
+ }
50
+
51
+ await wait(delay);
52
+ } catch (err) {
53
+ console.error((err as Error).message);
54
+ errorCount += 1;
55
+ await wait(2 ** errorCount * delay);
56
+ }
57
+
58
+ await execute();
59
+ };
60
+
61
+ if (startAfter) {
62
+ setTimeout(execute, startAfter);
63
+ } else {
64
+ execute();
65
+ }
66
+ });
67
+
68
+ return {
69
+ result,
70
+ cancel,
71
+ };
72
+ };
@@ -0,0 +1,33 @@
1
+ import { wait } from './wait';
2
+
3
+ describe('wait', () => {
4
+ const delay = 5000;
5
+
6
+ beforeEach(() => {
7
+ jest.useFakeTimers();
8
+ });
9
+
10
+ afterAll(() => {
11
+ jest.useRealTimers();
12
+ });
13
+
14
+ it('delays the execution correctly', (done) => {
15
+ const test = async () => {
16
+ const testFn = jest.fn();
17
+ const promiseFn = async () => {
18
+ await wait(delay);
19
+ testFn();
20
+ };
21
+
22
+ const promise = promiseFn();
23
+ expect(testFn).not.toHaveBeenCalled();
24
+
25
+ await jest.advanceTimersByTimeAsync(delay);
26
+
27
+ expect(testFn).toHaveBeenCalledTimes(1);
28
+ promise.then(done);
29
+ };
30
+
31
+ test();
32
+ });
33
+ });
@@ -0,0 +1,4 @@
1
+ export const wait = async (time: number) =>
2
+ new Promise((res) => {
3
+ setTimeout(res, time);
4
+ });
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "verbatimModuleSyntax": false,
5
+ "esModuleInterop": true
6
+ }
7
+ }
package/tsconfig.json CHANGED
@@ -2,7 +2,8 @@
2
2
  "extends": "@internal/tsconfig/tsconfig.base.json",
3
3
  "compilerOptions": {
4
4
  "outDir": "./dist",
5
+ "paths": { "abitype": ["./node_modules/abitype"] },
5
6
  "incremental": false // Need to turn off because of tsup dts
6
7
  },
7
- "include": ["src", "../shared/src/types"]
8
+ "include": ["src", "src/types"]
8
9
  }
@@ -1,18 +0,0 @@
1
- import { submitBridgeTransactionStep } from './handlers/submit-bridge-transaction-step';
2
- import { getBridgeRouter } from './handlers/get-bridge-router';
3
- import { submitAndWatchBridgeTransaction } from './handlers/submit-and-watch-bridge-transaction';
4
- import type { Environment, MaybePromise, NativeToken } from '@internal/bridge-shared';
5
-
6
- export type BridgeServiceConfig = {
7
- environment: `${Environment}`;
8
- getChainNativeAsset: ({ evmChainId }: { evmChainId: string }) => MaybePromise<NativeToken>;
9
- getChainErc20Asset: ({ evmChainId, address }: { evmChainId: string; address: string }) => MaybePromise<NativeToken>;
10
- };
11
-
12
- export const createBridgeService = (_options: BridgeServiceConfig) => {
13
- return {
14
- getBridgeRouter,
15
- submitBridgeTransactionStep,
16
- submitAndWatchBridgeTransaction,
17
- };
18
- };
@@ -1,25 +0,0 @@
1
- import { NotImplementedError } from '../errors';
2
- import type { BridgePlatform, Token } from '@internal/bridge-shared';
3
-
4
- type GetBridgeRouterParams = {
5
- fromChainId: string;
6
- toChainId: string;
7
- fromToken: Token;
8
- };
9
-
10
- type BridgeTransactionStep = {
11
- fromChainId: string;
12
- toChainId: string;
13
- bridgePlatform: BridgePlatform;
14
- fromToken: Token;
15
- toToken: Token;
16
- };
17
-
18
- /**
19
- * Returns the steps required to complete a bridge transaction.
20
- * It could sometimes take multiple transactions to complete a bridge from chain A to B.
21
- */
22
- export const getBridgeRouter = (_params: GetBridgeRouterParams): BridgeTransactionStep[] => {
23
- // Implement smart logic for determining how to get the token from chain A to B
24
- throw new NotImplementedError();
25
- };
@@ -1 +0,0 @@
1
- export const submitAndWatchBridgeTransaction = () => {};
@@ -1,22 +0,0 @@
1
- import { type Address } from 'viem';
2
- import type { Token } from '@internal/bridge-shared';
3
- import { NotImplementedError } from '../errors';
4
-
5
- type SubmitBridgeTransactionParams = {
6
- fromChainId: string;
7
- toChainId: string;
8
- fromAddress: Address;
9
- token: Token;
10
- amount: string;
11
- };
12
-
13
- export const submitBridgeTransactionStep = async (_params: SubmitBridgeTransactionParams) => {
14
- /**
15
- * 1. Validate the token is supported on the fromChainId
16
- * 2. Validate the token is supported on the toChainId
17
- * 3. Figure out which bridge platform to use based on the from/to chains and the token being transferred
18
- * - Ex: Ethereum -> Avalanche USDC = use CCTP, but Ethereum -> Avalanche WAVAX use Avalanche bridge
19
- * 4. Return the transaction id
20
- */
21
- throw new NotImplementedError();
22
- };