@atomiqlabs/base 10.0.0-dev.2 → 10.0.0-dev.21

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 (131) hide show
  1. package/LICENSE +201 -201
  2. package/dist/btc/BitcoinNetwork.d.ts +6 -6
  3. package/dist/btc/BitcoinNetwork.js +10 -10
  4. package/dist/btc/rpc/BitcoinRpc.d.ts +68 -68
  5. package/dist/btc/rpc/BitcoinRpc.js +2 -2
  6. package/dist/btcrelay/BtcRelay.d.ts +61 -61
  7. package/dist/btcrelay/BtcRelay.js +2 -2
  8. package/dist/btcrelay/synchronizer/RelaySynchronizer.d.ts +18 -18
  9. package/dist/btcrelay/synchronizer/RelaySynchronizer.js +2 -2
  10. package/dist/btcrelay/types/BtcBlock.d.ts +13 -13
  11. package/dist/btcrelay/types/BtcBlock.js +2 -2
  12. package/dist/btcrelay/types/BtcHeader.d.ts +10 -10
  13. package/dist/btcrelay/types/BtcHeader.js +2 -2
  14. package/dist/btcrelay/types/BtcStoredHeader.d.ts +11 -11
  15. package/dist/btcrelay/types/BtcStoredHeader.js +2 -2
  16. package/dist/btcrelay/utils/StatePredictorUtils.d.ts +11 -11
  17. package/dist/btcrelay/utils/StatePredictorUtils.js +80 -80
  18. package/dist/chains/ChainData.d.ts +31 -31
  19. package/dist/chains/ChainData.js +2 -2
  20. package/dist/chains/ChainInterface.d.ts +133 -110
  21. package/dist/chains/ChainInterface.js +7 -2
  22. package/dist/chains/ChainType.d.ts +24 -23
  23. package/dist/chains/ChainType.js +2 -2
  24. package/dist/errors/CannotInitializeATAError.d.ts +3 -3
  25. package/dist/errors/CannotInitializeATAError.js +11 -11
  26. package/dist/errors/SignatureVerificationError.d.ts +3 -3
  27. package/dist/errors/SignatureVerificationError.js +11 -11
  28. package/dist/errors/SwapDataVerificationError.d.ts +3 -3
  29. package/dist/errors/SwapDataVerificationError.js +11 -11
  30. package/dist/errors/TransactionRevertedError.d.ts +3 -0
  31. package/dist/errors/TransactionRevertedError.js +11 -0
  32. package/dist/events/ChainEvents.d.ts +9 -9
  33. package/dist/events/ChainEvents.js +2 -2
  34. package/dist/events/types/ChainEvent.d.ts +7 -7
  35. package/dist/events/types/ChainEvent.js +6 -6
  36. package/dist/events/types/spv_vault/SpvVaultClaimEvent.d.ts +12 -12
  37. package/dist/events/types/spv_vault/SpvVaultClaimEvent.js +18 -18
  38. package/dist/events/types/spv_vault/SpvVaultCloseEvent.d.ts +7 -7
  39. package/dist/events/types/spv_vault/SpvVaultCloseEvent.js +13 -13
  40. package/dist/events/types/spv_vault/SpvVaultDepositEvent.d.ts +7 -7
  41. package/dist/events/types/spv_vault/SpvVaultDepositEvent.js +13 -13
  42. package/dist/events/types/spv_vault/SpvVaultEvent.d.ts +15 -15
  43. package/dist/events/types/spv_vault/SpvVaultEvent.js +20 -20
  44. package/dist/events/types/spv_vault/SpvVaultFrontEvent.d.ts +10 -10
  45. package/dist/events/types/spv_vault/SpvVaultFrontEvent.js +16 -16
  46. package/dist/events/types/spv_vault/SpvVaultOpenEvent.d.ts +7 -7
  47. package/dist/events/types/spv_vault/SpvVaultOpenEvent.js +13 -13
  48. package/dist/events/types/swap/ClaimEvent.d.ts +7 -7
  49. package/dist/events/types/swap/ClaimEvent.js +11 -11
  50. package/dist/events/types/swap/InitializeEvent.d.ts +9 -9
  51. package/dist/events/types/swap/InitializeEvent.js +12 -12
  52. package/dist/events/types/swap/RefundEvent.d.ts +5 -5
  53. package/dist/events/types/swap/RefundEvent.js +7 -7
  54. package/dist/events/types/swap/SwapEvent.d.ts +12 -12
  55. package/dist/events/types/swap/SwapEvent.js +17 -17
  56. package/dist/index.d.ts +42 -38
  57. package/dist/index.js +58 -54
  58. package/dist/lockable/Lockable.d.ts +6 -6
  59. package/dist/lockable/Lockable.js +28 -28
  60. package/dist/messaging/Messenger.d.ts +8 -0
  61. package/dist/messaging/Messenger.js +2 -0
  62. package/dist/messaging/messages/Message.d.ts +11 -0
  63. package/dist/messaging/messages/Message.js +22 -0
  64. package/dist/messaging/messages/SwapClaimWitnessMessage.d.ts +10 -0
  65. package/dist/messaging/messages/SwapClaimWitnessMessage.js +28 -0
  66. package/dist/spv_swap/SpvVaultContract.d.ts +258 -199
  67. package/dist/spv_swap/SpvVaultContract.js +2 -2
  68. package/dist/spv_swap/SpvVaultData.d.ts +35 -35
  69. package/dist/spv_swap/SpvVaultData.js +37 -37
  70. package/dist/spv_swap/SpvWithdrawalState.d.ts +31 -31
  71. package/dist/spv_swap/SpvWithdrawalState.js +10 -10
  72. package/dist/spv_swap/SpvWithdrawalTransactionData.d.ts +43 -43
  73. package/dist/spv_swap/SpvWithdrawalTransactionData.js +143 -143
  74. package/dist/storage/IStorageManager.d.ts +15 -15
  75. package/dist/storage/IStorageManager.js +2 -2
  76. package/dist/storage/StorageObject.d.ts +3 -3
  77. package/dist/storage/StorageObject.js +2 -2
  78. package/dist/swaps/ChainSwapType.d.ts +6 -6
  79. package/dist/swaps/ChainSwapType.js +10 -10
  80. package/dist/swaps/SwapCommitState.d.ts +39 -38
  81. package/dist/swaps/SwapCommitState.js +11 -11
  82. package/dist/swaps/SwapContract.d.ts +479 -467
  83. package/dist/swaps/SwapContract.js +2 -2
  84. package/dist/swaps/SwapData.d.ts +37 -36
  85. package/dist/swaps/SwapData.js +17 -14
  86. package/dist/utils/BigIntBufferUtils.d.ts +6 -6
  87. package/dist/utils/BigIntBufferUtils.js +31 -31
  88. package/package.json +31 -31
  89. package/src/btc/BitcoinNetwork.ts +6 -6
  90. package/src/btc/rpc/BitcoinRpc.ts +77 -77
  91. package/src/btcrelay/BtcRelay.ts +70 -70
  92. package/src/btcrelay/synchronizer/RelaySynchronizer.ts +17 -17
  93. package/src/btcrelay/types/BtcBlock.ts +15 -15
  94. package/src/btcrelay/types/BtcHeader.ts +11 -11
  95. package/src/btcrelay/types/BtcStoredHeader.ts +12 -12
  96. package/src/btcrelay/utils/StatePredictorUtils.ts +108 -108
  97. package/src/chains/ChainData.ts +40 -40
  98. package/src/chains/ChainInterface.ts +159 -132
  99. package/src/chains/ChainType.ts +40 -38
  100. package/src/errors/CannotInitializeATAError.ts +11 -11
  101. package/src/errors/SignatureVerificationError.ts +11 -11
  102. package/src/errors/SwapDataVerificationError.ts +11 -11
  103. package/src/errors/TransactionRevertedError.ts +11 -0
  104. package/src/events/ChainEvents.ts +13 -13
  105. package/src/events/types/ChainEvent.ts +10 -10
  106. package/src/events/types/spv_vault/SpvVaultClaimEvent.ts +31 -31
  107. package/src/events/types/spv_vault/SpvVaultCloseEvent.ts +17 -17
  108. package/src/events/types/spv_vault/SpvVaultDepositEvent.ts +17 -17
  109. package/src/events/types/spv_vault/SpvVaultEvent.ts +25 -25
  110. package/src/events/types/spv_vault/SpvVaultFrontEvent.ts +27 -27
  111. package/src/events/types/spv_vault/SpvVaultOpenEvent.ts +16 -16
  112. package/src/events/types/swap/ClaimEvent.ts +15 -15
  113. package/src/events/types/swap/InitializeEvent.ts +18 -18
  114. package/src/events/types/swap/RefundEvent.ts +6 -6
  115. package/src/events/types/swap/SwapEvent.ts +21 -21
  116. package/src/index.ts +51 -46
  117. package/src/lockable/Lockable.ts +30 -30
  118. package/src/messaging/Messenger.ts +11 -0
  119. package/src/messaging/messages/Message.ts +25 -0
  120. package/src/messaging/messages/SwapClaimWitnessMessage.ts +34 -0
  121. package/src/spv_swap/SpvVaultContract.ts +269 -230
  122. package/src/spv_swap/SpvVaultData.ts +70 -70
  123. package/src/spv_swap/SpvWithdrawalState.ts +40 -40
  124. package/src/spv_swap/SpvWithdrawalTransactionData.ts +169 -169
  125. package/src/storage/IStorageManager.ts +16 -16
  126. package/src/storage/StorageObject.ts +6 -6
  127. package/src/swaps/ChainSwapType.ts +6 -6
  128. package/src/swaps/SwapCommitState.ts +40 -39
  129. package/src/swaps/SwapContract.ts +564 -556
  130. package/src/swaps/SwapData.ts +69 -65
  131. package/src/utils/BigIntBufferUtils.ts +31 -31
@@ -1,13 +1,13 @@
1
- import {BtcHeader} from "./BtcHeader";
2
- import {Buffer} from "buffer";
3
-
4
- export interface BtcStoredHeader<T extends BtcHeader> {
5
-
6
- getChainWork(): Buffer,
7
- getHeader(): T,
8
- getLastDiffAdjustment(): number,
9
- getBlockheight(): number,
10
- getPrevBlockTimestamps(): number[],
11
- computeNext(header: T): BtcStoredHeader<T>;
12
-
1
+ import {BtcHeader} from "./BtcHeader";
2
+ import {Buffer} from "buffer";
3
+
4
+ export interface BtcStoredHeader<T extends BtcHeader> {
5
+
6
+ getChainWork(): Buffer,
7
+ getHeader(): T,
8
+ getLastDiffAdjustment(): number,
9
+ getBlockheight(): number,
10
+ getPrevBlockTimestamps(): number[],
11
+ computeNext(header: T): BtcStoredHeader<T>;
12
+
13
13
  }
@@ -1,108 +1,108 @@
1
- import {Buffer} from "buffer";
2
- import {BigIntBufferUtils} from "../../utils/BigIntBufferUtils";
3
-
4
- export class StatePredictorUtils {
5
-
6
- static readonly DIFF_ADJUSTMENT_PERIOD = 2016;
7
-
8
- static gtBuffer(a: Buffer, b: Buffer): boolean {
9
- for(let i=0;i<a.length;i++) {
10
- if(a[i]>b[i]) return true;
11
- if(a[i]<b[i]) return false;
12
- }
13
- return false;
14
- }
15
-
16
- static divInPlace(arr: Buffer, divisor: number): void {
17
-
18
- let remainder = 0;
19
-
20
- for(let i=0;i<32;i++) {
21
- const val = arr[i] + remainder;
22
- const result = Math.floor(val/divisor);
23
- remainder = (val % divisor) * 256;
24
- arr[i] = result;
25
- }
26
-
27
- }
28
-
29
- static addInPlace(arr: number[], add: number[]): void {
30
-
31
- let remainder = 0;
32
-
33
- for(let i=0;i<32;i++) {
34
- const pos = 31-i;
35
- const val = arr[pos] + add[pos] + remainder;
36
- const byte = val & 0xFF;
37
- remainder = val >> 8;
38
- arr[pos] = byte;
39
- }
40
-
41
- }
42
-
43
- static nbitsToTarget(nbits: number): Buffer {
44
-
45
- const target = Buffer.alloc(32, 0);
46
-
47
- const nSize = (nbits>>24) & 0xFF;
48
-
49
- const nWord = [
50
- ((nbits >> 16) & 0x7F),
51
- ((nbits >> 8) & 0xFF),
52
- ((nbits) & 0xFF)
53
- ];
54
-
55
- const start = 32-nSize;
56
-
57
- for(let i=0;i<3;i++) {
58
- if(start+i<32) {
59
- target[start+i] = nWord[i];
60
- }
61
- }
62
-
63
- return target;
64
-
65
- }
66
-
67
- static getDifficulty(nbits: number): Buffer {
68
-
69
- const target = StatePredictorUtils.nbitsToTarget(nbits);
70
-
71
- let start = 0;
72
- for(let i=0;i<32;i++) {
73
- if(target[i]>0) {
74
- start = i;
75
- break;
76
- }
77
- }
78
-
79
- const shift = 32 - start - 3;
80
-
81
- let num = 0;
82
-
83
- for(let i=0;i<3;i++) {
84
- num |= target[start+i] << ((2-i)*8);
85
- }
86
-
87
- const arr = Buffer.from("00000000FFFF0000000000000000000000000000000000000000000000000000", "hex");
88
-
89
- StatePredictorUtils.divInPlace(arr, num);
90
-
91
- const result = Buffer.alloc(32, 0);
92
-
93
- for(let i=0;i<32-shift;i++) {
94
- result[i+shift] = arr[i];
95
- }
96
-
97
- return result;
98
-
99
- }
100
-
101
- static getChainwork(nbits: number): Buffer {
102
- const target = StatePredictorUtils.nbitsToTarget(nbits);
103
- const targetBN = BigIntBufferUtils.fromBuffer(target);
104
- return BigIntBufferUtils.toBuffer(
105
- ((~targetBN & 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn) / (targetBN + 1n)) + 1n, "be", 32
106
- );
107
- }
108
- }
1
+ import {Buffer} from "buffer";
2
+ import {BigIntBufferUtils} from "../../utils/BigIntBufferUtils";
3
+
4
+ export class StatePredictorUtils {
5
+
6
+ static readonly DIFF_ADJUSTMENT_PERIOD = 2016;
7
+
8
+ static gtBuffer(a: Buffer, b: Buffer): boolean {
9
+ for(let i=0;i<a.length;i++) {
10
+ if(a[i]>b[i]) return true;
11
+ if(a[i]<b[i]) return false;
12
+ }
13
+ return false;
14
+ }
15
+
16
+ static divInPlace(arr: Buffer, divisor: number): void {
17
+
18
+ let remainder = 0;
19
+
20
+ for(let i=0;i<32;i++) {
21
+ const val = arr[i] + remainder;
22
+ const result = Math.floor(val/divisor);
23
+ remainder = (val % divisor) * 256;
24
+ arr[i] = result;
25
+ }
26
+
27
+ }
28
+
29
+ static addInPlace(arr: number[], add: number[]): void {
30
+
31
+ let remainder = 0;
32
+
33
+ for(let i=0;i<32;i++) {
34
+ const pos = 31-i;
35
+ const val = arr[pos] + add[pos] + remainder;
36
+ const byte = val & 0xFF;
37
+ remainder = val >> 8;
38
+ arr[pos] = byte;
39
+ }
40
+
41
+ }
42
+
43
+ static nbitsToTarget(nbits: number): Buffer {
44
+
45
+ const target = Buffer.alloc(32, 0);
46
+
47
+ const nSize = (nbits>>24) & 0xFF;
48
+
49
+ const nWord = [
50
+ ((nbits >> 16) & 0x7F),
51
+ ((nbits >> 8) & 0xFF),
52
+ ((nbits) & 0xFF)
53
+ ];
54
+
55
+ const start = 32-nSize;
56
+
57
+ for(let i=0;i<3;i++) {
58
+ if(start+i<32) {
59
+ target[start+i] = nWord[i];
60
+ }
61
+ }
62
+
63
+ return target;
64
+
65
+ }
66
+
67
+ static getDifficulty(nbits: number): Buffer {
68
+
69
+ const target = StatePredictorUtils.nbitsToTarget(nbits);
70
+
71
+ let start = 0;
72
+ for(let i=0;i<32;i++) {
73
+ if(target[i]>0) {
74
+ start = i;
75
+ break;
76
+ }
77
+ }
78
+
79
+ const shift = 32 - start - 3;
80
+
81
+ let num = 0;
82
+
83
+ for(let i=0;i<3;i++) {
84
+ num |= target[start+i] << ((2-i)*8);
85
+ }
86
+
87
+ const arr = Buffer.from("00000000FFFF0000000000000000000000000000000000000000000000000000", "hex");
88
+
89
+ StatePredictorUtils.divInPlace(arr, num);
90
+
91
+ const result = Buffer.alloc(32, 0);
92
+
93
+ for(let i=0;i<32-shift;i++) {
94
+ result[i+shift] = arr[i];
95
+ }
96
+
97
+ return result;
98
+
99
+ }
100
+
101
+ static getChainwork(nbits: number): Buffer {
102
+ const target = StatePredictorUtils.nbitsToTarget(nbits);
103
+ const targetBN = BigIntBufferUtils.fromBuffer(target);
104
+ return BigIntBufferUtils.toBuffer(
105
+ ((~targetBN & 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn) / (targetBN + 1n)) + 1n, "be", 32
106
+ );
107
+ }
108
+ }
@@ -1,40 +1,40 @@
1
- import {ChainType} from "./ChainType";
2
- import {BitcoinRpc} from "../btc/rpc/BitcoinRpc";
3
- import {BitcoinNetwork} from "../btc/BitcoinNetwork";
4
- import {IStorageManager} from "../storage/IStorageManager";
5
- import {StorageObject} from "../storage/StorageObject";
6
-
7
- export type BaseTokenType<T extends string = string> = {
8
- [ticker in T]: {
9
- address: string,
10
- decimals: number,
11
- displayDecimals?: number
12
- }
13
- };
14
-
15
- export type ChainData<T extends ChainType> = {
16
- chainId: ChainType["ChainId"],
17
- chainInterface: T["ChainInterface"],
18
- btcRelay: T["BtcRelay"],
19
- chainEvents: T["Events"],
20
- swapContract: T["Contract"],
21
- swapDataConstructor: new (data: any) => T["Data"],
22
- spvVaultContract: T["SpvVaultContract"],
23
- spvVaultDataConstructor: new (data: any) => T["SpvVaultData"],
24
- spvVaultWithdrawalDataConstructor: new (data: any) => T["SpvVaultWithdrawalData"]
25
- };
26
-
27
- export type ChainInitializerFn<O, C extends ChainType> = (
28
- options: O,
29
- bitcoinRpc: BitcoinRpc<any>,
30
- network: BitcoinNetwork,
31
- storageCtor: <T extends StorageObject>(name: string) => IStorageManager<T>
32
- ) => ChainData<C>;
33
-
34
- export type ChainInitializer<O, C extends ChainType, T extends BaseTokenType> = {
35
- chainId: C["ChainId"],
36
- chainType: C,
37
- initializer: ChainInitializerFn<O, C>,
38
- tokens: T,
39
- options: O
40
- }
1
+ import {ChainType} from "./ChainType";
2
+ import {BitcoinRpc} from "../btc/rpc/BitcoinRpc";
3
+ import {BitcoinNetwork} from "../btc/BitcoinNetwork";
4
+ import {IStorageManager} from "../storage/IStorageManager";
5
+ import {StorageObject} from "../storage/StorageObject";
6
+
7
+ export type BaseTokenType<T extends string = string> = {
8
+ [ticker in T]: {
9
+ address: string,
10
+ decimals: number,
11
+ displayDecimals?: number
12
+ }
13
+ };
14
+
15
+ export type ChainData<T extends ChainType> = {
16
+ chainId: ChainType["ChainId"],
17
+ chainInterface: T["ChainInterface"],
18
+ btcRelay: T["BtcRelay"],
19
+ chainEvents: T["Events"],
20
+ swapContract: T["Contract"],
21
+ swapDataConstructor: new (data: any) => T["Data"],
22
+ spvVaultContract: T["SpvVaultContract"],
23
+ spvVaultDataConstructor: new (data: any) => T["SpvVaultData"],
24
+ spvVaultWithdrawalDataConstructor: new (data: any) => T["SpvVaultWithdrawalData"]
25
+ };
26
+
27
+ export type ChainInitializerFn<O, C extends ChainType> = (
28
+ options: O,
29
+ bitcoinRpc: BitcoinRpc<any>,
30
+ network: BitcoinNetwork,
31
+ storageCtor: <T extends StorageObject>(name: string) => IStorageManager<T>
32
+ ) => ChainData<C>;
33
+
34
+ export type ChainInitializer<O, C extends ChainType, T extends BaseTokenType> = {
35
+ chainId: C["ChainId"],
36
+ chainType: C,
37
+ initializer: ChainInitializerFn<O, C>,
38
+ tokens: T,
39
+ options: O
40
+ }
@@ -1,133 +1,160 @@
1
- export type TransactionConfirmationOptions = {
2
- waitForConfirmation?: boolean,
3
- abortSignal?: AbortSignal,
4
- feeRate?: string
5
- };
6
-
7
- export type AbstractSigner = {
8
- getAddress: () => string
9
- };
10
-
11
- export interface ChainInterface<
12
- TX = any,
13
- Signer extends AbstractSigner = AbstractSigner,
14
- ChainId extends string = string
15
- > {
16
-
17
- readonly chainId: ChainId;
18
-
19
- /**
20
- * Returns the token balance of a specific address
21
- *
22
- * @param signer Address to check the balance of
23
- * @param token Token
24
- */
25
- getBalance(signer: string, token: string): Promise<bigint>;
26
-
27
- /**
28
- * Returns the token address of the native currency of the chain
29
- */
30
- getNativeCurrencyAddress(): string;
31
-
32
- /**
33
- * Checks if a given string is a valid wallet address
34
- *
35
- * @param address
36
- */
37
- isValidAddress(address: string): boolean;
38
-
39
- /**
40
- * Checks if a given string is a valid token identifier
41
- *
42
- * @param tokenIdentifier
43
- */
44
- isValidToken(tokenIdentifier: string): boolean;
45
-
46
- /**
47
- * Transfers the specific token to a given recipient
48
- *
49
- * @param signer Signer/owner of the tokens
50
- * @param token Token to transfer
51
- * @param amount Amount of token to transfer
52
- * @param dstAddress Destination address of the transfer
53
- * @param txOptions Transaction options
54
- */
55
- transfer(signer: Signer, token: string, amount: bigint, dstAddress: string, txOptions?: TransactionConfirmationOptions): Promise<string>;
56
-
57
- /**
58
- * Returns transactions for transferring a specific token to a given recipient
59
- *
60
- * @param signer Signer/owner of the tokens
61
- * @param token Token to transfer
62
- * @param amount Amount of token to transfer
63
- * @param dstAddress Destination address of the transfer
64
- * @param feeRate Optional fee rate to use for the transaction (fetched on-demand if not provided)
65
- */
66
- txsTransfer(signer: string, token: string, amount: bigint, dstAddress: string, feeRate?: string): Promise<TX[]>;
67
-
68
- /**
69
- * Serializes a given transaction to a string
70
- *
71
- * @param tx Transaction to serialize
72
- */
73
- serializeTx(tx: TX): Promise<string>;
74
-
75
- /**
76
- * Deserializes a transaction from string
77
- *
78
- * @param txData Serialized transaction data string
79
- */
80
- deserializeTx(txData: string): Promise<TX>;
81
-
82
- /**
83
- * Returns the status of the given serialized transaction
84
- *
85
- * @param tx Serialized transaction
86
- */
87
- getTxStatus(tx: string): Promise<"not_found" | "pending" | "success" | "reverted">;
88
-
89
- /**
90
- * Returns the status of the given transactionId (use getTxStatus whenever possible, it's more reliable)
91
- *
92
- * @param txId Transaction ID
93
- */
94
- getTxIdStatus(txId: string): Promise<"not_found" | "pending" | "success" | "reverted">;
95
-
96
- /**
97
- * Signs, sends a batch of transaction and optionally waits for their confirmation
98
- *
99
- * @param signer Signer to use for signing transactions
100
- * @param txs Transactions to send
101
- * @param waitForConfirmation Whether to wait for transaction confirmation (if parallel is not specified,
102
- * every transaction's confirmation except the last one is awaited)
103
- * @param abortSignal Abort signal
104
- * @param parallel Whether to send all transactions in parallel or one by one (always waiting for the previous TX to confirm)
105
- * @param onBeforePublish Callback called before a tx is broadcast
106
- */
107
- sendAndConfirm(signer: Signer, txs: TX[], waitForConfirmation?: boolean, abortSignal?: AbortSignal, parallel?: boolean, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string[]>;
108
-
109
- /**
110
- * Callback called when transaction is being replaced (used for EVM, when fee is bumped on an unconfirmed tx)
111
- *
112
- * @param callback
113
- */
114
- onBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): void;
115
-
116
- /**
117
- * Remove tx replace callback
118
- *
119
- * @param callback
120
- */
121
- offBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): boolean;
122
-
123
- /**
124
- * Returns a random valid wallet address
125
- */
126
- randomAddress(): string;
127
-
128
- /**
129
- * Returns randomly generated signer
130
- */
131
- randomSigner(): Signer;
132
-
1
+ export type TransactionConfirmationOptions = {
2
+ waitForConfirmation?: boolean,
3
+ abortSignal?: AbortSignal,
4
+ feeRate?: string
5
+ };
6
+
7
+ export type AbstractSigner = {
8
+ type: "AtomiqAbstractSigner",
9
+ getAddress: () => string,
10
+ init?: () => Promise<void>,
11
+ stop?: () => Promise<void>
12
+ };
13
+
14
+ export function isAbstractSigner(val: any): val is AbstractSigner {
15
+ return typeof(val)==="object" && val.type==="AtomiqAbstractSigner" && typeof(val.getAddress)==="function";
16
+ }
17
+
18
+ export interface ChainInterface<
19
+ TX = any,
20
+ Signer extends AbstractSigner = AbstractSigner,
21
+ ChainId extends string = string,
22
+ NativeSigner = any
23
+ > {
24
+
25
+ readonly chainId: ChainId;
26
+
27
+ /**
28
+ * Returns the token balance of a specific address
29
+ *
30
+ * @param signer Address to check the balance of
31
+ * @param token Token
32
+ */
33
+ getBalance(signer: string, token: string): Promise<bigint>;
34
+
35
+ /**
36
+ * Returns the token address of the native currency of the chain
37
+ */
38
+ getNativeCurrencyAddress(): string;
39
+
40
+ /**
41
+ * Checks if a given string is a valid wallet address
42
+ *
43
+ * @param address
44
+ * @param lenient Whether a lenient parsing should be used (i.e. don't strictly enforce the Starknet address lengths)
45
+ */
46
+ isValidAddress(address: string, lenient?: boolean): boolean;
47
+
48
+ /**
49
+ * Normalizes a given address i.e. pads it to the specific size
50
+ *
51
+ * @param address
52
+ */
53
+ normalizeAddress(address: string): string;
54
+
55
+ /**
56
+ * Checks if a given string is a valid token identifier
57
+ *
58
+ * @param tokenIdentifier
59
+ */
60
+ isValidToken(tokenIdentifier: string): boolean;
61
+
62
+ /**
63
+ * Transfers the specific token to a given recipient
64
+ *
65
+ * @param signer Signer/owner of the tokens
66
+ * @param token Token to transfer
67
+ * @param amount Amount of token to transfer
68
+ * @param dstAddress Destination address of the transfer
69
+ * @param txOptions Transaction options
70
+ */
71
+ transfer(signer: Signer, token: string, amount: bigint, dstAddress: string, txOptions?: TransactionConfirmationOptions): Promise<string>;
72
+
73
+ /**
74
+ * Returns transactions for transferring a specific token to a given recipient
75
+ *
76
+ * @param signer Signer/owner of the tokens
77
+ * @param token Token to transfer
78
+ * @param amount Amount of token to transfer
79
+ * @param dstAddress Destination address of the transfer
80
+ * @param feeRate Optional fee rate to use for the transaction (fetched on-demand if not provided)
81
+ */
82
+ txsTransfer(signer: string, token: string, amount: bigint, dstAddress: string, feeRate?: string): Promise<TX[]>;
83
+
84
+ /**
85
+ * Serializes a given transaction to a string
86
+ *
87
+ * @param tx Transaction to serialize
88
+ */
89
+ serializeTx(tx: TX): Promise<string>;
90
+
91
+ /**
92
+ * Deserializes a transaction from string
93
+ *
94
+ * @param txData Serialized transaction data string
95
+ */
96
+ deserializeTx(txData: string): Promise<TX>;
97
+
98
+ /**
99
+ * Returns the status of the given serialized transaction
100
+ *
101
+ * @param tx Serialized transaction
102
+ */
103
+ getTxStatus(tx: string): Promise<"not_found" | "pending" | "success" | "reverted">;
104
+
105
+ /**
106
+ * Returns the status of the given transactionId (use getTxStatus whenever possible, it's more reliable)
107
+ *
108
+ * @param txId Transaction ID
109
+ */
110
+ getTxIdStatus(txId: string): Promise<"not_found" | "pending" | "success" | "reverted">;
111
+
112
+ /**
113
+ * Returns the latest known finalized block data (this is a block with 100% certainty of not getting re-org, i.e.
114
+ * a block already committed on L1)
115
+ */
116
+ getFinalizedBlock(): Promise<{height: number, blockHash: string}>;
117
+
118
+ /**
119
+ * Signs, sends a batch of transaction and optionally waits for their confirmation
120
+ *
121
+ * @param signer Signer to use for signing transactions
122
+ * @param txs Transactions to send
123
+ * @param waitForConfirmation Whether to wait for transaction confirmation (if parallel is not specified,
124
+ * every transaction's confirmation except the last one is awaited)
125
+ * @param abortSignal Abort signal
126
+ * @param parallel Whether to send all transactions in parallel or one by one (always waiting for the previous TX to confirm)
127
+ * @param onBeforePublish Callback called before a tx is broadcast
128
+ */
129
+ sendAndConfirm(signer: Signer, txs: TX[], waitForConfirmation?: boolean, abortSignal?: AbortSignal, parallel?: boolean, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string[]>;
130
+
131
+ /**
132
+ * Callback called when transaction is being replaced (used for EVM, when fee is bumped on an unconfirmed tx)
133
+ *
134
+ * @param callback
135
+ */
136
+ onBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): void;
137
+
138
+ /**
139
+ * Remove tx replace callback
140
+ *
141
+ * @param callback
142
+ */
143
+ offBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): boolean;
144
+
145
+ /**
146
+ * Returns a random valid wallet address
147
+ */
148
+ randomAddress(): string;
149
+
150
+ /**
151
+ * Returns randomly generated signer
152
+ */
153
+ randomSigner(): Signer;
154
+
155
+ /**
156
+ * Wraps a native chain signer object to an atomiq-understandable AbstractSigner
157
+ */
158
+ wrapSigner(signer: NativeSigner): Promise<Signer>;
159
+
133
160
  }