@atomiqlabs/chain-solana 7.3.8 → 8.0.0-beta.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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/solana/SolanaChains.d.ts +14 -0
- package/dist/solana/SolanaChains.js +18 -0
- package/dist/solana/SolanaInitializer.d.ts +18 -0
- package/dist/solana/SolanaInitializer.js +59 -0
- package/dist/solana/base/SolanaAction.js +25 -38
- package/dist/solana/base/modules/SolanaBlocks.js +18 -29
- package/dist/solana/base/modules/SolanaEvents.js +15 -26
- package/dist/solana/base/modules/SolanaFees.d.ts +4 -5
- package/dist/solana/base/modules/SolanaFees.js +168 -182
- package/dist/solana/base/modules/SolanaSlots.js +16 -29
- package/dist/solana/base/modules/SolanaTokens.d.ts +6 -7
- package/dist/solana/base/modules/SolanaTokens.js +63 -84
- package/dist/solana/base/modules/SolanaTransactions.d.ts +2 -2
- package/dist/solana/base/modules/SolanaTransactions.js +143 -165
- package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +2 -3
- package/dist/solana/btcrelay/SolanaBtcRelay.js +185 -221
- package/dist/solana/events/SolanaChainEvents.js +113 -138
- package/dist/solana/events/SolanaChainEventsBrowser.js +56 -65
- package/dist/solana/program/modules/SolanaProgramEvents.js +13 -24
- package/dist/solana/swaps/SolanaSwapData.d.ts +15 -11
- package/dist/solana/swaps/SolanaSwapData.js +29 -13
- package/dist/solana/swaps/SolanaSwapProgram.d.ts +40 -41
- package/dist/solana/swaps/SolanaSwapProgram.js +212 -221
- package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +2 -3
- package/dist/solana/swaps/modules/SolanaDataAccount.js +117 -141
- package/dist/solana/swaps/modules/SolanaLpVault.d.ts +4 -5
- package/dist/solana/swaps/modules/SolanaLpVault.js +87 -110
- package/dist/solana/swaps/modules/SwapClaim.d.ts +4 -4
- package/dist/solana/swaps/modules/SwapClaim.js +122 -134
- package/dist/solana/swaps/modules/SwapInit.d.ts +2 -3
- package/dist/solana/swaps/modules/SwapInit.js +254 -285
- package/dist/solana/swaps/modules/SwapRefund.d.ts +2 -3
- package/dist/solana/swaps/modules/SwapRefund.js +103 -113
- package/dist/utils/Utils.d.ts +10 -0
- package/dist/utils/Utils.js +62 -35
- package/package.json +9 -6
- package/src/index.ts +1 -0
- package/src/solana/SolanaChains.ts +16 -0
- package/src/solana/SolanaInitializer.ts +98 -0
- package/src/solana/base/SolanaBase.ts +1 -2
- package/src/solana/base/modules/SolanaEvents.ts +0 -1
- package/src/solana/base/modules/SolanaFees.ts +35 -31
- package/src/solana/base/modules/SolanaTokens.ts +26 -27
- package/src/solana/base/modules/SolanaTransactions.ts +33 -28
- package/src/solana/btcrelay/SolanaBtcRelay.ts +6 -10
- package/src/solana/events/SolanaChainEventsBrowser.ts +14 -10
- package/src/solana/program/modules/SolanaProgramEvents.ts +1 -1
- package/src/solana/swaps/SolanaSwapData.ts +38 -19
- package/src/solana/swaps/SolanaSwapProgram.ts +133 -105
- package/src/solana/swaps/modules/SolanaDataAccount.ts +4 -6
- package/src/solana/swaps/modules/SolanaLpVault.ts +18 -18
- package/src/solana/swaps/modules/SwapClaim.ts +14 -17
- package/src/solana/swaps/modules/SwapInit.ts +35 -38
- package/src/solana/swaps/modules/SwapRefund.ts +20 -23
- package/src/utils/Utils.ts +35 -0
|
@@ -1,17 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.SolanaSwapProgram = void 0;
|
|
13
4
|
const SolanaSwapData_1 = require("./SolanaSwapData");
|
|
14
|
-
const BN = require("bn.js");
|
|
15
5
|
const web3_js_1 = require("@solana/web3.js");
|
|
16
6
|
const createHash = require("create-hash");
|
|
17
7
|
const programIdl = require("./programIdl.json");
|
|
@@ -27,6 +17,9 @@ const SolanaLpVault_1 = require("./modules/SolanaLpVault");
|
|
|
27
17
|
const buffer_1 = require("buffer");
|
|
28
18
|
const SolanaSigner_1 = require("../wallet/SolanaSigner");
|
|
29
19
|
const SolanaKeypairWallet_1 = require("../wallet/SolanaKeypairWallet");
|
|
20
|
+
const Utils_1 = require("../../utils/Utils");
|
|
21
|
+
const SolanaTokens_1 = require("../base/modules/SolanaTokens");
|
|
22
|
+
const BN = require("bn.js");
|
|
30
23
|
function toPublicKeyOrNull(str) {
|
|
31
24
|
return str == null ? null : new web3_js_1.PublicKey(str);
|
|
32
25
|
}
|
|
@@ -57,10 +50,8 @@ class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
57
50
|
this.DataAccount = new SolanaDataAccount_1.SolanaDataAccount(this, storage);
|
|
58
51
|
this.LpVault = new SolanaLpVault_1.SolanaLpVault(this);
|
|
59
52
|
}
|
|
60
|
-
start() {
|
|
61
|
-
|
|
62
|
-
yield this.DataAccount.init();
|
|
63
|
-
});
|
|
53
|
+
async start() {
|
|
54
|
+
await this.DataAccount.init();
|
|
64
55
|
}
|
|
65
56
|
getClaimableDeposits(signer) {
|
|
66
57
|
return this.DataAccount.getDataAccountsInfo(new web3_js_1.PublicKey(signer));
|
|
@@ -108,26 +99,24 @@ class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
108
99
|
* @param signer
|
|
109
100
|
* @param data
|
|
110
101
|
*/
|
|
111
|
-
isClaimable(signer, data) {
|
|
102
|
+
async isClaimable(signer, data) {
|
|
112
103
|
if (!data.isClaimer(signer))
|
|
113
|
-
return
|
|
114
|
-
if (this.isExpired(signer, data))
|
|
115
|
-
return
|
|
116
|
-
return this.isCommited(data);
|
|
104
|
+
return false;
|
|
105
|
+
if (await this.isExpired(signer, data))
|
|
106
|
+
return false;
|
|
107
|
+
return await this.isCommited(data);
|
|
117
108
|
}
|
|
118
109
|
/**
|
|
119
110
|
* Checks whether a swap is commited, i.e. the swap still exists on-chain and was not claimed nor refunded
|
|
120
111
|
*
|
|
121
112
|
* @param swapData
|
|
122
113
|
*/
|
|
123
|
-
isCommited(swapData) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
return swapData.correctPDA(account);
|
|
130
|
-
});
|
|
114
|
+
async isCommited(swapData) {
|
|
115
|
+
const paymentHash = buffer_1.Buffer.from(swapData.paymentHash, "hex");
|
|
116
|
+
const account = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHash));
|
|
117
|
+
if (account == null)
|
|
118
|
+
return false;
|
|
119
|
+
return swapData.correctPDA(account);
|
|
131
120
|
}
|
|
132
121
|
/**
|
|
133
122
|
* Checks whether the swap is expired, takes into consideration possible on-chain time skew, therefore for claimer
|
|
@@ -142,7 +131,7 @@ class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
142
131
|
currentTimestamp = currentTimestamp.sub(new BN(this.refundGracePeriod));
|
|
143
132
|
if (data.isOfferer(signer))
|
|
144
133
|
currentTimestamp = currentTimestamp.add(new BN(this.claimGracePeriod));
|
|
145
|
-
return data.expiry.lt(currentTimestamp);
|
|
134
|
+
return Promise.resolve(data.expiry.lt(currentTimestamp));
|
|
146
135
|
}
|
|
147
136
|
/**
|
|
148
137
|
* Checks if the swap is refundable by us, checks if we are offerer, if the swap is already expired & if the swap
|
|
@@ -151,27 +140,36 @@ class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
151
140
|
* @param signer
|
|
152
141
|
* @param data
|
|
153
142
|
*/
|
|
154
|
-
isRequestRefundable(signer, data) {
|
|
143
|
+
async isRequestRefundable(signer, data) {
|
|
155
144
|
//Swap can only be refunded by the offerer
|
|
156
145
|
if (!data.isOfferer(signer))
|
|
157
|
-
return
|
|
158
|
-
if (!this.isExpired(signer, data))
|
|
159
|
-
return
|
|
160
|
-
return this.isCommited(data);
|
|
146
|
+
return false;
|
|
147
|
+
if (!(await this.isExpired(signer, data)))
|
|
148
|
+
return false;
|
|
149
|
+
return await this.isCommited(data);
|
|
161
150
|
}
|
|
162
151
|
/**
|
|
163
152
|
* Get the swap payment hash to be used for an on-chain swap, this just uses a sha256 hash of the values
|
|
164
153
|
*
|
|
165
154
|
* @param outputScript output script required to claim the swap
|
|
166
155
|
* @param amount sats sent required to claim the swap
|
|
156
|
+
* @param confirmations
|
|
167
157
|
* @param nonce swap nonce uniquely identifying the transaction to prevent replay attacks
|
|
168
158
|
*/
|
|
169
|
-
getHashForOnchain(outputScript, amount, nonce) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
159
|
+
getHashForOnchain(outputScript, amount, confirmations, nonce) {
|
|
160
|
+
nonce ?? (nonce = 0n);
|
|
161
|
+
const paymentHash = createHash("sha256").update(buffer_1.Buffer.concat([
|
|
162
|
+
base_1.BigIntBufferUtils.toBuffer(nonce, "le", 8),
|
|
163
|
+
base_1.BigIntBufferUtils.toBuffer(amount, "le", 8),
|
|
173
164
|
outputScript
|
|
174
|
-
])).digest();
|
|
165
|
+
])).digest().toString("hex");
|
|
166
|
+
return buffer_1.Buffer.from((0, Utils_1.toClaimHash)(paymentHash, nonce, confirmations), "hex");
|
|
167
|
+
}
|
|
168
|
+
getHashForHtlc(swapHash) {
|
|
169
|
+
return buffer_1.Buffer.from((0, Utils_1.toClaimHash)(swapHash.toString("hex"), 0n, 0), "hex");
|
|
170
|
+
}
|
|
171
|
+
getHashForTxId(txId, confirmations) {
|
|
172
|
+
return buffer_1.Buffer.from((0, Utils_1.toClaimHash)(buffer_1.Buffer.from(txId, "hex").reverse().toString("hex"), 0n, confirmations), "hex");
|
|
175
173
|
}
|
|
176
174
|
////////////////////////////////////////////
|
|
177
175
|
//// Swap data getters
|
|
@@ -182,115 +180,119 @@ class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
182
180
|
* @param signer
|
|
183
181
|
* @param data
|
|
184
182
|
*/
|
|
185
|
-
getCommitStatus(signer, data) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
return base_1.SwapCommitStatus.EXPIRED;
|
|
197
|
-
return base_1.SwapCommitStatus.NOT_COMMITED;
|
|
183
|
+
async getCommitStatus(signer, data) {
|
|
184
|
+
const escrowStateKey = this.SwapEscrowState(buffer_1.Buffer.from(data.paymentHash, "hex"));
|
|
185
|
+
const [escrowState, isExpired] = await Promise.all([
|
|
186
|
+
this.program.account.escrowState.fetchNullable(escrowStateKey),
|
|
187
|
+
this.isExpired(signer, data)
|
|
188
|
+
]);
|
|
189
|
+
if (escrowState != null) {
|
|
190
|
+
if (data.correctPDA(escrowState)) {
|
|
191
|
+
if (data.isOfferer(signer) && isExpired)
|
|
192
|
+
return base_1.SwapCommitStatus.REFUNDABLE;
|
|
193
|
+
return base_1.SwapCommitStatus.COMMITED;
|
|
198
194
|
}
|
|
199
|
-
|
|
200
|
-
const status = yield this.Events.findInEvents(escrowStateKey, (event) => __awaiter(this, void 0, void 0, function* () {
|
|
201
|
-
if (event.name === "ClaimEvent") {
|
|
202
|
-
if (!event.data.sequence.eq(data.sequence))
|
|
203
|
-
return null;
|
|
204
|
-
return base_1.SwapCommitStatus.PAID;
|
|
205
|
-
}
|
|
206
|
-
if (event.name === "RefundEvent") {
|
|
207
|
-
if (!event.data.sequence.eq(data.sequence))
|
|
208
|
-
return null;
|
|
209
|
-
if (this.isExpired(signer, data))
|
|
210
|
-
return base_1.SwapCommitStatus.EXPIRED;
|
|
211
|
-
return base_1.SwapCommitStatus.NOT_COMMITED;
|
|
212
|
-
}
|
|
213
|
-
}));
|
|
214
|
-
if (status != null)
|
|
215
|
-
return status;
|
|
216
|
-
if (this.isExpired(signer, data)) {
|
|
195
|
+
if (data.isOfferer(signer) && isExpired)
|
|
217
196
|
return base_1.SwapCommitStatus.EXPIRED;
|
|
218
|
-
}
|
|
219
197
|
return base_1.SwapCommitStatus.NOT_COMMITED;
|
|
198
|
+
}
|
|
199
|
+
//Check if paid or what
|
|
200
|
+
const status = await this.Events.findInEvents(escrowStateKey, async (event) => {
|
|
201
|
+
if (event.name === "ClaimEvent") {
|
|
202
|
+
if (!event.data.sequence.eq(data.sequence))
|
|
203
|
+
return null;
|
|
204
|
+
return base_1.SwapCommitStatus.PAID;
|
|
205
|
+
}
|
|
206
|
+
if (event.name === "RefundEvent") {
|
|
207
|
+
if (!event.data.sequence.eq(data.sequence))
|
|
208
|
+
return null;
|
|
209
|
+
if (isExpired)
|
|
210
|
+
return base_1.SwapCommitStatus.EXPIRED;
|
|
211
|
+
return base_1.SwapCommitStatus.NOT_COMMITED;
|
|
212
|
+
}
|
|
220
213
|
});
|
|
214
|
+
if (status != null)
|
|
215
|
+
return status;
|
|
216
|
+
if (isExpired) {
|
|
217
|
+
return base_1.SwapCommitStatus.EXPIRED;
|
|
218
|
+
}
|
|
219
|
+
return base_1.SwapCommitStatus.NOT_COMMITED;
|
|
221
220
|
}
|
|
222
221
|
/**
|
|
223
222
|
* Checks the status of the specific payment hash
|
|
224
223
|
*
|
|
225
|
-
* @param
|
|
224
|
+
* @param claimHash
|
|
226
225
|
*/
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
});
|
|
241
|
-
const escrowState = yield this.program.account.escrowState.fetchNullable(escrowStateKey);
|
|
242
|
-
abortController.signal.throwIfAborted();
|
|
243
|
-
if (escrowState != null) {
|
|
244
|
-
abortController.abort();
|
|
245
|
-
return base_1.SwapCommitStatus.COMMITED;
|
|
246
|
-
}
|
|
247
|
-
//Check if paid or what
|
|
248
|
-
const eventsStatus = yield eventsPromise;
|
|
249
|
-
abortController.signal.throwIfAborted();
|
|
250
|
-
if (eventsStatus != null)
|
|
251
|
-
return eventsStatus;
|
|
252
|
-
return base_1.SwapCommitStatus.NOT_COMMITED;
|
|
226
|
+
async getClaimHashStatus(claimHash) {
|
|
227
|
+
const { paymentHash } = (0, Utils_1.fromClaimHash)(claimHash);
|
|
228
|
+
const escrowStateKey = this.SwapEscrowState(buffer_1.Buffer.from(paymentHash, "hex"));
|
|
229
|
+
const abortController = new AbortController();
|
|
230
|
+
//Start fetching events before checking escrow PDA, this call is used when quoting, so saving 100ms here helps a lot!
|
|
231
|
+
const eventsPromise = this.Events.findInEvents(escrowStateKey, async (event) => {
|
|
232
|
+
if (event.name === "ClaimEvent")
|
|
233
|
+
return base_1.SwapCommitStatus.PAID;
|
|
234
|
+
if (event.name === "RefundEvent")
|
|
235
|
+
return base_1.SwapCommitStatus.NOT_COMMITED;
|
|
236
|
+
}, abortController.signal).catch(e => {
|
|
237
|
+
abortController.abort(e);
|
|
238
|
+
return null;
|
|
253
239
|
});
|
|
240
|
+
const escrowState = await this.program.account.escrowState.fetchNullable(escrowStateKey);
|
|
241
|
+
abortController.signal.throwIfAborted();
|
|
242
|
+
if (escrowState != null) {
|
|
243
|
+
abortController.abort();
|
|
244
|
+
return base_1.SwapCommitStatus.COMMITED;
|
|
245
|
+
}
|
|
246
|
+
//Check if paid or what
|
|
247
|
+
const eventsStatus = await eventsPromise;
|
|
248
|
+
abortController.signal.throwIfAborted();
|
|
249
|
+
if (eventsStatus != null)
|
|
250
|
+
return eventsStatus;
|
|
251
|
+
return base_1.SwapCommitStatus.NOT_COMMITED;
|
|
254
252
|
}
|
|
255
253
|
/**
|
|
256
254
|
* Returns the data committed for a specific payment hash, or null if no data is currently commited for
|
|
257
255
|
* the specific swap
|
|
258
256
|
*
|
|
259
|
-
* @param
|
|
257
|
+
* @param claimHashHex
|
|
260
258
|
*/
|
|
261
|
-
getCommitedData(
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
});
|
|
259
|
+
async getCommitedData(claimHashHex) {
|
|
260
|
+
const { paymentHash } = (0, Utils_1.fromClaimHash)(claimHashHex);
|
|
261
|
+
const paymentHashBuffer = buffer_1.Buffer.from(paymentHash, "hex");
|
|
262
|
+
const account = await this.program.account.escrowState.fetchNullable(this.SwapEscrowState(paymentHashBuffer));
|
|
263
|
+
if (account == null)
|
|
264
|
+
return null;
|
|
265
|
+
return SolanaSwapData_1.SolanaSwapData.fromEscrowState(account);
|
|
269
266
|
}
|
|
270
267
|
////////////////////////////////////////////
|
|
271
268
|
//// Swap data initializer
|
|
272
|
-
createSwapData(type, offerer, claimer, token, amount,
|
|
269
|
+
createSwapData(type, offerer, claimer, token, amount, claimHash, sequence, expiry, payIn, payOut, securityDeposit, claimerBounty, depositToken) {
|
|
270
|
+
if (depositToken != null) {
|
|
271
|
+
if (!new web3_js_1.PublicKey(depositToken).equals(SolanaTokens_1.SolanaTokens.WSOL_ADDRESS))
|
|
272
|
+
throw new Error("Only SOL supported as deposit token!");
|
|
273
|
+
}
|
|
273
274
|
const tokenAddr = new web3_js_1.PublicKey(token);
|
|
274
275
|
const offererKey = offerer == null ? null : new web3_js_1.PublicKey(offerer);
|
|
275
276
|
const claimerKey = claimer == null ? null : new web3_js_1.PublicKey(claimer);
|
|
276
|
-
|
|
277
|
+
const { paymentHash, nonce, confirmations } = (0, Utils_1.fromClaimHash)(claimHash);
|
|
278
|
+
return Promise.resolve(new SolanaSwapData_1.SolanaSwapData(offererKey, claimerKey, tokenAddr, (0, Utils_1.toBN)(amount), paymentHash, (0, Utils_1.toBN)(sequence), (0, Utils_1.toBN)(expiry), nonce, confirmations, payOut, type == null ? null : SolanaSwapData_1.SolanaSwapData.typeToKind(type), payIn, offererKey == null ? null : payIn ? (0, spl_token_1.getAssociatedTokenAddressSync)(tokenAddr, offererKey) : web3_js_1.PublicKey.default, claimerKey == null ? null : payOut ? (0, spl_token_1.getAssociatedTokenAddressSync)(tokenAddr, claimerKey) : web3_js_1.PublicKey.default, (0, Utils_1.toBN)(securityDeposit), (0, Utils_1.toBN)(claimerBounty), null));
|
|
277
279
|
}
|
|
278
280
|
////////////////////////////////////////////
|
|
279
281
|
//// Utils
|
|
280
|
-
getBalance(signer, tokenAddress, inContract) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
282
|
+
async getBalance(signer, tokenAddress, inContract) {
|
|
283
|
+
const token = new web3_js_1.PublicKey(tokenAddress);
|
|
284
|
+
const publicKey = new web3_js_1.PublicKey(signer);
|
|
285
|
+
if (inContract)
|
|
286
|
+
return await this.getIntermediaryBalance(publicKey, token);
|
|
287
|
+
let { balance } = await this.Tokens.getTokenBalance(publicKey, token);
|
|
288
|
+
if (token.equals(SolanaTokens_1.SolanaTokens.WSOL_ADDRESS)) {
|
|
289
|
+
const accountRentExemptCost = 1000000n;
|
|
290
|
+
balance = balance - accountRentExemptCost;
|
|
291
|
+
if (balance < 0n)
|
|
292
|
+
balance = 0n;
|
|
293
|
+
}
|
|
294
|
+
this.logger.debug("getBalance(): token balance, token: " + token.toBase58() + " balance: " + balance.toString(10));
|
|
295
|
+
return balance;
|
|
294
296
|
}
|
|
295
297
|
getIntermediaryData(address, token) {
|
|
296
298
|
return this.LpVault.getIntermediaryData(new web3_js_1.PublicKey(address), new web3_js_1.PublicKey(token));
|
|
@@ -309,27 +311,31 @@ class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
309
311
|
}
|
|
310
312
|
////////////////////////////////////////////
|
|
311
313
|
//// Transaction initializers
|
|
312
|
-
txsClaimWithSecret(signer, swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck) {
|
|
313
|
-
return
|
|
314
|
-
return this.Claim.txsClaimWithSecret(typeof (signer) === "string" ? new web3_js_1.PublicKey(signer) : signer.getPublicKey(), swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck);
|
|
315
|
-
});
|
|
314
|
+
async txsClaimWithSecret(signer, swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck) {
|
|
315
|
+
return this.Claim.txsClaimWithSecret(typeof (signer) === "string" ? new web3_js_1.PublicKey(signer) : signer.getPublicKey(), swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck);
|
|
316
316
|
}
|
|
317
|
-
txsClaimWithTxData(signer, swapData,
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
317
|
+
async txsClaimWithTxData(signer, swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, initAta, feeRate, storageAccHolder) {
|
|
318
|
+
if (swapData.confirmations !== requiredConfirmations)
|
|
319
|
+
throw new Error("Invalid requiredConfirmations provided!");
|
|
320
|
+
return this.Claim.txsClaimWithTxData(typeof (signer) === "string" ? new web3_js_1.PublicKey(signer) : signer, swapData, tx, vout, commitedHeader, synchronizer, initAta, storageAccHolder, feeRate);
|
|
321
321
|
}
|
|
322
|
-
txsRefund(swapData, check, initAta, feeRate) {
|
|
322
|
+
txsRefund(signer, swapData, check, initAta, feeRate) {
|
|
323
|
+
if (!swapData.isOfferer(signer))
|
|
324
|
+
throw new Error("Only offerer can refund on Solana");
|
|
323
325
|
return this.Refund.txsRefund(swapData, check, initAta, feeRate);
|
|
324
326
|
}
|
|
325
|
-
txsRefundWithAuthorization(swapData, { timeout, prefix, signature }, check, initAta, feeRate) {
|
|
327
|
+
txsRefundWithAuthorization(signer, swapData, { timeout, prefix, signature }, check, initAta, feeRate) {
|
|
328
|
+
if (!swapData.isOfferer(signer))
|
|
329
|
+
throw new Error("Only offerer can refund on Solana");
|
|
326
330
|
return this.Refund.txsRefundWithAuthorization(swapData, timeout, prefix, signature, check, initAta, feeRate);
|
|
327
331
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
332
|
+
txsInit(swapData, { timeout, prefix, signature }, skipChecks, feeRate) {
|
|
333
|
+
if (swapData.isPayIn()) {
|
|
334
|
+
return this.Init.txsInitPayIn(swapData, timeout, prefix, signature, skipChecks, feeRate);
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
return this.Init.txsInit(swapData, timeout, prefix, signature, skipChecks, feeRate);
|
|
338
|
+
}
|
|
333
339
|
}
|
|
334
340
|
txsWithdraw(signer, token, amount, feeRate) {
|
|
335
341
|
return this.LpVault.txsWithdraw(new web3_js_1.PublicKey(signer), new web3_js_1.PublicKey(token), amount, feeRate);
|
|
@@ -342,92 +348,69 @@ class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
342
348
|
}
|
|
343
349
|
////////////////////////////////////////////
|
|
344
350
|
//// Executors
|
|
345
|
-
claimWithSecret(signer, swapData, secret, checkExpiry, initAta, txOptions) {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
return
|
|
351
|
+
async claimWithSecret(signer, swapData, secret, checkExpiry, initAta, txOptions) {
|
|
352
|
+
const result = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, checkExpiry, initAta, txOptions?.feeRate);
|
|
353
|
+
const [signature] = await this.Transactions.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
354
|
+
return signature;
|
|
355
|
+
}
|
|
356
|
+
async claimWithTxData(signer, swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, initAta, txOptions) {
|
|
357
|
+
if (requiredConfirmations !== swapData.confirmations)
|
|
358
|
+
throw new Error("Invalid requiredConfirmations provided!");
|
|
359
|
+
const data = {
|
|
360
|
+
storageAcc: null
|
|
361
|
+
};
|
|
362
|
+
const txs = await this.Claim.txsClaimWithTxData(signer, swapData, tx, vout, commitedHeader, synchronizer, initAta, data, txOptions?.feeRate);
|
|
363
|
+
if (txs === null)
|
|
364
|
+
throw new Error("Btc relay not synchronized to required blockheight!");
|
|
365
|
+
//TODO: This doesn't return proper tx signature
|
|
366
|
+
const [signature] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
367
|
+
await this.DataAccount.removeDataAccount(data.storageAcc);
|
|
368
|
+
return signature;
|
|
369
|
+
}
|
|
370
|
+
async refund(signer, swapData, check, initAta, txOptions) {
|
|
371
|
+
let result = await this.txsRefund(signer.getAddress(), swapData, check, initAta, txOptions?.feeRate);
|
|
372
|
+
const [signature] = await this.Transactions.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
373
|
+
return signature;
|
|
374
|
+
}
|
|
375
|
+
async refundWithAuthorization(signer, swapData, signature, check, initAta, txOptions) {
|
|
376
|
+
let result = await this.txsRefundWithAuthorization(signer.getAddress(), swapData, signature, check, initAta, txOptions?.feeRate);
|
|
377
|
+
const [txSignature] = await this.Transactions.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
378
|
+
return txSignature;
|
|
379
|
+
}
|
|
380
|
+
async init(signer, swapData, signature, skipChecks, txOptions) {
|
|
381
|
+
if (swapData.isPayIn()) {
|
|
368
382
|
if (!signer.getPublicKey().equals(swapData.offerer))
|
|
369
383
|
throw new Error("Invalid signer provided!");
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
return signature;
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
refundWithAuthorization(signer, swapData, signature, check, initAta, txOptions) {
|
|
376
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
377
|
-
if (!signer.getPublicKey().equals(swapData.offerer))
|
|
378
|
-
throw new Error("Invalid signer provided!");
|
|
379
|
-
let result = yield this.txsRefundWithAuthorization(swapData, signature, check, initAta, txOptions === null || txOptions === void 0 ? void 0 : txOptions.feeRate);
|
|
380
|
-
const [txSignature] = yield this.Transactions.sendAndConfirm(signer, result, txOptions === null || txOptions === void 0 ? void 0 : txOptions.waitForConfirmation, txOptions === null || txOptions === void 0 ? void 0 : txOptions.abortSignal);
|
|
381
|
-
return txSignature;
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
initPayIn(signer, swapData, signature, skipChecks, txOptions) {
|
|
385
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
386
|
-
if (!signer.getPublicKey().equals(swapData.offerer))
|
|
387
|
-
throw new Error("Invalid signer provided!");
|
|
388
|
-
let result = yield this.txsInitPayIn(swapData, signature, skipChecks, txOptions === null || txOptions === void 0 ? void 0 : txOptions.feeRate);
|
|
389
|
-
const signatures = yield this.Transactions.sendAndConfirm(signer, result, txOptions === null || txOptions === void 0 ? void 0 : txOptions.waitForConfirmation, txOptions === null || txOptions === void 0 ? void 0 : txOptions.abortSignal);
|
|
390
|
-
return signatures[signatures.length - 1];
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
init(signer, swapData, signature, txoHash, skipChecks, txOptions) {
|
|
394
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
395
|
-
if (!signer.getPublicKey().equals(swapData.claimer))
|
|
396
|
-
throw new Error("Invalid signer provided!");
|
|
397
|
-
let result = yield this.txsInit(swapData, signature, txoHash, skipChecks, txOptions === null || txOptions === void 0 ? void 0 : txOptions.feeRate);
|
|
398
|
-
const [txSignature] = yield this.Transactions.sendAndConfirm(signer, result, txOptions === null || txOptions === void 0 ? void 0 : txOptions.waitForConfirmation, txOptions === null || txOptions === void 0 ? void 0 : txOptions.abortSignal);
|
|
399
|
-
return txSignature;
|
|
400
|
-
});
|
|
401
|
-
}
|
|
402
|
-
initAndClaimWithSecret(signer, swapData, signature, secret, skipChecks, txOptions) {
|
|
403
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
404
386
|
if (!signer.getPublicKey().equals(swapData.claimer))
|
|
405
387
|
throw new Error("Invalid signer provided!");
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
return
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
388
|
+
}
|
|
389
|
+
const result = await this.txsInit(swapData, signature, skipChecks, txOptions?.feeRate);
|
|
390
|
+
const [txSignature] = await this.Transactions.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
391
|
+
return txSignature;
|
|
392
|
+
}
|
|
393
|
+
async initAndClaimWithSecret(signer, swapData, signature, secret, skipChecks, txOptions) {
|
|
394
|
+
if (!signer.getPublicKey().equals(swapData.claimer))
|
|
395
|
+
throw new Error("Invalid signer provided!");
|
|
396
|
+
const txsCommit = await this.txsInit(swapData, signature, skipChecks, txOptions?.feeRate);
|
|
397
|
+
const txsClaim = await this.Claim.txsClaimWithSecret(signer.getPublicKey(), swapData, secret, true, false, txOptions?.feeRate, true);
|
|
398
|
+
return await this.Transactions.sendAndConfirm(signer, txsCommit.concat(txsClaim), txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
399
|
+
}
|
|
400
|
+
async withdraw(signer, token, amount, txOptions) {
|
|
401
|
+
const txs = await this.LpVault.txsWithdraw(signer.getPublicKey(), new web3_js_1.PublicKey(token), amount, txOptions?.feeRate);
|
|
402
|
+
const [txId] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
|
|
403
|
+
return txId;
|
|
404
|
+
}
|
|
405
|
+
async deposit(signer, token, amount, txOptions) {
|
|
406
|
+
const txs = await this.LpVault.txsDeposit(signer.getPublicKey(), new web3_js_1.PublicKey(token), amount, txOptions?.feeRate);
|
|
407
|
+
const [txId] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
|
|
408
|
+
return txId;
|
|
409
|
+
}
|
|
410
|
+
async transfer(signer, token, amount, dstAddress, txOptions) {
|
|
411
|
+
const txs = await this.Tokens.txsTransfer(signer.getPublicKey(), new web3_js_1.PublicKey(token), amount, new web3_js_1.PublicKey(dstAddress), txOptions?.feeRate);
|
|
412
|
+
const [txId] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
|
|
413
|
+
return txId;
|
|
431
414
|
}
|
|
432
415
|
////////////////////////////////////////////
|
|
433
416
|
//// Transactions
|
|
@@ -448,10 +431,12 @@ class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
448
431
|
}
|
|
449
432
|
////////////////////////////////////////////
|
|
450
433
|
//// Fees
|
|
451
|
-
getInitPayInFeeRate(offerer, claimer, token,
|
|
434
|
+
getInitPayInFeeRate(offerer, claimer, token, claimHash) {
|
|
435
|
+
const paymentHash = claimHash == null ? null : (0, Utils_1.fromClaimHash)(claimHash).paymentHash;
|
|
452
436
|
return this.Init.getInitPayInFeeRate(toPublicKeyOrNull(offerer), toPublicKeyOrNull(claimer), toPublicKeyOrNull(token), paymentHash);
|
|
453
437
|
}
|
|
454
|
-
getInitFeeRate(offerer, claimer, token,
|
|
438
|
+
getInitFeeRate(offerer, claimer, token, claimHash) {
|
|
439
|
+
const paymentHash = claimHash == null ? null : (0, Utils_1.fromClaimHash)(claimHash).paymentHash;
|
|
455
440
|
return this.Init.getInitFeeRate(toPublicKeyOrNull(offerer), toPublicKeyOrNull(claimer), toPublicKeyOrNull(token), paymentHash);
|
|
456
441
|
}
|
|
457
442
|
getRefundFeeRate(swapData) {
|
|
@@ -525,5 +510,11 @@ class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
|
|
|
525
510
|
const wallet = new SolanaKeypairWallet_1.SolanaKeypairWallet(keypair);
|
|
526
511
|
return new SolanaSigner_1.SolanaSigner(wallet, keypair);
|
|
527
512
|
}
|
|
513
|
+
getExtraData(outputScript, amount, confirmations, nonce) {
|
|
514
|
+
return createHash("sha256").update(buffer_1.Buffer.concat([
|
|
515
|
+
base_1.BigIntBufferUtils.toBuffer(amount, "le", 8),
|
|
516
|
+
outputScript
|
|
517
|
+
])).digest();
|
|
518
|
+
}
|
|
528
519
|
}
|
|
529
520
|
exports.SolanaSwapProgram = SolanaSwapProgram;
|
|
@@ -5,7 +5,6 @@ import { IStorageManager, StorageObject } from "@atomiqlabs/base";
|
|
|
5
5
|
import { SolanaSwapProgram } from "../SolanaSwapProgram";
|
|
6
6
|
import { SolanaTx } from "../../base/modules/SolanaTransactions";
|
|
7
7
|
import { SolanaSigner } from "../../wallet/SolanaSigner";
|
|
8
|
-
import * as BN from "bn.js";
|
|
9
8
|
export declare class StoredDataAccount implements StorageObject {
|
|
10
9
|
accountKey: PublicKey;
|
|
11
10
|
owner: PublicKey;
|
|
@@ -71,7 +70,7 @@ export declare class SolanaDataAccount extends SolanaSwapModule {
|
|
|
71
70
|
getDataAccountsInfo(signer: PublicKey): Promise<{
|
|
72
71
|
closePublicKeys: PublicKey[];
|
|
73
72
|
count: number;
|
|
74
|
-
totalValue:
|
|
73
|
+
totalValue: bigint;
|
|
75
74
|
}>;
|
|
76
75
|
/**
|
|
77
76
|
* Sweeps all old data accounts, reclaiming the SOL locked in the PDAs
|
|
@@ -79,7 +78,7 @@ export declare class SolanaDataAccount extends SolanaSwapModule {
|
|
|
79
78
|
sweepDataAccounts(signer: SolanaSigner): Promise<{
|
|
80
79
|
txIds: string[];
|
|
81
80
|
count: number;
|
|
82
|
-
totalValue:
|
|
81
|
+
totalValue: bigint;
|
|
83
82
|
}>;
|
|
84
83
|
/**
|
|
85
84
|
* Adds the transactions writing (and also initializing if it doesn't exist) data to the data account
|