@avalabs/bridge-unified 0.0.0-unified-bridge-init-20230824160802 → 2.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.
Files changed (75) 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 +11 -1
  5. package/README.md +123 -4
  6. package/dist/index.cjs +12 -2
  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 +5 -2
  11. package/dist/index.js.map +1 -1
  12. package/jest.config.js +9 -0
  13. package/package.json +16 -10
  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 +42 -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 +775 -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 +2 -0
  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 +84 -13
  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/handlers/check-for-pending-bridge-journey.ts +0 -20
  74. package/src/handlers/execute-bridge-journey-step.ts +0 -20
  75. package/src/handlers/plan-bridge-journey.ts +0 -25
@@ -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,20 +0,0 @@
1
- import type { Address } from 'viem';
2
- import { NotImplementedError } from '../errors';
3
-
4
- /**
5
- * Checks for any currently pending bridge transactions. Useful for dApps that want
6
- * to display the current tx's status after a page refresh.
7
- */
8
- export const checkForPendingBridgeJourney = async ({
9
- address: _address,
10
- chainIds: _chainIds,
11
- }: {
12
- address: Address;
13
- chainIds: string[];
14
- }) => {
15
- /**
16
- * 1. If wallet is Core, request current bridge journey. Otherwise, check glacier to see if the provided wallet has bridge tx in the past 100 transactions on the provided chains.
17
- * 3. Returns an array of txIds for all pending transactions
18
- */
19
- throw new NotImplementedError();
20
- };
@@ -1,20 +0,0 @@
1
- import { NotImplementedError } from '../errors';
2
-
3
- type ExecuteBridgeJourneyStepParams = {
4
- stepIndex: number;
5
- journey: unknown;
6
- };
7
-
8
- export const executeBridgeJourneyStep = async (_params: ExecuteBridgeJourneyStepParams) => {
9
- /**
10
- * 1. Validate the journey
11
- * 3. Forward request along to the right bridge platform's package.
12
- * 4. Return an observable that emits events when:
13
- * - User signs the transaction (returns the fromChain txId)
14
- * - Every block confirmation on fromChain
15
- * - fromChain tx completed, starting the toChain tx (returns the toChainTxId)
16
- * - Every block confirmation on toChain
17
- * - toChain tx completed
18
- */
19
- throw new NotImplementedError();
20
- };
@@ -1,25 +0,0 @@
1
- import { NotImplementedError } from '../errors';
2
- import { BridgePlatform, type 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 a list of transaction steps needed to complete a bridge from chain A to B.
20
- * The response may include multiple steps if the bridge requires multiple hops.
21
- */
22
- export const planBridgeJourney = (_params: GetBridgeRouterParams): BridgeTransactionStep[] => {
23
- // Implement smart logic for determining how to get the token from chain A to B
24
- throw new NotImplementedError();
25
- };