@atomiqlabs/chain-solana 13.5.13 → 13.5.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +73 -73
  3. package/dist/index.d.ts +81 -81
  4. package/dist/index.js +102 -102
  5. package/dist/node/index.d.ts +9 -9
  6. package/dist/node/index.js +13 -13
  7. package/dist/solana/SolanaChainType.d.ts +15 -15
  8. package/dist/solana/SolanaChainType.js +2 -2
  9. package/dist/solana/SolanaChains.d.ts +12 -12
  10. package/dist/solana/SolanaChains.js +45 -45
  11. package/dist/solana/SolanaInitializer.d.ts +94 -94
  12. package/dist/solana/SolanaInitializer.js +174 -174
  13. package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +222 -222
  14. package/dist/solana/btcrelay/SolanaBtcRelay.js +455 -455
  15. package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +84 -84
  16. package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +70 -70
  17. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +92 -92
  18. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +109 -109
  19. package/dist/solana/btcrelay/program/programIdl.json +671 -671
  20. package/dist/solana/chain/SolanaAction.d.ts +26 -26
  21. package/dist/solana/chain/SolanaAction.js +87 -87
  22. package/dist/solana/chain/SolanaChainInterface.d.ts +224 -224
  23. package/dist/solana/chain/SolanaChainInterface.js +275 -275
  24. package/dist/solana/chain/SolanaModule.d.ts +14 -14
  25. package/dist/solana/chain/SolanaModule.js +13 -13
  26. package/dist/solana/chain/modules/SolanaAddresses.d.ts +8 -8
  27. package/dist/solana/chain/modules/SolanaAddresses.js +22 -22
  28. package/dist/solana/chain/modules/SolanaBlocks.d.ts +32 -32
  29. package/dist/solana/chain/modules/SolanaBlocks.js +78 -78
  30. package/dist/solana/chain/modules/SolanaEvents.d.ts +68 -68
  31. package/dist/solana/chain/modules/SolanaEvents.js +238 -238
  32. package/dist/solana/chain/modules/SolanaFees.d.ts +189 -189
  33. package/dist/solana/chain/modules/SolanaFees.js +434 -434
  34. package/dist/solana/chain/modules/SolanaSignatures.d.ts +24 -24
  35. package/dist/solana/chain/modules/SolanaSignatures.js +39 -39
  36. package/dist/solana/chain/modules/SolanaSlots.d.ts +33 -33
  37. package/dist/solana/chain/modules/SolanaSlots.js +72 -72
  38. package/dist/solana/chain/modules/SolanaTokens.d.ts +123 -123
  39. package/dist/solana/chain/modules/SolanaTokens.js +242 -242
  40. package/dist/solana/chain/modules/SolanaTransactions.d.ts +149 -149
  41. package/dist/solana/chain/modules/SolanaTransactions.js +445 -445
  42. package/dist/solana/connection/ConnectionWithRetries.d.ts +35 -35
  43. package/dist/solana/connection/ConnectionWithRetries.js +86 -71
  44. package/dist/solana/events/SolanaChainEvents.d.ts +45 -45
  45. package/dist/solana/events/SolanaChainEvents.js +108 -108
  46. package/dist/solana/events/SolanaChainEventsBrowser.d.ts +205 -205
  47. package/dist/solana/events/SolanaChainEventsBrowser.js +404 -404
  48. package/dist/solana/program/SolanaProgramBase.d.ts +73 -73
  49. package/dist/solana/program/SolanaProgramBase.js +54 -54
  50. package/dist/solana/program/SolanaProgramModule.d.ts +8 -8
  51. package/dist/solana/program/SolanaProgramModule.js +11 -11
  52. package/dist/solana/program/modules/SolanaProgramEvents.d.ts +53 -53
  53. package/dist/solana/program/modules/SolanaProgramEvents.js +117 -117
  54. package/dist/solana/swaps/SolanaSwapData.d.ts +333 -333
  55. package/dist/solana/swaps/SolanaSwapData.js +535 -535
  56. package/dist/solana/swaps/SolanaSwapModule.d.ts +11 -11
  57. package/dist/solana/swaps/SolanaSwapModule.js +12 -12
  58. package/dist/solana/swaps/SolanaSwapProgram.d.ts +376 -376
  59. package/dist/solana/swaps/SolanaSwapProgram.js +769 -769
  60. package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -11
  61. package/dist/solana/swaps/SwapTypeEnum.js +43 -43
  62. package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +95 -95
  63. package/dist/solana/swaps/modules/SolanaDataAccount.js +232 -232
  64. package/dist/solana/swaps/modules/SolanaLpVault.d.ts +69 -69
  65. package/dist/solana/swaps/modules/SolanaLpVault.js +171 -171
  66. package/dist/solana/swaps/modules/SwapClaim.d.ts +126 -126
  67. package/dist/solana/swaps/modules/SwapClaim.js +294 -294
  68. package/dist/solana/swaps/modules/SwapInit.d.ts +213 -213
  69. package/dist/solana/swaps/modules/SwapInit.js +658 -658
  70. package/dist/solana/swaps/modules/SwapRefund.d.ts +87 -87
  71. package/dist/solana/swaps/modules/SwapRefund.js +293 -293
  72. package/dist/solana/swaps/programIdl.json +945 -945
  73. package/dist/solana/swaps/programTypes.d.ts +943 -943
  74. package/dist/solana/swaps/programTypes.js +945 -945
  75. package/dist/solana/swaps/v1/programIdl.json +945 -945
  76. package/dist/solana/swaps/v1/programTypes.d.ts +943 -943
  77. package/dist/solana/swaps/v1/programTypes.js +945 -945
  78. package/dist/solana/swaps/v2/programIdl.json +952 -952
  79. package/dist/solana/swaps/v2/programTypes.d.ts +950 -950
  80. package/dist/solana/swaps/v2/programTypes.js +952 -952
  81. package/dist/solana/wallet/SolanaKeypairWallet.d.ts +29 -29
  82. package/dist/solana/wallet/SolanaKeypairWallet.js +50 -50
  83. package/dist/solana/wallet/SolanaSigner.d.ts +30 -30
  84. package/dist/solana/wallet/SolanaSigner.js +30 -30
  85. package/dist/utils/Utils.d.ts +58 -58
  86. package/dist/utils/Utils.js +170 -170
  87. package/node/index.d.ts +1 -1
  88. package/node/index.js +3 -3
  89. package/package.json +46 -46
  90. package/src/index.ts +87 -87
  91. package/src/node/index.ts +9 -9
  92. package/src/solana/SolanaChainType.ts +32 -32
  93. package/src/solana/SolanaChains.ts +46 -46
  94. package/src/solana/SolanaInitializer.ts +278 -278
  95. package/src/solana/btcrelay/SolanaBtcRelay.ts +615 -615
  96. package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +116 -116
  97. package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +148 -148
  98. package/src/solana/btcrelay/program/programIdl.json +670 -670
  99. package/src/solana/chain/SolanaAction.ts +109 -109
  100. package/src/solana/chain/SolanaChainInterface.ts +404 -404
  101. package/src/solana/chain/SolanaModule.ts +20 -20
  102. package/src/solana/chain/modules/SolanaAddresses.ts +20 -20
  103. package/src/solana/chain/modules/SolanaBlocks.ts +89 -89
  104. package/src/solana/chain/modules/SolanaEvents.ts +271 -271
  105. package/src/solana/chain/modules/SolanaFees.ts +522 -522
  106. package/src/solana/chain/modules/SolanaSignatures.ts +39 -39
  107. package/src/solana/chain/modules/SolanaSlots.ts +85 -85
  108. package/src/solana/chain/modules/SolanaTokens.ts +300 -300
  109. package/src/solana/chain/modules/SolanaTransactions.ts +503 -503
  110. package/src/solana/connection/ConnectionWithRetries.ts +113 -96
  111. package/src/solana/events/SolanaChainEvents.ts +127 -127
  112. package/src/solana/events/SolanaChainEventsBrowser.ts +495 -495
  113. package/src/solana/program/SolanaProgramBase.ts +119 -119
  114. package/src/solana/program/SolanaProgramModule.ts +15 -15
  115. package/src/solana/program/modules/SolanaProgramEvents.ts +157 -157
  116. package/src/solana/swaps/SolanaSwapData.ts +735 -735
  117. package/src/solana/swaps/SolanaSwapModule.ts +19 -19
  118. package/src/solana/swaps/SolanaSwapProgram.ts +1074 -1074
  119. package/src/solana/swaps/SwapTypeEnum.ts +30 -30
  120. package/src/solana/swaps/modules/SolanaDataAccount.ts +302 -302
  121. package/src/solana/swaps/modules/SolanaLpVault.ts +208 -208
  122. package/src/solana/swaps/modules/SwapClaim.ts +387 -387
  123. package/src/solana/swaps/modules/SwapInit.ts +785 -785
  124. package/src/solana/swaps/modules/SwapRefund.ts +353 -353
  125. package/src/solana/swaps/v1/programIdl.json +944 -944
  126. package/src/solana/swaps/v1/programTypes.ts +1885 -1885
  127. package/src/solana/swaps/v2/programIdl.json +951 -951
  128. package/src/solana/swaps/v2/programTypes.ts +1899 -1899
  129. package/src/solana/wallet/SolanaKeypairWallet.ts +56 -56
  130. package/src/solana/wallet/SolanaSigner.ts +43 -43
  131. package/src/utils/Utils.ts +194 -194
@@ -1,242 +1,242 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SolanaTokens = void 0;
4
- const SolanaModule_1 = require("../SolanaModule");
5
- const web3_js_1 = require("@solana/web3.js");
6
- const spl_token_1 = require("@solana/spl-token");
7
- const SolanaAction_1 = require("../SolanaAction");
8
- class SolanaTokens extends SolanaModule_1.SolanaModule {
9
- /**
10
- * Creates an ATA for a specific public key & token, the ATA creation is paid for by the underlying provider's
11
- * public key
12
- *
13
- * @param signer
14
- * @param publicKey public key address of the user for which to initiate the ATA
15
- * @param token token identification for which the ATA should be initialized
16
- * @param requiredAta optional required ata address to use, if the address doesn't match it returns null
17
- */
18
- InitAta(signer, publicKey, token, requiredAta) {
19
- const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(token, publicKey, true);
20
- if (requiredAta != null && !ata.equals(requiredAta))
21
- return null;
22
- return new SolanaAction_1.SolanaAction(signer, this.root, (0, spl_token_1.createAssociatedTokenAccountInstruction)(signer, ata, publicKey, token), SolanaTokens.CUCosts.ATA_INIT);
23
- }
24
- /**
25
- * Action for wrapping SOL to WSOL for a specific public key
26
- *
27
- * @param publicKey public key of the user for which to wrap the SOL
28
- * @param amount amount of SOL in lamports (smallest unit) to wrap
29
- * @param initAta whether we should also initialize the ATA before depositing SOL
30
- */
31
- Wrap(publicKey, amount, initAta) {
32
- const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(SolanaTokens.WSOL_ADDRESS, publicKey, true);
33
- const action = new SolanaAction_1.SolanaAction(publicKey, this.root);
34
- if (initAta)
35
- action.addIx((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, ata, publicKey, SolanaTokens.WSOL_ADDRESS), SolanaTokens.CUCosts.ATA_INIT);
36
- action.addIx(web3_js_1.SystemProgram.transfer({
37
- fromPubkey: publicKey,
38
- toPubkey: ata,
39
- lamports: amount
40
- }), SolanaTokens.CUCosts.WRAP_SOL);
41
- action.addIx((0, spl_token_1.createSyncNativeInstruction)(ata));
42
- return action;
43
- }
44
- /**
45
- * Action for unwrapping WSOL to SOL for a specific public key
46
- *
47
- * @param publicKey public key of the user for which to unwrap the sol
48
- */
49
- Unwrap(publicKey) {
50
- const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(SolanaTokens.WSOL_ADDRESS, publicKey, true);
51
- return new SolanaAction_1.SolanaAction(publicKey, this.root, (0, spl_token_1.createCloseAccountInstruction)(ata, publicKey, publicKey), SolanaTokens.CUCosts.ATA_CLOSE);
52
- }
53
- /**
54
- * Action for transferring the native SOL token, uses provider's public key as a sender
55
- *
56
- * @param signer
57
- * @param recipient
58
- * @param amount
59
- * @private
60
- */
61
- SolTransfer(signer, recipient, amount) {
62
- return new SolanaAction_1.SolanaAction(signer, this.root, web3_js_1.SystemProgram.transfer({
63
- fromPubkey: signer,
64
- toPubkey: recipient,
65
- lamports: amount
66
- }), SolanaTokens.CUCosts.TRANSFER_SOL);
67
- }
68
- /**
69
- * Action for transferring the SPL token, uses provider's public key as a sender
70
- *
71
- * @param signer
72
- * @param recipient
73
- * @param token
74
- * @param amount
75
- * @private
76
- */
77
- Transfer(signer, recipient, token, amount) {
78
- const srcAta = (0, spl_token_1.getAssociatedTokenAddressSync)(token, signer, true);
79
- const dstAta = (0, spl_token_1.getAssociatedTokenAddressSync)(token, recipient, true);
80
- return new SolanaAction_1.SolanaAction(signer, this.root, (0, spl_token_1.createTransferInstruction)(srcAta, dstAta, signer, amount), SolanaTokens.CUCosts.TRANSFER);
81
- }
82
- /**
83
- * Creates transactions for sending SOL (the native token)
84
- *
85
- * @param signer
86
- * @param amount amount of the SOL in lamports (smallest unit) to send
87
- * @param recipient recipient's address
88
- * @param feeRate fee rate to use for the transactions
89
- * @private
90
- */
91
- async txsTransferSol(signer, amount, recipient, feeRate) {
92
- const wsolAta = (0, spl_token_1.getAssociatedTokenAddressSync)(SolanaTokens.WSOL_ADDRESS, signer, true);
93
- const shouldUnwrap = await this.ataExists(wsolAta);
94
- const action = new SolanaAction_1.SolanaAction(signer, this.root);
95
- if (shouldUnwrap) {
96
- feeRate = feeRate || await this.root.Fees.getFeeRate([signer, recipient, wsolAta]);
97
- action.add(this.Unwrap(signer));
98
- }
99
- else {
100
- feeRate = feeRate || await this.root.Fees.getFeeRate([signer, recipient]);
101
- }
102
- action.add(this.SolTransfer(signer, recipient, amount));
103
- this.logger.debug("txsTransferSol(): transfer native solana TX created, recipient: " + recipient.toString() +
104
- " amount: " + amount.toString(10) + " unwrapping: " + shouldUnwrap);
105
- return [await action.tx(feeRate)];
106
- }
107
- /**
108
- * Creates transactions for sending the over the tokens
109
- *
110
- * @param signer
111
- * @param token token to send
112
- * @param amount amount of the token to send
113
- * @param recipient recipient's address
114
- * @param feeRate fee rate to use for the transactions
115
- * @private
116
- */
117
- async txsTransferTokens(signer, token, amount, recipient, feeRate) {
118
- const srcAta = (0, spl_token_1.getAssociatedTokenAddressSync)(token, signer, true);
119
- const dstAta = (0, spl_token_1.getAssociatedTokenAddressSync)(token, recipient, true);
120
- feeRate = feeRate || await this.root.Fees.getFeeRate([signer, srcAta, dstAta]);
121
- const initAta = !await this.ataExists(dstAta);
122
- const action = new SolanaAction_1.SolanaAction(signer, this.root);
123
- if (initAta) {
124
- action.add(this.InitAta(signer, recipient, token));
125
- }
126
- action.add(this.Transfer(signer, recipient, token, amount));
127
- this.logger.debug("txsTransferTokens(): transfer TX created, recipient: " + recipient.toString() +
128
- " token: " + token.toString() + " amount: " + amount.toString(10) + " initAta: " + initAta);
129
- return [await action.tx(feeRate)];
130
- }
131
- ///////////////////
132
- //// Tokens
133
- /**
134
- * Checks if the provided string is a valid solana token
135
- *
136
- * @param token
137
- */
138
- isValidToken(token) {
139
- try {
140
- new web3_js_1.PublicKey(token);
141
- return true;
142
- }
143
- catch (e) {
144
- return false;
145
- }
146
- }
147
- /**
148
- * Returns the specific ATA or null if it doesn't exist
149
- *
150
- * @param ata
151
- */
152
- getATAOrNull(ata) {
153
- return (0, spl_token_1.getAccount)(this.connection, ata).catch(e => {
154
- if (e instanceof spl_token_1.TokenAccountNotFoundError)
155
- return null;
156
- throw e;
157
- });
158
- }
159
- /**
160
- * Checks whether the specific ATA exists, uses tryWithRetries so retries on failure
161
- *
162
- * @param ata
163
- */
164
- async ataExists(ata) {
165
- const account = await this.getATAOrNull(ata);
166
- return account != null;
167
- }
168
- /**
169
- * Returns the rent exempt deposit required to initiate the ATA
170
- */
171
- getATARentExemptLamports() {
172
- return Promise.resolve(BigInt(SolanaTokens.SPL_ATA_RENT_EXEMPT));
173
- }
174
- /**
175
- * Returns the token balance of the public key
176
- *
177
- * @param publicKey
178
- * @param token
179
- */
180
- async getTokenBalance(publicKey, token) {
181
- const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(token, publicKey, true);
182
- const [ataAccount, balance] = await Promise.all([
183
- this.getATAOrNull(ata),
184
- (token != null && token.equals(SolanaTokens.WSOL_ADDRESS)) ? this.connection.getBalance(publicKey) : Promise.resolve(null)
185
- ]);
186
- let ataExists = ataAccount != null;
187
- let sum = 0n;
188
- if (ataExists) {
189
- sum += ataAccount.amount;
190
- }
191
- if (balance != null) {
192
- let balanceLamports = BigInt(balance);
193
- if (!ataExists)
194
- balanceLamports = balanceLamports - await this.getATARentExemptLamports();
195
- if (balanceLamports >= 0n)
196
- sum += balanceLamports;
197
- }
198
- this.logger.debug("getTokenBalance(): token balance fetched, token: " + token.toString() +
199
- " address: " + publicKey.toString() + " amount: " + sum.toString());
200
- return { balance: sum, ataExists };
201
- }
202
- /**
203
- * Returns the native currency address, we use WSOL address as placeholder for SOL
204
- */
205
- getNativeCurrencyAddress() {
206
- return SolanaTokens.WSOL_ADDRESS;
207
- }
208
- /**
209
- * Parses string base58 representation of the token address to a PublicKey object
210
- * @param address
211
- */
212
- toTokenAddress(address) {
213
- return new web3_js_1.PublicKey(address);
214
- }
215
- ///////////////////
216
- //// Transfers
217
- /**
218
- * Create transactions for sending a specific token to a destination address
219
- *
220
- * @param signer
221
- * @param token token to use for the transfer
222
- * @param amount amount of token in base units to transfer
223
- * @param dstAddress destination address of the recipient
224
- * @param feeRate fee rate to use for the transaction
225
- */
226
- txsTransfer(signer, token, amount, dstAddress, feeRate) {
227
- if (SolanaTokens.WSOL_ADDRESS.equals(token)) {
228
- return this.txsTransferSol(signer, amount, dstAddress, feeRate);
229
- }
230
- return this.txsTransferTokens(signer, token, amount, dstAddress, feeRate);
231
- }
232
- }
233
- exports.SolanaTokens = SolanaTokens;
234
- SolanaTokens.CUCosts = {
235
- WRAP_SOL: 10000,
236
- ATA_CLOSE: 10000,
237
- ATA_INIT: 40000,
238
- TRANSFER: 50000,
239
- TRANSFER_SOL: 5000
240
- };
241
- SolanaTokens.WSOL_ADDRESS = new web3_js_1.PublicKey("So11111111111111111111111111111111111111112");
242
- SolanaTokens.SPL_ATA_RENT_EXEMPT = 2039280;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SolanaTokens = void 0;
4
+ const SolanaModule_1 = require("../SolanaModule");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ const spl_token_1 = require("@solana/spl-token");
7
+ const SolanaAction_1 = require("../SolanaAction");
8
+ class SolanaTokens extends SolanaModule_1.SolanaModule {
9
+ /**
10
+ * Creates an ATA for a specific public key & token, the ATA creation is paid for by the underlying provider's
11
+ * public key
12
+ *
13
+ * @param signer
14
+ * @param publicKey public key address of the user for which to initiate the ATA
15
+ * @param token token identification for which the ATA should be initialized
16
+ * @param requiredAta optional required ata address to use, if the address doesn't match it returns null
17
+ */
18
+ InitAta(signer, publicKey, token, requiredAta) {
19
+ const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(token, publicKey, true);
20
+ if (requiredAta != null && !ata.equals(requiredAta))
21
+ return null;
22
+ return new SolanaAction_1.SolanaAction(signer, this.root, (0, spl_token_1.createAssociatedTokenAccountInstruction)(signer, ata, publicKey, token), SolanaTokens.CUCosts.ATA_INIT);
23
+ }
24
+ /**
25
+ * Action for wrapping SOL to WSOL for a specific public key
26
+ *
27
+ * @param publicKey public key of the user for which to wrap the SOL
28
+ * @param amount amount of SOL in lamports (smallest unit) to wrap
29
+ * @param initAta whether we should also initialize the ATA before depositing SOL
30
+ */
31
+ Wrap(publicKey, amount, initAta) {
32
+ const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(SolanaTokens.WSOL_ADDRESS, publicKey, true);
33
+ const action = new SolanaAction_1.SolanaAction(publicKey, this.root);
34
+ if (initAta)
35
+ action.addIx((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, ata, publicKey, SolanaTokens.WSOL_ADDRESS), SolanaTokens.CUCosts.ATA_INIT);
36
+ action.addIx(web3_js_1.SystemProgram.transfer({
37
+ fromPubkey: publicKey,
38
+ toPubkey: ata,
39
+ lamports: amount
40
+ }), SolanaTokens.CUCosts.WRAP_SOL);
41
+ action.addIx((0, spl_token_1.createSyncNativeInstruction)(ata));
42
+ return action;
43
+ }
44
+ /**
45
+ * Action for unwrapping WSOL to SOL for a specific public key
46
+ *
47
+ * @param publicKey public key of the user for which to unwrap the sol
48
+ */
49
+ Unwrap(publicKey) {
50
+ const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(SolanaTokens.WSOL_ADDRESS, publicKey, true);
51
+ return new SolanaAction_1.SolanaAction(publicKey, this.root, (0, spl_token_1.createCloseAccountInstruction)(ata, publicKey, publicKey), SolanaTokens.CUCosts.ATA_CLOSE);
52
+ }
53
+ /**
54
+ * Action for transferring the native SOL token, uses provider's public key as a sender
55
+ *
56
+ * @param signer
57
+ * @param recipient
58
+ * @param amount
59
+ * @private
60
+ */
61
+ SolTransfer(signer, recipient, amount) {
62
+ return new SolanaAction_1.SolanaAction(signer, this.root, web3_js_1.SystemProgram.transfer({
63
+ fromPubkey: signer,
64
+ toPubkey: recipient,
65
+ lamports: amount
66
+ }), SolanaTokens.CUCosts.TRANSFER_SOL);
67
+ }
68
+ /**
69
+ * Action for transferring the SPL token, uses provider's public key as a sender
70
+ *
71
+ * @param signer
72
+ * @param recipient
73
+ * @param token
74
+ * @param amount
75
+ * @private
76
+ */
77
+ Transfer(signer, recipient, token, amount) {
78
+ const srcAta = (0, spl_token_1.getAssociatedTokenAddressSync)(token, signer, true);
79
+ const dstAta = (0, spl_token_1.getAssociatedTokenAddressSync)(token, recipient, true);
80
+ return new SolanaAction_1.SolanaAction(signer, this.root, (0, spl_token_1.createTransferInstruction)(srcAta, dstAta, signer, amount), SolanaTokens.CUCosts.TRANSFER);
81
+ }
82
+ /**
83
+ * Creates transactions for sending SOL (the native token)
84
+ *
85
+ * @param signer
86
+ * @param amount amount of the SOL in lamports (smallest unit) to send
87
+ * @param recipient recipient's address
88
+ * @param feeRate fee rate to use for the transactions
89
+ * @private
90
+ */
91
+ async txsTransferSol(signer, amount, recipient, feeRate) {
92
+ const wsolAta = (0, spl_token_1.getAssociatedTokenAddressSync)(SolanaTokens.WSOL_ADDRESS, signer, true);
93
+ const shouldUnwrap = await this.ataExists(wsolAta);
94
+ const action = new SolanaAction_1.SolanaAction(signer, this.root);
95
+ if (shouldUnwrap) {
96
+ feeRate = feeRate || await this.root.Fees.getFeeRate([signer, recipient, wsolAta]);
97
+ action.add(this.Unwrap(signer));
98
+ }
99
+ else {
100
+ feeRate = feeRate || await this.root.Fees.getFeeRate([signer, recipient]);
101
+ }
102
+ action.add(this.SolTransfer(signer, recipient, amount));
103
+ this.logger.debug("txsTransferSol(): transfer native solana TX created, recipient: " + recipient.toString() +
104
+ " amount: " + amount.toString(10) + " unwrapping: " + shouldUnwrap);
105
+ return [await action.tx(feeRate)];
106
+ }
107
+ /**
108
+ * Creates transactions for sending the over the tokens
109
+ *
110
+ * @param signer
111
+ * @param token token to send
112
+ * @param amount amount of the token to send
113
+ * @param recipient recipient's address
114
+ * @param feeRate fee rate to use for the transactions
115
+ * @private
116
+ */
117
+ async txsTransferTokens(signer, token, amount, recipient, feeRate) {
118
+ const srcAta = (0, spl_token_1.getAssociatedTokenAddressSync)(token, signer, true);
119
+ const dstAta = (0, spl_token_1.getAssociatedTokenAddressSync)(token, recipient, true);
120
+ feeRate = feeRate || await this.root.Fees.getFeeRate([signer, srcAta, dstAta]);
121
+ const initAta = !await this.ataExists(dstAta);
122
+ const action = new SolanaAction_1.SolanaAction(signer, this.root);
123
+ if (initAta) {
124
+ action.add(this.InitAta(signer, recipient, token));
125
+ }
126
+ action.add(this.Transfer(signer, recipient, token, amount));
127
+ this.logger.debug("txsTransferTokens(): transfer TX created, recipient: " + recipient.toString() +
128
+ " token: " + token.toString() + " amount: " + amount.toString(10) + " initAta: " + initAta);
129
+ return [await action.tx(feeRate)];
130
+ }
131
+ ///////////////////
132
+ //// Tokens
133
+ /**
134
+ * Checks if the provided string is a valid solana token
135
+ *
136
+ * @param token
137
+ */
138
+ isValidToken(token) {
139
+ try {
140
+ new web3_js_1.PublicKey(token);
141
+ return true;
142
+ }
143
+ catch (e) {
144
+ return false;
145
+ }
146
+ }
147
+ /**
148
+ * Returns the specific ATA or null if it doesn't exist
149
+ *
150
+ * @param ata
151
+ */
152
+ getATAOrNull(ata) {
153
+ return (0, spl_token_1.getAccount)(this.connection, ata).catch(e => {
154
+ if (e instanceof spl_token_1.TokenAccountNotFoundError)
155
+ return null;
156
+ throw e;
157
+ });
158
+ }
159
+ /**
160
+ * Checks whether the specific ATA exists, uses tryWithRetries so retries on failure
161
+ *
162
+ * @param ata
163
+ */
164
+ async ataExists(ata) {
165
+ const account = await this.getATAOrNull(ata);
166
+ return account != null;
167
+ }
168
+ /**
169
+ * Returns the rent exempt deposit required to initiate the ATA
170
+ */
171
+ getATARentExemptLamports() {
172
+ return Promise.resolve(BigInt(SolanaTokens.SPL_ATA_RENT_EXEMPT));
173
+ }
174
+ /**
175
+ * Returns the token balance of the public key
176
+ *
177
+ * @param publicKey
178
+ * @param token
179
+ */
180
+ async getTokenBalance(publicKey, token) {
181
+ const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(token, publicKey, true);
182
+ const [ataAccount, balance] = await Promise.all([
183
+ this.getATAOrNull(ata),
184
+ (token != null && token.equals(SolanaTokens.WSOL_ADDRESS)) ? this.connection.getBalance(publicKey) : Promise.resolve(null)
185
+ ]);
186
+ let ataExists = ataAccount != null;
187
+ let sum = 0n;
188
+ if (ataExists) {
189
+ sum += ataAccount.amount;
190
+ }
191
+ if (balance != null) {
192
+ let balanceLamports = BigInt(balance);
193
+ if (!ataExists)
194
+ balanceLamports = balanceLamports - await this.getATARentExemptLamports();
195
+ if (balanceLamports >= 0n)
196
+ sum += balanceLamports;
197
+ }
198
+ this.logger.debug("getTokenBalance(): token balance fetched, token: " + token.toString() +
199
+ " address: " + publicKey.toString() + " amount: " + sum.toString());
200
+ return { balance: sum, ataExists };
201
+ }
202
+ /**
203
+ * Returns the native currency address, we use WSOL address as placeholder for SOL
204
+ */
205
+ getNativeCurrencyAddress() {
206
+ return SolanaTokens.WSOL_ADDRESS;
207
+ }
208
+ /**
209
+ * Parses string base58 representation of the token address to a PublicKey object
210
+ * @param address
211
+ */
212
+ toTokenAddress(address) {
213
+ return new web3_js_1.PublicKey(address);
214
+ }
215
+ ///////////////////
216
+ //// Transfers
217
+ /**
218
+ * Create transactions for sending a specific token to a destination address
219
+ *
220
+ * @param signer
221
+ * @param token token to use for the transfer
222
+ * @param amount amount of token in base units to transfer
223
+ * @param dstAddress destination address of the recipient
224
+ * @param feeRate fee rate to use for the transaction
225
+ */
226
+ txsTransfer(signer, token, amount, dstAddress, feeRate) {
227
+ if (SolanaTokens.WSOL_ADDRESS.equals(token)) {
228
+ return this.txsTransferSol(signer, amount, dstAddress, feeRate);
229
+ }
230
+ return this.txsTransferTokens(signer, token, amount, dstAddress, feeRate);
231
+ }
232
+ }
233
+ exports.SolanaTokens = SolanaTokens;
234
+ SolanaTokens.CUCosts = {
235
+ WRAP_SOL: 10000,
236
+ ATA_CLOSE: 10000,
237
+ ATA_INIT: 40000,
238
+ TRANSFER: 50000,
239
+ TRANSFER_SOL: 5000
240
+ };
241
+ SolanaTokens.WSOL_ADDRESS = new web3_js_1.PublicKey("So11111111111111111111111111111111111111112");
242
+ SolanaTokens.SPL_ATA_RENT_EXEMPT = 2039280;