@atomiqlabs/base 12.0.4 → 13.0.4

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 (113) hide show
  1. package/dist/btc/BitcoinNetwork.d.ts +5 -0
  2. package/dist/btc/BitcoinNetwork.js +5 -0
  3. package/dist/btc/rpc/BitcoinRpc.d.ts +168 -7
  4. package/dist/btcrelay/BtcRelay.d.ts +115 -6
  5. package/dist/btcrelay/synchronizer/RelaySynchronizer.d.ts +55 -14
  6. package/dist/btcrelay/types/BtcBlock.d.ts +32 -0
  7. package/dist/btcrelay/types/BtcHeader.d.ts +23 -0
  8. package/dist/btcrelay/types/BtcStoredHeader.d.ts +26 -0
  9. package/dist/btcrelay/utils/StatePredictorUtils.d.ts +5 -0
  10. package/dist/btcrelay/utils/StatePredictorUtils.js +5 -0
  11. package/dist/chains/ChainData.d.ts +41 -1
  12. package/dist/chains/ChainInterface.d.ts +44 -1
  13. package/dist/chains/ChainInterface.js +5 -0
  14. package/dist/chains/ChainType.d.ts +7 -1
  15. package/dist/errors/CannotInitializeATAError.d.ts +5 -0
  16. package/dist/errors/CannotInitializeATAError.js +5 -0
  17. package/dist/errors/SignatureVerificationError.d.ts +5 -0
  18. package/dist/errors/SignatureVerificationError.js +5 -0
  19. package/dist/errors/SwapDataVerificationError.d.ts +5 -0
  20. package/dist/errors/SwapDataVerificationError.js +5 -0
  21. package/dist/errors/TransactionRevertedError.d.ts +5 -0
  22. package/dist/errors/TransactionRevertedError.js +5 -0
  23. package/dist/events/ChainEvents.d.ts +40 -2
  24. package/dist/events/types/ChainEvent.d.ts +8 -0
  25. package/dist/events/types/ChainEvent.js +5 -0
  26. package/dist/events/types/spv_vault/SpvVaultClaimEvent.d.ts +29 -0
  27. package/dist/events/types/spv_vault/SpvVaultClaimEvent.js +5 -0
  28. package/dist/events/types/spv_vault/SpvVaultCloseEvent.d.ts +13 -0
  29. package/dist/events/types/spv_vault/SpvVaultCloseEvent.js +7 -0
  30. package/dist/events/types/spv_vault/SpvVaultDepositEvent.d.ts +12 -0
  31. package/dist/events/types/spv_vault/SpvVaultDepositEvent.js +5 -0
  32. package/dist/events/types/spv_vault/SpvVaultEvent.d.ts +18 -2
  33. package/dist/events/types/spv_vault/SpvVaultEvent.js +10 -0
  34. package/dist/events/types/spv_vault/SpvVaultFrontEvent.d.ts +22 -0
  35. package/dist/events/types/spv_vault/SpvVaultFrontEvent.js +5 -0
  36. package/dist/events/types/spv_vault/SpvVaultOpenEvent.d.ts +11 -0
  37. package/dist/events/types/spv_vault/SpvVaultOpenEvent.js +5 -0
  38. package/dist/events/types/swap/ClaimEvent.d.ts +8 -0
  39. package/dist/events/types/swap/ClaimEvent.js +6 -0
  40. package/dist/events/types/swap/InitializeEvent.d.ts +13 -2
  41. package/dist/events/types/swap/InitializeEvent.js +6 -0
  42. package/dist/events/types/swap/RefundEvent.d.ts +5 -0
  43. package/dist/events/types/swap/RefundEvent.js +9 -0
  44. package/dist/events/types/swap/SwapEvent.d.ts +15 -2
  45. package/dist/events/types/swap/SwapEvent.js +10 -0
  46. package/dist/lockable/Lockable.d.ts +18 -0
  47. package/dist/lockable/Lockable.js +18 -0
  48. package/dist/messaging/Messenger.d.ts +32 -0
  49. package/dist/messaging/messages/Message.d.ts +24 -1
  50. package/dist/messaging/messages/Message.js +23 -0
  51. package/dist/messaging/messages/SwapClaimWitnessMessage.d.ts +22 -4
  52. package/dist/messaging/messages/SwapClaimWitnessMessage.js +12 -0
  53. package/dist/spv_swap/SpvVaultContract.d.ts +18 -10
  54. package/dist/spv_swap/SpvVaultData.d.ts +76 -0
  55. package/dist/spv_swap/SpvVaultData.js +19 -0
  56. package/dist/spv_swap/SpvWithdrawalState.d.ts +46 -0
  57. package/dist/spv_swap/SpvWithdrawalState.js +19 -0
  58. package/dist/spv_swap/SpvWithdrawalTransactionData.d.ts +94 -3
  59. package/dist/spv_swap/SpvWithdrawalTransactionData.js +81 -4
  60. package/dist/storage/IStorageManager.d.ts +37 -0
  61. package/dist/storage/StorageObject.d.ts +8 -0
  62. package/dist/swaps/ChainSwapType.d.ts +22 -0
  63. package/dist/swaps/ChainSwapType.js +22 -0
  64. package/dist/swaps/SwapCommitState.d.ts +50 -0
  65. package/dist/swaps/SwapCommitState.js +20 -0
  66. package/dist/swaps/SwapContract.d.ts +58 -2
  67. package/dist/swaps/SwapData.d.ts +147 -6
  68. package/dist/swaps/SwapData.js +20 -0
  69. package/dist/utils/BigIntBufferUtils.d.ts +5 -0
  70. package/dist/utils/BigIntBufferUtils.js +6 -1
  71. package/package.json +5 -3
  72. package/src/btc/BitcoinNetwork.ts +5 -1
  73. package/src/btc/rpc/BitcoinRpc.ts +175 -8
  74. package/src/btcrelay/BtcRelay.ts +121 -7
  75. package/src/btcrelay/synchronizer/RelaySynchronizer.ts +51 -10
  76. package/src/btcrelay/types/BtcBlock.ts +40 -0
  77. package/src/btcrelay/types/BtcHeader.ts +31 -3
  78. package/src/btcrelay/types/BtcStoredHeader.ts +31 -0
  79. package/src/btcrelay/utils/StatePredictorUtils.ts +5 -0
  80. package/src/chains/ChainData.ts +41 -1
  81. package/src/chains/ChainInterface.ts +47 -0
  82. package/src/chains/ChainType.ts +10 -3
  83. package/src/errors/CannotInitializeATAError.ts +5 -1
  84. package/src/errors/SignatureVerificationError.ts +5 -1
  85. package/src/errors/SwapDataVerificationError.ts +5 -1
  86. package/src/errors/TransactionRevertedError.ts +5 -1
  87. package/src/events/ChainEvents.ts +44 -2
  88. package/src/events/types/ChainEvent.ts +8 -0
  89. package/src/events/types/spv_vault/SpvVaultClaimEvent.ts +29 -0
  90. package/src/events/types/spv_vault/SpvVaultCloseEvent.ts +13 -0
  91. package/src/events/types/spv_vault/SpvVaultDepositEvent.ts +13 -0
  92. package/src/events/types/spv_vault/SpvVaultEvent.ts +18 -3
  93. package/src/events/types/spv_vault/SpvVaultFrontEvent.ts +22 -0
  94. package/src/events/types/spv_vault/SpvVaultOpenEvent.ts +11 -0
  95. package/src/events/types/swap/ClaimEvent.ts +9 -2
  96. package/src/events/types/swap/InitializeEvent.ts +14 -4
  97. package/src/events/types/swap/RefundEvent.ts +6 -1
  98. package/src/events/types/swap/SwapEvent.ts +15 -3
  99. package/src/lockable/Lockable.ts +18 -2
  100. package/src/messaging/Messenger.ts +37 -0
  101. package/src/messaging/messages/Message.ts +24 -3
  102. package/src/messaging/messages/SwapClaimWitnessMessage.ts +23 -6
  103. package/src/spv_swap/SpvVaultContract.ts +19 -12
  104. package/src/spv_swap/SpvVaultData.ts +84 -0
  105. package/src/spv_swap/SpvWithdrawalState.ts +46 -1
  106. package/src/spv_swap/SpvWithdrawalTransactionData.ts +102 -7
  107. package/src/storage/IStorageManager.ts +41 -0
  108. package/src/storage/StorageObject.ts +8 -2
  109. package/src/swaps/ChainSwapType.ts +22 -0
  110. package/src/swaps/SwapCommitState.ts +50 -0
  111. package/src/swaps/SwapContract.ts +58 -2
  112. package/src/swaps/SwapData.ts +160 -7
  113. package/src/utils/BigIntBufferUtils.ts +6 -1
@@ -3,6 +3,11 @@ import {BtcBlock} from "./types/BtcBlock";
3
3
  import {Buffer} from "buffer";
4
4
  import {AbstractSigner} from "../chains/ChainInterface";
5
5
 
6
+ /**
7
+ * Represents a BTC relay bitcoin light client contract, which verifies the bitcoin blockheaders on smart chains
8
+ *
9
+ * @category BTC Relay
10
+ */
6
11
  export interface BtcRelay<
7
12
  V extends BtcStoredHeader<any>,
8
13
  T,
@@ -10,48 +15,126 @@ export interface BtcRelay<
10
15
  Signer extends AbstractSigner = AbstractSigner
11
16
  > {
12
17
 
18
+ /**
19
+ * Maximum blockheaders that fit in a single transaction
20
+ */
13
21
  maxHeadersPerTx: number;
22
+ /**
23
+ * Maximum amount of fork headers that fit in a single transactions
24
+ */
14
25
  maxForkHeadersPerTx: number;
26
+ /**
27
+ * Maximum amount of fork headers that can be submitted at once using the short fork method
28
+ */
15
29
  maxShortForkHeadersPerTx?: number;
16
30
 
31
+ /**
32
+ * Returns data about current main chain tip stored in the btc relay
33
+ */
17
34
  getTipData(): Promise<{
18
35
  blockheight: number,
19
36
  blockhash: string,
20
37
  commitHash: string,
21
38
  chainWork: Buffer
22
- }>;
39
+ } | null>;
23
40
 
41
+ /**
42
+ * Retrieves blockheader with a specific blockhash, returns null if `requiredBlockheight` is provided and
43
+ * btc relay contract is not synced up to the desired blockheight
44
+ *
45
+ * @param blockData
46
+ * @param requiredBlockheight
47
+ */
24
48
  retrieveLogAndBlockheight(blockData: {blockhash: string, height: number}, requiredBlockheight?: number): Promise<{
25
49
  header: V,
26
50
  height: number
27
- }>;
28
- retrieveLogByCommitHash(commitHash: string, blockData: {blockhash: string, height: number}): Promise<V>;
51
+ } | null>;
52
+
53
+ /**
54
+ * Retrieves stored bitcoin blockheader data by blockheader's commit hash and provided
55
+ * blockhash from `blockData`
56
+ *
57
+ * @param commitHash
58
+ * @param blockData
59
+ */
60
+ retrieveLogByCommitHash(commitHash: string, blockData: {blockhash: string, height: number}): Promise<V | null>;
29
61
 
62
+ /**
63
+ * Retrieves latest known bitcoin blockheader stored in the BTC Relay and also known to the bitcoin RPC
64
+ */
30
65
  retrieveLatestKnownBlockLog(): Promise<{
31
66
  resultStoredHeader: V,
32
67
  resultBitcoinHeader: B
33
- }>;
34
- //retrieveOnchainTip(): Promise<B>;
68
+ } | null>;
35
69
 
70
+ /**
71
+ * Initializes the underlying BTC Relay contract
72
+ *
73
+ * @param signer A signer's address to use for the transaction
74
+ * @param header Main chain blockheader to use as a genesis
75
+ * @param epochStart Timestamp of the first block in this difficulty epoch
76
+ * @param pastBlocksTimestamps Timestamps of the last 11 blockheaders, for median block time rule
77
+ * @param feeRate Optional fee rate for the transaction
78
+ */
36
79
  saveInitialHeader(signer: string, header: B, epochStart: number, pastBlocksTimestamps: number[], feeRate?: string): Promise<T>;
80
+
81
+ /**
82
+ * Returns a transaction that submits bitcoin blockheaders as a bitcoin main chain to the btc relay
83
+ *
84
+ * @param signer A signer's address for the transaction
85
+ * @param mainHeaders New bitcoin blockheaders to submit
86
+ * @param storedHeader Latest committed and stored bitcoin blockheader in the BTC relay
87
+ * @param feeRate Optional fee rate for the transaction
88
+ */
37
89
  saveMainHeaders(signer: string, mainHeaders: B[], storedHeader: V, feeRate?: string): Promise<{
38
90
  forkId: number,
39
91
  lastStoredHeader: V,
40
92
  tx: T,
41
93
  computedCommitedHeaders: V[]
42
94
  }>;
95
+
96
+ /**
97
+ * Returns a transaction that submits a new long fork and submits the first headers to it
98
+ *
99
+ * @param signer A signer's address for the transaction
100
+ * @param forkHeaders New fork bitcoin blockheaders to submit
101
+ * @param storedHeader Committed and stored bitcoin blockheader in the BTC relay from which to fork
102
+ * @param tipWork Chainwork of the current BTC Relay main chain tip
103
+ * @param feeRate Optional fee rate for the transaction
104
+ */
43
105
  saveNewForkHeaders(signer: string, forkHeaders: B[], storedHeader: V, tipWork: Buffer, feeRate?: string): Promise<{
44
106
  forkId: number,
45
107
  lastStoredHeader: V,
46
108
  tx: T,
47
109
  computedCommitedHeaders: V[]
48
110
  }>;
111
+
112
+ /**
113
+ * Returns a transaction that continues submitting blockheaders to an existing long fork
114
+ *
115
+ * @param signer A signer's address for the transaction
116
+ * @param forkHeaders New fork bitcoin blockheaders to submit
117
+ * @param storedHeader Committed and stored bitcoin blockheader in the BTC relay from which to fork
118
+ * @param forkId Fork ID to submit the blockheaders to
119
+ * @param tipWork Chainwork of the current BTC Relay main chain tip
120
+ * @param feeRate Optional fee rate for the transaction
121
+ */
49
122
  saveForkHeaders(signer: string, forkHeaders: B[], storedHeader: V, forkId: number, tipWork: Buffer, feeRate?: string): Promise<{
50
123
  forkId: number,
51
124
  lastStoredHeader: V,
52
125
  tx: T,
53
126
  computedCommitedHeaders: V[]
54
127
  }>;
128
+
129
+ /**
130
+ * Returns a transaction that submits a short fork with the provided blockheaders
131
+ *
132
+ * @param signer A signer's address for the transaction
133
+ * @param forkHeaders New fork bitcoin blockheaders to submit
134
+ * @param storedHeader Committed and stored bitcoin blockheader in the BTC relay from which to fork
135
+ * @param tipWork Chainwork of the current BTC Relay main chain tip
136
+ * @param feeRate Optional fee rate for the transaction
137
+ */
55
138
  saveShortForkHeaders?(signer: string, forkHeaders: B[], storedHeader: V, tipWork: Buffer, feeRate?: string): Promise<{
56
139
  forkId: number,
57
140
  lastStoredHeader: V,
@@ -59,13 +142,44 @@ export interface BtcRelay<
59
142
  computedCommitedHeaders: V[]
60
143
  }>;
61
144
 
62
- getMainFeeRate?(signer: string): Promise<string>;
63
- getForkFeeRate?(signer: string, forkId: number): Promise<string>;
145
+ /**
146
+ * Gets fee rate required for submitting blockheaders to the main chain
147
+ *
148
+ * @param signer A signer's address to use for the estimation
149
+ */
150
+ getMainFeeRate(signer: string): Promise<string>;
151
+
152
+ /**
153
+ * Gets fee rate required for submitting blockheaders to the specific fork
154
+ *
155
+ * @param signer A signer's address to use for the estimation
156
+ * @param forkId A fork ID to use for estimation
157
+ */
158
+ getForkFeeRate(signer: string, forkId: number): Promise<string>;
64
159
 
160
+ /**
161
+ * Estimate required synchronization fee (worst case) to synchronize btc relay to the required blockheight
162
+ *
163
+ * @param requiredBlockheight Blockheight to which to synchronize
164
+ * @param feeRate Optional fee rate to use for the estimation
165
+ */
65
166
  estimateSynchronizeFee(requiredBlockheight: number, feeRate?: string): Promise<bigint>;
66
167
 
168
+ /**
169
+ * Returns required fee in native token to synchronize a single block to btc relay
170
+ *
171
+ * @param feeRate Optional fee rate to use for the estimation
172
+ */
67
173
  getFeePerBlock(feeRate?: any): Promise<bigint>;
68
174
 
175
+ /**
176
+ * Checks and sweeps data accounts which contain yet unused fork data (use for Solana's PDAs)
177
+ *
178
+ * @param signer A signer's address to check account for
179
+ * @param lastSweepTimestamp Timestamp of the last sweep
180
+ *
181
+ * @returns A number of data accounts swept
182
+ */
69
183
  sweepForkData?(signer: Signer, lastSweepTimestamp?: number): Promise<number | null>;
70
184
 
71
185
  }
@@ -1,18 +1,59 @@
1
1
  import {BtcStoredHeader} from "../types/BtcStoredHeader";
2
2
  import {BtcBlock} from "../types/BtcBlock";
3
3
 
4
+ export type SynchronizationResponse<V extends BtcStoredHeader<any>, T, B extends BtcBlock> = {
5
+ /**
6
+ * Transactions required to synchronize the btc relay
7
+ */
8
+ txs: T[],
9
+ /**
10
+ * Latest committed header after synchronization
11
+ */
12
+ targetCommitedHeader: V,
13
+ /**
14
+ * Latest block header after synchronization
15
+ */
16
+ latestBlockHeader: B,
17
+ /**
18
+ * Mapping of synchronized committed headers, based on blockheight
19
+ */
20
+ computedHeaderMap: {[blockheight: number]: V},
21
+ /**
22
+ * Mapping of synchronized block headers, based on blockheight
23
+ */
24
+ blockHeaderMap: {[blockheight: number]: B},
25
+ /**
26
+ * Tip committed header of the btc relay before synchronization
27
+ */
28
+ btcRelayTipCommitedHeader: V,
29
+ /**
30
+ * Tip block header of the btc relay before synchronization
31
+ */
32
+ btcRelayTipBlockHeader: B,
33
+ /**
34
+ * A fork ID that was used to re-org the chain to the current canonical chain
35
+ */
36
+ startForkId?: number
37
+ };
4
38
 
39
+ /**
40
+ * An interface for a synchronizer of the BTC relay bitcoin light client contract, produces transactions
41
+ * necessary to synchronize the underlying relay contract to the current tip of the canonical chain,
42
+ * automatically handles forking if necessary
43
+ *
44
+ * @category BTC Relay
45
+ */
5
46
  export interface RelaySynchronizer<V extends BtcStoredHeader<any>, T, B extends BtcBlock> {
6
47
 
7
- syncToLatestTxs(signer: string, feeRate?: string): Promise<{
8
- txs: T[], //Transactions required to synchronize the btc relay
9
- targetCommitedHeader: V, //Latest committed header after synchronization
10
- latestBlockHeader: B, //Latest block header after synchronization
11
- computedHeaderMap: {[blockheight: number]: V}, //Mapping of synchronized committed headers
12
- blockHeaderMap: {[blockheight: number]: B}, //Mapping of synchronized block headers
13
- btcRelayTipCommitedHeader: V, //Tip committed header of the btc relay before synchronization
14
- btcRelayTipBlockHeader: B, //Tip block header of the btc relay before synchronization
15
- startForkId?: number
16
- }>;
48
+ /**
49
+ * Returns the transactions necessary to synchronize the BTC relay contract to the
50
+ * tip of the canonical chain. Also returns the various bitcoin blockheaders that
51
+ * are to-be-synced to the relay, such that they can be used to already pre-create
52
+ * transactions, which require the stored blockheaders
53
+ *
54
+ * @param signer Transactions signer's address
55
+ * @param feeRate Optional fee rate to use for the transactions
56
+ */
57
+ syncToLatestTxs(signer: string, feeRate?: string): Promise<SynchronizationResponse<V, T, B>>;
17
58
 
18
59
  }
@@ -1,15 +1,55 @@
1
1
  import {Buffer} from "buffer";
2
2
 
3
+ /**
4
+ * Represents a bitcoin block header as fetched from the RPC
5
+ *
6
+ * @category Bitcoin
7
+ */
3
8
  export interface BtcBlock {
4
9
 
10
+ /**
11
+ * Block's version field
12
+ */
5
13
  getVersion(): number;
14
+
15
+ /**
16
+ * Previous block hash
17
+ */
6
18
  getPrevBlockhash(): string;
19
+
20
+ /**
21
+ * Merkle root of the transactions tree
22
+ */
7
23
  getMerkleRoot(): string;
24
+
25
+ /**
26
+ * Timestamp of the block
27
+ */
8
28
  getTimestamp(): number;
29
+
30
+ /**
31
+ * nBits field of the block
32
+ */
9
33
  getNbits(): number;
34
+
35
+ /**
36
+ * Nonce of the block
37
+ */
10
38
  getNonce(): number;
39
+
40
+ /**
41
+ * Block hash of this block
42
+ */
11
43
  getHash(): string;
44
+
45
+ /**
46
+ * Height at which this block was mined
47
+ */
12
48
  getHeight(): number;
49
+
50
+ /**
51
+ * Total accumulated chainwork at this block
52
+ */
13
53
  getChainWork(): Buffer;
14
54
 
15
55
  }
@@ -1,12 +1,40 @@
1
1
  import {Buffer} from "buffer";
2
2
 
3
+ /**
4
+ * Represents a raw bitcoin header (80-bytes) that can be synchronized to the underlying BTC relay light client contract
5
+ *
6
+ * @category BTC Relay
7
+ */
3
8
  export interface BtcHeader {
4
9
 
10
+ /**
11
+ * Version of the block
12
+ */
5
13
  getVersion(): number,
14
+
15
+ /**
16
+ * Hash of the previous block in little-endian representation
17
+ */
6
18
  getReversedPrevBlockhash(): Buffer,
19
+
20
+ /**
21
+ * Merkle root of the transactions tree
22
+ */
7
23
  getMerkleRoot(): Buffer,
8
- getTimestamp(): number,
9
- getNbits(): number,
10
- getNonce(): number
24
+
25
+ /**
26
+ * Timestamp of the block
27
+ */
28
+ getTimestamp(): number;
29
+
30
+ /**
31
+ * nBits field of the block
32
+ */
33
+ getNbits(): number;
34
+
35
+ /**
36
+ * Nonce of the block
37
+ */
38
+ getNonce(): number;
11
39
 
12
40
  }
@@ -1,13 +1,44 @@
1
1
  import {BtcHeader} from "./BtcHeader";
2
2
  import {Buffer} from "buffer";
3
3
 
4
+ /**
5
+ * Represents a bitcoin blockheader that has already been synchronized and saved (committed) in the BTC relay
6
+ * contract
7
+ *
8
+ * @category BTC Relay
9
+ */
4
10
  export interface BtcStoredHeader<T extends BtcHeader> {
5
11
 
12
+ /**
13
+ * Total accumulated chainwork at this block
14
+ */
6
15
  getChainWork(): Buffer,
16
+
17
+ /**
18
+ * The actual blockheader that was saved
19
+ */
7
20
  getHeader(): T,
21
+
22
+ /**
23
+ * UNIX seconds timestamp of the last difficulty adjustment
24
+ */
8
25
  getLastDiffAdjustment(): number,
26
+
27
+ /**
28
+ * Blockheight of the current block
29
+ */
9
30
  getBlockheight(): number,
31
+
32
+ /**
33
+ * UNIX seconds timestamps of the last 11 blocks, used for checking the median block time rule
34
+ */
10
35
  getPrevBlockTimestamps(): number[],
36
+
37
+ /**
38
+ * Computes and returns a new stored blockheader after adding a new blockheader on top of it
39
+ *
40
+ * @param header The new blockheader to append to the chain
41
+ */
11
42
  computeNext(header: T): BtcStoredHeader<T>;
12
43
 
13
44
  }
@@ -1,6 +1,11 @@
1
1
  import {Buffer} from "buffer";
2
2
  import {BigIntBufferUtils} from "../../utils/BigIntBufferUtils";
3
3
 
4
+ /**
5
+ * Big integer arithmetic helpers for block difficulty calculations
6
+ *
7
+ * @internal
8
+ */
4
9
  export class StatePredictorUtils {
5
10
 
6
11
  static readonly DIFF_ADJUSTMENT_PERIOD = 2016;
@@ -4,6 +4,11 @@ import {BitcoinNetwork} from "../btc/BitcoinNetwork";
4
4
  import {IStorageManager} from "../storage/IStorageManager";
5
5
  import {StorageObject} from "../storage/StorageObject";
6
6
 
7
+ /**
8
+ * A type defining tokens available on the selected chain, indexed by ticker
9
+ *
10
+ * @category Tokens
11
+ */
7
12
  export type BaseTokenType<T extends string = string> = {
8
13
  [ticker in T]: {
9
14
  address: string,
@@ -12,6 +17,11 @@ export type BaseTokenType<T extends string = string> = {
12
17
  }
13
18
  };
14
19
 
20
+ /**
21
+ * A comprehensive chain data type for a specific chain
22
+ *
23
+ * @category Chains
24
+ */
15
25
  export type ChainData<T extends ChainType> = {
16
26
  chainId: ChainType["ChainId"],
17
27
  chainInterface: T["ChainInterface"],
@@ -24,6 +34,12 @@ export type ChainData<T extends ChainType> = {
24
34
  spvVaultWithdrawalDataConstructor: new (data: any) => T["SpvVaultWithdrawalData"]
25
35
  };
26
36
 
37
+ /**
38
+ * An initializer function that returns populated {@link ChainData} for a given chain based on the passed
39
+ * arguments
40
+ *
41
+ * @category Chains
42
+ */
27
43
  export type ChainInitializerFn<O, C extends ChainType> = (
28
44
  options: O,
29
45
  bitcoinRpc: BitcoinRpc<any>,
@@ -31,10 +47,34 @@ export type ChainInitializerFn<O, C extends ChainType> = (
31
47
  storageCtor: <T extends StorageObject>(name: string) => IStorageManager<T>
32
48
  ) => ChainData<C>;
33
49
 
50
+ /**
51
+ * Chain definition containing
52
+ *
53
+ * @category Chains
54
+ */
34
55
  export type ChainInitializer<O, C extends ChainType, T extends BaseTokenType> = {
56
+ /**
57
+ * Chain identifier string
58
+ */
35
59
  chainId: C["ChainId"],
36
- chainType: C,
60
+ /**
61
+ * Initializer function returning the {@link ChainData}
62
+ */
37
63
  initializer: ChainInitializerFn<O, C>,
64
+ /**
65
+ * Available tokens on the chain
66
+ */
38
67
  tokens: T,
68
+ /**
69
+ * Chain type
70
+ *
71
+ * NOTE: This is just a type reference, should not be used as value
72
+ */
73
+ chainType: C,
74
+ /**
75
+ * The type of the options to be passed to the initializer function
76
+ *
77
+ * NOTE: This is just a type reference, should not be used as value
78
+ */
39
79
  options: O
40
80
  }
@@ -1,9 +1,18 @@
1
+ /**
2
+ * @category Chain
3
+ */
1
4
  export type TransactionConfirmationOptions = {
2
5
  waitForConfirmation?: boolean,
3
6
  abortSignal?: AbortSignal,
4
7
  feeRate?: string
5
8
  };
6
9
 
10
+ /**
11
+ * Defines of a signer, contains an address getter and optionally init() & stop() functions
12
+ * to initiate internal processes inside the signer (if required and exposed)
13
+ *
14
+ * @category Signer
15
+ */
7
16
  export type AbstractSigner = {
8
17
  type: "AtomiqAbstractSigner",
9
18
  getAddress: () => string,
@@ -11,17 +20,29 @@ export type AbstractSigner = {
11
20
  stop?: () => Promise<void>
12
21
  };
13
22
 
23
+ /**
24
+ * Type guard for the {@link AbstractSigner} type
25
+ *
26
+ * @param val
27
+ */
14
28
  export function isAbstractSigner(val: any): val is AbstractSigner {
15
29
  return typeof(val)==="object" && val.type==="AtomiqAbstractSigner" && typeof(val.getAddress)==="function";
16
30
  }
17
31
 
32
+ /**
33
+ * An interface representing a smart chain, allowing basic operations on the chain and reading chain data
34
+ */
18
35
  export interface ChainInterface<
19
36
  TX = any,
37
+ SignedTX = any,
20
38
  Signer extends AbstractSigner = AbstractSigner,
21
39
  ChainId extends string = string,
22
40
  NativeSigner = any
23
41
  > {
24
42
 
43
+ /**
44
+ * Chain identifier string
45
+ */
25
46
  readonly chainId: ChainId;
26
47
 
27
48
  /**
@@ -95,6 +116,20 @@ export interface ChainInterface<
95
116
  */
96
117
  deserializeTx(txData: string): Promise<TX>;
97
118
 
119
+ /**
120
+ * Serializes a given transaction to a string
121
+ *
122
+ * @param signedTX Transaction to serialize
123
+ */
124
+ serializeSignedTx(signedTX: SignedTX): Promise<string>;
125
+
126
+ /**
127
+ * Deserializes a transaction from string
128
+ *
129
+ * @param txData Serialized transaction data string
130
+ */
131
+ deserializeSignedTx(txData: string): Promise<SignedTX>;
132
+
98
133
  /**
99
134
  * Returns the status of the given serialized transaction
100
135
  *
@@ -128,6 +163,18 @@ export interface ChainInterface<
128
163
  */
129
164
  sendAndConfirm(signer: Signer, txs: TX[], waitForConfirmation?: boolean, abortSignal?: AbortSignal, parallel?: boolean, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string[]>;
130
165
 
166
+ /**
167
+ * Sends already signed transactions and optionally waits for their confirmation
168
+ *
169
+ * @param signedTxs Signed transactions to be sent
170
+ * @param waitForConfirmation Whether to wait for transaction confirmation (if parallel is not specified,
171
+ * every transaction's confirmation except the last one is awaited)
172
+ * @param abortSignal Abort signal
173
+ * @param parallel Whether to send all transactions in parallel or one by one (always waiting for the previous TX to confirm)
174
+ * @param onBeforePublish Callback called before a tx is broadcast
175
+ */
176
+ sendSignedAndConfirm(signedTxs: SignedTX[], waitForConfirmation?: boolean, abortSignal?: AbortSignal, parallel?: boolean, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string[]>;
177
+
131
178
  /**
132
179
  * Callback called when transaction is being replaced (used for EVM, when fee is bumped on an unconfirmed tx)
133
180
  *
@@ -7,26 +7,33 @@ import {SpvVaultData} from "../spv_swap/SpvVaultData";
7
7
  import {SpvVaultContract} from "../spv_swap/SpvVaultContract";
8
8
  import {SpvWithdrawalTransactionData} from "../spv_swap/SpvWithdrawalTransactionData";
9
9
 
10
+ /**
11
+ * A comprehensive unified type struct for a given chain, contains all the type definitions of a given chain
12
+ *
13
+ * @category Chains
14
+ */
10
15
  export type ChainType<
11
16
  ChainId extends string = string,
12
17
  PreFetchData = any,
13
18
  PreFetchVerification = any,
14
19
  TXType = any,
20
+ SignedTXType = any,
15
21
  Signer extends AbstractSigner = AbstractSigner,
16
22
  NativeSigner = any,
17
23
  T extends SwapData = SwapData,
18
24
  C extends SwapContract<T, TXType, PreFetchData, PreFetchVerification, Signer, ChainId> = SwapContract<T, TXType, PreFetchData, PreFetchVerification, Signer, ChainId>,
19
- I extends ChainInterface<TXType, Signer, ChainId, NativeSigner> = ChainInterface<TXType, Signer, ChainId, NativeSigner>,
25
+ I extends ChainInterface<TXType, SignedTXType, Signer, ChainId, NativeSigner> = ChainInterface<TXType, SignedTXType, Signer, ChainId, NativeSigner>,
20
26
  E extends ChainEvents<T> = ChainEvents<T>,
21
27
  B extends BtcRelay<any, TXType, any, Signer> = BtcRelay<any, TXType, any, Signer>,
22
- SpvData extends SpvVaultData = SpvVaultData,
23
28
  SpvWithdrawalData extends SpvWithdrawalTransactionData = SpvWithdrawalTransactionData,
24
- SpvContract extends SpvVaultContract<TXType, Signer, ChainId, SpvData, SpvWithdrawalData> = SpvVaultContract<TXType, Signer, ChainId, SpvData, SpvWithdrawalData>
29
+ SpvData extends SpvVaultData<SpvWithdrawalData> = SpvVaultData<SpvWithdrawalData>,
30
+ SpvContract extends SpvVaultContract<TXType, Signer, ChainId, SpvWithdrawalData, SpvData> = SpvVaultContract<TXType, Signer, ChainId, SpvWithdrawalData, SpvData>
25
31
  > = {
26
32
  ChainId: ChainId,
27
33
  PreFetchData: PreFetchData,
28
34
  PreFetchVerification: PreFetchVerification,
29
35
  TX: TXType,
36
+ SignedTXType: SignedTXType,
30
37
  Signer: Signer,
31
38
  ChainInterface: I,
32
39
  Data: T,
@@ -1,4 +1,8 @@
1
-
1
+ /**
2
+ * Represents a Solana-specific error when an ATA (associated token account) couldn't be initialized
3
+ *
4
+ * @category Errors
5
+ */
2
6
  export class CannotInitializeATAError extends Error {
3
7
 
4
8
  constructor(msg: string) {
@@ -1,4 +1,8 @@
1
-
1
+ /**
2
+ * Represents a signature verification error
3
+ *
4
+ * @category Errors
5
+ */
2
6
  export class SignatureVerificationError extends Error {
3
7
 
4
8
  constructor(msg: string) {
@@ -1,4 +1,8 @@
1
-
1
+ /**
2
+ * Represents an error or inconsistency when verifying {@link SwapData}
3
+ *
4
+ * @category Errors
5
+ */
2
6
  export class SwapDataVerificationError extends Error {
3
7
 
4
8
  constructor(msg: string) {
@@ -1,4 +1,8 @@
1
-
1
+ /**
2
+ * Indicates that the sent transaction has reverted (i.e. was confirmed but the execution failed)
3
+ *
4
+ * @category Errors
5
+ */
2
6
  export class TransactionRevertedError extends Error {
3
7
 
4
8
  constructor(msg: string) {