@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,13 +1,4 @@
|
|
|
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.SolanaDataAccount = exports.StoredDataAccount = void 0;
|
|
13
4
|
const SolanaSwapModule_1 = require("../SolanaSwapModule");
|
|
@@ -16,7 +7,6 @@ const SolanaAction_1 = require("../../base/SolanaAction");
|
|
|
16
7
|
const Utils_1 = require("../../../utils/Utils");
|
|
17
8
|
const randomBytes = require("randombytes");
|
|
18
9
|
const SolanaSigner_1 = require("../../wallet/SolanaSigner");
|
|
19
|
-
const BN = require("bn.js");
|
|
20
10
|
class StoredDataAccount {
|
|
21
11
|
constructor(accountKeyOrData, owner) {
|
|
22
12
|
if (accountKeyOrData instanceof web3_js_1.PublicKey) {
|
|
@@ -45,27 +35,25 @@ class SolanaDataAccount extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
45
35
|
* @param dataLength
|
|
46
36
|
* @private
|
|
47
37
|
*/
|
|
48
|
-
InitDataAccount(signer, accountKey, dataLength) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
], SolanaDataAccount.CUCosts.DATA_CREATE, null, [accountKey]);
|
|
68
|
-
});
|
|
38
|
+
async InitDataAccount(signer, accountKey, dataLength) {
|
|
39
|
+
const accountSize = 32 + dataLength;
|
|
40
|
+
const lamportsDeposit = await (0, Utils_1.tryWithRetries)(() => this.connection.getMinimumBalanceForRentExemption(accountSize), this.retryPolicy);
|
|
41
|
+
return new SolanaAction_1.SolanaAction(signer, this.root, [
|
|
42
|
+
web3_js_1.SystemProgram.createAccount({
|
|
43
|
+
fromPubkey: signer,
|
|
44
|
+
newAccountPubkey: accountKey.publicKey,
|
|
45
|
+
lamports: lamportsDeposit,
|
|
46
|
+
space: accountSize,
|
|
47
|
+
programId: this.program.programId
|
|
48
|
+
}),
|
|
49
|
+
await this.program.methods
|
|
50
|
+
.initData()
|
|
51
|
+
.accounts({
|
|
52
|
+
signer,
|
|
53
|
+
data: accountKey.publicKey
|
|
54
|
+
})
|
|
55
|
+
.instruction(),
|
|
56
|
+
], SolanaDataAccount.CUCosts.DATA_CREATE, null, [accountKey]);
|
|
69
57
|
}
|
|
70
58
|
/**
|
|
71
59
|
* Action for closing the specific data account
|
|
@@ -73,16 +61,14 @@ class SolanaDataAccount extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
73
61
|
* @param signer
|
|
74
62
|
* @param publicKey
|
|
75
63
|
*/
|
|
76
|
-
CloseDataAccount(signer, publicKey) {
|
|
77
|
-
return
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
.instruction(), SolanaDataAccount.CUCosts.DATA_REMOVE, yield this.root.Fees.getFeeRate([signer, publicKey]));
|
|
85
|
-
});
|
|
64
|
+
async CloseDataAccount(signer, publicKey) {
|
|
65
|
+
return new SolanaAction_1.SolanaAction(signer, this.root, await this.program.methods
|
|
66
|
+
.closeData()
|
|
67
|
+
.accounts({
|
|
68
|
+
signer,
|
|
69
|
+
data: publicKey
|
|
70
|
+
})
|
|
71
|
+
.instruction(), SolanaDataAccount.CUCosts.DATA_REMOVE, await this.root.Fees.getFeeRate([signer, publicKey]));
|
|
86
72
|
}
|
|
87
73
|
/**
|
|
88
74
|
* Action for writing data to a data account, writes up to sizeLimit starting from the offset position of the
|
|
@@ -96,20 +82,18 @@ class SolanaDataAccount extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
96
82
|
* @private
|
|
97
83
|
* @returns {Promise<{bytesWritten: number, action: SolanaAction}>} bytes written to the data account & action
|
|
98
84
|
*/
|
|
99
|
-
WriteData(signer, accountKey, writeData, offset, sizeLimit) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
};
|
|
112
|
-
});
|
|
85
|
+
async WriteData(signer, accountKey, writeData, offset, sizeLimit) {
|
|
86
|
+
const writeLen = Math.min(writeData.length - offset, sizeLimit);
|
|
87
|
+
return {
|
|
88
|
+
bytesWritten: writeLen,
|
|
89
|
+
action: new SolanaAction_1.SolanaAction(signer, this.root, await this.program.methods
|
|
90
|
+
.writeData(offset, writeData.slice(offset, offset + writeLen))
|
|
91
|
+
.accounts({
|
|
92
|
+
signer,
|
|
93
|
+
data: accountKey.publicKey
|
|
94
|
+
})
|
|
95
|
+
.instruction(), SolanaDataAccount.CUCosts.DATA_WRITE)
|
|
96
|
+
};
|
|
113
97
|
}
|
|
114
98
|
constructor(root, storage) {
|
|
115
99
|
super(root);
|
|
@@ -131,12 +115,10 @@ class SolanaDataAccount extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
131
115
|
/**
|
|
132
116
|
* Initializes the data account handler, loads the existing data accounts which should be checked and closed
|
|
133
117
|
*/
|
|
134
|
-
init() {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
this.logger.info("init(): initialized & loaded stored data accounts, count: " + loadedData.length);
|
|
139
|
-
});
|
|
118
|
+
async init() {
|
|
119
|
+
await this.storage.init();
|
|
120
|
+
const loadedData = await this.storage.loadData(StoredDataAccount);
|
|
121
|
+
this.logger.info("init(): initialized & loaded stored data accounts, count: " + loadedData.length);
|
|
140
122
|
}
|
|
141
123
|
/**
|
|
142
124
|
* Removes data account from the list of accounts that should be checked for reclaiming the locked SOL, this should
|
|
@@ -147,59 +129,55 @@ class SolanaDataAccount extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
147
129
|
removeDataAccount(publicKey) {
|
|
148
130
|
return this.storage.removeData(publicKey.toBase58());
|
|
149
131
|
}
|
|
150
|
-
getDataAccountsInfo(signer) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
132
|
+
async getDataAccountsInfo(signer) {
|
|
133
|
+
const closePublicKeys = [];
|
|
134
|
+
let totalLocked = 0n;
|
|
135
|
+
for (let key in this.storage.data) {
|
|
136
|
+
const { accountKey, owner } = this.storage.data[key];
|
|
137
|
+
if (!owner.equals(signer))
|
|
138
|
+
continue;
|
|
139
|
+
try {
|
|
140
|
+
const fetchedDataAccount = await this.connection.getAccountInfo(accountKey);
|
|
141
|
+
if (fetchedDataAccount == null) {
|
|
142
|
+
await this.removeDataAccount(accountKey);
|
|
157
143
|
continue;
|
|
158
|
-
try {
|
|
159
|
-
const fetchedDataAccount = yield this.connection.getAccountInfo(accountKey);
|
|
160
|
-
if (fetchedDataAccount == null) {
|
|
161
|
-
yield this.removeDataAccount(accountKey);
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
closePublicKeys.push(accountKey);
|
|
165
|
-
totalLocked = totalLocked.add(new BN(fetchedDataAccount.lamports));
|
|
166
144
|
}
|
|
167
|
-
|
|
145
|
+
closePublicKeys.push(accountKey);
|
|
146
|
+
totalLocked += BigInt(fetchedDataAccount.lamports);
|
|
168
147
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
148
|
+
catch (e) { }
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
closePublicKeys,
|
|
152
|
+
count: closePublicKeys.length,
|
|
153
|
+
totalValue: totalLocked
|
|
154
|
+
};
|
|
175
155
|
}
|
|
176
156
|
/**
|
|
177
157
|
* Sweeps all old data accounts, reclaiming the SOL locked in the PDAs
|
|
178
158
|
*/
|
|
179
|
-
sweepDataAccounts(signer) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
};
|
|
202
|
-
});
|
|
159
|
+
async sweepDataAccounts(signer) {
|
|
160
|
+
const { closePublicKeys, totalValue } = await this.getDataAccountsInfo(signer.getPublicKey());
|
|
161
|
+
if (closePublicKeys.length === 0) {
|
|
162
|
+
this.logger.debug("sweepDataAccounts(): no old data accounts found, no need to close any!");
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
this.logger.debug("sweepDataAccounts(): closing old data accounts: ", closePublicKeys);
|
|
166
|
+
let txns = [];
|
|
167
|
+
for (let publicKey of closePublicKeys) {
|
|
168
|
+
await (await this.CloseDataAccount(signer.getPublicKey(), publicKey)).addToTxs(txns);
|
|
169
|
+
}
|
|
170
|
+
const result = await this.root.Transactions.sendAndConfirm(signer, txns, true, null, true);
|
|
171
|
+
this.logger.info("sweepDataAccounts(): old data accounts closed: " +
|
|
172
|
+
closePublicKeys.map(pk => pk.toBase58()).join());
|
|
173
|
+
for (let publicKey of closePublicKeys) {
|
|
174
|
+
await this.removeDataAccount(publicKey);
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
txIds: result,
|
|
178
|
+
count: closePublicKeys.length,
|
|
179
|
+
totalValue: totalValue
|
|
180
|
+
};
|
|
203
181
|
}
|
|
204
182
|
/**
|
|
205
183
|
* Adds the transactions writing (and also initializing if it doesn't exist) data to the data account
|
|
@@ -210,40 +188,38 @@ class SolanaDataAccount extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
210
188
|
* @param txs solana transactions array, where txns for writing & initializing will be added
|
|
211
189
|
* @param feeRate fee rate to use for the transactions
|
|
212
190
|
*/
|
|
213
|
-
addTxsWriteData(signer, reversedTxId, writeData, txs, feeRate) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
return txDataKey.publicKey;
|
|
246
|
-
});
|
|
191
|
+
async addTxsWriteData(signer, reversedTxId, writeData, txs, feeRate) {
|
|
192
|
+
let txDataKey;
|
|
193
|
+
let fetchedDataAccount = null;
|
|
194
|
+
if (signer instanceof SolanaSigner_1.SolanaSigner && signer.keypair != null) {
|
|
195
|
+
txDataKey = this.SwapTxDataAlt(reversedTxId, signer.keypair);
|
|
196
|
+
fetchedDataAccount = await (0, Utils_1.tryWithRetries)(() => this.connection.getAccountInfo(txDataKey.publicKey), this.retryPolicy);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
const secret = randomBytes(32);
|
|
200
|
+
txDataKey = this.SwapTxDataAltBuffer(reversedTxId, secret);
|
|
201
|
+
}
|
|
202
|
+
const signerKey = signer instanceof SolanaSigner_1.SolanaSigner ? signer.getPublicKey() : signer;
|
|
203
|
+
let pointer = 0;
|
|
204
|
+
if (fetchedDataAccount == null) {
|
|
205
|
+
const action = new SolanaAction_1.SolanaAction(signerKey, this.root);
|
|
206
|
+
action.add(await this.InitDataAccount(signerKey, txDataKey, writeData.length));
|
|
207
|
+
const { bytesWritten, action: writeAction } = await this.WriteData(signerKey, txDataKey, writeData, pointer, 420);
|
|
208
|
+
this.logger.debug("addTxsWriteData(): Write partial data (" + pointer + " .. " + (pointer + bytesWritten) + ")/" + writeData.length +
|
|
209
|
+
" key: " + txDataKey.publicKey.toBase58());
|
|
210
|
+
pointer += bytesWritten;
|
|
211
|
+
action.add(writeAction);
|
|
212
|
+
await action.addToTxs(txs, feeRate);
|
|
213
|
+
await this.saveDataAccount(signerKey, txDataKey.publicKey);
|
|
214
|
+
}
|
|
215
|
+
while (pointer < writeData.length) {
|
|
216
|
+
const { bytesWritten, action } = await this.WriteData(signerKey, txDataKey, writeData, pointer, 950);
|
|
217
|
+
this.logger.debug("addTxsWriteData(): Write partial data (" + pointer + " .. " + (pointer + bytesWritten) + ")/" + writeData.length +
|
|
218
|
+
" key: " + txDataKey.publicKey.toBase58());
|
|
219
|
+
pointer += bytesWritten;
|
|
220
|
+
await action.addToTxs(txs, feeRate);
|
|
221
|
+
}
|
|
222
|
+
return txDataKey.publicKey;
|
|
247
223
|
}
|
|
248
224
|
}
|
|
249
225
|
exports.SolanaDataAccount = SolanaDataAccount;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { SolanaSwapModule } from "../SolanaSwapModule";
|
|
2
|
-
import * as BN from "bn.js";
|
|
3
2
|
import { PublicKey } from "@solana/web3.js";
|
|
4
3
|
import { SolanaTx } from "../../base/modules/SolanaTransactions";
|
|
5
4
|
import { IntermediaryReputationType } from "@atomiqlabs/base";
|
|
@@ -32,7 +31,7 @@ export declare class SolanaLpVault extends SolanaSwapModule {
|
|
|
32
31
|
* @param token
|
|
33
32
|
*/
|
|
34
33
|
getIntermediaryData(address: PublicKey, token: PublicKey): Promise<{
|
|
35
|
-
balance:
|
|
34
|
+
balance: bigint;
|
|
36
35
|
reputation: IntermediaryReputationType;
|
|
37
36
|
}>;
|
|
38
37
|
/**
|
|
@@ -48,7 +47,7 @@ export declare class SolanaLpVault extends SolanaSwapModule {
|
|
|
48
47
|
* @param address
|
|
49
48
|
* @param token
|
|
50
49
|
*/
|
|
51
|
-
getIntermediaryBalance(address: PublicKey, token: PublicKey): Promise<
|
|
50
|
+
getIntermediaryBalance(address: PublicKey, token: PublicKey): Promise<bigint>;
|
|
52
51
|
/**
|
|
53
52
|
* Creates transactions for withdrawing funds from the LP vault, creates ATA if it doesn't exist and unwraps
|
|
54
53
|
* WSOL to SOL if required
|
|
@@ -58,7 +57,7 @@ export declare class SolanaLpVault extends SolanaSwapModule {
|
|
|
58
57
|
* @param amount
|
|
59
58
|
* @param feeRate
|
|
60
59
|
*/
|
|
61
|
-
txsWithdraw(signer: PublicKey, token: PublicKey, amount:
|
|
60
|
+
txsWithdraw(signer: PublicKey, token: PublicKey, amount: bigint, feeRate?: string): Promise<SolanaTx[]>;
|
|
62
61
|
/**
|
|
63
62
|
* Creates transaction for depositing funds into the LP vault, wraps SOL to WSOL if required
|
|
64
63
|
*
|
|
@@ -67,6 +66,6 @@ export declare class SolanaLpVault extends SolanaSwapModule {
|
|
|
67
66
|
* @param amount
|
|
68
67
|
* @param feeRate
|
|
69
68
|
*/
|
|
70
|
-
txsDeposit(signer: PublicKey, token: PublicKey, amount:
|
|
69
|
+
txsDeposit(signer: PublicKey, token: PublicKey, amount: bigint, feeRate?: string): Promise<SolanaTx[]>;
|
|
71
70
|
getFeeRate(signer: PublicKey, token: PublicKey): Promise<string>;
|
|
72
71
|
}
|
|
@@ -1,21 +1,12 @@
|
|
|
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.SolanaLpVault = void 0;
|
|
13
4
|
const SolanaSwapModule_1 = require("../SolanaSwapModule");
|
|
14
5
|
const SolanaAction_1 = require("../../base/SolanaAction");
|
|
15
|
-
const BN = require("bn.js");
|
|
16
6
|
const web3_js_1 = require("@solana/web3.js");
|
|
17
7
|
const spl_token_1 = require("@solana/spl-token");
|
|
18
8
|
const Utils_1 = require("../../../utils/Utils");
|
|
9
|
+
const SolanaTokens_1 = require("../../base/modules/SolanaTokens");
|
|
19
10
|
class SolanaLpVault extends SolanaSwapModule_1.SolanaSwapModule {
|
|
20
11
|
/**
|
|
21
12
|
* Action for withdrawing funds from the LP vault
|
|
@@ -26,22 +17,20 @@ class SolanaLpVault extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
26
17
|
* @constructor
|
|
27
18
|
* @private
|
|
28
19
|
*/
|
|
29
|
-
Withdraw(signer, token, amount) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
.instruction(), SolanaLpVault.CUCosts.WITHDRAW);
|
|
44
|
-
});
|
|
20
|
+
async Withdraw(signer, token, amount) {
|
|
21
|
+
const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(token, signer);
|
|
22
|
+
return new SolanaAction_1.SolanaAction(signer, this.root, await this.program.methods
|
|
23
|
+
.withdraw((0, Utils_1.toBN)(amount))
|
|
24
|
+
.accounts({
|
|
25
|
+
signer,
|
|
26
|
+
signerAta: ata,
|
|
27
|
+
userData: this.root.SwapUserVault(signer, token),
|
|
28
|
+
vault: this.root.SwapVault(token),
|
|
29
|
+
vaultAuthority: this.root.SwapVaultAuthority,
|
|
30
|
+
mint: token,
|
|
31
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID
|
|
32
|
+
})
|
|
33
|
+
.instruction(), SolanaLpVault.CUCosts.WITHDRAW);
|
|
45
34
|
}
|
|
46
35
|
/**
|
|
47
36
|
* Action for depositing funds to the LP vault
|
|
@@ -52,23 +41,21 @@ class SolanaLpVault extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
52
41
|
* @constructor
|
|
53
42
|
* @private
|
|
54
43
|
*/
|
|
55
|
-
Deposit(signer, token, amount) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
.instruction(), SolanaLpVault.CUCosts.DEPOSIT);
|
|
71
|
-
});
|
|
44
|
+
async Deposit(signer, token, amount) {
|
|
45
|
+
const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(token, signer);
|
|
46
|
+
return new SolanaAction_1.SolanaAction(signer, this.root, await this.program.methods
|
|
47
|
+
.deposit((0, Utils_1.toBN)(amount))
|
|
48
|
+
.accounts({
|
|
49
|
+
signer,
|
|
50
|
+
signerAta: ata,
|
|
51
|
+
userData: this.root.SwapUserVault(signer, token),
|
|
52
|
+
vault: this.root.SwapVault(token),
|
|
53
|
+
vaultAuthority: this.root.SwapVaultAuthority,
|
|
54
|
+
mint: token,
|
|
55
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
56
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID
|
|
57
|
+
})
|
|
58
|
+
.instruction(), SolanaLpVault.CUCosts.DEPOSIT);
|
|
72
59
|
}
|
|
73
60
|
/**
|
|
74
61
|
* Returns intermediary's reputation & vault balance for a specific token
|
|
@@ -76,27 +63,25 @@ class SolanaLpVault extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
76
63
|
* @param address
|
|
77
64
|
* @param token
|
|
78
65
|
*/
|
|
79
|
-
getIntermediaryData(address, token) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
coopCloseCount: data.coopCloseCount[i]
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
return {
|
|
96
|
-
balance: data.amount,
|
|
97
|
-
reputation: response
|
|
66
|
+
async getIntermediaryData(address, token) {
|
|
67
|
+
const data = await this.program.account.userAccount.fetchNullable(this.root.SwapUserVault(address, token));
|
|
68
|
+
if (data == null)
|
|
69
|
+
return null;
|
|
70
|
+
const response = [];
|
|
71
|
+
for (let i = 0; i < data.successVolume.length; i++) {
|
|
72
|
+
response[i] = {
|
|
73
|
+
successVolume: data.successVolume[i],
|
|
74
|
+
successCount: data.successCount[i],
|
|
75
|
+
failVolume: data.failVolume[i],
|
|
76
|
+
failCount: data.failCount[i],
|
|
77
|
+
coopCloseVolume: data.coopCloseVolume[i],
|
|
78
|
+
coopCloseCount: data.coopCloseCount[i]
|
|
98
79
|
};
|
|
99
|
-
}
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
balance: (0, Utils_1.toBigInt)(data.amount),
|
|
83
|
+
reputation: response
|
|
84
|
+
};
|
|
100
85
|
}
|
|
101
86
|
/**
|
|
102
87
|
* Returns intermediary's reputation for a specific token
|
|
@@ -104,11 +89,9 @@ class SolanaLpVault extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
104
89
|
* @param address
|
|
105
90
|
* @param token
|
|
106
91
|
*/
|
|
107
|
-
getIntermediaryReputation(address, token) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return intermediaryData === null || intermediaryData === void 0 ? void 0 : intermediaryData.reputation;
|
|
111
|
-
});
|
|
92
|
+
async getIntermediaryReputation(address, token) {
|
|
93
|
+
const intermediaryData = await this.getIntermediaryData(address, token);
|
|
94
|
+
return intermediaryData?.reputation;
|
|
112
95
|
}
|
|
113
96
|
/**
|
|
114
97
|
* Returns the balance of the token an intermediary has in his LP vault
|
|
@@ -116,14 +99,12 @@ class SolanaLpVault extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
116
99
|
* @param address
|
|
117
100
|
* @param token
|
|
118
101
|
*/
|
|
119
|
-
getIntermediaryBalance(address, token) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return intermediaryData === null || intermediaryData === void 0 ? void 0 : intermediaryData.balance;
|
|
126
|
-
});
|
|
102
|
+
async getIntermediaryBalance(address, token) {
|
|
103
|
+
const intermediaryData = await this.getIntermediaryData(address, token);
|
|
104
|
+
const balance = intermediaryData?.balance;
|
|
105
|
+
this.logger.debug("getIntermediaryBalance(): token LP balance fetched, token: " + token.toString() +
|
|
106
|
+
" address: " + address + " amount: " + (balance == null ? "null" : balance.toString()));
|
|
107
|
+
return balance;
|
|
127
108
|
}
|
|
128
109
|
/**
|
|
129
110
|
* Creates transactions for withdrawing funds from the LP vault, creates ATA if it doesn't exist and unwraps
|
|
@@ -134,22 +115,20 @@ class SolanaLpVault extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
134
115
|
* @param amount
|
|
135
116
|
* @param feeRate
|
|
136
117
|
*/
|
|
137
|
-
txsWithdraw(signer, token, amount, feeRate) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
return [yield action.tx(feeRate)];
|
|
152
|
-
});
|
|
118
|
+
async txsWithdraw(signer, token, amount, feeRate) {
|
|
119
|
+
const ata = await (0, spl_token_1.getAssociatedTokenAddress)(token, signer);
|
|
120
|
+
feeRate = feeRate || await this.getFeeRate(signer, token);
|
|
121
|
+
const action = new SolanaAction_1.SolanaAction(signer, this.root);
|
|
122
|
+
if (!await this.root.Tokens.ataExists(ata)) {
|
|
123
|
+
action.add(this.root.Tokens.InitAta(signer, signer, token));
|
|
124
|
+
}
|
|
125
|
+
action.add(await this.Withdraw(signer, token, amount));
|
|
126
|
+
const shouldUnwrap = token.equals(SolanaTokens_1.SolanaTokens.WSOL_ADDRESS);
|
|
127
|
+
if (shouldUnwrap)
|
|
128
|
+
action.add(this.root.Tokens.Unwrap(signer));
|
|
129
|
+
this.logger.debug("txsWithdraw(): withdraw TX created, token: " + token.toString() +
|
|
130
|
+
" amount: " + amount.toString(10) + " unwrapping: " + shouldUnwrap);
|
|
131
|
+
return [await action.tx(feeRate)];
|
|
153
132
|
}
|
|
154
133
|
/**
|
|
155
134
|
* Creates transaction for depositing funds into the LP vault, wraps SOL to WSOL if required
|
|
@@ -159,25 +138,23 @@ class SolanaLpVault extends SolanaSwapModule_1.SolanaSwapModule {
|
|
|
159
138
|
* @param amount
|
|
160
139
|
* @param feeRate
|
|
161
140
|
*/
|
|
162
|
-
txsDeposit(signer, token, amount, feeRate) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
wrapping = true;
|
|
174
|
-
}
|
|
141
|
+
async txsDeposit(signer, token, amount, feeRate) {
|
|
142
|
+
const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(token, signer);
|
|
143
|
+
feeRate = feeRate || await this.getFeeRate(signer, token);
|
|
144
|
+
const action = new SolanaAction_1.SolanaAction(signer, this.root);
|
|
145
|
+
let wrapping = false;
|
|
146
|
+
if (token.equals(SolanaTokens_1.SolanaTokens.WSOL_ADDRESS)) {
|
|
147
|
+
const account = await (0, Utils_1.tryWithRetries)(() => this.root.Tokens.getATAOrNull(ata), this.retryPolicy);
|
|
148
|
+
let balance = account == null ? 0n : account.amount;
|
|
149
|
+
if (balance < amount) {
|
|
150
|
+
action.add(this.root.Tokens.Wrap(signer, amount - balance, account == null));
|
|
151
|
+
wrapping = true;
|
|
175
152
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
153
|
+
}
|
|
154
|
+
action.addAction(await this.Deposit(signer, token, amount));
|
|
155
|
+
this.logger.debug("txsDeposit(): deposit TX created, token: " + token.toString() +
|
|
156
|
+
" amount: " + amount.toString(10) + " wrapping: " + wrapping);
|
|
157
|
+
return [await action.tx(feeRate)];
|
|
181
158
|
}
|
|
182
159
|
getFeeRate(signer, token) {
|
|
183
160
|
const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(token, signer);
|