@atomiqlabs/chain-solana 12.0.11 → 12.0.13

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 (114) hide show
  1. package/LICENSE +201 -201
  2. package/dist/index.d.ts +29 -29
  3. package/dist/index.js +45 -45
  4. package/dist/solana/SolanaChainType.d.ts +11 -11
  5. package/dist/solana/SolanaChainType.js +2 -2
  6. package/dist/solana/SolanaChains.d.ts +20 -20
  7. package/dist/solana/SolanaChains.js +25 -25
  8. package/dist/solana/SolanaInitializer.d.ts +18 -18
  9. package/dist/solana/SolanaInitializer.js +63 -63
  10. package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +228 -228
  11. package/dist/solana/btcrelay/SolanaBtcRelay.js +441 -441
  12. package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +29 -29
  13. package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +34 -34
  14. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +46 -46
  15. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +78 -78
  16. package/dist/solana/btcrelay/program/programIdl.json +671 -671
  17. package/dist/solana/chain/SolanaAction.d.ts +26 -26
  18. package/dist/solana/chain/SolanaAction.js +86 -86
  19. package/dist/solana/chain/SolanaChainInterface.d.ts +65 -65
  20. package/dist/solana/chain/SolanaChainInterface.js +125 -125
  21. package/dist/solana/chain/SolanaModule.d.ts +14 -14
  22. package/dist/solana/chain/SolanaModule.js +13 -13
  23. package/dist/solana/chain/modules/SolanaAddresses.d.ts +8 -8
  24. package/dist/solana/chain/modules/SolanaAddresses.js +22 -22
  25. package/dist/solana/chain/modules/SolanaBlocks.d.ts +28 -28
  26. package/dist/solana/chain/modules/SolanaBlocks.js +72 -72
  27. package/dist/solana/chain/modules/SolanaEvents.d.ts +68 -68
  28. package/dist/solana/chain/modules/SolanaEvents.js +225 -225
  29. package/dist/solana/chain/modules/SolanaFees.d.ts +121 -121
  30. package/dist/solana/chain/modules/SolanaFees.js +379 -379
  31. package/dist/solana/chain/modules/SolanaSignatures.d.ts +23 -23
  32. package/dist/solana/chain/modules/SolanaSignatures.js +39 -39
  33. package/dist/solana/chain/modules/SolanaSlots.d.ts +31 -31
  34. package/dist/solana/chain/modules/SolanaSlots.js +68 -68
  35. package/dist/solana/chain/modules/SolanaTokens.d.ts +136 -136
  36. package/dist/solana/chain/modules/SolanaTokens.js +248 -248
  37. package/dist/solana/chain/modules/SolanaTransactions.d.ts +124 -124
  38. package/dist/solana/chain/modules/SolanaTransactions.js +323 -323
  39. package/dist/solana/events/SolanaChainEvents.d.ts +88 -88
  40. package/dist/solana/events/SolanaChainEvents.js +256 -256
  41. package/dist/solana/events/SolanaChainEventsBrowser.d.ts +75 -75
  42. package/dist/solana/events/SolanaChainEventsBrowser.js +172 -172
  43. package/dist/solana/program/SolanaProgramBase.d.ts +40 -40
  44. package/dist/solana/program/SolanaProgramBase.js +43 -43
  45. package/dist/solana/program/SolanaProgramModule.d.ts +8 -8
  46. package/dist/solana/program/SolanaProgramModule.js +11 -11
  47. package/dist/solana/program/modules/SolanaProgramEvents.d.ts +53 -53
  48. package/dist/solana/program/modules/SolanaProgramEvents.js +114 -114
  49. package/dist/solana/swaps/SolanaSwapData.d.ts +71 -71
  50. package/dist/solana/swaps/SolanaSwapData.js +292 -292
  51. package/dist/solana/swaps/SolanaSwapModule.d.ts +10 -10
  52. package/dist/solana/swaps/SolanaSwapModule.js +11 -11
  53. package/dist/solana/swaps/SolanaSwapProgram.d.ts +224 -224
  54. package/dist/solana/swaps/SolanaSwapProgram.js +570 -567
  55. package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -11
  56. package/dist/solana/swaps/SwapTypeEnum.js +42 -42
  57. package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +94 -94
  58. package/dist/solana/swaps/modules/SolanaDataAccount.js +231 -231
  59. package/dist/solana/swaps/modules/SolanaLpVault.d.ts +71 -71
  60. package/dist/solana/swaps/modules/SolanaLpVault.js +173 -173
  61. package/dist/solana/swaps/modules/SwapClaim.d.ts +129 -129
  62. package/dist/solana/swaps/modules/SwapClaim.js +291 -291
  63. package/dist/solana/swaps/modules/SwapInit.d.ts +217 -217
  64. package/dist/solana/swaps/modules/SwapInit.js +519 -519
  65. package/dist/solana/swaps/modules/SwapRefund.d.ts +82 -82
  66. package/dist/solana/swaps/modules/SwapRefund.js +262 -262
  67. package/dist/solana/swaps/programIdl.json +945 -945
  68. package/dist/solana/swaps/programTypes.d.ts +943 -943
  69. package/dist/solana/swaps/programTypes.js +945 -945
  70. package/dist/solana/wallet/SolanaKeypairWallet.d.ts +9 -9
  71. package/dist/solana/wallet/SolanaKeypairWallet.js +33 -33
  72. package/dist/solana/wallet/SolanaSigner.d.ts +11 -11
  73. package/dist/solana/wallet/SolanaSigner.js +17 -17
  74. package/dist/utils/Utils.d.ts +53 -53
  75. package/dist/utils/Utils.js +170 -170
  76. package/package.json +41 -41
  77. package/src/index.ts +36 -36
  78. package/src/solana/SolanaChainType.ts +27 -27
  79. package/src/solana/SolanaChains.ts +23 -23
  80. package/src/solana/SolanaInitializer.ts +102 -102
  81. package/src/solana/btcrelay/SolanaBtcRelay.ts +589 -589
  82. package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +57 -57
  83. package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +102 -102
  84. package/src/solana/btcrelay/program/programIdl.json +670 -670
  85. package/src/solana/chain/SolanaAction.ts +108 -108
  86. package/src/solana/chain/SolanaChainInterface.ts +192 -192
  87. package/src/solana/chain/SolanaModule.ts +20 -20
  88. package/src/solana/chain/modules/SolanaAddresses.ts +20 -20
  89. package/src/solana/chain/modules/SolanaBlocks.ts +78 -78
  90. package/src/solana/chain/modules/SolanaEvents.ts +256 -256
  91. package/src/solana/chain/modules/SolanaFees.ts +450 -450
  92. package/src/solana/chain/modules/SolanaSignatures.ts +39 -39
  93. package/src/solana/chain/modules/SolanaSlots.ts +82 -82
  94. package/src/solana/chain/modules/SolanaTokens.ts +307 -307
  95. package/src/solana/chain/modules/SolanaTransactions.ts +365 -365
  96. package/src/solana/events/SolanaChainEvents.ts +299 -299
  97. package/src/solana/events/SolanaChainEventsBrowser.ts +209 -209
  98. package/src/solana/program/SolanaProgramBase.ts +79 -79
  99. package/src/solana/program/SolanaProgramModule.ts +15 -15
  100. package/src/solana/program/modules/SolanaProgramEvents.ts +155 -155
  101. package/src/solana/swaps/SolanaSwapData.ts +430 -430
  102. package/src/solana/swaps/SolanaSwapModule.ts +16 -16
  103. package/src/solana/swaps/SolanaSwapProgram.ts +854 -849
  104. package/src/solana/swaps/SwapTypeEnum.ts +29 -29
  105. package/src/solana/swaps/modules/SolanaDataAccount.ts +307 -307
  106. package/src/solana/swaps/modules/SolanaLpVault.ts +215 -215
  107. package/src/solana/swaps/modules/SwapClaim.ts +389 -389
  108. package/src/solana/swaps/modules/SwapInit.ts +663 -663
  109. package/src/solana/swaps/modules/SwapRefund.ts +323 -323
  110. package/src/solana/swaps/programIdl.json +944 -944
  111. package/src/solana/swaps/programTypes.ts +1885 -1885
  112. package/src/solana/wallet/SolanaKeypairWallet.ts +36 -36
  113. package/src/solana/wallet/SolanaSigner.ts +24 -24
  114. package/src/utils/Utils.ts +180 -180
@@ -1,109 +1,109 @@
1
- import {PublicKey, Signer, Transaction, TransactionInstruction} from "@solana/web3.js";
2
- import {SolanaTx} from "./modules/SolanaTransactions";
3
- import {SolanaChainInterface} from "./SolanaChainInterface";
4
-
5
-
6
- export class SolanaAction {
7
-
8
- computeBudget: number;
9
- readonly mainSigner: PublicKey;
10
- private readonly root: SolanaChainInterface;
11
- private readonly instructions: TransactionInstruction[];
12
- private feeRate: string;
13
- private readonly signers: Signer[];
14
- private firstIxBeforeComputeBudget: boolean = false;
15
-
16
- constructor(
17
- mainSigner: PublicKey,
18
- root: SolanaChainInterface,
19
- instructions: TransactionInstruction[] | TransactionInstruction = [],
20
- computeBudget: number = 0,
21
- feeRate?: string,
22
- signers?: Signer[],
23
- firstIxBeforeComputeBudget?: boolean
24
- ) {
25
- this.mainSigner = mainSigner;
26
- this.root = root;
27
- this.instructions = Array.isArray(instructions) ? instructions : [instructions];
28
- this.computeBudget = computeBudget;
29
- this.feeRate = feeRate;
30
- this.signers = signers || [];
31
- if(firstIxBeforeComputeBudget!=null) this.firstIxBeforeComputeBudget = firstIxBeforeComputeBudget;
32
- }
33
-
34
- private estimateFee(): Promise<string> {
35
- const mutableAccounts: PublicKey[] = [];
36
- this.instructions.forEach(
37
- ix => ix.keys.forEach(
38
- key => key.isWritable && mutableAccounts.push(key.pubkey)
39
- )
40
- );
41
- return this.root.Fees.getFeeRate(mutableAccounts);
42
- }
43
-
44
- public addIx(instruction: TransactionInstruction, computeBudget?: number, signers?: Signer[]) {
45
- this.instructions.push(instruction);
46
- if(this.computeBudget==null) {
47
- this.computeBudget = computeBudget;
48
- } else {
49
- if(computeBudget!=null) this.computeBudget+=computeBudget;
50
- }
51
- }
52
-
53
- public add(action: SolanaAction): this {
54
- return this.addAction(action);
55
- }
56
-
57
- public addAction(action: SolanaAction, index: number = this.instructions.length): this {
58
- if(action.firstIxBeforeComputeBudget) {
59
- if(this.instructions.length>0)
60
- throw new Error("Tried to add firstIxBeforeComputeBudget action to existing action with instructions");
61
- this.firstIxBeforeComputeBudget = true;
62
- }
63
- if(this.firstIxBeforeComputeBudget && this.instructions.length>0 && index===0)
64
- throw new Error("Tried adding to firstIxBeforeComputeBudget action on 0th index");
65
- if(!action.mainSigner.equals(this.mainSigner)) throw new Error("Actions need to have the same signer!");
66
- if(this.computeBudget==null && action.computeBudget!=null) this.computeBudget = action.computeBudget;
67
- if(this.computeBudget!=null && action.computeBudget!=null) this.computeBudget += action.computeBudget;
68
- this.instructions.splice(index, 0, ...action.instructions);
69
- this.signers.push(...action.signers);
70
- if(this.feeRate==null) this.feeRate = action.feeRate;
71
- return this;
72
- }
73
-
74
- public async tx(feeRate?: string, block?: {blockhash: string, blockHeight: number}): Promise<SolanaTx> {
75
- const tx = new Transaction();
76
- tx.feePayer = this.mainSigner;
77
-
78
- if(feeRate==null) feeRate = this.feeRate;
79
- if(feeRate==null) feeRate = await this.estimateFee();
80
-
81
- let instructions = this.instructions;
82
- if(instructions.length>0 && this.firstIxBeforeComputeBudget) {
83
- tx.add(this.instructions[0]);
84
- instructions = this.instructions.slice(1);
85
- }
86
- this.root.Fees.applyFeeRateBegin(tx, this.computeBudget, feeRate);
87
- instructions.forEach(ix => tx.add(ix));
88
- this.root.Fees.applyFeeRateEnd(tx, this.computeBudget, feeRate);
89
-
90
- if(block!=null) {
91
- tx.recentBlockhash = block.blockhash;
92
- tx.lastValidBlockHeight = block.blockHeight + this.root.TX_SLOT_VALIDITY;
93
- }
94
-
95
- return {
96
- tx,
97
- signers: this.signers
98
- };
99
- }
100
-
101
- public async addToTxs(txs: SolanaTx[], feeRate?: string, block?: {blockhash: string, blockHeight: number}): Promise<void> {
102
- txs.push(await this.tx(feeRate, block));
103
- }
104
-
105
- public ixsLength(): number {
106
- return this.instructions.length;
107
- }
108
-
1
+ import {PublicKey, Signer, Transaction, TransactionInstruction} from "@solana/web3.js";
2
+ import {SolanaTx} from "./modules/SolanaTransactions";
3
+ import {SolanaChainInterface} from "./SolanaChainInterface";
4
+
5
+
6
+ export class SolanaAction {
7
+
8
+ computeBudget: number;
9
+ readonly mainSigner: PublicKey;
10
+ private readonly root: SolanaChainInterface;
11
+ private readonly instructions: TransactionInstruction[];
12
+ private feeRate: string;
13
+ private readonly signers: Signer[];
14
+ private firstIxBeforeComputeBudget: boolean = false;
15
+
16
+ constructor(
17
+ mainSigner: PublicKey,
18
+ root: SolanaChainInterface,
19
+ instructions: TransactionInstruction[] | TransactionInstruction = [],
20
+ computeBudget: number = 0,
21
+ feeRate?: string,
22
+ signers?: Signer[],
23
+ firstIxBeforeComputeBudget?: boolean
24
+ ) {
25
+ this.mainSigner = mainSigner;
26
+ this.root = root;
27
+ this.instructions = Array.isArray(instructions) ? instructions : [instructions];
28
+ this.computeBudget = computeBudget;
29
+ this.feeRate = feeRate;
30
+ this.signers = signers || [];
31
+ if(firstIxBeforeComputeBudget!=null) this.firstIxBeforeComputeBudget = firstIxBeforeComputeBudget;
32
+ }
33
+
34
+ private estimateFee(): Promise<string> {
35
+ const mutableAccounts: PublicKey[] = [];
36
+ this.instructions.forEach(
37
+ ix => ix.keys.forEach(
38
+ key => key.isWritable && mutableAccounts.push(key.pubkey)
39
+ )
40
+ );
41
+ return this.root.Fees.getFeeRate(mutableAccounts);
42
+ }
43
+
44
+ public addIx(instruction: TransactionInstruction, computeBudget?: number, signers?: Signer[]) {
45
+ this.instructions.push(instruction);
46
+ if(this.computeBudget==null) {
47
+ this.computeBudget = computeBudget;
48
+ } else {
49
+ if(computeBudget!=null) this.computeBudget+=computeBudget;
50
+ }
51
+ }
52
+
53
+ public add(action: SolanaAction): this {
54
+ return this.addAction(action);
55
+ }
56
+
57
+ public addAction(action: SolanaAction, index: number = this.instructions.length): this {
58
+ if(action.firstIxBeforeComputeBudget) {
59
+ if(this.instructions.length>0)
60
+ throw new Error("Tried to add firstIxBeforeComputeBudget action to existing action with instructions");
61
+ this.firstIxBeforeComputeBudget = true;
62
+ }
63
+ if(this.firstIxBeforeComputeBudget && this.instructions.length>0 && index===0)
64
+ throw new Error("Tried adding to firstIxBeforeComputeBudget action on 0th index");
65
+ if(!action.mainSigner.equals(this.mainSigner)) throw new Error("Actions need to have the same signer!");
66
+ if(this.computeBudget==null && action.computeBudget!=null) this.computeBudget = action.computeBudget;
67
+ if(this.computeBudget!=null && action.computeBudget!=null) this.computeBudget += action.computeBudget;
68
+ this.instructions.splice(index, 0, ...action.instructions);
69
+ this.signers.push(...action.signers);
70
+ if(this.feeRate==null) this.feeRate = action.feeRate;
71
+ return this;
72
+ }
73
+
74
+ public async tx(feeRate?: string, block?: {blockhash: string, blockHeight: number}): Promise<SolanaTx> {
75
+ const tx = new Transaction();
76
+ tx.feePayer = this.mainSigner;
77
+
78
+ if(feeRate==null) feeRate = this.feeRate;
79
+ if(feeRate==null) feeRate = await this.estimateFee();
80
+
81
+ let instructions = this.instructions;
82
+ if(instructions.length>0 && this.firstIxBeforeComputeBudget) {
83
+ tx.add(this.instructions[0]);
84
+ instructions = this.instructions.slice(1);
85
+ }
86
+ this.root.Fees.applyFeeRateBegin(tx, this.computeBudget, feeRate);
87
+ instructions.forEach(ix => tx.add(ix));
88
+ this.root.Fees.applyFeeRateEnd(tx, this.computeBudget, feeRate);
89
+
90
+ if(block!=null) {
91
+ tx.recentBlockhash = block.blockhash;
92
+ tx.lastValidBlockHeight = block.blockHeight + this.root.TX_SLOT_VALIDITY;
93
+ }
94
+
95
+ return {
96
+ tx,
97
+ signers: this.signers
98
+ };
99
+ }
100
+
101
+ public async addToTxs(txs: SolanaTx[], feeRate?: string, block?: {blockhash: string, blockHeight: number}): Promise<void> {
102
+ txs.push(await this.tx(feeRate, block));
103
+ }
104
+
105
+ public ixsLength(): number {
106
+ return this.instructions.length;
107
+ }
108
+
109
109
  }
@@ -1,193 +1,193 @@
1
- import {Connection, Keypair, PublicKey, SendOptions} from "@solana/web3.js";
2
- import {SolanaFees} from "./modules/SolanaFees";
3
- import {SolanaBlocks} from "./modules/SolanaBlocks";
4
- import {SolanaSlots} from "./modules/SolanaSlots";
5
- import {SolanaTokens} from "./modules/SolanaTokens";
6
- import {SolanaTransactions, SolanaTx} from "./modules/SolanaTransactions";
7
- import {SolanaSignatures} from "./modules/SolanaSignatures";
8
- import {SolanaEvents} from "./modules/SolanaEvents";
9
- import {getLogger} from "../../utils/Utils";
10
- import {ChainInterface, TransactionConfirmationOptions} from "@atomiqlabs/base";
11
- import {SolanaAddresses} from "./modules/SolanaAddresses";
12
- import {SolanaSigner} from "../wallet/SolanaSigner";
13
- import {Buffer} from "buffer";
14
- import {SolanaKeypairWallet} from "../wallet/SolanaKeypairWallet";
15
- import {Wallet} from "@coral-xyz/anchor/dist/cjs/provider";
16
-
17
- export type SolanaRetryPolicy = {
18
- maxRetries?: number,
19
- delay?: number,
20
- exponential?: boolean,
21
- transactionResendInterval?: number
22
- }
23
-
24
- export class SolanaChainInterface implements ChainInterface<
25
- SolanaTx,
26
- SolanaSigner,
27
- "SOLANA",
28
- Wallet
29
- > {
30
- readonly chainId = "SOLANA";
31
-
32
- public readonly SLOT_TIME = 400;
33
- public readonly TX_SLOT_VALIDITY = 151;
34
-
35
- readonly connection: Connection;
36
- readonly retryPolicy: SolanaRetryPolicy;
37
-
38
- public readonly Blocks: SolanaBlocks;
39
- public Fees: SolanaFees;
40
- public readonly Slots: SolanaSlots;
41
- public readonly Tokens: SolanaTokens;
42
- public readonly Transactions: SolanaTransactions;
43
- public readonly Signatures: SolanaSignatures;
44
- public readonly Events: SolanaEvents;
45
-
46
- protected readonly logger = getLogger(this.constructor.name+": ");
47
-
48
- constructor(
49
- connection: Connection,
50
- retryPolicy?: SolanaRetryPolicy,
51
- solanaFeeEstimator: SolanaFees = new SolanaFees(connection)
52
- ) {
53
- this.connection = connection;
54
- this.retryPolicy = retryPolicy;
55
-
56
- this.Blocks = new SolanaBlocks(this);
57
- this.Fees = solanaFeeEstimator;
58
- this.Slots = new SolanaSlots(this);
59
- this.Tokens = new SolanaTokens(this);
60
- this.Transactions = new SolanaTransactions(this);
61
- this.Signatures = new SolanaSignatures(this);
62
- this.Events = new SolanaEvents(this);
63
- }
64
-
65
- async getBalance(signer: string, tokenAddress: string): Promise<bigint> {
66
- const token = new PublicKey(tokenAddress);
67
- const publicKey = new PublicKey(signer);
68
-
69
- let { balance } = await this.Tokens.getTokenBalance(publicKey, token);
70
- if(token.equals(SolanaTokens.WSOL_ADDRESS)) {
71
- const accountRentExemptCost = 1000000n;
72
- balance = balance - accountRentExemptCost;
73
- if(balance < 0n) balance = 0n;
74
- }
75
- this.logger.debug("getBalance(): token balance, token: "+token.toBase58()+" balance: "+balance.toString(10));
76
- return balance;
77
- }
78
-
79
- isValidAddress(address: string): boolean {
80
- return SolanaAddresses.isValidAddress(address);
81
- }
82
-
83
- normalizeAddress(address: string): string {
84
- return address;
85
- }
86
-
87
- getNativeCurrencyAddress(): string {
88
- return this.Tokens.getNativeCurrencyAddress().toString();
89
- }
90
-
91
- txsTransfer(signer: string, token: string, amount: bigint, dstAddress: string, feeRate?: string): Promise<SolanaTx[]> {
92
- return this.Tokens.txsTransfer(new PublicKey(signer), new PublicKey(token), amount, new PublicKey(dstAddress), feeRate);
93
- }
94
-
95
- async transfer(
96
- signer: SolanaSigner,
97
- token: string,
98
- amount: bigint,
99
- dstAddress: string,
100
- txOptions?: TransactionConfirmationOptions
101
- ): Promise<string> {
102
- const txs = await this.Tokens.txsTransfer(signer.getPublicKey(), new PublicKey(token), amount, new PublicKey(dstAddress), txOptions?.feeRate);
103
- const [txId] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
104
- return txId;
105
- }
106
-
107
-
108
- ////////////////////////////////////////////
109
- //// Transactions
110
- sendAndConfirm(
111
- signer: SolanaSigner,
112
- txs: SolanaTx[],
113
- waitForConfirmation?: boolean,
114
- abortSignal?: AbortSignal,
115
- parallel?: boolean,
116
- onBeforePublish?: (txId: string, rawTx: string) => Promise<void>
117
- ): Promise<string[]> {
118
- return this.Transactions.sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish);
119
- }
120
-
121
- serializeTx(tx: SolanaTx): Promise<string> {
122
- return this.Transactions.serializeTx(tx);
123
- }
124
-
125
- deserializeTx(txData: string): Promise<SolanaTx> {
126
- return this.Transactions.deserializeTx(txData);
127
- }
128
-
129
- getTxIdStatus(txId: string): Promise<"not_found" | "pending" | "success" | "reverted"> {
130
- return this.Transactions.getTxIdStatus(txId);
131
- }
132
-
133
- getTxStatus(tx: string): Promise<"not_found" | "pending" | "success" | "reverted"> {
134
- return this.Transactions.getTxStatus(tx);
135
- }
136
-
137
- async getFinalizedBlock(): Promise<{ height: number; blockHash: string }> {
138
- const {block} = await this.Blocks.findLatestParsedBlock("finalized");
139
- return {
140
- height: block.blockHeight,
141
- blockHash: block.blockhash
142
- };
143
- }
144
-
145
-
146
- ///////////////////////////////////
147
- //// Callbacks & handlers
148
- offBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): boolean {
149
- return true;
150
- }
151
-
152
- onBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): void {}
153
-
154
- onBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): void {
155
- this.Transactions.onBeforeTxSigned(callback);
156
- }
157
-
158
- offBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): boolean {
159
- return this.Transactions.offBeforeTxSigned(callback);
160
- }
161
-
162
- onSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): void {
163
- this.Transactions.onSendTransaction(callback);
164
- }
165
-
166
- offSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): boolean {
167
- return this.Transactions.offSendTransaction(callback);
168
- }
169
-
170
- isValidToken(tokenIdentifier: string): boolean {
171
- try {
172
- new PublicKey(tokenIdentifier);
173
- return true;
174
- } catch (e) {
175
- return false;
176
- }
177
- }
178
-
179
- randomAddress(): string {
180
- return Keypair.generate().publicKey.toString();
181
- }
182
-
183
- randomSigner(): SolanaSigner {
184
- const keypair = Keypair.generate();
185
- const wallet = new SolanaKeypairWallet(keypair);
186
- return new SolanaSigner(wallet, keypair);
187
- }
188
-
189
- wrapSigner(signer: Wallet): Promise<SolanaSigner> {
190
- return Promise.resolve(new SolanaSigner(signer));
191
- }
192
-
1
+ import {Connection, Keypair, PublicKey, SendOptions} from "@solana/web3.js";
2
+ import {SolanaFees} from "./modules/SolanaFees";
3
+ import {SolanaBlocks} from "./modules/SolanaBlocks";
4
+ import {SolanaSlots} from "./modules/SolanaSlots";
5
+ import {SolanaTokens} from "./modules/SolanaTokens";
6
+ import {SolanaTransactions, SolanaTx} from "./modules/SolanaTransactions";
7
+ import {SolanaSignatures} from "./modules/SolanaSignatures";
8
+ import {SolanaEvents} from "./modules/SolanaEvents";
9
+ import {getLogger} from "../../utils/Utils";
10
+ import {ChainInterface, TransactionConfirmationOptions} from "@atomiqlabs/base";
11
+ import {SolanaAddresses} from "./modules/SolanaAddresses";
12
+ import {SolanaSigner} from "../wallet/SolanaSigner";
13
+ import {Buffer} from "buffer";
14
+ import {SolanaKeypairWallet} from "../wallet/SolanaKeypairWallet";
15
+ import {Wallet} from "@coral-xyz/anchor/dist/cjs/provider";
16
+
17
+ export type SolanaRetryPolicy = {
18
+ maxRetries?: number,
19
+ delay?: number,
20
+ exponential?: boolean,
21
+ transactionResendInterval?: number
22
+ }
23
+
24
+ export class SolanaChainInterface implements ChainInterface<
25
+ SolanaTx,
26
+ SolanaSigner,
27
+ "SOLANA",
28
+ Wallet
29
+ > {
30
+ readonly chainId = "SOLANA";
31
+
32
+ public readonly SLOT_TIME = 400;
33
+ public readonly TX_SLOT_VALIDITY = 151;
34
+
35
+ readonly connection: Connection;
36
+ readonly retryPolicy: SolanaRetryPolicy;
37
+
38
+ public readonly Blocks: SolanaBlocks;
39
+ public Fees: SolanaFees;
40
+ public readonly Slots: SolanaSlots;
41
+ public readonly Tokens: SolanaTokens;
42
+ public readonly Transactions: SolanaTransactions;
43
+ public readonly Signatures: SolanaSignatures;
44
+ public readonly Events: SolanaEvents;
45
+
46
+ protected readonly logger = getLogger(this.constructor.name+": ");
47
+
48
+ constructor(
49
+ connection: Connection,
50
+ retryPolicy?: SolanaRetryPolicy,
51
+ solanaFeeEstimator: SolanaFees = new SolanaFees(connection)
52
+ ) {
53
+ this.connection = connection;
54
+ this.retryPolicy = retryPolicy;
55
+
56
+ this.Blocks = new SolanaBlocks(this);
57
+ this.Fees = solanaFeeEstimator;
58
+ this.Slots = new SolanaSlots(this);
59
+ this.Tokens = new SolanaTokens(this);
60
+ this.Transactions = new SolanaTransactions(this);
61
+ this.Signatures = new SolanaSignatures(this);
62
+ this.Events = new SolanaEvents(this);
63
+ }
64
+
65
+ async getBalance(signer: string, tokenAddress: string): Promise<bigint> {
66
+ const token = new PublicKey(tokenAddress);
67
+ const publicKey = new PublicKey(signer);
68
+
69
+ let { balance } = await this.Tokens.getTokenBalance(publicKey, token);
70
+ if(token.equals(SolanaTokens.WSOL_ADDRESS)) {
71
+ const accountRentExemptCost = 1000000n;
72
+ balance = balance - accountRentExemptCost;
73
+ if(balance < 0n) balance = 0n;
74
+ }
75
+ this.logger.debug("getBalance(): token balance, token: "+token.toBase58()+" balance: "+balance.toString(10));
76
+ return balance;
77
+ }
78
+
79
+ isValidAddress(address: string): boolean {
80
+ return SolanaAddresses.isValidAddress(address);
81
+ }
82
+
83
+ normalizeAddress(address: string): string {
84
+ return address;
85
+ }
86
+
87
+ getNativeCurrencyAddress(): string {
88
+ return this.Tokens.getNativeCurrencyAddress().toString();
89
+ }
90
+
91
+ txsTransfer(signer: string, token: string, amount: bigint, dstAddress: string, feeRate?: string): Promise<SolanaTx[]> {
92
+ return this.Tokens.txsTransfer(new PublicKey(signer), new PublicKey(token), amount, new PublicKey(dstAddress), feeRate);
93
+ }
94
+
95
+ async transfer(
96
+ signer: SolanaSigner,
97
+ token: string,
98
+ amount: bigint,
99
+ dstAddress: string,
100
+ txOptions?: TransactionConfirmationOptions
101
+ ): Promise<string> {
102
+ const txs = await this.Tokens.txsTransfer(signer.getPublicKey(), new PublicKey(token), amount, new PublicKey(dstAddress), txOptions?.feeRate);
103
+ const [txId] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
104
+ return txId;
105
+ }
106
+
107
+
108
+ ////////////////////////////////////////////
109
+ //// Transactions
110
+ sendAndConfirm(
111
+ signer: SolanaSigner,
112
+ txs: SolanaTx[],
113
+ waitForConfirmation?: boolean,
114
+ abortSignal?: AbortSignal,
115
+ parallel?: boolean,
116
+ onBeforePublish?: (txId: string, rawTx: string) => Promise<void>
117
+ ): Promise<string[]> {
118
+ return this.Transactions.sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish);
119
+ }
120
+
121
+ serializeTx(tx: SolanaTx): Promise<string> {
122
+ return this.Transactions.serializeTx(tx);
123
+ }
124
+
125
+ deserializeTx(txData: string): Promise<SolanaTx> {
126
+ return this.Transactions.deserializeTx(txData);
127
+ }
128
+
129
+ getTxIdStatus(txId: string): Promise<"not_found" | "pending" | "success" | "reverted"> {
130
+ return this.Transactions.getTxIdStatus(txId);
131
+ }
132
+
133
+ getTxStatus(tx: string): Promise<"not_found" | "pending" | "success" | "reverted"> {
134
+ return this.Transactions.getTxStatus(tx);
135
+ }
136
+
137
+ async getFinalizedBlock(): Promise<{ height: number; blockHash: string }> {
138
+ const {block} = await this.Blocks.findLatestParsedBlock("finalized");
139
+ return {
140
+ height: block.blockHeight,
141
+ blockHash: block.blockhash
142
+ };
143
+ }
144
+
145
+
146
+ ///////////////////////////////////
147
+ //// Callbacks & handlers
148
+ offBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): boolean {
149
+ return true;
150
+ }
151
+
152
+ onBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): void {}
153
+
154
+ onBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): void {
155
+ this.Transactions.onBeforeTxSigned(callback);
156
+ }
157
+
158
+ offBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): boolean {
159
+ return this.Transactions.offBeforeTxSigned(callback);
160
+ }
161
+
162
+ onSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): void {
163
+ this.Transactions.onSendTransaction(callback);
164
+ }
165
+
166
+ offSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): boolean {
167
+ return this.Transactions.offSendTransaction(callback);
168
+ }
169
+
170
+ isValidToken(tokenIdentifier: string): boolean {
171
+ try {
172
+ new PublicKey(tokenIdentifier);
173
+ return true;
174
+ } catch (e) {
175
+ return false;
176
+ }
177
+ }
178
+
179
+ randomAddress(): string {
180
+ return Keypair.generate().publicKey.toString();
181
+ }
182
+
183
+ randomSigner(): SolanaSigner {
184
+ const keypair = Keypair.generate();
185
+ const wallet = new SolanaKeypairWallet(keypair);
186
+ return new SolanaSigner(wallet, keypair);
187
+ }
188
+
189
+ wrapSigner(signer: Wallet): Promise<SolanaSigner> {
190
+ return Promise.resolve(new SolanaSigner(signer));
191
+ }
192
+
193
193
  }
@@ -1,21 +1,21 @@
1
- import {Connection} from "@solana/web3.js";
2
- import {SolanaChainInterface, SolanaRetryPolicy} from "./SolanaChainInterface";
3
- import {getLogger} from "../../utils/Utils";
4
-
5
- export class SolanaModule {
6
-
7
- protected readonly connection: Connection;
8
- protected readonly retryPolicy: SolanaRetryPolicy;
9
- protected readonly root: SolanaChainInterface;
10
-
11
- protected readonly logger = getLogger(this.constructor.name+": ");
12
-
13
- constructor(
14
- root: SolanaChainInterface
15
- ) {
16
- this.connection = root.connection;
17
- this.retryPolicy = root.retryPolicy;
18
- this.root = root;
19
- }
20
-
1
+ import {Connection} from "@solana/web3.js";
2
+ import {SolanaChainInterface, SolanaRetryPolicy} from "./SolanaChainInterface";
3
+ import {getLogger} from "../../utils/Utils";
4
+
5
+ export class SolanaModule {
6
+
7
+ protected readonly connection: Connection;
8
+ protected readonly retryPolicy: SolanaRetryPolicy;
9
+ protected readonly root: SolanaChainInterface;
10
+
11
+ protected readonly logger = getLogger(this.constructor.name+": ");
12
+
13
+ constructor(
14
+ root: SolanaChainInterface
15
+ ) {
16
+ this.connection = root.connection;
17
+ this.retryPolicy = root.retryPolicy;
18
+ this.root = root;
19
+ }
20
+
21
21
  }