@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.
- package/dist/btc/BitcoinNetwork.d.ts +5 -0
- package/dist/btc/BitcoinNetwork.js +5 -0
- package/dist/btc/rpc/BitcoinRpc.d.ts +168 -7
- 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/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/package.json +5 -3
- package/src/btc/BitcoinNetwork.ts +5 -1
- package/src/btc/rpc/BitcoinRpc.ts +175 -8
- 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/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
|
@@ -5,43 +5,127 @@ import {SpvVaultCloseEvent} from "../events/types/spv_vault/SpvVaultCloseEvent";
|
|
|
5
5
|
import {SpvVaultOpenEvent} from "../events/types/spv_vault/SpvVaultOpenEvent";
|
|
6
6
|
import {SpvVaultDepositEvent} from "../events/types/spv_vault/SpvVaultDepositEvent";
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Balance for a specific token inside a vault
|
|
10
|
+
*
|
|
11
|
+
* @category Swaps
|
|
12
|
+
*/
|
|
8
13
|
export type SpvVaultTokenBalance = SpvVaultTokenData & {
|
|
14
|
+
/**
|
|
15
|
+
* A raw amount of the token, exactly as specified in the vault state
|
|
16
|
+
*/
|
|
9
17
|
rawAmount: bigint,
|
|
18
|
+
/**
|
|
19
|
+
* An actual amount of funds in the vault, calculated as `rawAmount` * `multiplier`
|
|
20
|
+
*/
|
|
10
21
|
scaledAmount: bigint
|
|
11
22
|
};
|
|
12
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Configuration for a specific token inside an SPV vault (UTXO-controlled vault)
|
|
26
|
+
*
|
|
27
|
+
* @category Swaps
|
|
28
|
+
*/
|
|
13
29
|
export type SpvVaultTokenData = {
|
|
30
|
+
/**
|
|
31
|
+
* Address of the token used
|
|
32
|
+
*/
|
|
14
33
|
token: string,
|
|
34
|
+
/**
|
|
35
|
+
* A multiplier to scale the token value with
|
|
36
|
+
*/
|
|
15
37
|
multiplier: bigint
|
|
16
38
|
}
|
|
17
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Represents the state of a single SPV vault (UTXO-controlled vault)
|
|
42
|
+
*
|
|
43
|
+
* @category Swaps
|
|
44
|
+
*/
|
|
18
45
|
export abstract class SpvVaultData<T extends SpvWithdrawalTransactionData = SpvWithdrawalTransactionData> implements StorageObject {
|
|
19
46
|
|
|
47
|
+
/**
|
|
48
|
+
* A mapping of deserializers for different spv vault data types coming from different smart chain implementations
|
|
49
|
+
*/
|
|
20
50
|
static deserializers: {
|
|
21
51
|
[type: string]: new (serialized: any) => any,
|
|
22
52
|
} = {};
|
|
23
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Deserializer parsing the chain-specific spv vault data from a JSON-compatible object representation
|
|
56
|
+
*
|
|
57
|
+
* @param data
|
|
58
|
+
*/
|
|
24
59
|
static deserialize<T extends SpvVaultData>(data: any): T {
|
|
25
60
|
if (SpvVaultData.deserializers[data.type] != null) {
|
|
26
61
|
return new SpvVaultData.deserializers[data.type](data) as unknown as T;
|
|
27
62
|
}
|
|
63
|
+
throw new Error(`No deserializer found for spv vault data type: ${data.type}`);
|
|
28
64
|
}
|
|
29
65
|
|
|
66
|
+
/**
|
|
67
|
+
* @inheritDoc
|
|
68
|
+
*/
|
|
30
69
|
abstract serialize(): any;
|
|
31
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Gets the owner of the vault
|
|
73
|
+
*/
|
|
32
74
|
abstract getOwner(): string;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Gets the vault ID, this along with the owner uniquely identifies a vault
|
|
78
|
+
*/
|
|
33
79
|
abstract getVaultId(): bigint;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns the configuration of the tokens supported by the vault
|
|
83
|
+
*/
|
|
34
84
|
abstract getTokenData(): SpvVaultTokenData[];
|
|
35
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Returns the token balance currently available in the vault
|
|
88
|
+
*/
|
|
36
89
|
abstract getBalances(): SpvVaultTokenBalance[];
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Returns the UTXO in [txId]:[vout] format which currently controls the vault
|
|
93
|
+
*/
|
|
37
94
|
abstract getUtxo(): string;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Gets the required number of confirmations that a bitcoin transaction has to get in order for the
|
|
98
|
+
* vault claim (withdrawal) to be authorized
|
|
99
|
+
*/
|
|
38
100
|
abstract getConfirmations(): number;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Returns the current number of claims (withdrawals) processed by the vault
|
|
104
|
+
*/
|
|
39
105
|
abstract getWithdrawalCount(): number;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Returns the current number of deposits deposited into the vault
|
|
109
|
+
*/
|
|
40
110
|
abstract getDepositCount(): number;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Checks whether a vault is opened and available to process claims (withdrawals)
|
|
114
|
+
*/
|
|
41
115
|
abstract isOpened(): boolean;
|
|
42
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Updates the state of the spv vault data from either an on-chain event or a {@link SpvWithdrawalTransactionData}
|
|
119
|
+
*
|
|
120
|
+
* @param withdrawalTxOrEvent
|
|
121
|
+
*/
|
|
43
122
|
abstract updateState(withdrawalTxOrEvent: T | SpvVaultClaimEvent | SpvVaultCloseEvent | SpvVaultOpenEvent | SpvVaultDepositEvent): void;
|
|
44
123
|
|
|
124
|
+
/**
|
|
125
|
+
* A helper function which calculates the vault balances after processing an array of claims (withdrawals)
|
|
126
|
+
*
|
|
127
|
+
* @param priorWithdrawalTxs
|
|
128
|
+
*/
|
|
45
129
|
calculateStateAfter(priorWithdrawalTxs: T[]): {withdrawalCount: number, balances: SpvVaultTokenBalance[]} {
|
|
46
130
|
const balances = [...this.getBalances()];
|
|
47
131
|
let withdrawalCount = this.getWithdrawalCount();
|
|
@@ -1,11 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Possible states of a vault withdrawal (claim) transaction
|
|
3
|
+
*
|
|
4
|
+
* @category Swaps
|
|
5
|
+
*/
|
|
2
6
|
export enum SpvWithdrawalStateType {
|
|
7
|
+
/**
|
|
8
|
+
* An SPV vault (UTXO-controlled vault), has been closed as a result of the claim (withdrawal) transaction,
|
|
9
|
+
* something probably went wrong with the bitcoin transaction format or parsing
|
|
10
|
+
*/
|
|
3
11
|
CLOSED = -1,
|
|
12
|
+
/**
|
|
13
|
+
* The withdrawal is not yet known on the smart chain side, i.e. the transaction on the bitcoin side
|
|
14
|
+
* didn't achieve enough confirmations yet, or it was not yet submitted on the smart chain side
|
|
15
|
+
*/
|
|
4
16
|
NOT_FOUND = 0,
|
|
17
|
+
/**
|
|
18
|
+
* The withdrawal was successfully processed on the smart chain side
|
|
19
|
+
*/
|
|
5
20
|
CLAIMED = 1,
|
|
21
|
+
/**
|
|
22
|
+
* The withdrawal has been fronted by some 3rd party
|
|
23
|
+
*/
|
|
6
24
|
FRONTED = 2
|
|
7
25
|
}
|
|
8
26
|
|
|
27
|
+
/**
|
|
28
|
+
* The withdrawal is not yet known on the smart chain side, i.e. the transaction on the bitcoin side
|
|
29
|
+
* didn't achieve enough confirmations yet, or it was not yet submitted on the smart chain side
|
|
30
|
+
*
|
|
31
|
+
* @category Swaps
|
|
32
|
+
*/
|
|
9
33
|
export type SpvWithdrawalNotFoundState = {
|
|
10
34
|
type: SpvWithdrawalStateType.NOT_FOUND
|
|
11
35
|
};
|
|
@@ -16,6 +40,11 @@ type SpvWithdrawalStateCommon = {
|
|
|
16
40
|
vaultId: bigint,
|
|
17
41
|
};
|
|
18
42
|
|
|
43
|
+
/**
|
|
44
|
+
* The withdrawal was successfully processed on the smart chain side
|
|
45
|
+
*
|
|
46
|
+
* @category Swaps
|
|
47
|
+
*/
|
|
19
48
|
export type SpvWithdrawalClaimedState = {
|
|
20
49
|
type: SpvWithdrawalStateType.CLAIMED,
|
|
21
50
|
recipient: string,
|
|
@@ -23,17 +52,33 @@ export type SpvWithdrawalClaimedState = {
|
|
|
23
52
|
fronter: string
|
|
24
53
|
} & SpvWithdrawalStateCommon;
|
|
25
54
|
|
|
55
|
+
/**
|
|
56
|
+
* The withdrawal has been fronted by some 3rd party
|
|
57
|
+
*
|
|
58
|
+
* @category Swaps
|
|
59
|
+
*/
|
|
26
60
|
export type SpvWithdrawalFrontedState = {
|
|
27
61
|
type: SpvWithdrawalStateType.FRONTED,
|
|
28
62
|
recipient: string,
|
|
29
63
|
fronter: string
|
|
30
64
|
} & SpvWithdrawalStateCommon;
|
|
31
65
|
|
|
66
|
+
/**
|
|
67
|
+
* An SPV vault (UTXO-controlled vault), has been closed as a result of the claim (withdrawal) transaction,
|
|
68
|
+
* something probably went wrong with the bitcoin transaction format or parsing
|
|
69
|
+
*
|
|
70
|
+
* @category Swaps
|
|
71
|
+
*/
|
|
32
72
|
export type SpvWithdrawalClosedState = {
|
|
33
73
|
type: SpvWithdrawalStateType.CLOSED,
|
|
34
74
|
error: string
|
|
35
75
|
} & SpvWithdrawalStateCommon;
|
|
36
76
|
|
|
77
|
+
/**
|
|
78
|
+
* A union type for the state of the spv vault claim (withdrawal)
|
|
79
|
+
*
|
|
80
|
+
* @category Swaps
|
|
81
|
+
*/
|
|
37
82
|
export type SpvWithdrawalState = SpvWithdrawalNotFoundState |
|
|
38
83
|
SpvWithdrawalClaimedState |
|
|
39
84
|
SpvWithdrawalFrontedState |
|
|
@@ -2,24 +2,47 @@ import {BtcTx} from "../btc/rpc/BitcoinRpc";
|
|
|
2
2
|
import {Buffer} from "buffer";
|
|
3
3
|
import {StorageObject} from "../storage/StorageObject";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Execution data assigned to the withdrawal
|
|
7
|
+
*/
|
|
5
8
|
export type ExecutionData = {
|
|
6
9
|
executionHash: string,
|
|
7
10
|
executionExpiry: number
|
|
8
11
|
};
|
|
9
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Represents the data of a single SPV vault (UTXO-controlled) vault withdrawal
|
|
15
|
+
*
|
|
16
|
+
* @category Swaps
|
|
17
|
+
*/
|
|
10
18
|
export abstract class SpvWithdrawalTransactionData implements StorageObject {
|
|
11
19
|
|
|
20
|
+
/**
|
|
21
|
+
* A mapping of deserializers for different spv vault withdrawal data types coming from different smart chain implementations
|
|
22
|
+
*/
|
|
12
23
|
static deserializers: {
|
|
13
24
|
[type: string]: new (serialized: any) => any,
|
|
14
25
|
} = {};
|
|
15
26
|
|
|
16
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Deserializer parsing the chain-specific spv vault withdrawal data from a JSON-compatible object representation
|
|
29
|
+
*
|
|
30
|
+
* @param data
|
|
31
|
+
*/
|
|
32
|
+
static deserialize<T extends SpvWithdrawalTransactionData>(data: any): T | null {
|
|
17
33
|
if (SpvWithdrawalTransactionData.deserializers[data.type] != null) {
|
|
18
34
|
return new SpvWithdrawalTransactionData.deserializers[data.type](data) as unknown as T;
|
|
19
35
|
}
|
|
36
|
+
throw new Error(`No deserializer found for spv withdrawal tx data type: ${data.type}`);
|
|
20
37
|
}
|
|
21
38
|
|
|
22
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Parses the data from the OP_RETURN script for a specific underlying chain
|
|
41
|
+
*
|
|
42
|
+
* @param data
|
|
43
|
+
* @protected
|
|
44
|
+
*/
|
|
45
|
+
protected abstract fromOpReturnData(data: Buffer): {recipient: string, rawAmounts: bigint[], executionHash?: string};
|
|
23
46
|
|
|
24
47
|
readonly recipient: string;
|
|
25
48
|
readonly rawAmounts: bigint[];
|
|
@@ -28,11 +51,19 @@ export abstract class SpvWithdrawalTransactionData implements StorageObject {
|
|
|
28
51
|
readonly executionFeeRate: bigint;
|
|
29
52
|
readonly frontingFeeRate: bigint;
|
|
30
53
|
|
|
31
|
-
readonly executionHash
|
|
54
|
+
readonly executionHash?: string;
|
|
32
55
|
readonly executionExpiry: number;
|
|
33
56
|
|
|
57
|
+
/**
|
|
58
|
+
* A bitcoin transaction which contains this vault withdrawal data
|
|
59
|
+
*/
|
|
34
60
|
readonly btcTx: BtcTx;
|
|
35
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Parses and creates a withdrawal data from the bitcoin transaction
|
|
64
|
+
*
|
|
65
|
+
* @throws {Error} If the bitcoin transaction has invalid formatting
|
|
66
|
+
*/
|
|
36
67
|
constructor(btcTx: BtcTx) {
|
|
37
68
|
if(btcTx.ins.length<2) throw new Error("Need at least 2 inputs");
|
|
38
69
|
if(btcTx.outs.length<2) throw new Error("Need at least 2 outputs");
|
|
@@ -54,14 +85,17 @@ export abstract class SpvWithdrawalTransactionData implements StorageObject {
|
|
|
54
85
|
const opReturnData = Buffer.from(opReturnOutput.scriptPubKey.hex, "hex");
|
|
55
86
|
if(opReturnData.length===0) throw new Error("Output 1 empty script");
|
|
56
87
|
if(opReturnData.at(0)!==0x6a) throw new Error("Output 1 is not OP_RETURN");
|
|
57
|
-
|
|
88
|
+
const secondByte = opReturnData.at(1);
|
|
89
|
+
if(secondByte==null) throw new Error("Output 1 OP_RETURN empty");
|
|
90
|
+
if(secondByte===0) throw new Error("Output 1 OP_RETURN followed by OP_0");
|
|
58
91
|
let data: Buffer;
|
|
59
|
-
if(
|
|
92
|
+
if(secondByte === 0x4c) { //OP_PUSHDATA1
|
|
60
93
|
const dataLength = opReturnData.at(2);
|
|
94
|
+
if(dataLength==null) throw new Error("Output 1 OP_RETURN OP_PUSHDATA1 invalid length!");
|
|
61
95
|
data = opReturnData.subarray(3, 3+dataLength);
|
|
62
96
|
if(data.length !== dataLength) throw new Error("Output 1 OP_RETURN data length mismatch!");
|
|
63
|
-
} else if(
|
|
64
|
-
const dataLength =
|
|
97
|
+
} else if(secondByte <= 0x4b) { //OP_PUSH<length>
|
|
98
|
+
const dataLength = secondByte;
|
|
65
99
|
data = opReturnData.subarray(2, 2+dataLength);
|
|
66
100
|
if(data.length !== dataLength) throw new Error("Output 1 OP_RETURN data length mismatch!");
|
|
67
101
|
} else {
|
|
@@ -84,34 +118,77 @@ export abstract class SpvWithdrawalTransactionData implements StorageObject {
|
|
|
84
118
|
this.btcTx = btcTx;
|
|
85
119
|
}
|
|
86
120
|
|
|
121
|
+
/**
|
|
122
|
+
* @inheritDoc
|
|
123
|
+
*/
|
|
87
124
|
serialize(): any {
|
|
88
125
|
return this.btcTx;
|
|
89
126
|
}
|
|
90
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Gets the recipient of the funds for this withdrawal
|
|
130
|
+
*/
|
|
91
131
|
getRecipient(): string {
|
|
92
132
|
return this.recipient;
|
|
93
133
|
}
|
|
94
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Checks if the provided recipient is the actual recipient of the funds in this withdrawal
|
|
137
|
+
*
|
|
138
|
+
* @param address
|
|
139
|
+
*/
|
|
95
140
|
abstract isRecipient(address: string): boolean;
|
|
96
141
|
|
|
142
|
+
/**
|
|
143
|
+
* Computes a unique withdrawal fronting ID that is to be used when fronting the withdrawal
|
|
144
|
+
*/
|
|
97
145
|
abstract getFrontingId(): string;
|
|
98
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Returns the amounts of tokens that the recipient is gonna receive (NOTE: This returns raw token amounts,
|
|
149
|
+
* which must be scaled by their respective vault configured multiplier to represent the actual amount
|
|
150
|
+
* of tokens)
|
|
151
|
+
*/
|
|
99
152
|
getOutputWithoutFees(): bigint[] {
|
|
100
153
|
return this.rawAmounts;
|
|
101
154
|
}
|
|
102
155
|
|
|
156
|
+
/**
|
|
157
|
+
* Returns the fee paid out to the caller which submits the withdrawal transaction data on the smart chain,
|
|
158
|
+
* the fee is paid out from all the respective tokens being withdrawn from the vault (NOTE: This returns raw token amounts,
|
|
159
|
+
* which must be scaled by their respective vault configured multiplier to represent the actual amount
|
|
160
|
+
* of tokens)
|
|
161
|
+
*/
|
|
103
162
|
getCallerFee(): bigint[] {
|
|
104
163
|
return this.rawAmounts.map(val => val * this.callerFeeRate / 100_000n);
|
|
105
164
|
}
|
|
106
165
|
|
|
166
|
+
/**
|
|
167
|
+
* Returns the fee paid out to the fronter which fronts the actual withdrawal on the smart chain,
|
|
168
|
+
* the fee is paid out from all the respective tokens being withdrawn from the vault (NOTE: This returns raw token amounts,
|
|
169
|
+
* which must be scaled by their respective vault configured multiplier to represent the actual amount
|
|
170
|
+
* of tokens)
|
|
171
|
+
*/
|
|
107
172
|
getFrontingFee(): bigint[] {
|
|
108
173
|
return this.rawAmounts.map(val => val * this.frontingFeeRate / 100_000n);
|
|
109
174
|
}
|
|
110
175
|
|
|
176
|
+
/**
|
|
177
|
+
* Returns the fee that is transferred to the execution contract if swap+ execution action is assigned,
|
|
178
|
+
* (NOTE: This returns raw token amounts, which must be scaled by their respective vault configured
|
|
179
|
+
* multiplier to represent the actual amount of tokens)
|
|
180
|
+
*/
|
|
111
181
|
getExecutionFee(): bigint[] {
|
|
112
182
|
return [this.rawAmounts[0] * this.executionFeeRate / 100_000n];
|
|
113
183
|
}
|
|
114
184
|
|
|
185
|
+
/**
|
|
186
|
+
* Returns the total amount of tokens withdrawn from the vault (including all the fees) (NOTE: This returns
|
|
187
|
+
* raw token amounts, which must be scaled by their respective vault configured multiplier to represent
|
|
188
|
+
* the actual amount of tokens)
|
|
189
|
+
*
|
|
190
|
+
* @throws {Error} In case the amounts overflow
|
|
191
|
+
*/
|
|
115
192
|
getTotalOutput(): bigint[] {
|
|
116
193
|
const amounts = [...this.getOutputWithoutFees()];
|
|
117
194
|
const callerFee = this.getCallerFee();
|
|
@@ -138,6 +215,9 @@ export abstract class SpvWithdrawalTransactionData implements StorageObject {
|
|
|
138
215
|
return amounts;
|
|
139
216
|
}
|
|
140
217
|
|
|
218
|
+
/**
|
|
219
|
+
* Returns the execution action to be scheduled via the execution contract (swap+) or `null` if none specified
|
|
220
|
+
*/
|
|
141
221
|
getExecutionData(): ExecutionData | null {
|
|
142
222
|
if(this.executionHash==null) return null;
|
|
143
223
|
return {
|
|
@@ -146,23 +226,38 @@ export abstract class SpvWithdrawalTransactionData implements StorageObject {
|
|
|
146
226
|
}
|
|
147
227
|
}
|
|
148
228
|
|
|
229
|
+
/**
|
|
230
|
+
* Gets the transaction ID of the bitcoin transaction authorizing the withdrawal
|
|
231
|
+
*/
|
|
149
232
|
getTxId(): string {
|
|
150
233
|
return this.btcTx.txid;
|
|
151
234
|
}
|
|
152
235
|
|
|
236
|
+
/**
|
|
237
|
+
* Gets the vault ownership UTXO that the bitcoin transaction spends
|
|
238
|
+
*/
|
|
153
239
|
getSpentVaultUtxo(): string {
|
|
154
240
|
const in0 = this.btcTx.ins[0];
|
|
155
241
|
return in0.txid+":"+in0.vout;
|
|
156
242
|
}
|
|
157
243
|
|
|
244
|
+
/**
|
|
245
|
+
* Gets the new vault ownership UTXO created by this transaction
|
|
246
|
+
*/
|
|
158
247
|
getCreatedVaultUtxo(): string {
|
|
159
248
|
return this.getTxId()+":0";
|
|
160
249
|
}
|
|
161
250
|
|
|
251
|
+
/**
|
|
252
|
+
* Gets the output locking script used for the new vault ownership UTXO
|
|
253
|
+
*/
|
|
162
254
|
getNewVaultScript(): Buffer {
|
|
163
255
|
return Buffer.from(this.btcTx.outs[0].scriptPubKey.hex, "hex");
|
|
164
256
|
}
|
|
165
257
|
|
|
258
|
+
/**
|
|
259
|
+
* Gets the output btc amount (in satoshis) assigned to the new vault ownership UTXO
|
|
260
|
+
*/
|
|
166
261
|
getNewVaultBtcAmount(): number {
|
|
167
262
|
return this.btcTx.outs[0].value;
|
|
168
263
|
}
|
|
@@ -1,17 +1,58 @@
|
|
|
1
1
|
import {StorageObject} from "./StorageObject";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Interface for storage managers that persist StorageObject instances.
|
|
5
|
+
* Provides basic CRUD operations with in-memory caching via the data property.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - Type of StorageObject to manage
|
|
8
|
+
*
|
|
9
|
+
* @category Storage
|
|
10
|
+
*/
|
|
3
11
|
export interface IStorageManager<T extends StorageObject> {
|
|
4
12
|
|
|
13
|
+
/** In-memory cache of stored objects, keyed by hash */
|
|
5
14
|
data: {
|
|
6
15
|
[key: string]: T
|
|
7
16
|
};
|
|
8
17
|
|
|
18
|
+
/** Initializes the storage backend */
|
|
9
19
|
init(): Promise<void>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Saves an object to storage
|
|
23
|
+
*
|
|
24
|
+
* @param hash Unique identifier for the object
|
|
25
|
+
* @param object Object to save
|
|
26
|
+
*/
|
|
10
27
|
saveData(hash: string, object: T): Promise<void>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Removes an object from storage
|
|
31
|
+
*
|
|
32
|
+
* @param hash Identifier of the object to remove
|
|
33
|
+
*/
|
|
11
34
|
removeData(hash: string): Promise<void>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Loads all stored objects and deserializes them using the provided constructor
|
|
38
|
+
*
|
|
39
|
+
* @param type Constructor function to instantiate each object
|
|
40
|
+
* @returns Array of deserialized objects
|
|
41
|
+
*/
|
|
12
42
|
loadData(type: new(data: any) => T): Promise<T[]>;
|
|
13
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Removes multiple objects from storage (optional batch operation)
|
|
46
|
+
*
|
|
47
|
+
* @param keys Array of identifiers to remove
|
|
48
|
+
*/
|
|
14
49
|
removeDataArr?(keys: string[]): Promise<void>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Saves multiple objects to storage (optional batch operation)
|
|
53
|
+
*
|
|
54
|
+
* @param values Array of id-object pairs to save
|
|
55
|
+
*/
|
|
15
56
|
saveDataArr?(values: {id: string, object: T}[]): Promise<void>;
|
|
16
57
|
|
|
17
58
|
}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Represents an object that is serializable down to a JSON-compatible object (i.e. no bigints, functions, etc.)
|
|
3
|
+
*
|
|
4
|
+
* @category Storage
|
|
5
|
+
*/
|
|
3
6
|
export interface StorageObject {
|
|
4
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Serializes the object to a JSON-compatible object (i.e. no bigints, functions, etc.)
|
|
10
|
+
*/
|
|
5
11
|
serialize(): any;
|
|
6
12
|
|
|
7
13
|
}
|
|
@@ -1,6 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Smart chain escrow swap types
|
|
3
|
+
*
|
|
4
|
+
* @category Swaps
|
|
5
|
+
*/
|
|
1
6
|
export enum ChainSwapType {
|
|
7
|
+
/**
|
|
8
|
+
* A hashlock based swap identified by a payment hash `P` requiring a hash preimage witness `s` such that `H(s) = P`,
|
|
9
|
+
* actual chain implementations might slightly differ!
|
|
10
|
+
*/
|
|
2
11
|
HTLC = 0,
|
|
12
|
+
/**
|
|
13
|
+
* A proof-time locked based swap (PrTLC), which verifies a bitcoin transaction via a light client, and enforces
|
|
14
|
+
* that the transaction pays out exactly `x` BTC to a pre-determined output script
|
|
15
|
+
*/
|
|
3
16
|
CHAIN = 1,
|
|
17
|
+
/**
|
|
18
|
+
* A proof-time locked based swap (PrTLC), which verifies a bitcoin transaction via a light client, enforces
|
|
19
|
+
* that the transaction pays out exactly `x` BTC to a pre-determined output script and also checks the
|
|
20
|
+
* transaction nonce to prevent replay attacks (the nonce is composed of transaction input nSequence fields
|
|
21
|
+
* and transaction's locktime)
|
|
22
|
+
*/
|
|
4
23
|
CHAIN_NONCED = 2,
|
|
24
|
+
/**
|
|
25
|
+
* A simple proof-time locked based swap (PrTLC), which verifies a specific bitcoin transaction ID via a light client
|
|
26
|
+
*/
|
|
5
27
|
CHAIN_TXID = 3
|
|
6
28
|
}
|
|
@@ -1,31 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Possible on-chain states of the swap escrow
|
|
3
|
+
*
|
|
4
|
+
* @category Swaps
|
|
5
|
+
*/
|
|
1
6
|
export enum SwapCommitStateType {
|
|
7
|
+
/**
|
|
8
|
+
* The escrow specified by the swap data isn't currently active and the swap data has already expired
|
|
9
|
+
*/
|
|
2
10
|
EXPIRED=0,
|
|
11
|
+
/**
|
|
12
|
+
* The escrow specified by the swap data isn't currently active, could've been refunded already
|
|
13
|
+
*/
|
|
3
14
|
NOT_COMMITED=1,
|
|
15
|
+
/**
|
|
16
|
+
* The escrow is active on-chain and can be claimed
|
|
17
|
+
*/
|
|
4
18
|
COMMITED=2,
|
|
19
|
+
/**
|
|
20
|
+
* The escrow has been finalized and funds claimed by the claimer
|
|
21
|
+
*/
|
|
5
22
|
PAID=3,
|
|
23
|
+
/**
|
|
24
|
+
* The escrow is active on-chain and can be refunded by the offerer
|
|
25
|
+
*/
|
|
6
26
|
REFUNDABLE=4
|
|
7
27
|
}
|
|
8
28
|
|
|
29
|
+
/**
|
|
30
|
+
* The escrow specified by the swap data isn't currently active, could've been refunded already
|
|
31
|
+
*
|
|
32
|
+
* @category Swaps
|
|
33
|
+
*/
|
|
9
34
|
export type SwapNotCommitedState = {
|
|
10
35
|
type: SwapCommitStateType.NOT_COMMITED,
|
|
11
36
|
getRefundTxId?: () => Promise<string>,
|
|
12
37
|
getTxBlock?: () => Promise<{blockHeight: number, blockTime: number}>
|
|
13
38
|
};
|
|
14
39
|
|
|
40
|
+
/**
|
|
41
|
+
* The escrow specified by the swap data isn't currently active and the swap data has already expired
|
|
42
|
+
*
|
|
43
|
+
* @category Swaps
|
|
44
|
+
*/
|
|
15
45
|
export type SwapExpiredState = {
|
|
16
46
|
type: SwapCommitStateType.EXPIRED,
|
|
17
47
|
getRefundTxId?: () => Promise<string>,
|
|
18
48
|
getTxBlock?: () => Promise<{blockHeight: number, blockTime: number}>
|
|
19
49
|
};
|
|
20
50
|
|
|
51
|
+
/**
|
|
52
|
+
* The escrow is active on-chain and can be refunded by the offerer
|
|
53
|
+
*
|
|
54
|
+
* @category Swaps
|
|
55
|
+
*/
|
|
21
56
|
export type SwapRefundableState = {
|
|
22
57
|
type: SwapCommitStateType.REFUNDABLE
|
|
23
58
|
};
|
|
24
59
|
|
|
60
|
+
/**
|
|
61
|
+
* The escrow is active on-chain and can be claimed
|
|
62
|
+
*
|
|
63
|
+
* @category Swaps
|
|
64
|
+
*/
|
|
25
65
|
export type SwapCommitedState = {
|
|
26
66
|
type: SwapCommitStateType.COMMITED
|
|
27
67
|
};
|
|
28
68
|
|
|
69
|
+
/**
|
|
70
|
+
* The escrow has been finalized and funds claimed by the claimer
|
|
71
|
+
*
|
|
72
|
+
* @category Swaps
|
|
73
|
+
*/
|
|
29
74
|
export type SwapPaidState = {
|
|
30
75
|
type: SwapCommitStateType.PAID,
|
|
31
76
|
getClaimTxId: () => Promise<string>,
|
|
@@ -33,6 +78,11 @@ export type SwapPaidState = {
|
|
|
33
78
|
getTxBlock: () => Promise<{blockHeight: number, blockTime: number}>
|
|
34
79
|
};
|
|
35
80
|
|
|
81
|
+
/**
|
|
82
|
+
* A union type for the state of the escrow
|
|
83
|
+
*
|
|
84
|
+
* @category Swaps
|
|
85
|
+
*/
|
|
36
86
|
export type SwapCommitState = SwapNotCommitedState |
|
|
37
87
|
SwapExpiredState |
|
|
38
88
|
SwapRefundableState |
|