@atomiqlabs/base 12.0.4 → 13.1.2
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/dist/btc/BitcoinNetwork.d.ts +5 -0
- package/dist/btc/BitcoinNetwork.js +5 -0
- package/dist/btc/lightning/LightningNetworkApi.d.ts +23 -0
- package/dist/btc/lightning/LightningNetworkApi.js +2 -0
- package/dist/btc/rpc/BitcoinRpc.d.ts +174 -7
- package/dist/btc/rpc/BitcoinRpcWithAddressIndex.d.ts +112 -0
- package/dist/btc/rpc/BitcoinRpcWithAddressIndex.js +2 -0
- package/dist/btcrelay/BtcRelay.d.ts +115 -6
- package/dist/btcrelay/synchronizer/RelaySynchronizer.d.ts +55 -14
- package/dist/btcrelay/types/BtcBlock.d.ts +32 -0
- package/dist/btcrelay/types/BtcHeader.d.ts +23 -0
- package/dist/btcrelay/types/BtcStoredHeader.d.ts +26 -0
- package/dist/btcrelay/utils/StatePredictorUtils.d.ts +5 -0
- package/dist/btcrelay/utils/StatePredictorUtils.js +5 -0
- package/dist/chains/ChainData.d.ts +41 -1
- package/dist/chains/ChainInterface.d.ts +44 -1
- package/dist/chains/ChainInterface.js +5 -0
- package/dist/chains/ChainType.d.ts +7 -1
- package/dist/errors/CannotInitializeATAError.d.ts +5 -0
- package/dist/errors/CannotInitializeATAError.js +5 -0
- package/dist/errors/SignatureVerificationError.d.ts +5 -0
- package/dist/errors/SignatureVerificationError.js +5 -0
- package/dist/errors/SwapDataVerificationError.d.ts +5 -0
- package/dist/errors/SwapDataVerificationError.js +5 -0
- package/dist/errors/TransactionRevertedError.d.ts +5 -0
- package/dist/errors/TransactionRevertedError.js +5 -0
- package/dist/events/ChainEvents.d.ts +40 -2
- package/dist/events/types/ChainEvent.d.ts +8 -0
- package/dist/events/types/ChainEvent.js +5 -0
- package/dist/events/types/spv_vault/SpvVaultClaimEvent.d.ts +29 -0
- package/dist/events/types/spv_vault/SpvVaultClaimEvent.js +5 -0
- package/dist/events/types/spv_vault/SpvVaultCloseEvent.d.ts +13 -0
- package/dist/events/types/spv_vault/SpvVaultCloseEvent.js +7 -0
- package/dist/events/types/spv_vault/SpvVaultDepositEvent.d.ts +12 -0
- package/dist/events/types/spv_vault/SpvVaultDepositEvent.js +5 -0
- package/dist/events/types/spv_vault/SpvVaultEvent.d.ts +18 -2
- package/dist/events/types/spv_vault/SpvVaultEvent.js +10 -0
- package/dist/events/types/spv_vault/SpvVaultFrontEvent.d.ts +22 -0
- package/dist/events/types/spv_vault/SpvVaultFrontEvent.js +5 -0
- package/dist/events/types/spv_vault/SpvVaultOpenEvent.d.ts +11 -0
- package/dist/events/types/spv_vault/SpvVaultOpenEvent.js +5 -0
- package/dist/events/types/swap/ClaimEvent.d.ts +8 -0
- package/dist/events/types/swap/ClaimEvent.js +6 -0
- package/dist/events/types/swap/InitializeEvent.d.ts +13 -2
- package/dist/events/types/swap/InitializeEvent.js +6 -0
- package/dist/events/types/swap/RefundEvent.d.ts +5 -0
- package/dist/events/types/swap/RefundEvent.js +9 -0
- package/dist/events/types/swap/SwapEvent.d.ts +15 -2
- package/dist/events/types/swap/SwapEvent.js +10 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/lockable/Lockable.d.ts +18 -0
- package/dist/lockable/Lockable.js +18 -0
- package/dist/messaging/Messenger.d.ts +32 -0
- package/dist/messaging/messages/Message.d.ts +24 -1
- package/dist/messaging/messages/Message.js +23 -0
- package/dist/messaging/messages/SwapClaimWitnessMessage.d.ts +22 -4
- package/dist/messaging/messages/SwapClaimWitnessMessage.js +12 -0
- package/dist/spv_swap/SpvVaultContract.d.ts +18 -10
- package/dist/spv_swap/SpvVaultData.d.ts +76 -0
- package/dist/spv_swap/SpvVaultData.js +19 -0
- package/dist/spv_swap/SpvWithdrawalState.d.ts +46 -0
- package/dist/spv_swap/SpvWithdrawalState.js +19 -0
- package/dist/spv_swap/SpvWithdrawalTransactionData.d.ts +94 -3
- package/dist/spv_swap/SpvWithdrawalTransactionData.js +81 -4
- package/dist/storage/IStorageManager.d.ts +37 -0
- package/dist/storage/StorageObject.d.ts +8 -0
- package/dist/swaps/ChainSwapType.d.ts +22 -0
- package/dist/swaps/ChainSwapType.js +22 -0
- package/dist/swaps/SwapCommitState.d.ts +50 -0
- package/dist/swaps/SwapCommitState.js +20 -0
- package/dist/swaps/SwapContract.d.ts +58 -2
- package/dist/swaps/SwapData.d.ts +147 -6
- package/dist/swaps/SwapData.js +20 -0
- package/dist/utils/BigIntBufferUtils.d.ts +5 -0
- package/dist/utils/BigIntBufferUtils.js +6 -1
- package/dist/utils/Logger.d.ts +17 -0
- package/dist/utils/Logger.js +17 -0
- package/dist/utils/RetryUtils.d.ts +23 -0
- package/dist/utils/RetryUtils.js +68 -0
- package/dist/utils/TimeoutUtils.d.ts +19 -0
- package/dist/utils/TimeoutUtils.js +57 -0
- package/package.json +5 -3
- package/src/btc/BitcoinNetwork.ts +5 -1
- package/src/btc/lightning/LightningNetworkApi.ts +27 -0
- package/src/btc/rpc/BitcoinRpc.ts +182 -8
- package/src/btc/rpc/BitcoinRpcWithAddressIndex.ts +138 -0
- package/src/btcrelay/BtcRelay.ts +121 -7
- package/src/btcrelay/synchronizer/RelaySynchronizer.ts +51 -10
- package/src/btcrelay/types/BtcBlock.ts +40 -0
- package/src/btcrelay/types/BtcHeader.ts +31 -3
- package/src/btcrelay/types/BtcStoredHeader.ts +31 -0
- package/src/btcrelay/utils/StatePredictorUtils.ts +5 -0
- package/src/chains/ChainData.ts +41 -1
- package/src/chains/ChainInterface.ts +47 -0
- package/src/chains/ChainType.ts +10 -3
- package/src/errors/CannotInitializeATAError.ts +5 -1
- package/src/errors/SignatureVerificationError.ts +5 -1
- package/src/errors/SwapDataVerificationError.ts +5 -1
- package/src/errors/TransactionRevertedError.ts +5 -1
- package/src/events/ChainEvents.ts +44 -2
- package/src/events/types/ChainEvent.ts +8 -0
- package/src/events/types/spv_vault/SpvVaultClaimEvent.ts +29 -0
- package/src/events/types/spv_vault/SpvVaultCloseEvent.ts +13 -0
- package/src/events/types/spv_vault/SpvVaultDepositEvent.ts +13 -0
- package/src/events/types/spv_vault/SpvVaultEvent.ts +18 -3
- package/src/events/types/spv_vault/SpvVaultFrontEvent.ts +22 -0
- package/src/events/types/spv_vault/SpvVaultOpenEvent.ts +11 -0
- package/src/events/types/swap/ClaimEvent.ts +9 -2
- package/src/events/types/swap/InitializeEvent.ts +14 -4
- package/src/events/types/swap/RefundEvent.ts +6 -1
- package/src/events/types/swap/SwapEvent.ts +15 -3
- package/src/index.ts +5 -0
- package/src/lockable/Lockable.ts +18 -2
- package/src/messaging/Messenger.ts +37 -0
- package/src/messaging/messages/Message.ts +24 -3
- package/src/messaging/messages/SwapClaimWitnessMessage.ts +23 -6
- package/src/spv_swap/SpvVaultContract.ts +19 -12
- package/src/spv_swap/SpvVaultData.ts +84 -0
- package/src/spv_swap/SpvWithdrawalState.ts +46 -1
- package/src/spv_swap/SpvWithdrawalTransactionData.ts +102 -7
- package/src/storage/IStorageManager.ts +41 -0
- package/src/storage/StorageObject.ts +8 -2
- package/src/swaps/ChainSwapType.ts +22 -0
- package/src/swaps/SwapCommitState.ts +50 -0
- package/src/swaps/SwapContract.ts +58 -2
- package/src/swaps/SwapData.ts +160 -7
- package/src/utils/BigIntBufferUtils.ts +6 -1
- package/src/utils/Logger.ts +25 -0
- package/src/utils/RetryUtils.ts +73 -0
- package/src/utils/TimeoutUtils.ts +52 -0
|
@@ -6,6 +6,9 @@ import {Buffer} from "buffer";
|
|
|
6
6
|
import {AbstractSigner, TransactionConfirmationOptions} from "../chains/ChainInterface";
|
|
7
7
|
import {SwapCommitState} from "./SwapCommitState";
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Represents intermediary (LP) reputation across different swap types
|
|
11
|
+
*/
|
|
9
12
|
export type IntermediaryReputationType = {
|
|
10
13
|
[key in ChainSwapType]: {
|
|
11
14
|
successVolume: bigint,
|
|
@@ -17,20 +20,46 @@ export type IntermediaryReputationType = {
|
|
|
17
20
|
}
|
|
18
21
|
};
|
|
19
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Signature authorization data
|
|
25
|
+
*/
|
|
20
26
|
export type SignatureData = {
|
|
21
27
|
prefix: string,
|
|
22
28
|
timeout: string,
|
|
23
29
|
signature: string
|
|
24
30
|
};
|
|
25
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Simplified bitcoin transaction data
|
|
34
|
+
*/
|
|
26
35
|
export type BitcoinTransactionData = {
|
|
36
|
+
/**
|
|
37
|
+
* Blockhash of a block which contains this transaction
|
|
38
|
+
*/
|
|
27
39
|
blockhash: string,
|
|
40
|
+
/**
|
|
41
|
+
* The number of confirmations of a block containing this transaction
|
|
42
|
+
*/
|
|
28
43
|
confirmations: number,
|
|
44
|
+
/**
|
|
45
|
+
* Transaction ID identifier of the transaction
|
|
46
|
+
*/
|
|
29
47
|
txid: string,
|
|
48
|
+
/**
|
|
49
|
+
* A hexadecimal serialized representation of the transaction with witness data stripped (legacy transaction format)
|
|
50
|
+
*/
|
|
30
51
|
hex: string,
|
|
52
|
+
/**
|
|
53
|
+
* The blockheight of a block containing this transaction
|
|
54
|
+
*/
|
|
31
55
|
height: number
|
|
32
56
|
};
|
|
33
57
|
|
|
58
|
+
/**
|
|
59
|
+
* A contract for interacting with escrow manager swaps - escrow based swaps like HTLCs and PrTLCs
|
|
60
|
+
*
|
|
61
|
+
* @category Swaps
|
|
62
|
+
*/
|
|
34
63
|
export interface SwapContract<
|
|
35
64
|
T extends SwapData = SwapData,
|
|
36
65
|
TX = any,
|
|
@@ -40,10 +69,28 @@ export interface SwapContract<
|
|
|
40
69
|
ChainId extends string = string
|
|
41
70
|
> {
|
|
42
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Chain identifier string
|
|
74
|
+
*/
|
|
43
75
|
readonly chainId: ChainId;
|
|
76
|
+
/**
|
|
77
|
+
* A timeout to be used when claiming the escrow with a secret (HTLC), recommended to wait at least this long after
|
|
78
|
+
* sending a transaction before considering it a failure
|
|
79
|
+
*/
|
|
44
80
|
readonly claimWithSecretTimeout: number;
|
|
81
|
+
/**
|
|
82
|
+
* A timeout to be used when claiming the escrow with bitcoin transaction data (PrTLC), recommended to wait at
|
|
83
|
+
* least this long after sending a transaction before considering it a failure
|
|
84
|
+
*/
|
|
45
85
|
readonly claimWithTxDataTimeout: number;
|
|
86
|
+
/**
|
|
87
|
+
* A timeout to be used when refunding the escrow, recommended to wait at
|
|
88
|
+
* least this long after sending a transaction before considering it a failure
|
|
89
|
+
*/
|
|
46
90
|
readonly refundTimeout: number;
|
|
91
|
+
/**
|
|
92
|
+
* Whether this chain supports sending initialization without a direct authorization by the claimer
|
|
93
|
+
*/
|
|
47
94
|
readonly supportsInitWithoutClaimer?: boolean;
|
|
48
95
|
|
|
49
96
|
/**
|
|
@@ -265,7 +312,7 @@ export interface SwapContract<
|
|
|
265
312
|
state: SwapCommitState
|
|
266
313
|
}
|
|
267
314
|
},
|
|
268
|
-
latestBlockheight
|
|
315
|
+
latestBlockheight?: number
|
|
269
316
|
}>;
|
|
270
317
|
|
|
271
318
|
/**
|
|
@@ -410,7 +457,7 @@ export interface SwapContract<
|
|
|
410
457
|
* @param address
|
|
411
458
|
* @param token
|
|
412
459
|
*/
|
|
413
|
-
getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType>;
|
|
460
|
+
getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType | null>;
|
|
414
461
|
|
|
415
462
|
/**
|
|
416
463
|
* Returns the fee in native token base units to commit (initiate) the swap
|
|
@@ -501,6 +548,15 @@ export interface SwapContract<
|
|
|
501
548
|
*/
|
|
502
549
|
getClaimFeeRate(signer: string, swapData: T): Promise<string>;
|
|
503
550
|
|
|
551
|
+
/**
|
|
552
|
+
* Returns additional data to be included in the data-carrier (extra data) part of the PrTLC escrow initialization
|
|
553
|
+
* this provides helpers for 3rd party claimers
|
|
554
|
+
*
|
|
555
|
+
* @param outputScript A bitcoin output script required in the bitcoin transaction to claim the escrow
|
|
556
|
+
* @param amount An amount of bitcoin (in satoshis) required in the bitcoin transaction to claim the escrow
|
|
557
|
+
* @param confirmations Confirmations of the bitcoin transaction required for claiming the escrow
|
|
558
|
+
* @param nonce An optional nonce applied to the transaction
|
|
559
|
+
*/
|
|
504
560
|
getExtraData(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer;
|
|
505
561
|
|
|
506
562
|
/**
|
package/src/swaps/SwapData.ts
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
import {ChainSwapType} from "./ChainSwapType";
|
|
2
2
|
import {StorageObject} from "../storage/StorageObject";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Represents full escrow swap data and parameters
|
|
6
|
+
*
|
|
7
|
+
* @category Swaps
|
|
8
|
+
*/
|
|
4
9
|
export abstract class SwapData implements StorageObject {
|
|
5
10
|
|
|
11
|
+
/**
|
|
12
|
+
* A mapping of deserializers for different escrow swap data types coming from different smart chain implementations
|
|
13
|
+
*/
|
|
6
14
|
static deserializers: {
|
|
7
15
|
[type: string]: new (serialized: any) => any,
|
|
8
16
|
} = {};
|
|
9
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Deserializer parsing the chain-specific escrow swap data from a JSON-compatible object representation
|
|
20
|
+
*
|
|
21
|
+
* @param data
|
|
22
|
+
*/
|
|
10
23
|
static deserialize<T extends SwapData>(data: any): T {
|
|
11
24
|
const deserializer = SwapData.deserializers[data.type];
|
|
12
25
|
if(deserializer != null) {
|
|
@@ -14,56 +27,196 @@ export abstract class SwapData implements StorageObject {
|
|
|
14
27
|
}
|
|
15
28
|
throw new Error(`No deserializer found for swap data type: ${data.type}`);
|
|
16
29
|
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Gets the offerer, which funds the escrow
|
|
33
|
+
*/
|
|
17
34
|
abstract getOfferer(): string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Sets the offerer to fund the escrow
|
|
38
|
+
*
|
|
39
|
+
* @param newOfferer
|
|
40
|
+
*/
|
|
18
41
|
abstract setOfferer(newOfferer: string): void;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Checks whether the provided address is an offerer for this escrow
|
|
45
|
+
*
|
|
46
|
+
* @param address
|
|
47
|
+
*/
|
|
19
48
|
abstract isOfferer(address: string): boolean;
|
|
20
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Gets the claim, which receives funds from the escrow
|
|
52
|
+
*/
|
|
21
53
|
abstract getClaimer(): string;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Sets the claimer which receives funds from the escrow
|
|
57
|
+
*
|
|
58
|
+
* @param newClaimer
|
|
59
|
+
*/
|
|
22
60
|
abstract setClaimer(newClaimer: string): void;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Checks whether the provided address is a claimer for this escrow
|
|
64
|
+
*
|
|
65
|
+
* @param address
|
|
66
|
+
*/
|
|
23
67
|
abstract isClaimer(address: string): boolean;
|
|
24
68
|
|
|
69
|
+
/**
|
|
70
|
+
* @inheritDoc
|
|
71
|
+
*/
|
|
25
72
|
abstract serialize(): any;
|
|
26
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Returns the type of this escrow
|
|
76
|
+
*/
|
|
27
77
|
abstract getType(): ChainSwapType;
|
|
28
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Gets the amount of token deposited into the vault by the offerer
|
|
81
|
+
*/
|
|
29
82
|
abstract getAmount(): bigint;
|
|
30
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Gets the address of the deposited token in a vault
|
|
86
|
+
*/
|
|
31
87
|
abstract getToken(): string;
|
|
32
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Checks whether the provided token address is the actual token used by this escrow
|
|
91
|
+
*
|
|
92
|
+
* @param token
|
|
93
|
+
*/
|
|
33
94
|
abstract isToken(token: string): boolean;
|
|
34
95
|
|
|
96
|
+
/**
|
|
97
|
+
* Returns the expiration time of this escrow, after the timeout an offerer is able to refund unilaterally.
|
|
98
|
+
* taking back funds from the escrow
|
|
99
|
+
*/
|
|
35
100
|
abstract getExpiry(): bigint;
|
|
36
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Whether this escrow will pay out the claimer, when `true` it pays out normally to the claimer's address, when
|
|
104
|
+
* `false` it instead keeps the funds inside the contract and assigns them to the claimer's vault inside the
|
|
105
|
+
* contract
|
|
106
|
+
*/
|
|
37
107
|
abstract isPayOut(): boolean;
|
|
38
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Whether this escrow will be funded from the offerer's wallet or from the offerer's vault inside the contract,
|
|
111
|
+
* when `true` it takes funds normally from the offerer's address, when `false` it pulls the funds from the
|
|
112
|
+
* offerer's vault inside the contract
|
|
113
|
+
*/
|
|
39
114
|
abstract isPayIn(): boolean;
|
|
40
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Whether the outcome of this escrow should be used to track reputation of the claimer
|
|
118
|
+
*/
|
|
119
|
+
abstract isTrackingReputation(): boolean;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Returns the claim hash of the escrow, this specifies a condition that needs to be satisfied to claim
|
|
123
|
+
* the funds from the escrow
|
|
124
|
+
*/
|
|
41
125
|
abstract getClaimHash(): string;
|
|
42
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Returns a unique hash of this escrow
|
|
129
|
+
*/
|
|
43
130
|
abstract getEscrowHash(): string;
|
|
44
131
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
abstract
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
132
|
+
/**
|
|
133
|
+
* Returns a random variable sequence of this vault, used to ensure the escrow hash is always different
|
|
134
|
+
*/
|
|
135
|
+
abstract getSequence(): bigint;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Gets the extraneous data specified when creating this escrow, this provides helpers for 3rd party claimers
|
|
139
|
+
*/
|
|
140
|
+
abstract getExtraData(): string | null;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Retrieves required number of confirmations as a hint from the extra data provided, or `null` if no extra
|
|
144
|
+
* data has been provided when creating the escrow
|
|
145
|
+
*/
|
|
146
|
+
abstract getConfirmationsHint(): number | null;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Retrieves the PrTLC transaction nonce as a hint from the extra data provided, or `null` if no extra
|
|
150
|
+
* data has been provided when creating the escrow
|
|
151
|
+
*/
|
|
152
|
+
abstract getNonceHint(): bigint | null;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Retrieves the PrTLC transaction txo (transaction output) hash as a hint from the extra data provided,
|
|
156
|
+
* or `null` if no extra data has been provided when creating the escrow
|
|
157
|
+
*/
|
|
158
|
+
abstract getTxoHashHint(): string | null;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Retrieves a hint providing an HTLC hash from the extra data provided, or `null` if no extra data
|
|
162
|
+
* has been provided when creating the escrow
|
|
163
|
+
*/
|
|
164
|
+
getHTLCHashHint(): string | null {
|
|
52
165
|
return null;
|
|
53
166
|
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Sets the extra data with hints for this escrow
|
|
170
|
+
*
|
|
171
|
+
* @param extraData
|
|
172
|
+
*/
|
|
54
173
|
abstract setExtraData(extraData: string): void;
|
|
55
174
|
|
|
175
|
+
/**
|
|
176
|
+
* Gets the security deposit, which is an amount of native tokens deposited by the caller (initiator) on
|
|
177
|
+
* escrow initialization to guarantee the execution, this acts as a compensation for offerer if he has
|
|
178
|
+
* to refund the escrow, otherwise it is returned back to claimer after a successful claim of the escrow
|
|
179
|
+
*/
|
|
56
180
|
abstract getSecurityDeposit(): bigint;
|
|
57
181
|
|
|
182
|
+
/**
|
|
183
|
+
* Gets the claimer bounty, an amount of native tokens deposited by the caller (initiator) on escrow
|
|
184
|
+
* initialization, that is paid out to a caller which claims the escrow as a reward, otherwise
|
|
185
|
+
* returned to the claimer if escrow is refunded
|
|
186
|
+
*/
|
|
58
187
|
abstract getClaimerBounty(): bigint;
|
|
59
188
|
|
|
189
|
+
/**
|
|
190
|
+
* Returns the total deposit in native token to be deposited by the caller (initiator) on escrow initialization,
|
|
191
|
+
* due to how claimer bounty and security deposits work, this is basically a max(claimer bounty, security deposit),
|
|
192
|
+
* because either a swap is claimed (and claimer bounty is paid) or a swap is refunded (and security deposit is paid),
|
|
193
|
+
* never both!
|
|
194
|
+
*/
|
|
60
195
|
abstract getTotalDeposit(): bigint;
|
|
61
196
|
|
|
197
|
+
/**
|
|
198
|
+
* Returns the token used for the deposit (security deposit & claimer bounty) (usually a native token of the
|
|
199
|
+
* underlying smart chain)
|
|
200
|
+
*/
|
|
62
201
|
abstract getDepositToken(): string;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Checks whether a provided token is used as a deposit token (security deposit & claimer bounty) for the escrow
|
|
205
|
+
*
|
|
206
|
+
* @param token
|
|
207
|
+
*/
|
|
63
208
|
abstract isDepositToken(token: string): boolean;
|
|
64
209
|
|
|
210
|
+
/**
|
|
211
|
+
* Checks equality between 2 swap data objects
|
|
212
|
+
*
|
|
213
|
+
* @param other
|
|
214
|
+
*/
|
|
65
215
|
abstract equals(other: SwapData): boolean;
|
|
66
216
|
|
|
217
|
+
/**
|
|
218
|
+
* Checks whether the escrow contains an execution action (swap+)
|
|
219
|
+
*/
|
|
67
220
|
hasSuccessAction(): boolean {
|
|
68
221
|
return false;
|
|
69
222
|
}
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import {Buffer} from "buffer";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* A helper for serializing and deserializing bigints from/to buffers
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
3
8
|
export const BigIntBufferUtils: {
|
|
4
9
|
toBuffer: (value: bigint, endianness?: "be" | "le", length?: number) => Buffer
|
|
5
10
|
fromBuffer: (value: Buffer, endianness?: "be" | "le") => bigint
|
|
6
11
|
} = {
|
|
7
|
-
toBuffer: (value: bigint, endianness: "be" | "le" = "be", length: number) => {
|
|
12
|
+
toBuffer: (value: bigint, endianness: "be" | "le" = "be", length: number = 32) => {
|
|
8
13
|
let values: number[] = Array(length);
|
|
9
14
|
for(let i=0;i<length;i++) {
|
|
10
15
|
values[i] = Number(value & 0xffn);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type of the logger
|
|
3
|
+
*
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export type LoggerType = {
|
|
7
|
+
debug: (msg: string, ...args: any[]) => void,
|
|
8
|
+
info: (msg: string, ...args: any[]) => void,
|
|
9
|
+
warn: (msg: string, ...args: any[]) => void,
|
|
10
|
+
error: (msg: string, ...args: any[]) => void
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Getter function to create a logger instance
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
export function getLogger(prefix: string): LoggerType {
|
|
19
|
+
return {
|
|
20
|
+
debug: (msg, ...args) => (global as any).atomiqLogLevel >= 3 && console.debug(prefix + msg, ...args),
|
|
21
|
+
info: (msg, ...args) => (global as any).atomiqLogLevel >= 2 && console.info(prefix + msg, ...args),
|
|
22
|
+
warn: (msg, ...args) => ((global as any).atomiqLogLevel == null || (global as any).atomiqLogLevel >= 1) && console.warn(prefix + msg, ...args),
|
|
23
|
+
error: (msg, ...args) => ((global as any).atomiqLogLevel == null || (global as any).atomiqLogLevel >= 0) && console.error(prefix + msg, ...args)
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {getLogger} from "./Logger";
|
|
2
|
+
import {timeoutPromise} from "./TimeoutUtils";
|
|
3
|
+
|
|
4
|
+
const logger = getLogger("RetryUtils: ");
|
|
5
|
+
|
|
6
|
+
type Constructor<T = any> = new (...args: any[]) => T;
|
|
7
|
+
|
|
8
|
+
function isConstructor(fn: any): fn is Constructor {
|
|
9
|
+
return (
|
|
10
|
+
typeof fn === 'function' &&
|
|
11
|
+
fn.prototype != null &&
|
|
12
|
+
fn.prototype.constructor === fn
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isConstructorArray(fnArr: any): fnArr is Constructor[] {
|
|
17
|
+
return Array.isArray(fnArr) && fnArr.every(isConstructor);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Checks whether the passed error is allowed to pass through
|
|
22
|
+
*
|
|
23
|
+
* @param e Error in question
|
|
24
|
+
* @param errorAllowed Allowed errors as defined as a callback function, specific error type, or an array of error types
|
|
25
|
+
*/
|
|
26
|
+
function checkError(e: any, errorAllowed: ((e: any) => boolean) | Constructor<Error> | Constructor<Error>[]) {
|
|
27
|
+
if (isConstructorArray(errorAllowed)) return errorAllowed.find(error => e instanceof error) != null;
|
|
28
|
+
if (isConstructor(errorAllowed)) return e instanceof errorAllowed;
|
|
29
|
+
return errorAllowed(e);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Runs the passed function multiple times if it fails
|
|
34
|
+
*
|
|
35
|
+
* @param func A callback for executing the action
|
|
36
|
+
* @param func.retryCount Count of the current retry, starting from 0 for original request and increasing
|
|
37
|
+
* @param retryPolicy Retry policy
|
|
38
|
+
* @param retryPolicy.maxRetries How many retries to attempt in total
|
|
39
|
+
* @param retryPolicy.delay How long should the delay be
|
|
40
|
+
* @param retryPolicy.exponential Whether to use exponentially increasing delays
|
|
41
|
+
* @param errorAllowed A callback for determining whether a given error is allowed, and we should therefore not retry
|
|
42
|
+
* @param abortSignal
|
|
43
|
+
*
|
|
44
|
+
* @returns Result of the action executing callback
|
|
45
|
+
*
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
export async function tryWithRetries<T>(func: (retryCount: number) => Promise<T>, retryPolicy?: {
|
|
49
|
+
maxRetries?: number, delay?: number, exponential?: boolean
|
|
50
|
+
}, errorAllowed?: ((e: any) => boolean) | Constructor<Error> | Constructor<Error>[], abortSignal?: AbortSignal): Promise<T> {
|
|
51
|
+
retryPolicy = retryPolicy || {};
|
|
52
|
+
retryPolicy.maxRetries = retryPolicy.maxRetries || 5;
|
|
53
|
+
retryPolicy.delay = retryPolicy.delay || 500;
|
|
54
|
+
retryPolicy.exponential = retryPolicy.exponential == null ? true : retryPolicy.exponential;
|
|
55
|
+
|
|
56
|
+
let err = null;
|
|
57
|
+
|
|
58
|
+
for (let i = 0; i < retryPolicy.maxRetries; i++) {
|
|
59
|
+
try {
|
|
60
|
+
return await func(i);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
if (errorAllowed != null && checkError(e, errorAllowed)) throw e;
|
|
63
|
+
err = e;
|
|
64
|
+
logger.warn("tryWithRetries(): Error on try number: " + i, e);
|
|
65
|
+
}
|
|
66
|
+
if (abortSignal != null && abortSignal.aborted) throw (abortSignal.reason || new Error("Aborted"));
|
|
67
|
+
if (i !== retryPolicy.maxRetries - 1) {
|
|
68
|
+
await timeoutPromise(retryPolicy.exponential ? retryPolicy.delay * Math.pow(2, i) : retryPolicy.delay, abortSignal);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
throw err;
|
|
73
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a promise that resolves after given amount seconds
|
|
3
|
+
*
|
|
4
|
+
* @param timeout how many milliseconds to wait for
|
|
5
|
+
* @param abortSignal
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export function timeoutPromise(timeout: number, abortSignal?: AbortSignal): Promise<void> {
|
|
10
|
+
return new Promise<void>((resolve, reject) => {
|
|
11
|
+
if (abortSignal != null && abortSignal.aborted) {
|
|
12
|
+
reject(abortSignal.reason);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let abortSignalListener: () => void;
|
|
17
|
+
let timeoutHandle: any | null = setTimeout(() => {
|
|
18
|
+
if (abortSignalListener != null && abortSignal != null) abortSignal.removeEventListener("abort", abortSignalListener);
|
|
19
|
+
resolve();
|
|
20
|
+
}, timeout);
|
|
21
|
+
|
|
22
|
+
if (abortSignal != null) {
|
|
23
|
+
abortSignal.addEventListener("abort", abortSignalListener = () => {
|
|
24
|
+
if (timeoutHandle != null) clearTimeout(timeoutHandle);
|
|
25
|
+
timeoutHandle = null;
|
|
26
|
+
reject(abortSignal.reason);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns an abort signal that aborts after a specified timeout in milliseconds
|
|
34
|
+
*
|
|
35
|
+
* @param timeout Milliseconds to wait
|
|
36
|
+
* @param abortReason Abort with this abort reason
|
|
37
|
+
* @param abortSignal Abort signal to extend
|
|
38
|
+
*
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
export function timeoutSignal(timeout: number, abortReason?: any, abortSignal?: AbortSignal): AbortSignal {
|
|
42
|
+
if (timeout == null) throw new Error("Timeout seconds cannot be null!");
|
|
43
|
+
const abortController = new AbortController();
|
|
44
|
+
const timeoutHandle = setTimeout(() => abortController.abort(abortReason || new Error("Timed out")), timeout);
|
|
45
|
+
if (abortSignal != null) {
|
|
46
|
+
abortSignal.addEventListener("abort", () => {
|
|
47
|
+
clearTimeout(timeoutHandle);
|
|
48
|
+
abortController.abort(abortSignal.reason);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return abortController.signal;
|
|
52
|
+
}
|