@atomiqlabs/chain-solana 13.5.13 → 13.5.14
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/LICENSE +201 -201
- package/README.md +73 -73
- package/dist/index.d.ts +81 -81
- package/dist/index.js +102 -102
- package/dist/node/index.d.ts +9 -9
- package/dist/node/index.js +13 -13
- package/dist/solana/SolanaChainType.d.ts +15 -15
- package/dist/solana/SolanaChainType.js +2 -2
- package/dist/solana/SolanaChains.d.ts +12 -12
- package/dist/solana/SolanaChains.js +45 -45
- package/dist/solana/SolanaInitializer.d.ts +94 -94
- package/dist/solana/SolanaInitializer.js +174 -174
- package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +222 -222
- package/dist/solana/btcrelay/SolanaBtcRelay.js +455 -455
- package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +84 -84
- package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +70 -70
- package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +92 -92
- package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +109 -109
- package/dist/solana/btcrelay/program/programIdl.json +671 -671
- package/dist/solana/chain/SolanaAction.d.ts +26 -26
- package/dist/solana/chain/SolanaAction.js +87 -87
- package/dist/solana/chain/SolanaChainInterface.d.ts +224 -224
- package/dist/solana/chain/SolanaChainInterface.js +275 -275
- package/dist/solana/chain/SolanaModule.d.ts +14 -14
- package/dist/solana/chain/SolanaModule.js +13 -13
- package/dist/solana/chain/modules/SolanaAddresses.d.ts +8 -8
- package/dist/solana/chain/modules/SolanaAddresses.js +22 -22
- package/dist/solana/chain/modules/SolanaBlocks.d.ts +32 -32
- package/dist/solana/chain/modules/SolanaBlocks.js +78 -78
- package/dist/solana/chain/modules/SolanaEvents.d.ts +68 -68
- package/dist/solana/chain/modules/SolanaEvents.js +238 -238
- package/dist/solana/chain/modules/SolanaFees.d.ts +189 -189
- package/dist/solana/chain/modules/SolanaFees.js +434 -434
- package/dist/solana/chain/modules/SolanaSignatures.d.ts +24 -24
- package/dist/solana/chain/modules/SolanaSignatures.js +39 -39
- package/dist/solana/chain/modules/SolanaSlots.d.ts +33 -33
- package/dist/solana/chain/modules/SolanaSlots.js +72 -72
- package/dist/solana/chain/modules/SolanaTokens.d.ts +123 -123
- package/dist/solana/chain/modules/SolanaTokens.js +242 -242
- package/dist/solana/chain/modules/SolanaTransactions.d.ts +149 -149
- package/dist/solana/chain/modules/SolanaTransactions.js +445 -445
- package/dist/solana/connection/ConnectionWithRetries.d.ts +35 -35
- package/dist/solana/connection/ConnectionWithRetries.js +86 -71
- package/dist/solana/events/SolanaChainEvents.d.ts +45 -45
- package/dist/solana/events/SolanaChainEvents.js +108 -108
- package/dist/solana/events/SolanaChainEventsBrowser.d.ts +205 -205
- package/dist/solana/events/SolanaChainEventsBrowser.js +404 -404
- package/dist/solana/program/SolanaProgramBase.d.ts +73 -73
- package/dist/solana/program/SolanaProgramBase.js +54 -54
- package/dist/solana/program/SolanaProgramModule.d.ts +8 -8
- package/dist/solana/program/SolanaProgramModule.js +11 -11
- package/dist/solana/program/modules/SolanaProgramEvents.d.ts +53 -53
- package/dist/solana/program/modules/SolanaProgramEvents.js +117 -117
- package/dist/solana/swaps/SolanaSwapData.d.ts +333 -333
- package/dist/solana/swaps/SolanaSwapData.js +535 -535
- package/dist/solana/swaps/SolanaSwapModule.d.ts +11 -11
- package/dist/solana/swaps/SolanaSwapModule.js +12 -12
- package/dist/solana/swaps/SolanaSwapProgram.d.ts +376 -376
- package/dist/solana/swaps/SolanaSwapProgram.js +769 -769
- package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -11
- package/dist/solana/swaps/SwapTypeEnum.js +43 -43
- package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +95 -95
- package/dist/solana/swaps/modules/SolanaDataAccount.js +232 -232
- package/dist/solana/swaps/modules/SolanaLpVault.d.ts +69 -69
- package/dist/solana/swaps/modules/SolanaLpVault.js +171 -171
- package/dist/solana/swaps/modules/SwapClaim.d.ts +126 -126
- package/dist/solana/swaps/modules/SwapClaim.js +294 -294
- package/dist/solana/swaps/modules/SwapInit.d.ts +213 -213
- package/dist/solana/swaps/modules/SwapInit.js +658 -658
- package/dist/solana/swaps/modules/SwapRefund.d.ts +87 -87
- package/dist/solana/swaps/modules/SwapRefund.js +293 -293
- package/dist/solana/swaps/programIdl.json +945 -945
- package/dist/solana/swaps/programTypes.d.ts +943 -943
- package/dist/solana/swaps/programTypes.js +945 -945
- package/dist/solana/swaps/v1/programIdl.json +945 -945
- package/dist/solana/swaps/v1/programTypes.d.ts +943 -943
- package/dist/solana/swaps/v1/programTypes.js +945 -945
- package/dist/solana/swaps/v2/programIdl.json +952 -952
- package/dist/solana/swaps/v2/programTypes.d.ts +950 -950
- package/dist/solana/swaps/v2/programTypes.js +952 -952
- package/dist/solana/wallet/SolanaKeypairWallet.d.ts +29 -29
- package/dist/solana/wallet/SolanaKeypairWallet.js +50 -50
- package/dist/solana/wallet/SolanaSigner.d.ts +30 -30
- package/dist/solana/wallet/SolanaSigner.js +30 -30
- package/dist/utils/Utils.d.ts +58 -58
- package/dist/utils/Utils.js +170 -170
- package/node/index.d.ts +1 -1
- package/node/index.js +3 -3
- package/package.json +46 -46
- package/src/index.ts +87 -87
- package/src/node/index.ts +9 -9
- package/src/solana/SolanaChainType.ts +32 -32
- package/src/solana/SolanaChains.ts +46 -46
- package/src/solana/SolanaInitializer.ts +278 -278
- package/src/solana/btcrelay/SolanaBtcRelay.ts +615 -615
- package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +116 -116
- package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +148 -148
- package/src/solana/btcrelay/program/programIdl.json +670 -670
- package/src/solana/chain/SolanaAction.ts +109 -109
- package/src/solana/chain/SolanaChainInterface.ts +404 -404
- package/src/solana/chain/SolanaModule.ts +20 -20
- package/src/solana/chain/modules/SolanaAddresses.ts +20 -20
- package/src/solana/chain/modules/SolanaBlocks.ts +89 -89
- package/src/solana/chain/modules/SolanaEvents.ts +271 -271
- package/src/solana/chain/modules/SolanaFees.ts +522 -522
- package/src/solana/chain/modules/SolanaSignatures.ts +39 -39
- package/src/solana/chain/modules/SolanaSlots.ts +85 -85
- package/src/solana/chain/modules/SolanaTokens.ts +300 -300
- package/src/solana/chain/modules/SolanaTransactions.ts +503 -503
- package/src/solana/connection/ConnectionWithRetries.ts +113 -96
- package/src/solana/events/SolanaChainEvents.ts +127 -127
- package/src/solana/events/SolanaChainEventsBrowser.ts +495 -495
- package/src/solana/program/SolanaProgramBase.ts +119 -119
- package/src/solana/program/SolanaProgramModule.ts +15 -15
- package/src/solana/program/modules/SolanaProgramEvents.ts +157 -157
- package/src/solana/swaps/SolanaSwapData.ts +735 -735
- package/src/solana/swaps/SolanaSwapModule.ts +19 -19
- package/src/solana/swaps/SolanaSwapProgram.ts +1074 -1074
- package/src/solana/swaps/SwapTypeEnum.ts +30 -30
- package/src/solana/swaps/modules/SolanaDataAccount.ts +302 -302
- package/src/solana/swaps/modules/SolanaLpVault.ts +208 -208
- package/src/solana/swaps/modules/SwapClaim.ts +387 -387
- package/src/solana/swaps/modules/SwapInit.ts +785 -785
- package/src/solana/swaps/modules/SwapRefund.ts +353 -353
- package/src/solana/swaps/v1/programIdl.json +944 -944
- package/src/solana/swaps/v1/programTypes.ts +1885 -1885
- package/src/solana/swaps/v2/programIdl.json +951 -951
- package/src/solana/swaps/v2/programTypes.ts +1899 -1899
- package/src/solana/wallet/SolanaKeypairWallet.ts +56 -56
- package/src/solana/wallet/SolanaSigner.ts +43 -43
- package/src/utils/Utils.ts +194 -194
|
@@ -1,535 +1,535 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SolanaSwapDataV2 = exports.SolanaSwapDataV1 = exports.SolanaSwapData = exports.isSerializedData = void 0;
|
|
4
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
-
const BN = require("bn.js");
|
|
6
|
-
const base_1 = require("@atomiqlabs/base");
|
|
7
|
-
const SwapTypeEnum_1 = require("./SwapTypeEnum");
|
|
8
|
-
const buffer_1 = require("buffer");
|
|
9
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
10
|
-
const Utils_1 = require("../../utils/Utils");
|
|
11
|
-
const SolanaTokens_1 = require("../chain/modules/SolanaTokens");
|
|
12
|
-
const SolanaSwapProgram_1 = require("./SolanaSwapProgram");
|
|
13
|
-
const EXPIRY_BLOCKHEIGHT_THRESHOLD = new BN("1000000000");
|
|
14
|
-
function isSerializedData(obj) {
|
|
15
|
-
return obj.type === "sol";
|
|
16
|
-
}
|
|
17
|
-
exports.isSerializedData = isSerializedData;
|
|
18
|
-
/**
|
|
19
|
-
* Represents Solana swap data for executing PrTLC (on-chain) or HTLC (lightning) based swaps.
|
|
20
|
-
*
|
|
21
|
-
* @category Swaps
|
|
22
|
-
*/
|
|
23
|
-
class SolanaSwapData extends base_1.SwapData {
|
|
24
|
-
constructor(data) {
|
|
25
|
-
super();
|
|
26
|
-
if (!isSerializedData(data)) {
|
|
27
|
-
this.programId = data.programId;
|
|
28
|
-
this.version = data.version;
|
|
29
|
-
this.offerer = data.offerer;
|
|
30
|
-
this.claimer = data.claimer;
|
|
31
|
-
this.token = data.token;
|
|
32
|
-
this.amount = data.amount;
|
|
33
|
-
this.paymentHash = data.paymentHash;
|
|
34
|
-
this.sequence = data.sequence;
|
|
35
|
-
this.expiry = data.expiry;
|
|
36
|
-
this.nonce = data.nonce;
|
|
37
|
-
this.confirmations = data.confirmations;
|
|
38
|
-
this.payOut = data.payOut;
|
|
39
|
-
this.kind = data.kind;
|
|
40
|
-
this.payIn = data.payIn;
|
|
41
|
-
this.claimerAta = data.claimerAta;
|
|
42
|
-
this.offererAta = data.offererAta;
|
|
43
|
-
this.securityDeposit = data.securityDeposit;
|
|
44
|
-
this.claimerBounty = data.claimerBounty;
|
|
45
|
-
this.txoHash = data.txoHash;
|
|
46
|
-
this.offererInitializer = data.offererInitializer;
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
this.programId = new web3_js_1.PublicKey(data.programId ?? "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM");
|
|
50
|
-
this.version = data.version ?? "v1";
|
|
51
|
-
this.offerer = new web3_js_1.PublicKey(data.offerer);
|
|
52
|
-
this.claimer = new web3_js_1.PublicKey(data.claimer);
|
|
53
|
-
this.token = new web3_js_1.PublicKey(data.token);
|
|
54
|
-
this.amount = new BN(data.amount);
|
|
55
|
-
this.paymentHash = data.paymentHash;
|
|
56
|
-
this.sequence = new BN(data.sequence);
|
|
57
|
-
this.expiry = new BN(data.expiry);
|
|
58
|
-
this.nonce = new BN(data.nonce);
|
|
59
|
-
this.confirmations = data.confirmations;
|
|
60
|
-
this.payOut = data.payOut;
|
|
61
|
-
this.kind = data.kind;
|
|
62
|
-
this.payIn = data.payIn;
|
|
63
|
-
this.claimerAta = data.claimerAta == null ? undefined : new web3_js_1.PublicKey(data.claimerAta);
|
|
64
|
-
this.offererAta = data.offererAta == null ? undefined : new web3_js_1.PublicKey(data.offererAta);
|
|
65
|
-
this.securityDeposit = new BN(data.securityDeposit);
|
|
66
|
-
this.claimerBounty = new BN(data.claimerBounty);
|
|
67
|
-
this.txoHash = data.txoHash;
|
|
68
|
-
this.offererInitializer = data.offererInitializer;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* @inheritDoc
|
|
73
|
-
*/
|
|
74
|
-
getOfferer() {
|
|
75
|
-
return this.offerer.toBase58();
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* @inheritDoc
|
|
79
|
-
*/
|
|
80
|
-
setOfferer(newOfferer) {
|
|
81
|
-
this.offerer = new web3_js_1.PublicKey(newOfferer);
|
|
82
|
-
this.offererAta = (0, spl_token_1.getAssociatedTokenAddressSync)(this.token, this.offerer);
|
|
83
|
-
this.payIn = true;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* @inheritDoc
|
|
87
|
-
*/
|
|
88
|
-
getClaimer() {
|
|
89
|
-
return this.claimer.toBase58();
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* @inheritDoc
|
|
93
|
-
*/
|
|
94
|
-
setClaimer(newClaimer) {
|
|
95
|
-
this.claimer = new web3_js_1.PublicKey(newClaimer);
|
|
96
|
-
this.payIn = false;
|
|
97
|
-
this.payOut = true;
|
|
98
|
-
this.claimerAta = (0, spl_token_1.getAssociatedTokenAddressSync)(this.token, this.claimer);
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* @inheritDoc
|
|
102
|
-
*/
|
|
103
|
-
serialize() {
|
|
104
|
-
return {
|
|
105
|
-
type: "sol",
|
|
106
|
-
programId: this.programId?.toBase58(),
|
|
107
|
-
version: this.version,
|
|
108
|
-
offerer: this.offerer?.toBase58(),
|
|
109
|
-
claimer: this.claimer?.toBase58(),
|
|
110
|
-
token: this.token?.toBase58(),
|
|
111
|
-
amount: this.amount?.toString(10),
|
|
112
|
-
paymentHash: this.paymentHash,
|
|
113
|
-
sequence: this.sequence?.toString(10),
|
|
114
|
-
expiry: this.expiry?.toString(10),
|
|
115
|
-
nonce: this.nonce?.toString(10),
|
|
116
|
-
confirmations: this.confirmations,
|
|
117
|
-
payOut: this.payOut,
|
|
118
|
-
kind: this.kind,
|
|
119
|
-
payIn: this.payIn,
|
|
120
|
-
offererAta: this.offererAta?.toBase58(),
|
|
121
|
-
claimerAta: this.claimerAta?.toBase58(),
|
|
122
|
-
securityDeposit: this.securityDeposit?.toString(10),
|
|
123
|
-
claimerBounty: this.claimerBounty?.toString(10),
|
|
124
|
-
txoHash: this.txoHash,
|
|
125
|
-
offererInitializer: this.offererInitializer
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* @inheritDoc
|
|
130
|
-
*/
|
|
131
|
-
getAmount() {
|
|
132
|
-
return (0, Utils_1.toBigInt)(this.amount);
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* @inheritDoc
|
|
136
|
-
*/
|
|
137
|
-
getToken() {
|
|
138
|
-
return this.token.toString();
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* @inheritDoc
|
|
142
|
-
*/
|
|
143
|
-
isToken(token) {
|
|
144
|
-
return this.token.equals(new web3_js_1.PublicKey(token));
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* @inheritDoc
|
|
148
|
-
*/
|
|
149
|
-
getType() {
|
|
150
|
-
return SolanaSwapData.kindToType(this.kind);
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* @inheritDoc
|
|
154
|
-
*/
|
|
155
|
-
getExpiry() {
|
|
156
|
-
if (this.expiry.lt(EXPIRY_BLOCKHEIGHT_THRESHOLD))
|
|
157
|
-
throw new Error("Expiry expressed as bitcoin blockheight!");
|
|
158
|
-
return (0, Utils_1.toBigInt)(this.expiry);
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* @inheritDoc
|
|
162
|
-
*/
|
|
163
|
-
getConfirmationsHint() {
|
|
164
|
-
return this.confirmations;
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* @inheritDoc
|
|
168
|
-
*/
|
|
169
|
-
getNonceHint() {
|
|
170
|
-
return (0, Utils_1.toBigInt)(this.nonce);
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* @inheritDoc
|
|
174
|
-
*/
|
|
175
|
-
isPayIn() {
|
|
176
|
-
return this.payIn;
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* @inheritDoc
|
|
180
|
-
*/
|
|
181
|
-
isPayOut() {
|
|
182
|
-
return this.payOut;
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* @inheritDoc
|
|
186
|
-
*/
|
|
187
|
-
isTrackingReputation() {
|
|
188
|
-
return !this.payOut;
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* @inheritDoc
|
|
192
|
-
*/
|
|
193
|
-
getClaimHash() {
|
|
194
|
-
return (0, Utils_1.toClaimHash)(this.paymentHash, (0, Utils_1.toBigInt)(this.nonce), this.confirmations);
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* @inheritDoc
|
|
198
|
-
*/
|
|
199
|
-
getEscrowHash() {
|
|
200
|
-
return (0, Utils_1.toEscrowHash)(this.paymentHash, this.sequence);
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* @inheritDoc
|
|
204
|
-
*/
|
|
205
|
-
getSequence() {
|
|
206
|
-
return (0, Utils_1.toBigInt)(this.sequence);
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* @inheritDoc
|
|
210
|
-
*/
|
|
211
|
-
getTxoHashHint() {
|
|
212
|
-
if (this.txoHash === "0000000000000000000000000000000000000000000000000000000000000000")
|
|
213
|
-
return null; //Txo hash opt-out flag
|
|
214
|
-
return this.txoHash ?? null;
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* @inheritDoc
|
|
218
|
-
*/
|
|
219
|
-
getHTLCHashHint() {
|
|
220
|
-
if (this.getType() === base_1.ChainSwapType.HTLC)
|
|
221
|
-
return this.paymentHash;
|
|
222
|
-
return null;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* @inheritDoc
|
|
226
|
-
*/
|
|
227
|
-
getExtraData() {
|
|
228
|
-
return this.txoHash ?? null;
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* @inheritDoc
|
|
232
|
-
*/
|
|
233
|
-
setExtraData(txoHash) {
|
|
234
|
-
this.txoHash = txoHash;
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* @inheritDoc
|
|
238
|
-
*/
|
|
239
|
-
getSecurityDeposit() {
|
|
240
|
-
return (0, Utils_1.toBigInt)(this.securityDeposit);
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* @inheritDoc
|
|
244
|
-
*/
|
|
245
|
-
getClaimerBounty() {
|
|
246
|
-
return (0, Utils_1.toBigInt)(this.claimerBounty);
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* @inheritDoc
|
|
250
|
-
*/
|
|
251
|
-
getTotalDeposit() {
|
|
252
|
-
return (0, Utils_1.toBigInt)(this.claimerBounty.lt(this.securityDeposit) ? this.securityDeposit : this.claimerBounty);
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Serializes the swap data into the Solana program `SwapData` struct representation.
|
|
256
|
-
*/
|
|
257
|
-
toSwapDataStruct() {
|
|
258
|
-
return {
|
|
259
|
-
kind: SwapTypeEnum_1.SwapTypeEnum.fromNumber(this.kind),
|
|
260
|
-
confirmations: this.confirmations,
|
|
261
|
-
nonce: this.nonce,
|
|
262
|
-
hash: [...buffer_1.Buffer.from(this.paymentHash, "hex")],
|
|
263
|
-
payIn: this.payIn,
|
|
264
|
-
payOut: this.payOut,
|
|
265
|
-
amount: this.amount,
|
|
266
|
-
expiry: this.expiry,
|
|
267
|
-
sequence: this.sequence
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Checks whether the provided escrow account matches this swap data.
|
|
272
|
-
*
|
|
273
|
-
* @param account Escrow account data fetched from chain
|
|
274
|
-
*/
|
|
275
|
-
correctPDA(account) {
|
|
276
|
-
return SwapTypeEnum_1.SwapTypeEnum.toNumber(account.data.kind) === this.kind &&
|
|
277
|
-
account.data.confirmations === this.confirmations &&
|
|
278
|
-
this.nonce.eq(account.data.nonce) &&
|
|
279
|
-
buffer_1.Buffer.from(account.data.hash).toString("hex") === this.paymentHash &&
|
|
280
|
-
account.data.payIn === this.payIn &&
|
|
281
|
-
account.data.payOut === this.payOut &&
|
|
282
|
-
this.amount.eq(account.data.amount) &&
|
|
283
|
-
this.expiry.eq(account.data.expiry) &&
|
|
284
|
-
this.sequence.eq(account.data.sequence) &&
|
|
285
|
-
account.offerer.equals(this.offerer) &&
|
|
286
|
-
(this.offererAta == null || account.offererAta.equals(this.offererAta)) &&
|
|
287
|
-
account.claimer.equals(this.claimer) &&
|
|
288
|
-
(this.claimerAta == null || account.claimerAta.equals(this.claimerAta)) &&
|
|
289
|
-
account.mint.equals(this.token) &&
|
|
290
|
-
this.claimerBounty.eq(account.claimerBounty) &&
|
|
291
|
-
this.securityDeposit.eq(account.securityDeposit) &&
|
|
292
|
-
(this.offererInitializer == null || account.offererInitializer === this.offererInitializer);
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* @inheritDoc
|
|
296
|
-
*/
|
|
297
|
-
equals(other) {
|
|
298
|
-
if (this.claimerAta == null && other.claimerAta != null)
|
|
299
|
-
return false;
|
|
300
|
-
if (this.claimerAta != null && other.claimerAta == null)
|
|
301
|
-
return false;
|
|
302
|
-
if (this.claimerAta != null && other.claimerAta != null) {
|
|
303
|
-
if (!this.claimerAta.equals(other.claimerAta))
|
|
304
|
-
return false;
|
|
305
|
-
}
|
|
306
|
-
if (this.offererAta == null && other.offererAta != null)
|
|
307
|
-
return false;
|
|
308
|
-
if (this.offererAta != null && other.offererAta == null)
|
|
309
|
-
return false;
|
|
310
|
-
if (this.offererAta != null && other.offererAta != null) {
|
|
311
|
-
if (!this.offererAta.equals(other.offererAta))
|
|
312
|
-
return false;
|
|
313
|
-
}
|
|
314
|
-
if (this.offererInitializer == null && other.offererInitializer != null)
|
|
315
|
-
return false;
|
|
316
|
-
if (this.offererInitializer != null && other.offererInitializer == null)
|
|
317
|
-
return false;
|
|
318
|
-
if (this.offererInitializer != null && other.offererInitializer != null) {
|
|
319
|
-
if (this.offererInitializer !== other.offererInitializer)
|
|
320
|
-
return false;
|
|
321
|
-
}
|
|
322
|
-
return other.kind === this.kind &&
|
|
323
|
-
other.confirmations === this.confirmations &&
|
|
324
|
-
this.nonce.eq(other.nonce) &&
|
|
325
|
-
other.paymentHash === this.paymentHash &&
|
|
326
|
-
this.sequence.eq(other.sequence) &&
|
|
327
|
-
other.payIn === this.payIn &&
|
|
328
|
-
other.payOut === this.payOut &&
|
|
329
|
-
other.offerer.equals(this.offerer) &&
|
|
330
|
-
other.claimer.equals(this.claimer) &&
|
|
331
|
-
other.expiry.eq(this.expiry) &&
|
|
332
|
-
other.amount.eq(this.amount) &&
|
|
333
|
-
other.securityDeposit.eq(this.securityDeposit) &&
|
|
334
|
-
other.claimerBounty.eq(this.claimerBounty) &&
|
|
335
|
-
other.token.equals(this.token);
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Converts initialize instruction data into {@link SolanaSwapData}.
|
|
339
|
-
*
|
|
340
|
-
* @param programId
|
|
341
|
-
* @param version
|
|
342
|
-
* @param initIx Decoded initialize instruction
|
|
343
|
-
* @param txoHash Parsed txo hash hint from initialize event
|
|
344
|
-
* @returns Converted and parsed swap data
|
|
345
|
-
*/
|
|
346
|
-
static fromInstruction(programId, version, initIx, txoHash) {
|
|
347
|
-
const paymentHash = buffer_1.Buffer.from(initIx.data.swapData.hash);
|
|
348
|
-
let securityDeposit = new BN(0);
|
|
349
|
-
let claimerBounty = new BN(0);
|
|
350
|
-
let payIn = true;
|
|
351
|
-
if (initIx.name === "offererInitialize") {
|
|
352
|
-
payIn = false;
|
|
353
|
-
securityDeposit = initIx.data.securityDeposit;
|
|
354
|
-
claimerBounty = initIx.data.claimerBounty;
|
|
355
|
-
}
|
|
356
|
-
if (version !== "v1" && initIx.name === "offererInitializePayIn") {
|
|
357
|
-
payIn = true;
|
|
358
|
-
securityDeposit = initIx.data.securityDeposit;
|
|
359
|
-
claimerBounty = initIx.data.claimerBounty;
|
|
360
|
-
}
|
|
361
|
-
return new SolanaSwapData({
|
|
362
|
-
programId,
|
|
363
|
-
version,
|
|
364
|
-
offerer: initIx.accounts.offerer,
|
|
365
|
-
claimer: initIx.accounts.claimer,
|
|
366
|
-
token: initIx.accounts.mint,
|
|
367
|
-
amount: initIx.data.swapData.amount,
|
|
368
|
-
paymentHash: paymentHash.toString("hex"),
|
|
369
|
-
sequence: initIx.data.swapData.sequence,
|
|
370
|
-
expiry: initIx.data.swapData.expiry,
|
|
371
|
-
nonce: initIx.data.swapData.nonce,
|
|
372
|
-
confirmations: initIx.data.swapData.confirmations,
|
|
373
|
-
payOut: initIx.data.swapData.payOut,
|
|
374
|
-
kind: SwapTypeEnum_1.SwapTypeEnum.toNumber(initIx.data.swapData.kind),
|
|
375
|
-
payIn,
|
|
376
|
-
offererAta: initIx.name === "offererInitializePayIn" ? initIx.accounts.offererAta : web3_js_1.PublicKey.default,
|
|
377
|
-
claimerAta: initIx.data.swapData.payOut ? initIx.accounts.claimerAta : web3_js_1.PublicKey.default,
|
|
378
|
-
securityDeposit,
|
|
379
|
-
claimerBounty,
|
|
380
|
-
txoHash,
|
|
381
|
-
offererInitializer: initIx.accounts.initializer != null ? initIx.accounts.initializer.equals(initIx.accounts.offerer) : undefined
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
/**
|
|
385
|
-
* Deserializes swap data from an on-chain escrow account state.
|
|
386
|
-
*
|
|
387
|
-
* @param programId
|
|
388
|
-
* @param version
|
|
389
|
-
* @param account Escrow account state as returned by Anchor
|
|
390
|
-
*/
|
|
391
|
-
static fromEscrowState(programId, version, account) {
|
|
392
|
-
const data = account.data;
|
|
393
|
-
return new SolanaSwapData({
|
|
394
|
-
programId,
|
|
395
|
-
version,
|
|
396
|
-
offerer: account.offerer,
|
|
397
|
-
claimer: account.claimer,
|
|
398
|
-
token: account.mint,
|
|
399
|
-
amount: data.amount,
|
|
400
|
-
paymentHash: buffer_1.Buffer.from(data.hash).toString("hex"),
|
|
401
|
-
sequence: data.sequence,
|
|
402
|
-
expiry: data.expiry,
|
|
403
|
-
nonce: data.nonce,
|
|
404
|
-
confirmations: data.confirmations,
|
|
405
|
-
payOut: data.payOut,
|
|
406
|
-
kind: SwapTypeEnum_1.SwapTypeEnum.toNumber(data.kind),
|
|
407
|
-
payIn: data.payIn,
|
|
408
|
-
offererAta: account.offererAta,
|
|
409
|
-
claimerAta: account.claimerAta,
|
|
410
|
-
securityDeposit: account.securityDeposit,
|
|
411
|
-
claimerBounty: account.claimerBounty,
|
|
412
|
-
offererInitializer: account.offererInitializer ?? undefined
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Converts abstract swap type to Solana program kind discriminator.
|
|
417
|
-
*
|
|
418
|
-
* @param type Chain-agnostic swap type
|
|
419
|
-
*/
|
|
420
|
-
static typeToKind(type) {
|
|
421
|
-
switch (type) {
|
|
422
|
-
case base_1.ChainSwapType.HTLC:
|
|
423
|
-
return 0;
|
|
424
|
-
case base_1.ChainSwapType.CHAIN:
|
|
425
|
-
return 1;
|
|
426
|
-
case base_1.ChainSwapType.CHAIN_NONCED:
|
|
427
|
-
return 2;
|
|
428
|
-
case base_1.ChainSwapType.CHAIN_TXID:
|
|
429
|
-
return 3;
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
* Converts Solana program kind discriminator to abstract swap type.
|
|
434
|
-
*
|
|
435
|
-
* @param value Solana program swap kind value
|
|
436
|
-
*/
|
|
437
|
-
static kindToType(value) {
|
|
438
|
-
switch (value) {
|
|
439
|
-
case 0:
|
|
440
|
-
return base_1.ChainSwapType.HTLC;
|
|
441
|
-
case 1:
|
|
442
|
-
return base_1.ChainSwapType.CHAIN;
|
|
443
|
-
case 2:
|
|
444
|
-
return base_1.ChainSwapType.CHAIN_NONCED;
|
|
445
|
-
case 3:
|
|
446
|
-
return base_1.ChainSwapType.CHAIN_TXID;
|
|
447
|
-
}
|
|
448
|
-
throw new Error("Unknown swap kind type!");
|
|
449
|
-
}
|
|
450
|
-
/**
|
|
451
|
-
* @inheritDoc
|
|
452
|
-
*/
|
|
453
|
-
isClaimer(address) {
|
|
454
|
-
const _address = new web3_js_1.PublicKey(address);
|
|
455
|
-
if (this.isPayOut()) {
|
|
456
|
-
//Also check that swapData's ATA is correct
|
|
457
|
-
const ourAta = (0, spl_token_1.getAssociatedTokenAddressSync)(this.token, _address);
|
|
458
|
-
if (this.claimerAta == null || !this.claimerAta.equals(ourAta))
|
|
459
|
-
return false;
|
|
460
|
-
}
|
|
461
|
-
return this.claimer.equals(new web3_js_1.PublicKey(address));
|
|
462
|
-
}
|
|
463
|
-
/**
|
|
464
|
-
* @inheritDoc
|
|
465
|
-
*/
|
|
466
|
-
isOfferer(address) {
|
|
467
|
-
return this.offerer.equals(new web3_js_1.PublicKey(address));
|
|
468
|
-
}
|
|
469
|
-
/**
|
|
470
|
-
* @inheritDoc
|
|
471
|
-
*/
|
|
472
|
-
getDepositToken() {
|
|
473
|
-
return SolanaTokens_1.SolanaTokens.WSOL_ADDRESS.toString();
|
|
474
|
-
}
|
|
475
|
-
/**
|
|
476
|
-
* @inheritDoc
|
|
477
|
-
*/
|
|
478
|
-
isDepositToken(token) {
|
|
479
|
-
return SolanaTokens_1.SolanaTokens.WSOL_ADDRESS.equals(new web3_js_1.PublicKey(token));
|
|
480
|
-
}
|
|
481
|
-
/**
|
|
482
|
-
* @inheritDoc
|
|
483
|
-
*/
|
|
484
|
-
getEscrowStruct() {
|
|
485
|
-
return {
|
|
486
|
-
accounts: {
|
|
487
|
-
offerer: this.offerer.toString(),
|
|
488
|
-
claimer: this.claimer.toString(),
|
|
489
|
-
claimerAta: this.claimerAta == null || this.claimerAta.equals(web3_js_1.PublicKey.default) ? null : this.claimerAta.toString(),
|
|
490
|
-
offererAta: this.offererAta == null || this.offererAta.equals(web3_js_1.PublicKey.default) ? null : this.offererAta.toString(),
|
|
491
|
-
claimerUserData: SolanaSwapProgram_1.SolanaSwapProgram._SwapUserVault(this.programId, this.claimer, this.token).toString(),
|
|
492
|
-
offererUserData: SolanaSwapProgram_1.SolanaSwapProgram._SwapUserVault(this.programId, this.offerer, this.token).toString(),
|
|
493
|
-
initializer: this.offererInitializer != null
|
|
494
|
-
? (this.offererInitializer ? this.offerer.toString() : this.claimer.toString())
|
|
495
|
-
: (this.isPayIn() ? this.offerer.toString() : this.claimer.toString()),
|
|
496
|
-
escrowState: SolanaSwapProgram_1.SolanaSwapProgram._SwapEscrowState(this.programId, buffer_1.Buffer.from(this.paymentHash, "hex")).toString(),
|
|
497
|
-
mint: this.token.toString(),
|
|
498
|
-
vault: SolanaSwapProgram_1.SolanaSwapProgram._SwapVault(this.programId, this.token).toString(),
|
|
499
|
-
vaultAuthority: SolanaSwapProgram_1.SolanaSwapProgram._SwapVaultAuthority(this.programId).toString(),
|
|
500
|
-
systemProgram: web3_js_1.SystemProgram.programId.toString(),
|
|
501
|
-
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID.toString(),
|
|
502
|
-
ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY.toString(),
|
|
503
|
-
},
|
|
504
|
-
data: {
|
|
505
|
-
kind: SwapTypeEnum_1.SwapTypeEnum.fromNumber(this.kind),
|
|
506
|
-
confirmations: this.confirmations,
|
|
507
|
-
nonce: this.nonce.toString(10),
|
|
508
|
-
hash: [...buffer_1.Buffer.from(this.paymentHash, "hex")],
|
|
509
|
-
payIn: this.payIn,
|
|
510
|
-
payOut: this.payOut,
|
|
511
|
-
amount: this.amount.toString(10),
|
|
512
|
-
expiry: this.expiry.toString(10),
|
|
513
|
-
sequence: this.sequence.toString(10)
|
|
514
|
-
}
|
|
515
|
-
};
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
exports.SolanaSwapData = SolanaSwapData;
|
|
519
|
-
class SolanaSwapDataV1 extends SolanaSwapData {
|
|
520
|
-
constructor(data) {
|
|
521
|
-
super(data);
|
|
522
|
-
if (this.version !== "v1")
|
|
523
|
-
throw new Error(`Invalid swap data version, expected v1, got ${this.version}`);
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
exports.SolanaSwapDataV1 = SolanaSwapDataV1;
|
|
527
|
-
class SolanaSwapDataV2 extends SolanaSwapData {
|
|
528
|
-
constructor(data) {
|
|
529
|
-
super(data);
|
|
530
|
-
if (this.version !== "v2")
|
|
531
|
-
throw new Error(`Invalid swap data version, expected v2, got ${this.version}`);
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
exports.SolanaSwapDataV2 = SolanaSwapDataV2;
|
|
535
|
-
base_1.SwapData.deserializers["sol"] = SolanaSwapData;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SolanaSwapDataV2 = exports.SolanaSwapDataV1 = exports.SolanaSwapData = exports.isSerializedData = void 0;
|
|
4
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
+
const BN = require("bn.js");
|
|
6
|
+
const base_1 = require("@atomiqlabs/base");
|
|
7
|
+
const SwapTypeEnum_1 = require("./SwapTypeEnum");
|
|
8
|
+
const buffer_1 = require("buffer");
|
|
9
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
10
|
+
const Utils_1 = require("../../utils/Utils");
|
|
11
|
+
const SolanaTokens_1 = require("../chain/modules/SolanaTokens");
|
|
12
|
+
const SolanaSwapProgram_1 = require("./SolanaSwapProgram");
|
|
13
|
+
const EXPIRY_BLOCKHEIGHT_THRESHOLD = new BN("1000000000");
|
|
14
|
+
function isSerializedData(obj) {
|
|
15
|
+
return obj.type === "sol";
|
|
16
|
+
}
|
|
17
|
+
exports.isSerializedData = isSerializedData;
|
|
18
|
+
/**
|
|
19
|
+
* Represents Solana swap data for executing PrTLC (on-chain) or HTLC (lightning) based swaps.
|
|
20
|
+
*
|
|
21
|
+
* @category Swaps
|
|
22
|
+
*/
|
|
23
|
+
class SolanaSwapData extends base_1.SwapData {
|
|
24
|
+
constructor(data) {
|
|
25
|
+
super();
|
|
26
|
+
if (!isSerializedData(data)) {
|
|
27
|
+
this.programId = data.programId;
|
|
28
|
+
this.version = data.version;
|
|
29
|
+
this.offerer = data.offerer;
|
|
30
|
+
this.claimer = data.claimer;
|
|
31
|
+
this.token = data.token;
|
|
32
|
+
this.amount = data.amount;
|
|
33
|
+
this.paymentHash = data.paymentHash;
|
|
34
|
+
this.sequence = data.sequence;
|
|
35
|
+
this.expiry = data.expiry;
|
|
36
|
+
this.nonce = data.nonce;
|
|
37
|
+
this.confirmations = data.confirmations;
|
|
38
|
+
this.payOut = data.payOut;
|
|
39
|
+
this.kind = data.kind;
|
|
40
|
+
this.payIn = data.payIn;
|
|
41
|
+
this.claimerAta = data.claimerAta;
|
|
42
|
+
this.offererAta = data.offererAta;
|
|
43
|
+
this.securityDeposit = data.securityDeposit;
|
|
44
|
+
this.claimerBounty = data.claimerBounty;
|
|
45
|
+
this.txoHash = data.txoHash;
|
|
46
|
+
this.offererInitializer = data.offererInitializer;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.programId = new web3_js_1.PublicKey(data.programId ?? "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM");
|
|
50
|
+
this.version = data.version ?? "v1";
|
|
51
|
+
this.offerer = new web3_js_1.PublicKey(data.offerer);
|
|
52
|
+
this.claimer = new web3_js_1.PublicKey(data.claimer);
|
|
53
|
+
this.token = new web3_js_1.PublicKey(data.token);
|
|
54
|
+
this.amount = new BN(data.amount);
|
|
55
|
+
this.paymentHash = data.paymentHash;
|
|
56
|
+
this.sequence = new BN(data.sequence);
|
|
57
|
+
this.expiry = new BN(data.expiry);
|
|
58
|
+
this.nonce = new BN(data.nonce);
|
|
59
|
+
this.confirmations = data.confirmations;
|
|
60
|
+
this.payOut = data.payOut;
|
|
61
|
+
this.kind = data.kind;
|
|
62
|
+
this.payIn = data.payIn;
|
|
63
|
+
this.claimerAta = data.claimerAta == null ? undefined : new web3_js_1.PublicKey(data.claimerAta);
|
|
64
|
+
this.offererAta = data.offererAta == null ? undefined : new web3_js_1.PublicKey(data.offererAta);
|
|
65
|
+
this.securityDeposit = new BN(data.securityDeposit);
|
|
66
|
+
this.claimerBounty = new BN(data.claimerBounty);
|
|
67
|
+
this.txoHash = data.txoHash;
|
|
68
|
+
this.offererInitializer = data.offererInitializer;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @inheritDoc
|
|
73
|
+
*/
|
|
74
|
+
getOfferer() {
|
|
75
|
+
return this.offerer.toBase58();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* @inheritDoc
|
|
79
|
+
*/
|
|
80
|
+
setOfferer(newOfferer) {
|
|
81
|
+
this.offerer = new web3_js_1.PublicKey(newOfferer);
|
|
82
|
+
this.offererAta = (0, spl_token_1.getAssociatedTokenAddressSync)(this.token, this.offerer);
|
|
83
|
+
this.payIn = true;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @inheritDoc
|
|
87
|
+
*/
|
|
88
|
+
getClaimer() {
|
|
89
|
+
return this.claimer.toBase58();
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* @inheritDoc
|
|
93
|
+
*/
|
|
94
|
+
setClaimer(newClaimer) {
|
|
95
|
+
this.claimer = new web3_js_1.PublicKey(newClaimer);
|
|
96
|
+
this.payIn = false;
|
|
97
|
+
this.payOut = true;
|
|
98
|
+
this.claimerAta = (0, spl_token_1.getAssociatedTokenAddressSync)(this.token, this.claimer);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @inheritDoc
|
|
102
|
+
*/
|
|
103
|
+
serialize() {
|
|
104
|
+
return {
|
|
105
|
+
type: "sol",
|
|
106
|
+
programId: this.programId?.toBase58(),
|
|
107
|
+
version: this.version,
|
|
108
|
+
offerer: this.offerer?.toBase58(),
|
|
109
|
+
claimer: this.claimer?.toBase58(),
|
|
110
|
+
token: this.token?.toBase58(),
|
|
111
|
+
amount: this.amount?.toString(10),
|
|
112
|
+
paymentHash: this.paymentHash,
|
|
113
|
+
sequence: this.sequence?.toString(10),
|
|
114
|
+
expiry: this.expiry?.toString(10),
|
|
115
|
+
nonce: this.nonce?.toString(10),
|
|
116
|
+
confirmations: this.confirmations,
|
|
117
|
+
payOut: this.payOut,
|
|
118
|
+
kind: this.kind,
|
|
119
|
+
payIn: this.payIn,
|
|
120
|
+
offererAta: this.offererAta?.toBase58(),
|
|
121
|
+
claimerAta: this.claimerAta?.toBase58(),
|
|
122
|
+
securityDeposit: this.securityDeposit?.toString(10),
|
|
123
|
+
claimerBounty: this.claimerBounty?.toString(10),
|
|
124
|
+
txoHash: this.txoHash,
|
|
125
|
+
offererInitializer: this.offererInitializer
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* @inheritDoc
|
|
130
|
+
*/
|
|
131
|
+
getAmount() {
|
|
132
|
+
return (0, Utils_1.toBigInt)(this.amount);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* @inheritDoc
|
|
136
|
+
*/
|
|
137
|
+
getToken() {
|
|
138
|
+
return this.token.toString();
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* @inheritDoc
|
|
142
|
+
*/
|
|
143
|
+
isToken(token) {
|
|
144
|
+
return this.token.equals(new web3_js_1.PublicKey(token));
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* @inheritDoc
|
|
148
|
+
*/
|
|
149
|
+
getType() {
|
|
150
|
+
return SolanaSwapData.kindToType(this.kind);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* @inheritDoc
|
|
154
|
+
*/
|
|
155
|
+
getExpiry() {
|
|
156
|
+
if (this.expiry.lt(EXPIRY_BLOCKHEIGHT_THRESHOLD))
|
|
157
|
+
throw new Error("Expiry expressed as bitcoin blockheight!");
|
|
158
|
+
return (0, Utils_1.toBigInt)(this.expiry);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* @inheritDoc
|
|
162
|
+
*/
|
|
163
|
+
getConfirmationsHint() {
|
|
164
|
+
return this.confirmations;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @inheritDoc
|
|
168
|
+
*/
|
|
169
|
+
getNonceHint() {
|
|
170
|
+
return (0, Utils_1.toBigInt)(this.nonce);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* @inheritDoc
|
|
174
|
+
*/
|
|
175
|
+
isPayIn() {
|
|
176
|
+
return this.payIn;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* @inheritDoc
|
|
180
|
+
*/
|
|
181
|
+
isPayOut() {
|
|
182
|
+
return this.payOut;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* @inheritDoc
|
|
186
|
+
*/
|
|
187
|
+
isTrackingReputation() {
|
|
188
|
+
return !this.payOut;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* @inheritDoc
|
|
192
|
+
*/
|
|
193
|
+
getClaimHash() {
|
|
194
|
+
return (0, Utils_1.toClaimHash)(this.paymentHash, (0, Utils_1.toBigInt)(this.nonce), this.confirmations);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* @inheritDoc
|
|
198
|
+
*/
|
|
199
|
+
getEscrowHash() {
|
|
200
|
+
return (0, Utils_1.toEscrowHash)(this.paymentHash, this.sequence);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* @inheritDoc
|
|
204
|
+
*/
|
|
205
|
+
getSequence() {
|
|
206
|
+
return (0, Utils_1.toBigInt)(this.sequence);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* @inheritDoc
|
|
210
|
+
*/
|
|
211
|
+
getTxoHashHint() {
|
|
212
|
+
if (this.txoHash === "0000000000000000000000000000000000000000000000000000000000000000")
|
|
213
|
+
return null; //Txo hash opt-out flag
|
|
214
|
+
return this.txoHash ?? null;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* @inheritDoc
|
|
218
|
+
*/
|
|
219
|
+
getHTLCHashHint() {
|
|
220
|
+
if (this.getType() === base_1.ChainSwapType.HTLC)
|
|
221
|
+
return this.paymentHash;
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* @inheritDoc
|
|
226
|
+
*/
|
|
227
|
+
getExtraData() {
|
|
228
|
+
return this.txoHash ?? null;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* @inheritDoc
|
|
232
|
+
*/
|
|
233
|
+
setExtraData(txoHash) {
|
|
234
|
+
this.txoHash = txoHash;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* @inheritDoc
|
|
238
|
+
*/
|
|
239
|
+
getSecurityDeposit() {
|
|
240
|
+
return (0, Utils_1.toBigInt)(this.securityDeposit);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* @inheritDoc
|
|
244
|
+
*/
|
|
245
|
+
getClaimerBounty() {
|
|
246
|
+
return (0, Utils_1.toBigInt)(this.claimerBounty);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* @inheritDoc
|
|
250
|
+
*/
|
|
251
|
+
getTotalDeposit() {
|
|
252
|
+
return (0, Utils_1.toBigInt)(this.claimerBounty.lt(this.securityDeposit) ? this.securityDeposit : this.claimerBounty);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Serializes the swap data into the Solana program `SwapData` struct representation.
|
|
256
|
+
*/
|
|
257
|
+
toSwapDataStruct() {
|
|
258
|
+
return {
|
|
259
|
+
kind: SwapTypeEnum_1.SwapTypeEnum.fromNumber(this.kind),
|
|
260
|
+
confirmations: this.confirmations,
|
|
261
|
+
nonce: this.nonce,
|
|
262
|
+
hash: [...buffer_1.Buffer.from(this.paymentHash, "hex")],
|
|
263
|
+
payIn: this.payIn,
|
|
264
|
+
payOut: this.payOut,
|
|
265
|
+
amount: this.amount,
|
|
266
|
+
expiry: this.expiry,
|
|
267
|
+
sequence: this.sequence
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Checks whether the provided escrow account matches this swap data.
|
|
272
|
+
*
|
|
273
|
+
* @param account Escrow account data fetched from chain
|
|
274
|
+
*/
|
|
275
|
+
correctPDA(account) {
|
|
276
|
+
return SwapTypeEnum_1.SwapTypeEnum.toNumber(account.data.kind) === this.kind &&
|
|
277
|
+
account.data.confirmations === this.confirmations &&
|
|
278
|
+
this.nonce.eq(account.data.nonce) &&
|
|
279
|
+
buffer_1.Buffer.from(account.data.hash).toString("hex") === this.paymentHash &&
|
|
280
|
+
account.data.payIn === this.payIn &&
|
|
281
|
+
account.data.payOut === this.payOut &&
|
|
282
|
+
this.amount.eq(account.data.amount) &&
|
|
283
|
+
this.expiry.eq(account.data.expiry) &&
|
|
284
|
+
this.sequence.eq(account.data.sequence) &&
|
|
285
|
+
account.offerer.equals(this.offerer) &&
|
|
286
|
+
(this.offererAta == null || account.offererAta.equals(this.offererAta)) &&
|
|
287
|
+
account.claimer.equals(this.claimer) &&
|
|
288
|
+
(this.claimerAta == null || account.claimerAta.equals(this.claimerAta)) &&
|
|
289
|
+
account.mint.equals(this.token) &&
|
|
290
|
+
this.claimerBounty.eq(account.claimerBounty) &&
|
|
291
|
+
this.securityDeposit.eq(account.securityDeposit) &&
|
|
292
|
+
(this.offererInitializer == null || account.offererInitializer === this.offererInitializer);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* @inheritDoc
|
|
296
|
+
*/
|
|
297
|
+
equals(other) {
|
|
298
|
+
if (this.claimerAta == null && other.claimerAta != null)
|
|
299
|
+
return false;
|
|
300
|
+
if (this.claimerAta != null && other.claimerAta == null)
|
|
301
|
+
return false;
|
|
302
|
+
if (this.claimerAta != null && other.claimerAta != null) {
|
|
303
|
+
if (!this.claimerAta.equals(other.claimerAta))
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
if (this.offererAta == null && other.offererAta != null)
|
|
307
|
+
return false;
|
|
308
|
+
if (this.offererAta != null && other.offererAta == null)
|
|
309
|
+
return false;
|
|
310
|
+
if (this.offererAta != null && other.offererAta != null) {
|
|
311
|
+
if (!this.offererAta.equals(other.offererAta))
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
if (this.offererInitializer == null && other.offererInitializer != null)
|
|
315
|
+
return false;
|
|
316
|
+
if (this.offererInitializer != null && other.offererInitializer == null)
|
|
317
|
+
return false;
|
|
318
|
+
if (this.offererInitializer != null && other.offererInitializer != null) {
|
|
319
|
+
if (this.offererInitializer !== other.offererInitializer)
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
return other.kind === this.kind &&
|
|
323
|
+
other.confirmations === this.confirmations &&
|
|
324
|
+
this.nonce.eq(other.nonce) &&
|
|
325
|
+
other.paymentHash === this.paymentHash &&
|
|
326
|
+
this.sequence.eq(other.sequence) &&
|
|
327
|
+
other.payIn === this.payIn &&
|
|
328
|
+
other.payOut === this.payOut &&
|
|
329
|
+
other.offerer.equals(this.offerer) &&
|
|
330
|
+
other.claimer.equals(this.claimer) &&
|
|
331
|
+
other.expiry.eq(this.expiry) &&
|
|
332
|
+
other.amount.eq(this.amount) &&
|
|
333
|
+
other.securityDeposit.eq(this.securityDeposit) &&
|
|
334
|
+
other.claimerBounty.eq(this.claimerBounty) &&
|
|
335
|
+
other.token.equals(this.token);
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Converts initialize instruction data into {@link SolanaSwapData}.
|
|
339
|
+
*
|
|
340
|
+
* @param programId
|
|
341
|
+
* @param version
|
|
342
|
+
* @param initIx Decoded initialize instruction
|
|
343
|
+
* @param txoHash Parsed txo hash hint from initialize event
|
|
344
|
+
* @returns Converted and parsed swap data
|
|
345
|
+
*/
|
|
346
|
+
static fromInstruction(programId, version, initIx, txoHash) {
|
|
347
|
+
const paymentHash = buffer_1.Buffer.from(initIx.data.swapData.hash);
|
|
348
|
+
let securityDeposit = new BN(0);
|
|
349
|
+
let claimerBounty = new BN(0);
|
|
350
|
+
let payIn = true;
|
|
351
|
+
if (initIx.name === "offererInitialize") {
|
|
352
|
+
payIn = false;
|
|
353
|
+
securityDeposit = initIx.data.securityDeposit;
|
|
354
|
+
claimerBounty = initIx.data.claimerBounty;
|
|
355
|
+
}
|
|
356
|
+
if (version !== "v1" && initIx.name === "offererInitializePayIn") {
|
|
357
|
+
payIn = true;
|
|
358
|
+
securityDeposit = initIx.data.securityDeposit;
|
|
359
|
+
claimerBounty = initIx.data.claimerBounty;
|
|
360
|
+
}
|
|
361
|
+
return new SolanaSwapData({
|
|
362
|
+
programId,
|
|
363
|
+
version,
|
|
364
|
+
offerer: initIx.accounts.offerer,
|
|
365
|
+
claimer: initIx.accounts.claimer,
|
|
366
|
+
token: initIx.accounts.mint,
|
|
367
|
+
amount: initIx.data.swapData.amount,
|
|
368
|
+
paymentHash: paymentHash.toString("hex"),
|
|
369
|
+
sequence: initIx.data.swapData.sequence,
|
|
370
|
+
expiry: initIx.data.swapData.expiry,
|
|
371
|
+
nonce: initIx.data.swapData.nonce,
|
|
372
|
+
confirmations: initIx.data.swapData.confirmations,
|
|
373
|
+
payOut: initIx.data.swapData.payOut,
|
|
374
|
+
kind: SwapTypeEnum_1.SwapTypeEnum.toNumber(initIx.data.swapData.kind),
|
|
375
|
+
payIn,
|
|
376
|
+
offererAta: initIx.name === "offererInitializePayIn" ? initIx.accounts.offererAta : web3_js_1.PublicKey.default,
|
|
377
|
+
claimerAta: initIx.data.swapData.payOut ? initIx.accounts.claimerAta : web3_js_1.PublicKey.default,
|
|
378
|
+
securityDeposit,
|
|
379
|
+
claimerBounty,
|
|
380
|
+
txoHash,
|
|
381
|
+
offererInitializer: initIx.accounts.initializer != null ? initIx.accounts.initializer.equals(initIx.accounts.offerer) : undefined
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Deserializes swap data from an on-chain escrow account state.
|
|
386
|
+
*
|
|
387
|
+
* @param programId
|
|
388
|
+
* @param version
|
|
389
|
+
* @param account Escrow account state as returned by Anchor
|
|
390
|
+
*/
|
|
391
|
+
static fromEscrowState(programId, version, account) {
|
|
392
|
+
const data = account.data;
|
|
393
|
+
return new SolanaSwapData({
|
|
394
|
+
programId,
|
|
395
|
+
version,
|
|
396
|
+
offerer: account.offerer,
|
|
397
|
+
claimer: account.claimer,
|
|
398
|
+
token: account.mint,
|
|
399
|
+
amount: data.amount,
|
|
400
|
+
paymentHash: buffer_1.Buffer.from(data.hash).toString("hex"),
|
|
401
|
+
sequence: data.sequence,
|
|
402
|
+
expiry: data.expiry,
|
|
403
|
+
nonce: data.nonce,
|
|
404
|
+
confirmations: data.confirmations,
|
|
405
|
+
payOut: data.payOut,
|
|
406
|
+
kind: SwapTypeEnum_1.SwapTypeEnum.toNumber(data.kind),
|
|
407
|
+
payIn: data.payIn,
|
|
408
|
+
offererAta: account.offererAta,
|
|
409
|
+
claimerAta: account.claimerAta,
|
|
410
|
+
securityDeposit: account.securityDeposit,
|
|
411
|
+
claimerBounty: account.claimerBounty,
|
|
412
|
+
offererInitializer: account.offererInitializer ?? undefined
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Converts abstract swap type to Solana program kind discriminator.
|
|
417
|
+
*
|
|
418
|
+
* @param type Chain-agnostic swap type
|
|
419
|
+
*/
|
|
420
|
+
static typeToKind(type) {
|
|
421
|
+
switch (type) {
|
|
422
|
+
case base_1.ChainSwapType.HTLC:
|
|
423
|
+
return 0;
|
|
424
|
+
case base_1.ChainSwapType.CHAIN:
|
|
425
|
+
return 1;
|
|
426
|
+
case base_1.ChainSwapType.CHAIN_NONCED:
|
|
427
|
+
return 2;
|
|
428
|
+
case base_1.ChainSwapType.CHAIN_TXID:
|
|
429
|
+
return 3;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Converts Solana program kind discriminator to abstract swap type.
|
|
434
|
+
*
|
|
435
|
+
* @param value Solana program swap kind value
|
|
436
|
+
*/
|
|
437
|
+
static kindToType(value) {
|
|
438
|
+
switch (value) {
|
|
439
|
+
case 0:
|
|
440
|
+
return base_1.ChainSwapType.HTLC;
|
|
441
|
+
case 1:
|
|
442
|
+
return base_1.ChainSwapType.CHAIN;
|
|
443
|
+
case 2:
|
|
444
|
+
return base_1.ChainSwapType.CHAIN_NONCED;
|
|
445
|
+
case 3:
|
|
446
|
+
return base_1.ChainSwapType.CHAIN_TXID;
|
|
447
|
+
}
|
|
448
|
+
throw new Error("Unknown swap kind type!");
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* @inheritDoc
|
|
452
|
+
*/
|
|
453
|
+
isClaimer(address) {
|
|
454
|
+
const _address = new web3_js_1.PublicKey(address);
|
|
455
|
+
if (this.isPayOut()) {
|
|
456
|
+
//Also check that swapData's ATA is correct
|
|
457
|
+
const ourAta = (0, spl_token_1.getAssociatedTokenAddressSync)(this.token, _address);
|
|
458
|
+
if (this.claimerAta == null || !this.claimerAta.equals(ourAta))
|
|
459
|
+
return false;
|
|
460
|
+
}
|
|
461
|
+
return this.claimer.equals(new web3_js_1.PublicKey(address));
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* @inheritDoc
|
|
465
|
+
*/
|
|
466
|
+
isOfferer(address) {
|
|
467
|
+
return this.offerer.equals(new web3_js_1.PublicKey(address));
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* @inheritDoc
|
|
471
|
+
*/
|
|
472
|
+
getDepositToken() {
|
|
473
|
+
return SolanaTokens_1.SolanaTokens.WSOL_ADDRESS.toString();
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* @inheritDoc
|
|
477
|
+
*/
|
|
478
|
+
isDepositToken(token) {
|
|
479
|
+
return SolanaTokens_1.SolanaTokens.WSOL_ADDRESS.equals(new web3_js_1.PublicKey(token));
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* @inheritDoc
|
|
483
|
+
*/
|
|
484
|
+
getEscrowStruct() {
|
|
485
|
+
return {
|
|
486
|
+
accounts: {
|
|
487
|
+
offerer: this.offerer.toString(),
|
|
488
|
+
claimer: this.claimer.toString(),
|
|
489
|
+
claimerAta: this.claimerAta == null || this.claimerAta.equals(web3_js_1.PublicKey.default) ? null : this.claimerAta.toString(),
|
|
490
|
+
offererAta: this.offererAta == null || this.offererAta.equals(web3_js_1.PublicKey.default) ? null : this.offererAta.toString(),
|
|
491
|
+
claimerUserData: SolanaSwapProgram_1.SolanaSwapProgram._SwapUserVault(this.programId, this.claimer, this.token).toString(),
|
|
492
|
+
offererUserData: SolanaSwapProgram_1.SolanaSwapProgram._SwapUserVault(this.programId, this.offerer, this.token).toString(),
|
|
493
|
+
initializer: this.offererInitializer != null
|
|
494
|
+
? (this.offererInitializer ? this.offerer.toString() : this.claimer.toString())
|
|
495
|
+
: (this.isPayIn() ? this.offerer.toString() : this.claimer.toString()),
|
|
496
|
+
escrowState: SolanaSwapProgram_1.SolanaSwapProgram._SwapEscrowState(this.programId, buffer_1.Buffer.from(this.paymentHash, "hex")).toString(),
|
|
497
|
+
mint: this.token.toString(),
|
|
498
|
+
vault: SolanaSwapProgram_1.SolanaSwapProgram._SwapVault(this.programId, this.token).toString(),
|
|
499
|
+
vaultAuthority: SolanaSwapProgram_1.SolanaSwapProgram._SwapVaultAuthority(this.programId).toString(),
|
|
500
|
+
systemProgram: web3_js_1.SystemProgram.programId.toString(),
|
|
501
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID.toString(),
|
|
502
|
+
ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY.toString(),
|
|
503
|
+
},
|
|
504
|
+
data: {
|
|
505
|
+
kind: SwapTypeEnum_1.SwapTypeEnum.fromNumber(this.kind),
|
|
506
|
+
confirmations: this.confirmations,
|
|
507
|
+
nonce: this.nonce.toString(10),
|
|
508
|
+
hash: [...buffer_1.Buffer.from(this.paymentHash, "hex")],
|
|
509
|
+
payIn: this.payIn,
|
|
510
|
+
payOut: this.payOut,
|
|
511
|
+
amount: this.amount.toString(10),
|
|
512
|
+
expiry: this.expiry.toString(10),
|
|
513
|
+
sequence: this.sequence.toString(10)
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
exports.SolanaSwapData = SolanaSwapData;
|
|
519
|
+
class SolanaSwapDataV1 extends SolanaSwapData {
|
|
520
|
+
constructor(data) {
|
|
521
|
+
super(data);
|
|
522
|
+
if (this.version !== "v1")
|
|
523
|
+
throw new Error(`Invalid swap data version, expected v1, got ${this.version}`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
exports.SolanaSwapDataV1 = SolanaSwapDataV1;
|
|
527
|
+
class SolanaSwapDataV2 extends SolanaSwapData {
|
|
528
|
+
constructor(data) {
|
|
529
|
+
super(data);
|
|
530
|
+
if (this.version !== "v2")
|
|
531
|
+
throw new Error(`Invalid swap data version, expected v2, got ${this.version}`);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
exports.SolanaSwapDataV2 = SolanaSwapDataV2;
|
|
535
|
+
base_1.SwapData.deserializers["sol"] = SolanaSwapData;
|