@atomiqlabs/lp-lib 16.2.0 → 17.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/LICENSE +201 -201
  2. package/dist/fees/IBtcFeeEstimator.d.ts +3 -3
  3. package/dist/fees/IBtcFeeEstimator.js +2 -2
  4. package/dist/index.d.ts +42 -42
  5. package/dist/index.js +58 -58
  6. package/dist/info/InfoHandler.d.ts +17 -17
  7. package/dist/info/InfoHandler.js +60 -60
  8. package/dist/plugins/IPlugin.d.ts +144 -144
  9. package/dist/plugins/IPlugin.js +34 -34
  10. package/dist/plugins/PluginManager.d.ts +113 -113
  11. package/dist/plugins/PluginManager.js +274 -274
  12. package/dist/prices/BinanceSwapPrice.d.ts +29 -29
  13. package/dist/prices/BinanceSwapPrice.js +79 -79
  14. package/dist/prices/CoinGeckoSwapPrice.d.ts +33 -33
  15. package/dist/prices/CoinGeckoSwapPrice.js +51 -51
  16. package/dist/prices/ISwapPrice.d.ts +43 -43
  17. package/dist/prices/ISwapPrice.js +55 -55
  18. package/dist/prices/OKXSwapPrice.d.ts +29 -29
  19. package/dist/prices/OKXSwapPrice.js +79 -79
  20. package/dist/storage/IIntermediaryStorage.d.ts +18 -18
  21. package/dist/storage/IIntermediaryStorage.js +2 -2
  22. package/dist/storagemanager/IntermediaryStorageManager.d.ts +19 -19
  23. package/dist/storagemanager/IntermediaryStorageManager.js +111 -111
  24. package/dist/storagemanager/StorageManager.d.ts +13 -13
  25. package/dist/storagemanager/StorageManager.js +64 -64
  26. package/dist/swaps/SwapHandler.d.ts +171 -171
  27. package/dist/swaps/SwapHandler.js +217 -217
  28. package/dist/swaps/SwapHandlerSwap.d.ts +79 -79
  29. package/dist/swaps/SwapHandlerSwap.js +78 -78
  30. package/dist/swaps/assertions/AmountAssertions.d.ts +28 -28
  31. package/dist/swaps/assertions/AmountAssertions.js +74 -74
  32. package/dist/swaps/assertions/FromBtcAmountAssertions.d.ts +76 -76
  33. package/dist/swaps/assertions/FromBtcAmountAssertions.js +185 -185
  34. package/dist/swaps/assertions/LightningAssertions.d.ts +44 -44
  35. package/dist/swaps/assertions/LightningAssertions.js +86 -86
  36. package/dist/swaps/assertions/ToBtcAmountAssertions.d.ts +53 -53
  37. package/dist/swaps/assertions/ToBtcAmountAssertions.js +150 -150
  38. package/dist/swaps/escrow/EscrowHandler.d.ts +50 -50
  39. package/dist/swaps/escrow/EscrowHandler.js +151 -151
  40. package/dist/swaps/escrow/EscrowHandlerSwap.d.ts +35 -35
  41. package/dist/swaps/escrow/EscrowHandlerSwap.js +69 -69
  42. package/dist/swaps/escrow/FromBtcBaseSwap.d.ts +14 -14
  43. package/dist/swaps/escrow/FromBtcBaseSwap.js +32 -32
  44. package/dist/swaps/escrow/FromBtcBaseSwapHandler.d.ts +102 -102
  45. package/dist/swaps/escrow/FromBtcBaseSwapHandler.js +210 -210
  46. package/dist/swaps/escrow/ToBtcBaseSwap.d.ts +36 -36
  47. package/dist/swaps/escrow/ToBtcBaseSwap.js +67 -67
  48. package/dist/swaps/escrow/ToBtcBaseSwapHandler.d.ts +53 -53
  49. package/dist/swaps/escrow/ToBtcBaseSwapHandler.js +81 -81
  50. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.d.ts +84 -84
  51. package/dist/swaps/escrow/frombtc_abstract/FromBtcAbs.js +322 -322
  52. package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.d.ts +21 -21
  53. package/dist/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.js +50 -50
  54. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.d.ts +108 -108
  55. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.js +695 -695
  56. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.d.ts +33 -33
  57. package/dist/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.js +91 -91
  58. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.d.ts +112 -112
  59. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.js +708 -708
  60. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.d.ts +55 -55
  61. package/dist/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.js +120 -120
  62. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.d.ts +170 -170
  63. package/dist/swaps/escrow/tobtc_abstract/ToBtcAbs.js +746 -745
  64. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.d.ts +28 -28
  65. package/dist/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.js +64 -64
  66. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.d.ts +178 -178
  67. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.js +900 -899
  68. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.d.ts +24 -24
  69. package/dist/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.js +58 -58
  70. package/dist/swaps/spv_vault_swap/SpvVault.d.ts +44 -44
  71. package/dist/swaps/spv_vault_swap/SpvVault.js +145 -145
  72. package/dist/swaps/spv_vault_swap/SpvVaultSwap.d.ts +68 -68
  73. package/dist/swaps/spv_vault_swap/SpvVaultSwap.js +158 -158
  74. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.d.ts +68 -68
  75. package/dist/swaps/spv_vault_swap/SpvVaultSwapHandler.js +561 -561
  76. package/dist/swaps/spv_vault_swap/SpvVaults.d.ts +63 -63
  77. package/dist/swaps/spv_vault_swap/SpvVaults.js +491 -491
  78. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.d.ts +52 -52
  79. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrusted.js +662 -662
  80. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.d.ts +52 -52
  81. package/dist/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.js +118 -118
  82. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.d.ts +77 -77
  83. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.js +504 -504
  84. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.d.ts +34 -34
  85. package/dist/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.js +81 -81
  86. package/dist/utils/BitcoinUtils.d.ts +4 -4
  87. package/dist/utils/BitcoinUtils.js +61 -61
  88. package/dist/utils/Utils.d.ts +32 -32
  89. package/dist/utils/Utils.js +129 -129
  90. package/dist/utils/paramcoders/IParamReader.d.ts +5 -5
  91. package/dist/utils/paramcoders/IParamReader.js +2 -2
  92. package/dist/utils/paramcoders/IParamWriter.d.ts +4 -4
  93. package/dist/utils/paramcoders/IParamWriter.js +2 -2
  94. package/dist/utils/paramcoders/LegacyParamEncoder.d.ts +10 -10
  95. package/dist/utils/paramcoders/LegacyParamEncoder.js +22 -22
  96. package/dist/utils/paramcoders/ParamDecoder.d.ts +25 -25
  97. package/dist/utils/paramcoders/ParamDecoder.js +222 -222
  98. package/dist/utils/paramcoders/ParamEncoder.d.ts +9 -9
  99. package/dist/utils/paramcoders/ParamEncoder.js +22 -22
  100. package/dist/utils/paramcoders/SchemaVerifier.d.ts +21 -21
  101. package/dist/utils/paramcoders/SchemaVerifier.js +84 -84
  102. package/dist/utils/paramcoders/server/ServerParamDecoder.d.ts +8 -8
  103. package/dist/utils/paramcoders/server/ServerParamDecoder.js +107 -107
  104. package/dist/utils/paramcoders/server/ServerParamEncoder.d.ts +11 -11
  105. package/dist/utils/paramcoders/server/ServerParamEncoder.js +65 -65
  106. package/dist/wallets/IBitcoinWallet.d.ts +149 -149
  107. package/dist/wallets/IBitcoinWallet.js +97 -97
  108. package/dist/wallets/ILightningWallet.d.ts +136 -136
  109. package/dist/wallets/ILightningWallet.js +37 -37
  110. package/dist/wallets/ISpvVaultSigner.d.ts +7 -7
  111. package/dist/wallets/ISpvVaultSigner.js +2 -2
  112. package/package.json +36 -36
  113. package/src/fees/IBtcFeeEstimator.ts +6 -6
  114. package/src/index.ts +53 -53
  115. package/src/info/InfoHandler.ts +103 -103
  116. package/src/plugins/IPlugin.ts +174 -174
  117. package/src/plugins/PluginManager.ts +354 -354
  118. package/src/prices/BinanceSwapPrice.ts +101 -101
  119. package/src/prices/CoinGeckoSwapPrice.ts +75 -75
  120. package/src/prices/ISwapPrice.ts +88 -88
  121. package/src/prices/OKXSwapPrice.ts +101 -101
  122. package/src/storage/IIntermediaryStorage.ts +19 -19
  123. package/src/storagemanager/IntermediaryStorageManager.ts +118 -118
  124. package/src/storagemanager/StorageManager.ts +78 -78
  125. package/src/swaps/SwapHandler.ts +323 -323
  126. package/src/swaps/SwapHandlerSwap.ts +141 -141
  127. package/src/swaps/assertions/AmountAssertions.ts +77 -77
  128. package/src/swaps/assertions/FromBtcAmountAssertions.ts +251 -251
  129. package/src/swaps/assertions/LightningAssertions.ts +103 -103
  130. package/src/swaps/assertions/ToBtcAmountAssertions.ts +203 -203
  131. package/src/swaps/escrow/EscrowHandler.ts +172 -172
  132. package/src/swaps/escrow/EscrowHandlerSwap.ts +86 -86
  133. package/src/swaps/escrow/FromBtcBaseSwap.ts +38 -38
  134. package/src/swaps/escrow/FromBtcBaseSwapHandler.ts +286 -286
  135. package/src/swaps/escrow/ToBtcBaseSwap.ts +85 -85
  136. package/src/swaps/escrow/ToBtcBaseSwapHandler.ts +129 -129
  137. package/src/swaps/escrow/frombtc_abstract/FromBtcAbs.ts +457 -457
  138. package/src/swaps/escrow/frombtc_abstract/FromBtcSwapAbs.ts +61 -61
  139. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnAbs.ts +873 -873
  140. package/src/swaps/escrow/frombtcln_abstract/FromBtcLnSwapAbs.ts +141 -141
  141. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAuto.ts +866 -866
  142. package/src/swaps/escrow/frombtcln_autoinit/FromBtcLnAutoSwap.ts +196 -196
  143. package/src/swaps/escrow/tobtc_abstract/ToBtcAbs.ts +921 -920
  144. package/src/swaps/escrow/tobtc_abstract/ToBtcSwapAbs.ts +108 -108
  145. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnAbs.ts +1150 -1149
  146. package/src/swaps/escrow/tobtcln_abstract/ToBtcLnSwapAbs.ts +80 -80
  147. package/src/swaps/spv_vault_swap/SpvVault.ts +178 -178
  148. package/src/swaps/spv_vault_swap/SpvVaultSwap.ts +228 -228
  149. package/src/swaps/spv_vault_swap/SpvVaultSwapHandler.ts +718 -718
  150. package/src/swaps/spv_vault_swap/SpvVaults.ts +567 -567
  151. package/src/swaps/trusted/frombtc_trusted/FromBtcTrusted.ts +762 -762
  152. package/src/swaps/trusted/frombtc_trusted/FromBtcTrustedSwap.ts +185 -185
  153. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrusted.ts +603 -603
  154. package/src/swaps/trusted/frombtcln_trusted/FromBtcLnTrustedSwap.ts +121 -121
  155. package/src/utils/BitcoinUtils.ts +59 -59
  156. package/src/utils/Utils.ts +150 -150
  157. package/src/utils/paramcoders/IParamReader.ts +7 -7
  158. package/src/utils/paramcoders/IParamWriter.ts +8 -8
  159. package/src/utils/paramcoders/LegacyParamEncoder.ts +27 -27
  160. package/src/utils/paramcoders/ParamDecoder.ts +218 -218
  161. package/src/utils/paramcoders/ParamEncoder.ts +29 -29
  162. package/src/utils/paramcoders/SchemaVerifier.ts +96 -96
  163. package/src/utils/paramcoders/server/ServerParamDecoder.ts +118 -118
  164. package/src/utils/paramcoders/server/ServerParamEncoder.ts +75 -75
  165. package/src/wallets/IBitcoinWallet.ts +237 -237
  166. package/src/wallets/ILightningWallet.ts +200 -200
  167. package/src/wallets/ISpvVaultSigner.ts +10 -10
@@ -1,228 +1,228 @@
1
- import {SwapHandlerSwap} from "../SwapHandlerSwap";
2
- import {SpvVault} from "./SpvVault";
3
- import {deserializeBN, serializeBN} from "../../utils/Utils";
4
- import {SwapHandlerType} from "../SwapHandler";
5
-
6
- export enum SpvVaultSwapState {
7
- FAILED = -3,
8
- DOUBLE_SPENT = -2,
9
- EXPIRED = -1,
10
- CREATED = 0,
11
- SIGNED = 1,
12
- SENT = 2,
13
- BTC_CONFIRMED = 3,
14
- CLAIMED = 4
15
- }
16
-
17
- export class SpvVaultSwap extends SwapHandlerSwap<SpvVaultSwapState> {
18
-
19
- //Unsaved sending flag
20
- sending: boolean;
21
-
22
- readonly quoteId: string;
23
-
24
- readonly vaultOwner: string;
25
- readonly vaultId: bigint;
26
- readonly vaultUtxo: string;
27
- readonly vaultAddress: string;
28
-
29
- readonly expiry: number;
30
-
31
- readonly tokenMultiplier: bigint;
32
- readonly gasTokenMultiplier: bigint;
33
-
34
- readonly tokenSwapFee: bigint;
35
- readonly tokenSwapFeeInToken: bigint;
36
- readonly gasSwapFee: bigint;
37
- readonly gasSwapFeeInToken: bigint;
38
-
39
- readonly btcFeeRate: number;
40
- readonly btcAddress: string;
41
- readonly recipient: string;
42
- readonly amountBtc: bigint;
43
- readonly amountToken: bigint;
44
- readonly amountGasToken: bigint;
45
- readonly rawAmountToken: bigint;
46
- readonly rawAmountGasToken: bigint;
47
- readonly callerFeeShare: bigint;
48
- readonly frontingFeeShare: bigint;
49
- readonly executionFeeShare: bigint;
50
-
51
- readonly token: string;
52
- readonly gasToken: string;
53
-
54
- btcTxId: string;
55
-
56
- constructor(
57
- chainIdentifier: string, quoteId: string, expiry: number,
58
- vault: SpvVault, vaultUtxo: string,
59
- btcAddress: string, btcFeeRate: number, recipient: string, amountBtc: bigint, amountToken: bigint, amountGasToken: bigint,
60
- swapFee: bigint, swapFeeInToken: bigint,
61
- gasSwapFee: bigint, gasSwapFeeInToken: bigint,
62
- callerFeeShare: bigint, frontingFeeShare: bigint, executionFeeShare: bigint,
63
- token: string, gasToken: string
64
- );
65
- constructor(data: any);
66
- constructor(
67
- chainIdentifierOrObj: string | any, quoteId?: string, expiry?: number,
68
- vault?: SpvVault, vaultUtxo?: string,
69
- btcAddress?: string, btcFeeRate?: number, recipient?: string, amountBtc?: bigint, amountToken?: bigint, amountGasToken?: bigint,
70
- swapFee?: bigint, swapFeeInToken?: bigint,
71
- gasSwapFee?: bigint, gasSwapFeeInToken?: bigint,
72
- callerFeeShare?: bigint, frontingFeeShare?: bigint, executionFeeShare?: bigint,
73
- token?: string, gasToken?: string
74
- ) {
75
- if(typeof(chainIdentifierOrObj)==="string") {
76
- super(chainIdentifierOrObj, swapFee + gasSwapFee, swapFeeInToken);
77
- this.state = SpvVaultSwapState.CREATED;
78
- this.quoteId = quoteId;
79
- this.expiry = expiry;
80
- this.vaultOwner = vault.data.getOwner();
81
- this.vaultId = vault.data.getVaultId();
82
- this.vaultAddress = vault.btcAddress;
83
- this.vaultUtxo = vaultUtxo;
84
- this.tokenSwapFee = swapFee;
85
- this.tokenSwapFeeInToken = swapFeeInToken;
86
- this.gasSwapFee = gasSwapFee;
87
- this.gasSwapFeeInToken = gasSwapFeeInToken;
88
- this.btcFeeRate = btcFeeRate;
89
- this.btcAddress = btcAddress;
90
- this.recipient = recipient;
91
- this.amountBtc = amountBtc;
92
- this.amountToken = amountToken;
93
- this.amountGasToken = amountGasToken;
94
- const [rawAmountToken, rawAmountGasToken] = vault.toRawAmounts([amountToken, amountGasToken]);
95
- this.rawAmountToken = rawAmountToken;
96
- this.rawAmountGasToken = rawAmountGasToken;
97
- this.callerFeeShare = callerFeeShare;
98
- this.frontingFeeShare = frontingFeeShare;
99
- this.executionFeeShare = executionFeeShare;
100
- this.token = token;
101
- this.tokenMultiplier = vault.data.getTokenData()[0].multiplier;
102
- this.gasToken = gasToken;
103
- this.gasTokenMultiplier = vault.data.getTokenData()[1].multiplier;
104
- } else {
105
- super(chainIdentifierOrObj);
106
- this.quoteId = chainIdentifierOrObj.quoteId;
107
- this.expiry = chainIdentifierOrObj.expiry;
108
- this.vaultOwner = chainIdentifierOrObj.owner;
109
- this.vaultId = deserializeBN(chainIdentifierOrObj.vaultId);
110
- this.vaultAddress = chainIdentifierOrObj.vaultAddress;
111
- this.vaultUtxo = chainIdentifierOrObj.vaultUtxo;
112
- this.tokenSwapFee = deserializeBN(chainIdentifierOrObj.swapFee);
113
- this.tokenSwapFeeInToken = deserializeBN(chainIdentifierOrObj.swapFeeInToken);
114
- this.gasSwapFee = deserializeBN(chainIdentifierOrObj.gasSwapFee);
115
- this.gasSwapFeeInToken = deserializeBN(chainIdentifierOrObj.gasSwapFeeInToken);
116
- this.btcFeeRate = chainIdentifierOrObj.btcFeeRate;
117
- this.btcAddress = chainIdentifierOrObj.btcAddress;
118
- this.recipient = chainIdentifierOrObj.recipient;
119
- this.amountBtc = deserializeBN(chainIdentifierOrObj.amountBtc);
120
- this.amountToken = deserializeBN(chainIdentifierOrObj.amountToken);
121
- this.amountGasToken = deserializeBN(chainIdentifierOrObj.amountGasToken);
122
- this.rawAmountToken = deserializeBN(chainIdentifierOrObj.rawAmountToken);
123
- this.rawAmountGasToken = deserializeBN(chainIdentifierOrObj.rawAmountGasToken);
124
- this.callerFeeShare = deserializeBN(chainIdentifierOrObj.callerFeeShare);
125
- this.frontingFeeShare = deserializeBN(chainIdentifierOrObj.frontingFeeShare);
126
- this.executionFeeShare = deserializeBN(chainIdentifierOrObj.executionFeeShare);
127
- this.token = chainIdentifierOrObj.token;
128
- this.gasToken = chainIdentifierOrObj.gasToken;
129
- this.tokenMultiplier = deserializeBN(chainIdentifierOrObj.tokenMultiplier);
130
- this.gasTokenMultiplier = deserializeBN(chainIdentifierOrObj.gasTokenMultiplier);
131
- this.btcTxId = chainIdentifierOrObj.btcTxId;
132
- }
133
- this.type = SwapHandlerType.FROM_BTC_SPV;
134
- }
135
-
136
- serialize(): any {
137
- return {
138
- ...super.serialize(),
139
- quoteId: this.quoteId,
140
- expiry: this.expiry,
141
- owner: this.vaultOwner,
142
- vaultId: serializeBN(this.vaultId),
143
- vaultAddress: this.vaultAddress,
144
- vaultUtxo: this.vaultUtxo,
145
- tokenSwapFee: serializeBN(this.tokenSwapFee),
146
- tokenSwapFeeInToken: serializeBN(this.tokenSwapFeeInToken),
147
- gasSwapFee: serializeBN(this.gasSwapFee),
148
- gasSwapFeeInToken: serializeBN(this.gasSwapFeeInToken),
149
- btcFeeRate: this.btcFeeRate,
150
- btcAddress: this.btcAddress,
151
- recipient: this.recipient,
152
- amountBtc: serializeBN(this.amountBtc),
153
- amountToken: serializeBN(this.amountToken),
154
- amountGasToken: serializeBN(this.amountGasToken),
155
- rawAmountToken: serializeBN(this.rawAmountToken),
156
- rawAmountGasToken: serializeBN(this.rawAmountGasToken),
157
- callerFeeShare: serializeBN(this.callerFeeShare),
158
- frontingFeeShare: serializeBN(this.frontingFeeShare),
159
- executionFeeShare: serializeBN(this.executionFeeShare),
160
- token: this.token,
161
- gasToken: this.gasToken,
162
- tokenMultiplier: serializeBN(this.tokenMultiplier),
163
- gasTokenMultiplier: serializeBN(this.gasTokenMultiplier),
164
- btcTxId: this.btcTxId
165
- };
166
- }
167
-
168
- getIdentifierHash(): string {
169
- return this.quoteId;
170
- }
171
-
172
- getOutputGasAmount(): bigint {
173
- return this.amountGasToken;
174
- }
175
-
176
- getOutputAmount(): bigint {
177
- return this.amountToken;
178
- }
179
-
180
- getTotalOutputAmount(): bigint {
181
- return this.rawAmountToken * (100_000n + this.callerFeeShare + this.frontingFeeShare + this.executionFeeShare) / 100_000n * this.tokenMultiplier;
182
- }
183
-
184
- getTotalOutputGasAmount(): bigint {
185
- return this.rawAmountGasToken * (100_000n + this.callerFeeShare + this.frontingFeeShare) / 100_000n * this.gasTokenMultiplier;
186
- }
187
-
188
- getSequence(): bigint | null {
189
- return 0n;
190
- }
191
-
192
- getSwapFee(): { inInputToken: bigint; inOutputToken: bigint } {
193
- return {inInputToken: this.swapFee, inOutputToken: this.swapFeeInToken};
194
- }
195
-
196
- getTokenSwapFee(): { inInputToken: bigint; inOutputToken: bigint } {
197
- return {inInputToken: this.tokenSwapFee, inOutputToken: this.tokenSwapFeeInToken};
198
- }
199
-
200
- getGasSwapFee(): { inInputToken: bigint; inOutputToken: bigint } {
201
- return {inInputToken: this.gasSwapFee, inOutputToken: this.gasSwapFeeInToken};
202
- }
203
-
204
- getToken(): string {
205
- return this.token;
206
- }
207
-
208
- getGasToken(): string {
209
- return this.gasToken;
210
- }
211
-
212
- getTotalInputAmount(): bigint {
213
- return this.amountBtc;
214
- }
215
-
216
- isFailed(): boolean {
217
- return this.state===SpvVaultSwapState.FAILED || this.state===SpvVaultSwapState.DOUBLE_SPENT;
218
- }
219
-
220
- isInitiated(): boolean {
221
- return this.state!==SpvVaultSwapState.CREATED;
222
- }
223
-
224
- isSuccess(): boolean {
225
- return this.state===SpvVaultSwapState.CLAIMED;
226
- }
227
-
228
- }
1
+ import {SwapHandlerSwap} from "../SwapHandlerSwap";
2
+ import {SpvVault} from "./SpvVault";
3
+ import {deserializeBN, serializeBN} from "../../utils/Utils";
4
+ import {SwapHandlerType} from "../SwapHandler";
5
+
6
+ export enum SpvVaultSwapState {
7
+ FAILED = -3,
8
+ DOUBLE_SPENT = -2,
9
+ EXPIRED = -1,
10
+ CREATED = 0,
11
+ SIGNED = 1,
12
+ SENT = 2,
13
+ BTC_CONFIRMED = 3,
14
+ CLAIMED = 4
15
+ }
16
+
17
+ export class SpvVaultSwap extends SwapHandlerSwap<SpvVaultSwapState> {
18
+
19
+ //Unsaved sending flag
20
+ sending: boolean;
21
+
22
+ readonly quoteId: string;
23
+
24
+ readonly vaultOwner: string;
25
+ readonly vaultId: bigint;
26
+ readonly vaultUtxo: string;
27
+ readonly vaultAddress: string;
28
+
29
+ readonly expiry: number;
30
+
31
+ readonly tokenMultiplier: bigint;
32
+ readonly gasTokenMultiplier: bigint;
33
+
34
+ readonly tokenSwapFee: bigint;
35
+ readonly tokenSwapFeeInToken: bigint;
36
+ readonly gasSwapFee: bigint;
37
+ readonly gasSwapFeeInToken: bigint;
38
+
39
+ readonly btcFeeRate: number;
40
+ readonly btcAddress: string;
41
+ readonly recipient: string;
42
+ readonly amountBtc: bigint;
43
+ readonly amountToken: bigint;
44
+ readonly amountGasToken: bigint;
45
+ readonly rawAmountToken: bigint;
46
+ readonly rawAmountGasToken: bigint;
47
+ readonly callerFeeShare: bigint;
48
+ readonly frontingFeeShare: bigint;
49
+ readonly executionFeeShare: bigint;
50
+
51
+ readonly token: string;
52
+ readonly gasToken: string;
53
+
54
+ btcTxId: string;
55
+
56
+ constructor(
57
+ chainIdentifier: string, quoteId: string, expiry: number,
58
+ vault: SpvVault, vaultUtxo: string,
59
+ btcAddress: string, btcFeeRate: number, recipient: string, amountBtc: bigint, amountToken: bigint, amountGasToken: bigint,
60
+ swapFee: bigint, swapFeeInToken: bigint,
61
+ gasSwapFee: bigint, gasSwapFeeInToken: bigint,
62
+ callerFeeShare: bigint, frontingFeeShare: bigint, executionFeeShare: bigint,
63
+ token: string, gasToken: string
64
+ );
65
+ constructor(data: any);
66
+ constructor(
67
+ chainIdentifierOrObj: string | any, quoteId?: string, expiry?: number,
68
+ vault?: SpvVault, vaultUtxo?: string,
69
+ btcAddress?: string, btcFeeRate?: number, recipient?: string, amountBtc?: bigint, amountToken?: bigint, amountGasToken?: bigint,
70
+ swapFee?: bigint, swapFeeInToken?: bigint,
71
+ gasSwapFee?: bigint, gasSwapFeeInToken?: bigint,
72
+ callerFeeShare?: bigint, frontingFeeShare?: bigint, executionFeeShare?: bigint,
73
+ token?: string, gasToken?: string
74
+ ) {
75
+ if(typeof(chainIdentifierOrObj)==="string") {
76
+ super(chainIdentifierOrObj, swapFee + gasSwapFee, swapFeeInToken);
77
+ this.state = SpvVaultSwapState.CREATED;
78
+ this.quoteId = quoteId;
79
+ this.expiry = expiry;
80
+ this.vaultOwner = vault.data.getOwner();
81
+ this.vaultId = vault.data.getVaultId();
82
+ this.vaultAddress = vault.btcAddress;
83
+ this.vaultUtxo = vaultUtxo;
84
+ this.tokenSwapFee = swapFee;
85
+ this.tokenSwapFeeInToken = swapFeeInToken;
86
+ this.gasSwapFee = gasSwapFee;
87
+ this.gasSwapFeeInToken = gasSwapFeeInToken;
88
+ this.btcFeeRate = btcFeeRate;
89
+ this.btcAddress = btcAddress;
90
+ this.recipient = recipient;
91
+ this.amountBtc = amountBtc;
92
+ this.amountToken = amountToken;
93
+ this.amountGasToken = amountGasToken;
94
+ const [rawAmountToken, rawAmountGasToken] = vault.toRawAmounts([amountToken, amountGasToken]);
95
+ this.rawAmountToken = rawAmountToken;
96
+ this.rawAmountGasToken = rawAmountGasToken;
97
+ this.callerFeeShare = callerFeeShare;
98
+ this.frontingFeeShare = frontingFeeShare;
99
+ this.executionFeeShare = executionFeeShare;
100
+ this.token = token;
101
+ this.tokenMultiplier = vault.data.getTokenData()[0].multiplier;
102
+ this.gasToken = gasToken;
103
+ this.gasTokenMultiplier = vault.data.getTokenData()[1].multiplier;
104
+ } else {
105
+ super(chainIdentifierOrObj);
106
+ this.quoteId = chainIdentifierOrObj.quoteId;
107
+ this.expiry = chainIdentifierOrObj.expiry;
108
+ this.vaultOwner = chainIdentifierOrObj.owner;
109
+ this.vaultId = deserializeBN(chainIdentifierOrObj.vaultId);
110
+ this.vaultAddress = chainIdentifierOrObj.vaultAddress;
111
+ this.vaultUtxo = chainIdentifierOrObj.vaultUtxo;
112
+ this.tokenSwapFee = deserializeBN(chainIdentifierOrObj.swapFee);
113
+ this.tokenSwapFeeInToken = deserializeBN(chainIdentifierOrObj.swapFeeInToken);
114
+ this.gasSwapFee = deserializeBN(chainIdentifierOrObj.gasSwapFee);
115
+ this.gasSwapFeeInToken = deserializeBN(chainIdentifierOrObj.gasSwapFeeInToken);
116
+ this.btcFeeRate = chainIdentifierOrObj.btcFeeRate;
117
+ this.btcAddress = chainIdentifierOrObj.btcAddress;
118
+ this.recipient = chainIdentifierOrObj.recipient;
119
+ this.amountBtc = deserializeBN(chainIdentifierOrObj.amountBtc);
120
+ this.amountToken = deserializeBN(chainIdentifierOrObj.amountToken);
121
+ this.amountGasToken = deserializeBN(chainIdentifierOrObj.amountGasToken);
122
+ this.rawAmountToken = deserializeBN(chainIdentifierOrObj.rawAmountToken);
123
+ this.rawAmountGasToken = deserializeBN(chainIdentifierOrObj.rawAmountGasToken);
124
+ this.callerFeeShare = deserializeBN(chainIdentifierOrObj.callerFeeShare);
125
+ this.frontingFeeShare = deserializeBN(chainIdentifierOrObj.frontingFeeShare);
126
+ this.executionFeeShare = deserializeBN(chainIdentifierOrObj.executionFeeShare);
127
+ this.token = chainIdentifierOrObj.token;
128
+ this.gasToken = chainIdentifierOrObj.gasToken;
129
+ this.tokenMultiplier = deserializeBN(chainIdentifierOrObj.tokenMultiplier);
130
+ this.gasTokenMultiplier = deserializeBN(chainIdentifierOrObj.gasTokenMultiplier);
131
+ this.btcTxId = chainIdentifierOrObj.btcTxId;
132
+ }
133
+ this.type = SwapHandlerType.FROM_BTC_SPV;
134
+ }
135
+
136
+ serialize(): any {
137
+ return {
138
+ ...super.serialize(),
139
+ quoteId: this.quoteId,
140
+ expiry: this.expiry,
141
+ owner: this.vaultOwner,
142
+ vaultId: serializeBN(this.vaultId),
143
+ vaultAddress: this.vaultAddress,
144
+ vaultUtxo: this.vaultUtxo,
145
+ tokenSwapFee: serializeBN(this.tokenSwapFee),
146
+ tokenSwapFeeInToken: serializeBN(this.tokenSwapFeeInToken),
147
+ gasSwapFee: serializeBN(this.gasSwapFee),
148
+ gasSwapFeeInToken: serializeBN(this.gasSwapFeeInToken),
149
+ btcFeeRate: this.btcFeeRate,
150
+ btcAddress: this.btcAddress,
151
+ recipient: this.recipient,
152
+ amountBtc: serializeBN(this.amountBtc),
153
+ amountToken: serializeBN(this.amountToken),
154
+ amountGasToken: serializeBN(this.amountGasToken),
155
+ rawAmountToken: serializeBN(this.rawAmountToken),
156
+ rawAmountGasToken: serializeBN(this.rawAmountGasToken),
157
+ callerFeeShare: serializeBN(this.callerFeeShare),
158
+ frontingFeeShare: serializeBN(this.frontingFeeShare),
159
+ executionFeeShare: serializeBN(this.executionFeeShare),
160
+ token: this.token,
161
+ gasToken: this.gasToken,
162
+ tokenMultiplier: serializeBN(this.tokenMultiplier),
163
+ gasTokenMultiplier: serializeBN(this.gasTokenMultiplier),
164
+ btcTxId: this.btcTxId
165
+ };
166
+ }
167
+
168
+ getIdentifierHash(): string {
169
+ return this.quoteId;
170
+ }
171
+
172
+ getOutputGasAmount(): bigint {
173
+ return this.amountGasToken;
174
+ }
175
+
176
+ getOutputAmount(): bigint {
177
+ return this.amountToken;
178
+ }
179
+
180
+ getTotalOutputAmount(): bigint {
181
+ return this.rawAmountToken * (100_000n + this.callerFeeShare + this.frontingFeeShare + this.executionFeeShare) / 100_000n * this.tokenMultiplier;
182
+ }
183
+
184
+ getTotalOutputGasAmount(): bigint {
185
+ return this.rawAmountGasToken * (100_000n + this.callerFeeShare + this.frontingFeeShare) / 100_000n * this.gasTokenMultiplier;
186
+ }
187
+
188
+ getSequence(): bigint | null {
189
+ return 0n;
190
+ }
191
+
192
+ getSwapFee(): { inInputToken: bigint; inOutputToken: bigint } {
193
+ return {inInputToken: this.swapFee, inOutputToken: this.swapFeeInToken};
194
+ }
195
+
196
+ getTokenSwapFee(): { inInputToken: bigint; inOutputToken: bigint } {
197
+ return {inInputToken: this.tokenSwapFee, inOutputToken: this.tokenSwapFeeInToken};
198
+ }
199
+
200
+ getGasSwapFee(): { inInputToken: bigint; inOutputToken: bigint } {
201
+ return {inInputToken: this.gasSwapFee, inOutputToken: this.gasSwapFeeInToken};
202
+ }
203
+
204
+ getToken(): string {
205
+ return this.token;
206
+ }
207
+
208
+ getGasToken(): string {
209
+ return this.gasToken;
210
+ }
211
+
212
+ getTotalInputAmount(): bigint {
213
+ return this.amountBtc;
214
+ }
215
+
216
+ isFailed(): boolean {
217
+ return this.state===SpvVaultSwapState.FAILED || this.state===SpvVaultSwapState.DOUBLE_SPENT;
218
+ }
219
+
220
+ isInitiated(): boolean {
221
+ return this.state!==SpvVaultSwapState.CREATED;
222
+ }
223
+
224
+ isSuccess(): boolean {
225
+ return this.state===SpvVaultSwapState.CLAIMED;
226
+ }
227
+
228
+ }