@atomiqlabs/chain-solana 7.3.7 → 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 +36 -39
  56. package/src/solana/swaps/modules/SwapRefund.ts +20 -23
  57. package/src/utils/Utils.ts +35 -0
package/dist/index.d.ts CHANGED
@@ -25,4 +25,5 @@ export * from "./solana/swaps/modules/SwapRefund";
25
25
  export * from "./solana/wallet/SolanaKeypairWallet";
26
26
  export * from "./solana/wallet/SolanaSigner";
27
27
  export * from "./solana/SolanaChainType";
28
+ export * from "./solana/SolanaInitializer";
28
29
  export * from "./utils/Utils";
package/dist/index.js CHANGED
@@ -41,4 +41,5 @@ __exportStar(require("./solana/swaps/modules/SwapRefund"), exports);
41
41
  __exportStar(require("./solana/wallet/SolanaKeypairWallet"), exports);
42
42
  __exportStar(require("./solana/wallet/SolanaSigner"), exports);
43
43
  __exportStar(require("./solana/SolanaChainType"), exports);
44
+ __exportStar(require("./solana/SolanaInitializer"), exports);
44
45
  __exportStar(require("./utils/Utils"), exports);
@@ -0,0 +1,14 @@
1
+ export declare const SolanaChains: {
2
+ readonly 1: {
3
+ readonly addresses: {
4
+ readonly swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM";
5
+ readonly btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc";
6
+ };
7
+ };
8
+ readonly 0: {
9
+ readonly addresses: {
10
+ readonly swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM";
11
+ readonly btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc";
12
+ };
13
+ };
14
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SolanaChains = void 0;
4
+ const base_1 = require("@atomiqlabs/base");
5
+ exports.SolanaChains = {
6
+ [base_1.BitcoinNetwork.TESTNET]: {
7
+ addresses: {
8
+ swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM",
9
+ btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc"
10
+ }
11
+ },
12
+ [base_1.BitcoinNetwork.MAINNET]: {
13
+ addresses: {
14
+ swapContract: "4hfUykhqmD7ZRvNh1HuzVKEY7ToENixtdUKZspNDCrEM",
15
+ btcRelayContract: "3KHSHFpEK6bsjg3bqcxQ9qssJYtRCMi2S9TYVe4q6CQc"
16
+ }
17
+ }
18
+ };
@@ -0,0 +1,18 @@
1
+ import { BaseTokenType, BitcoinNetwork, BitcoinRpc, ChainData, ChainInitializer, IStorageManager, StorageObject } from "@atomiqlabs/base";
2
+ import { Connection } from "@solana/web3.js";
3
+ import { StoredDataAccount } from "./swaps/modules/SolanaDataAccount";
4
+ import { SolanaRetryPolicy } from "./base/SolanaBase";
5
+ import { SolanaFees } from "./base/modules/SolanaFees";
6
+ import { SolanaChainType } from "./SolanaChainType";
7
+ export type SolanaAssetsType = BaseTokenType<"WBTC" | "USDC" | "USDT" | "SOL" | "BONK">;
8
+ export type SolanaSwapperOptions = {
9
+ rpcUrl: string | Connection;
10
+ dataAccountStorage?: IStorageManager<StoredDataAccount>;
11
+ retryPolicy?: SolanaRetryPolicy;
12
+ btcRelayContract?: string;
13
+ swapContract?: string;
14
+ fees?: SolanaFees;
15
+ };
16
+ export declare function initializeSolana(options: SolanaSwapperOptions, bitcoinRpc: BitcoinRpc<any>, network: BitcoinNetwork, storageCtor: <T extends StorageObject>(name: string) => IStorageManager<T>): ChainData<SolanaChainType>;
17
+ export type SolanaInitializerType = ChainInitializer<SolanaSwapperOptions, SolanaChainType, SolanaAssetsType>;
18
+ export declare const SolanaInitializer: SolanaInitializerType;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SolanaInitializer = exports.initializeSolana = void 0;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ const SolanaFees_1 = require("./base/modules/SolanaFees");
6
+ const SolanaBtcRelay_1 = require("./btcrelay/SolanaBtcRelay");
7
+ const SolanaChains_1 = require("./SolanaChains");
8
+ const SolanaSwapProgram_1 = require("./swaps/SolanaSwapProgram");
9
+ const SolanaChainEventsBrowser_1 = require("./events/SolanaChainEventsBrowser");
10
+ const SolanaSwapData_1 = require("./swaps/SolanaSwapData");
11
+ const chainId = "SOLANA";
12
+ const SolanaAssets = {
13
+ WBTC: {
14
+ address: "3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh",
15
+ decimals: 8
16
+ },
17
+ USDC: {
18
+ address: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
19
+ decimals: 6
20
+ },
21
+ USDT: {
22
+ address: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB",
23
+ decimals: 6
24
+ },
25
+ SOL: {
26
+ address: "So11111111111111111111111111111111111111112",
27
+ decimals: 9
28
+ },
29
+ BONK: {
30
+ address: "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263",
31
+ decimals: 5
32
+ }
33
+ };
34
+ function initializeSolana(options, bitcoinRpc, network, storageCtor) {
35
+ const connection = typeof (options.rpcUrl) === "string" ?
36
+ new web3_js_1.Connection(options.rpcUrl) :
37
+ options.rpcUrl;
38
+ const Fees = options.fees ?? new SolanaFees_1.SolanaFees(connection, 200000, 4, 100);
39
+ const btcRelay = new SolanaBtcRelay_1.SolanaBtcRelay(connection, bitcoinRpc, options.btcRelayContract ?? SolanaChains_1.SolanaChains[network].addresses.btcRelayContract, Fees);
40
+ const swapContract = new SolanaSwapProgram_1.SolanaSwapProgram(connection, btcRelay, options.dataAccountStorage || storageCtor("solAccounts"), options.swapContract ?? SolanaChains_1.SolanaChains[network].addresses.swapContract, options.retryPolicy ?? { transactionResendInterval: 1000 }, Fees);
41
+ const chainEvents = new SolanaChainEventsBrowser_1.SolanaChainEventsBrowser(connection, swapContract);
42
+ return {
43
+ chainId,
44
+ btcRelay,
45
+ swapContract,
46
+ chainEvents,
47
+ swapDataConstructor: SolanaSwapData_1.SolanaSwapData,
48
+ //These are defined here to keep the data from old SolLightning-sdk, not needed for other chains
49
+ storagePrefix: "SOLv4-" + network + "-"
50
+ };
51
+ }
52
+ exports.initializeSolana = initializeSolana;
53
+ exports.SolanaInitializer = {
54
+ chainId,
55
+ chainType: null,
56
+ initializer: initializeSolana,
57
+ tokens: SolanaAssets,
58
+ options: null
59
+ };
@@ -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.SolanaAction = void 0;
13
4
  const web3_js_1 = require("@solana/web3.js");
@@ -61,36 +52,32 @@ class SolanaAction {
61
52
  this.feeRate = action.feeRate;
62
53
  return this;
63
54
  }
64
- tx(feeRate, block) {
65
- return __awaiter(this, void 0, void 0, function* () {
66
- const tx = new web3_js_1.Transaction();
67
- tx.feePayer = this.mainSigner;
68
- if (feeRate == null)
69
- feeRate = this.feeRate;
70
- if (feeRate == null)
71
- feeRate = yield this.estimateFee();
72
- let instructions = this.instructions;
73
- if (instructions.length > 0 && this.firstIxBeforeComputeBudget) {
74
- tx.add(this.instructions[0]);
75
- instructions = this.instructions.slice(1);
76
- }
77
- this.root.Fees.applyFeeRateBegin(tx, this.computeBudget, feeRate);
78
- instructions.forEach(ix => tx.add(ix));
79
- this.root.Fees.applyFeeRateEnd(tx, this.computeBudget, feeRate);
80
- if (block != null) {
81
- tx.recentBlockhash = block.blockhash;
82
- tx.lastValidBlockHeight = block.blockHeight + this.root.TX_SLOT_VALIDITY;
83
- }
84
- return {
85
- tx,
86
- signers: this.signers
87
- };
88
- });
55
+ async tx(feeRate, block) {
56
+ const tx = new web3_js_1.Transaction();
57
+ tx.feePayer = this.mainSigner;
58
+ if (feeRate == null)
59
+ feeRate = this.feeRate;
60
+ if (feeRate == null)
61
+ feeRate = await this.estimateFee();
62
+ let instructions = this.instructions;
63
+ if (instructions.length > 0 && this.firstIxBeforeComputeBudget) {
64
+ tx.add(this.instructions[0]);
65
+ instructions = this.instructions.slice(1);
66
+ }
67
+ this.root.Fees.applyFeeRateBegin(tx, this.computeBudget, feeRate);
68
+ instructions.forEach(ix => tx.add(ix));
69
+ this.root.Fees.applyFeeRateEnd(tx, this.computeBudget, feeRate);
70
+ if (block != null) {
71
+ tx.recentBlockhash = block.blockhash;
72
+ tx.lastValidBlockHeight = block.blockHeight + this.root.TX_SLOT_VALIDITY;
73
+ }
74
+ return {
75
+ tx,
76
+ signers: this.signers
77
+ };
89
78
  }
90
- addToTxs(txs, feeRate, block) {
91
- return __awaiter(this, void 0, void 0, function* () {
92
- txs.push(yield this.tx(feeRate, block));
93
- });
79
+ async addToTxs(txs, feeRate, block) {
80
+ txs.push(await this.tx(feeRate, block));
94
81
  }
95
82
  ixsLength() {
96
83
  return this.instructions.length;
@@ -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.SolanaBlocks = void 0;
13
4
  const SolanaModule_1 = require("../SolanaModule");
@@ -44,28 +35,26 @@ class SolanaBlocks extends SolanaModule_1.SolanaModule {
44
35
  *
45
36
  * @param commitment
46
37
  */
47
- findLatestParsedBlock(commitment) {
48
- return __awaiter(this, void 0, void 0, function* () {
49
- let slot = yield this.root.Slots.getSlot(commitment);
50
- for (let i = 0; i < 10; i++) {
51
- const block = yield this.getParsedBlock(slot).catch(e => {
52
- if (e.toString().startsWith("SolanaJSONRPCError: failed to get block: Block not available for slot")) {
53
- return null;
54
- }
55
- throw e;
56
- });
57
- if (block != null) {
58
- this.logger.debug("findLatestParsedBlock(): Found valid block, slot: " + slot +
59
- " blockhash: " + block.blockhash + " tries: " + i);
60
- return {
61
- block,
62
- slot
63
- };
38
+ async findLatestParsedBlock(commitment) {
39
+ let slot = await this.root.Slots.getSlot(commitment);
40
+ for (let i = 0; i < 10; i++) {
41
+ const block = await this.getParsedBlock(slot).catch(e => {
42
+ if (e.toString().startsWith("SolanaJSONRPCError: failed to get block: Block not available for slot")) {
43
+ return null;
64
44
  }
65
- slot--;
45
+ throw e;
46
+ });
47
+ if (block != null) {
48
+ this.logger.debug("findLatestParsedBlock(): Found valid block, slot: " + slot +
49
+ " blockhash: " + block.blockhash + " tries: " + i);
50
+ return {
51
+ block,
52
+ slot
53
+ };
66
54
  }
67
- throw new Error("Ran out of tries trying to find a parsedBlock");
68
- });
55
+ slot--;
56
+ }
57
+ throw new Error("Ran out of tries trying to find a parsedBlock");
69
58
  }
70
59
  /**
71
60
  * Gets parsed block for a given slot, uses block cache if the block was already fetched before
@@ -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.SolanaEvents = void 0;
13
4
  const SolanaModule_1 = require("../SolanaModule");
@@ -47,23 +38,21 @@ class SolanaEvents extends SolanaModule_1.SolanaModule {
47
38
  * @param abortSignal
48
39
  * @param logFetchLimit
49
40
  */
50
- findInSignatures(topicKey, processor, abortSignal, logFetchLimit) {
51
- return __awaiter(this, void 0, void 0, function* () {
52
- if (logFetchLimit == null || logFetchLimit > this.LOG_FETCH_LIMIT)
53
- logFetchLimit = this.LOG_FETCH_LIMIT;
54
- let signatures = null;
55
- while (signatures == null || signatures.length > 0) {
56
- signatures = yield this.getSignatures(topicKey, logFetchLimit, signatures != null ? signatures[signatures.length - 1].signature : null);
57
- if (abortSignal != null)
58
- abortSignal.throwIfAborted();
59
- const result = yield processor(signatures);
60
- if (result != null)
61
- return result;
62
- if (signatures.length < logFetchLimit)
63
- break;
64
- }
65
- return null;
66
- });
41
+ async findInSignatures(topicKey, processor, abortSignal, logFetchLimit) {
42
+ if (logFetchLimit == null || logFetchLimit > this.LOG_FETCH_LIMIT)
43
+ logFetchLimit = this.LOG_FETCH_LIMIT;
44
+ let signatures = null;
45
+ while (signatures == null || signatures.length > 0) {
46
+ signatures = await this.getSignatures(topicKey, logFetchLimit, signatures != null ? signatures[signatures.length - 1].signature : null);
47
+ if (abortSignal != null)
48
+ abortSignal.throwIfAborted();
49
+ const result = await processor(signatures);
50
+ if (result != null)
51
+ return result;
52
+ if (signatures.length < logFetchLimit)
53
+ break;
54
+ }
55
+ return null;
67
56
  }
68
57
  }
69
58
  exports.SolanaEvents = SolanaEvents;
@@ -1,10 +1,9 @@
1
1
  /// <reference types="node" />
2
- import * as BN from "bn.js";
3
2
  import { Connection, PublicKey, SendOptions, Transaction } from "@solana/web3.js";
4
3
  export type FeeBribeData = {
5
4
  address: string;
6
5
  endpoint: string;
7
- getBribeFee?: (original: BN) => BN;
6
+ getBribeFee?: (original: bigint) => bigint;
8
7
  };
9
8
  export declare class SolanaFees {
10
9
  private readonly connection;
@@ -18,7 +17,7 @@ export declare class SolanaFees {
18
17
  private readonly getStaticFee?;
19
18
  private readonly logger;
20
19
  private blockFeeCache;
21
- constructor(connection: Connection, maxFeeMicroLamports?: number, numSamples?: number, period?: number, useHeliusApi?: "yes" | "no" | "auto", heliusFeeLevel?: "min" | "low" | "medium" | "high" | "veryHigh" | "unsafeMax", getStaticFee?: (feeRate: BN) => BN, bribeData?: FeeBribeData);
20
+ constructor(connection: Connection, maxFeeMicroLamports?: number, numSamples?: number, period?: number, useHeliusApi?: "yes" | "no" | "auto", heliusFeeLevel?: "min" | "low" | "medium" | "high" | "veryHigh" | "unsafeMax", getStaticFee?: (feeRate: bigint) => bigint, bribeData?: FeeBribeData);
22
21
  /**
23
22
  * Returns solana block with transactionDetails="signatures"
24
23
  *
@@ -76,7 +75,7 @@ export declare class SolanaFees {
76
75
  *
77
76
  * @returns {Promise<BN>} global fee rate microLamports/CU
78
77
  */
79
- getGlobalFeeRate(): Promise<BN>;
78
+ getGlobalFeeRate(): Promise<bigint>;
80
79
  /**
81
80
  * Gets the combined microLamports/CU fee rate (from localized & global fee market), cached & adjusted as for
82
81
  * when bribe and/or static fee should be included, format: <uLamports/CU>;<static fee lamport>[;<bribe address>]
@@ -92,7 +91,7 @@ export declare class SolanaFees {
92
91
  * @param feeRate
93
92
  * @param includeStaticFee whether the include the static/base part of the fee rate
94
93
  */
95
- getPriorityFee(computeUnits: number, feeRate: string, includeStaticFee?: boolean): BN;
94
+ getPriorityFee(computeUnits: number, feeRate: string, includeStaticFee?: boolean): bigint;
96
95
  /**
97
96
  * Applies fee rate to a transaction at the beginning of the transaction (has to be called after
98
97
  * feePayer is set for the tx), specifically adds the setComputeUnitLimit & setComputeUnitPrice instruction