@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.
Files changed (57) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +1 -0
  3. package/dist/solana/SolanaChains.d.ts +14 -0
  4. package/dist/solana/SolanaChains.js +18 -0
  5. package/dist/solana/SolanaInitializer.d.ts +18 -0
  6. package/dist/solana/SolanaInitializer.js +59 -0
  7. package/dist/solana/base/SolanaAction.js +25 -38
  8. package/dist/solana/base/modules/SolanaBlocks.js +18 -29
  9. package/dist/solana/base/modules/SolanaEvents.js +15 -26
  10. package/dist/solana/base/modules/SolanaFees.d.ts +4 -5
  11. package/dist/solana/base/modules/SolanaFees.js +168 -182
  12. package/dist/solana/base/modules/SolanaSlots.js +16 -29
  13. package/dist/solana/base/modules/SolanaTokens.d.ts +6 -7
  14. package/dist/solana/base/modules/SolanaTokens.js +63 -84
  15. package/dist/solana/base/modules/SolanaTransactions.d.ts +2 -2
  16. package/dist/solana/base/modules/SolanaTransactions.js +143 -165
  17. package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +2 -3
  18. package/dist/solana/btcrelay/SolanaBtcRelay.js +185 -221
  19. package/dist/solana/events/SolanaChainEvents.js +113 -138
  20. package/dist/solana/events/SolanaChainEventsBrowser.js +56 -65
  21. package/dist/solana/program/modules/SolanaProgramEvents.js +13 -24
  22. package/dist/solana/swaps/SolanaSwapData.d.ts +15 -11
  23. package/dist/solana/swaps/SolanaSwapData.js +29 -13
  24. package/dist/solana/swaps/SolanaSwapProgram.d.ts +40 -41
  25. package/dist/solana/swaps/SolanaSwapProgram.js +212 -221
  26. package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +2 -3
  27. package/dist/solana/swaps/modules/SolanaDataAccount.js +117 -141
  28. package/dist/solana/swaps/modules/SolanaLpVault.d.ts +4 -5
  29. package/dist/solana/swaps/modules/SolanaLpVault.js +87 -110
  30. package/dist/solana/swaps/modules/SwapClaim.d.ts +4 -4
  31. package/dist/solana/swaps/modules/SwapClaim.js +122 -134
  32. package/dist/solana/swaps/modules/SwapInit.d.ts +2 -3
  33. package/dist/solana/swaps/modules/SwapInit.js +254 -285
  34. package/dist/solana/swaps/modules/SwapRefund.d.ts +2 -3
  35. package/dist/solana/swaps/modules/SwapRefund.js +103 -113
  36. package/dist/utils/Utils.d.ts +10 -0
  37. package/dist/utils/Utils.js +62 -35
  38. package/package.json +9 -6
  39. package/src/index.ts +1 -0
  40. package/src/solana/SolanaChains.ts +16 -0
  41. package/src/solana/SolanaInitializer.ts +98 -0
  42. package/src/solana/base/SolanaBase.ts +1 -2
  43. package/src/solana/base/modules/SolanaEvents.ts +0 -1
  44. package/src/solana/base/modules/SolanaFees.ts +35 -31
  45. package/src/solana/base/modules/SolanaTokens.ts +26 -27
  46. package/src/solana/base/modules/SolanaTransactions.ts +33 -28
  47. package/src/solana/btcrelay/SolanaBtcRelay.ts +6 -10
  48. package/src/solana/events/SolanaChainEventsBrowser.ts +14 -10
  49. package/src/solana/program/modules/SolanaProgramEvents.ts +1 -1
  50. package/src/solana/swaps/SolanaSwapData.ts +38 -19
  51. package/src/solana/swaps/SolanaSwapProgram.ts +133 -105
  52. package/src/solana/swaps/modules/SolanaDataAccount.ts +4 -6
  53. package/src/solana/swaps/modules/SolanaLpVault.ts +18 -18
  54. package/src/solana/swaps/modules/SwapClaim.ts +14 -17
  55. package/src/solana/swaps/modules/SwapInit.ts +35 -38
  56. package/src/solana/swaps/modules/SwapRefund.ts +20 -23
  57. 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
- return __awaiter(this, void 0, void 0, function* () {
50
- const accountSize = 32 + dataLength;
51
- const lamportsDeposit = yield (0, Utils_1.tryWithRetries)(() => this.connection.getMinimumBalanceForRentExemption(accountSize), this.retryPolicy);
52
- return new SolanaAction_1.SolanaAction(signer, this.root, [
53
- web3_js_1.SystemProgram.createAccount({
54
- fromPubkey: signer,
55
- newAccountPubkey: accountKey.publicKey,
56
- lamports: lamportsDeposit,
57
- space: accountSize,
58
- programId: this.program.programId
59
- }),
60
- yield this.program.methods
61
- .initData()
62
- .accounts({
63
- signer,
64
- data: accountKey.publicKey
65
- })
66
- .instruction(),
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 __awaiter(this, void 0, void 0, function* () {
78
- return new SolanaAction_1.SolanaAction(signer, this.root, yield this.program.methods
79
- .closeData()
80
- .accounts({
81
- signer,
82
- data: publicKey
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
- return __awaiter(this, void 0, void 0, function* () {
101
- const writeLen = Math.min(writeData.length - offset, sizeLimit);
102
- return {
103
- bytesWritten: writeLen,
104
- action: new SolanaAction_1.SolanaAction(signer, this.root, yield this.program.methods
105
- .writeData(offset, writeData.slice(offset, offset + writeLen))
106
- .accounts({
107
- signer,
108
- data: accountKey.publicKey
109
- })
110
- .instruction(), SolanaDataAccount.CUCosts.DATA_WRITE)
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
- return __awaiter(this, void 0, void 0, function* () {
136
- yield this.storage.init();
137
- const loadedData = yield this.storage.loadData(StoredDataAccount);
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
- return __awaiter(this, void 0, void 0, function* () {
152
- const closePublicKeys = [];
153
- let totalLocked = new BN(0);
154
- for (let key in this.storage.data) {
155
- const { accountKey, owner } = this.storage.data[key];
156
- if (!owner.equals(signer))
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
- catch (e) { }
145
+ closePublicKeys.push(accountKey);
146
+ totalLocked += BigInt(fetchedDataAccount.lamports);
168
147
  }
169
- return {
170
- closePublicKeys,
171
- count: closePublicKeys.length,
172
- totalValue: totalLocked
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
- return __awaiter(this, void 0, void 0, function* () {
181
- const { closePublicKeys, totalValue } = yield this.getDataAccountsInfo(signer.getPublicKey());
182
- if (closePublicKeys.length === 0) {
183
- this.logger.debug("sweepDataAccounts(): no old data accounts found, no need to close any!");
184
- return;
185
- }
186
- this.logger.debug("sweepDataAccounts(): closing old data accounts: ", closePublicKeys);
187
- let txns = [];
188
- for (let publicKey of closePublicKeys) {
189
- yield (yield this.CloseDataAccount(signer.getPublicKey(), publicKey)).addToTxs(txns);
190
- }
191
- const result = yield this.root.Transactions.sendAndConfirm(signer, txns, true, null, true);
192
- this.logger.info("sweepDataAccounts(): old data accounts closed: " +
193
- closePublicKeys.map(pk => pk.toBase58()).join());
194
- for (let publicKey of closePublicKeys) {
195
- yield this.removeDataAccount(publicKey);
196
- }
197
- return {
198
- txIds: result,
199
- count: closePublicKeys.length,
200
- totalValue: totalValue
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
- return __awaiter(this, void 0, void 0, function* () {
215
- let txDataKey;
216
- let fetchedDataAccount = null;
217
- if (signer instanceof SolanaSigner_1.SolanaSigner && signer.keypair != null) {
218
- txDataKey = this.SwapTxDataAlt(reversedTxId, signer.keypair);
219
- fetchedDataAccount = yield (0, Utils_1.tryWithRetries)(() => this.connection.getAccountInfo(txDataKey.publicKey), this.retryPolicy);
220
- }
221
- else {
222
- const secret = randomBytes(32);
223
- txDataKey = this.SwapTxDataAltBuffer(reversedTxId, secret);
224
- }
225
- const signerKey = signer instanceof SolanaSigner_1.SolanaSigner ? signer.getPublicKey() : signer;
226
- let pointer = 0;
227
- if (fetchedDataAccount == null) {
228
- const action = new SolanaAction_1.SolanaAction(signerKey, this.root);
229
- action.add(yield this.InitDataAccount(signerKey, txDataKey, writeData.length));
230
- const { bytesWritten, action: writeAction } = yield this.WriteData(signerKey, txDataKey, writeData, pointer, 420);
231
- this.logger.debug("addTxsWriteData(): Write partial data (" + pointer + " .. " + (pointer + bytesWritten) + ")/" + writeData.length +
232
- " key: " + txDataKey.publicKey.toBase58());
233
- pointer += bytesWritten;
234
- action.add(writeAction);
235
- yield action.addToTxs(txs, feeRate);
236
- yield this.saveDataAccount(signerKey, txDataKey.publicKey);
237
- }
238
- while (pointer < writeData.length) {
239
- const { bytesWritten, action } = yield this.WriteData(signerKey, txDataKey, writeData, pointer, 950);
240
- this.logger.debug("addTxsWriteData(): Write partial data (" + pointer + " .. " + (pointer + bytesWritten) + ")/" + writeData.length +
241
- " key: " + txDataKey.publicKey.toBase58());
242
- pointer += bytesWritten;
243
- yield action.addToTxs(txs, feeRate);
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: BN;
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<BN>;
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: BN, feeRate?: string): Promise<SolanaTx[]>;
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: BN, feeRate?: string): Promise<SolanaTx[]>;
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
- return __awaiter(this, void 0, void 0, function* () {
31
- const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(token, signer);
32
- return new SolanaAction_1.SolanaAction(signer, this.root, yield this.program.methods
33
- .withdraw(new BN(amount))
34
- .accounts({
35
- signer,
36
- signerAta: ata,
37
- userData: this.root.SwapUserVault(signer, token),
38
- vault: this.root.SwapVault(token),
39
- vaultAuthority: this.root.SwapVaultAuthority,
40
- mint: token,
41
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID
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
- return __awaiter(this, void 0, void 0, function* () {
57
- const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(token, signer);
58
- return new SolanaAction_1.SolanaAction(signer, this.root, yield this.program.methods
59
- .deposit(new BN(amount))
60
- .accounts({
61
- signer,
62
- signerAta: ata,
63
- userData: this.root.SwapUserVault(signer, token),
64
- vault: this.root.SwapVault(token),
65
- vaultAuthority: this.root.SwapVaultAuthority,
66
- mint: token,
67
- systemProgram: web3_js_1.SystemProgram.programId,
68
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID
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
- return __awaiter(this, void 0, void 0, function* () {
81
- const data = yield this.program.account.userAccount.fetchNullable(this.root.SwapUserVault(address, token));
82
- if (data == null)
83
- return null;
84
- const response = [];
85
- for (let i = 0; i < data.successVolume.length; i++) {
86
- response[i] = {
87
- successVolume: data.successVolume[i],
88
- successCount: data.successCount[i],
89
- failVolume: data.failVolume[i],
90
- failCount: data.failCount[i],
91
- coopCloseVolume: data.coopCloseVolume[i],
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
- return __awaiter(this, void 0, void 0, function* () {
109
- const intermediaryData = yield this.getIntermediaryData(address, token);
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
- return __awaiter(this, void 0, void 0, function* () {
121
- const intermediaryData = yield this.getIntermediaryData(address, token);
122
- const balance = intermediaryData === null || intermediaryData === void 0 ? void 0 : intermediaryData.balance;
123
- this.logger.debug("getIntermediaryBalance(): token LP balance fetched, token: " + token.toString() +
124
- " address: " + address + " amount: " + (balance == null ? "null" : balance.toString()));
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
- return __awaiter(this, void 0, void 0, function* () {
139
- const ata = yield (0, spl_token_1.getAssociatedTokenAddress)(token, signer);
140
- feeRate = feeRate || (yield this.getFeeRate(signer, token));
141
- const action = new SolanaAction_1.SolanaAction(signer, this.root);
142
- if (!(yield this.root.Tokens.ataExists(ata))) {
143
- action.add(this.root.Tokens.InitAta(signer, signer, token));
144
- }
145
- action.add(yield this.Withdraw(signer, token, amount));
146
- const shouldUnwrap = token.equals(this.root.Tokens.WSOL_ADDRESS);
147
- if (shouldUnwrap)
148
- action.add(this.root.Tokens.Unwrap(signer));
149
- this.logger.debug("txsWithdraw(): withdraw TX created, token: " + token.toString() +
150
- " amount: " + amount.toString(10) + " unwrapping: " + shouldUnwrap);
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
- return __awaiter(this, void 0, void 0, function* () {
164
- const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(token, signer);
165
- feeRate = feeRate || (yield this.getFeeRate(signer, token));
166
- const action = new SolanaAction_1.SolanaAction(signer, this.root);
167
- let wrapping = false;
168
- if (token.equals(this.root.Tokens.WSOL_ADDRESS)) {
169
- const account = yield (0, Utils_1.tryWithRetries)(() => this.root.Tokens.getATAOrNull(ata), this.retryPolicy);
170
- let balance = account == null ? new BN(0) : new BN(account.amount.toString());
171
- if (balance.lt(amount)) {
172
- action.add(this.root.Tokens.Wrap(signer, amount.sub(balance), account == null));
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
- action.addAction(yield this.Deposit(signer, token, amount));
177
- this.logger.debug("txsDeposit(): deposit TX created, token: " + token.toString() +
178
- " amount: " + amount.toString(10) + " wrapping: " + wrapping);
179
- return [yield action.tx(feeRate)];
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);