@atomiqlabs/chain-solana 10.0.0-dev.3 → 11.0.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 (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 +10 -10
  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 +58 -58
  20. package/dist/solana/chain/SolanaChainInterface.js +112 -112
  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 +25 -25
  28. package/dist/solana/chain/modules/SolanaEvents.js +58 -58
  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 +332 -332
  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 +85 -85
  42. package/dist/solana/events/SolanaChainEventsBrowser.js +194 -194
  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 +59 -59
  48. package/dist/solana/program/modules/SolanaProgramEvents.js +103 -103
  49. package/dist/solana/swaps/SolanaSwapData.d.ts +59 -59
  50. package/dist/solana/swaps/SolanaSwapData.js +267 -267
  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 +202 -202
  54. package/dist/solana/swaps/SolanaSwapProgram.js +470 -463
  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 +252 -252
  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 +10 -10
  73. package/dist/solana/wallet/SolanaSigner.js +16 -16
  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 +25 -25
  79. package/src/solana/SolanaChains.ts +23 -23
  80. package/src/solana/SolanaInitializer.ts +102 -102
  81. package/src/solana/btcrelay/SolanaBtcRelay.ts +589 -588
  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 +174 -174
  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 +56 -56
  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 +370 -370
  96. package/src/solana/events/SolanaChainEvents.ts +299 -299
  97. package/src/solana/events/SolanaChainEventsBrowser.ts +256 -256
  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 +140 -140
  101. package/src/solana/swaps/SolanaSwapData.ts +379 -379
  102. package/src/solana/swaps/SolanaSwapModule.ts +16 -16
  103. package/src/solana/swaps/SolanaSwapProgram.ts +697 -692
  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 +312 -312
  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 +23 -23
  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,175 +1,175 @@
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
-
16
- export type SolanaRetryPolicy = {
17
- maxRetries?: number,
18
- delay?: number,
19
- exponential?: boolean,
20
- transactionResendInterval?: number
21
- }
22
-
23
- export class SolanaChainInterface implements ChainInterface<
24
- SolanaTx,
25
- SolanaSigner,
26
- "SOLANA"
27
- > {
28
- readonly chainId = "SOLANA";
29
-
30
- public readonly SLOT_TIME = 400;
31
- public readonly TX_SLOT_VALIDITY = 151;
32
-
33
- readonly connection: Connection;
34
- readonly retryPolicy: SolanaRetryPolicy;
35
-
36
- public readonly Blocks: SolanaBlocks;
37
- public Fees: SolanaFees;
38
- public readonly Slots: SolanaSlots;
39
- public readonly Tokens: SolanaTokens;
40
- public readonly Transactions: SolanaTransactions;
41
- public readonly Signatures: SolanaSignatures;
42
- public readonly Events: SolanaEvents;
43
-
44
- protected readonly logger = getLogger(this.constructor.name+": ");
45
-
46
- constructor(
47
- connection: Connection,
48
- retryPolicy?: SolanaRetryPolicy,
49
- solanaFeeEstimator: SolanaFees = new SolanaFees(connection)
50
- ) {
51
- this.connection = connection;
52
- this.retryPolicy = retryPolicy;
53
-
54
- this.Blocks = new SolanaBlocks(this);
55
- this.Fees = solanaFeeEstimator;
56
- this.Slots = new SolanaSlots(this);
57
- this.Tokens = new SolanaTokens(this);
58
- this.Transactions = new SolanaTransactions(this);
59
- this.Signatures = new SolanaSignatures(this);
60
- this.Events = new SolanaEvents(this);
61
- }
62
-
63
- async getBalance(signer: string, tokenAddress: string): Promise<bigint> {
64
- const token = new PublicKey(tokenAddress);
65
- const publicKey = new PublicKey(signer);
66
-
67
- let { balance } = await this.Tokens.getTokenBalance(publicKey, token);
68
- if(token.equals(SolanaTokens.WSOL_ADDRESS)) {
69
- const accountRentExemptCost = 1000000n;
70
- balance = balance - accountRentExemptCost;
71
- if(balance < 0n) balance = 0n;
72
- }
73
- this.logger.debug("getBalance(): token balance, token: "+token.toBase58()+" balance: "+balance.toString(10));
74
- return balance;
75
- }
76
-
77
- isValidAddress(address: string): boolean {
78
- return SolanaAddresses.isValidAddress(address);
79
- }
80
-
81
- getNativeCurrencyAddress(): string {
82
- return this.Tokens.getNativeCurrencyAddress().toString();
83
- }
84
-
85
- txsTransfer(signer: string, token: string, amount: bigint, dstAddress: string, feeRate?: string): Promise<SolanaTx[]> {
86
- return this.Tokens.txsTransfer(new PublicKey(signer), new PublicKey(token), amount, new PublicKey(dstAddress), feeRate);
87
- }
88
-
89
- async transfer(
90
- signer: SolanaSigner,
91
- token: string,
92
- amount: bigint,
93
- dstAddress: string,
94
- txOptions?: TransactionConfirmationOptions
95
- ): Promise<string> {
96
- const txs = await this.Tokens.txsTransfer(signer.getPublicKey(), new PublicKey(token), amount, new PublicKey(dstAddress), txOptions?.feeRate);
97
- const [txId] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
98
- return txId;
99
- }
100
-
101
-
102
- ////////////////////////////////////////////
103
- //// Transactions
104
- sendAndConfirm(
105
- signer: SolanaSigner,
106
- txs: SolanaTx[],
107
- waitForConfirmation?: boolean,
108
- abortSignal?: AbortSignal,
109
- parallel?: boolean,
110
- onBeforePublish?: (txId: string, rawTx: string) => Promise<void>
111
- ): Promise<string[]> {
112
- return this.Transactions.sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish);
113
- }
114
-
115
- serializeTx(tx: SolanaTx): Promise<string> {
116
- return this.Transactions.serializeTx(tx);
117
- }
118
-
119
- deserializeTx(txData: string): Promise<SolanaTx> {
120
- return this.Transactions.deserializeTx(txData);
121
- }
122
-
123
- getTxIdStatus(txId: string): Promise<"not_found" | "pending" | "success" | "reverted"> {
124
- return this.Transactions.getTxIdStatus(txId);
125
- }
126
-
127
- getTxStatus(tx: string): Promise<"not_found" | "pending" | "success" | "reverted"> {
128
- return this.Transactions.getTxStatus(tx);
129
- }
130
-
131
-
132
- ///////////////////////////////////
133
- //// Callbacks & handlers
134
- offBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): boolean {
135
- return true;
136
- }
137
-
138
- onBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): void {}
139
-
140
- onBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): void {
141
- this.Transactions.onBeforeTxSigned(callback);
142
- }
143
-
144
- offBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): boolean {
145
- return this.Transactions.offBeforeTxSigned(callback);
146
- }
147
-
148
- onSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): void {
149
- this.Transactions.onSendTransaction(callback);
150
- }
151
-
152
- offSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): boolean {
153
- return this.Transactions.offSendTransaction(callback);
154
- }
155
-
156
- isValidToken(tokenIdentifier: string): boolean {
157
- try {
158
- new PublicKey(tokenIdentifier);
159
- return true;
160
- } catch (e) {
161
- return false;
162
- }
163
- }
164
-
165
- randomAddress(): string {
166
- return Keypair.generate().publicKey.toString();
167
- }
168
-
169
- randomSigner(): SolanaSigner {
170
- const keypair = Keypair.generate();
171
- const wallet = new SolanaKeypairWallet(keypair);
172
- return new SolanaSigner(wallet, keypair);
173
- }
174
-
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
+
16
+ export type SolanaRetryPolicy = {
17
+ maxRetries?: number,
18
+ delay?: number,
19
+ exponential?: boolean,
20
+ transactionResendInterval?: number
21
+ }
22
+
23
+ export class SolanaChainInterface implements ChainInterface<
24
+ SolanaTx,
25
+ SolanaSigner,
26
+ "SOLANA"
27
+ > {
28
+ readonly chainId = "SOLANA";
29
+
30
+ public readonly SLOT_TIME = 400;
31
+ public readonly TX_SLOT_VALIDITY = 151;
32
+
33
+ readonly connection: Connection;
34
+ readonly retryPolicy: SolanaRetryPolicy;
35
+
36
+ public readonly Blocks: SolanaBlocks;
37
+ public Fees: SolanaFees;
38
+ public readonly Slots: SolanaSlots;
39
+ public readonly Tokens: SolanaTokens;
40
+ public readonly Transactions: SolanaTransactions;
41
+ public readonly Signatures: SolanaSignatures;
42
+ public readonly Events: SolanaEvents;
43
+
44
+ protected readonly logger = getLogger(this.constructor.name+": ");
45
+
46
+ constructor(
47
+ connection: Connection,
48
+ retryPolicy?: SolanaRetryPolicy,
49
+ solanaFeeEstimator: SolanaFees = new SolanaFees(connection)
50
+ ) {
51
+ this.connection = connection;
52
+ this.retryPolicy = retryPolicy;
53
+
54
+ this.Blocks = new SolanaBlocks(this);
55
+ this.Fees = solanaFeeEstimator;
56
+ this.Slots = new SolanaSlots(this);
57
+ this.Tokens = new SolanaTokens(this);
58
+ this.Transactions = new SolanaTransactions(this);
59
+ this.Signatures = new SolanaSignatures(this);
60
+ this.Events = new SolanaEvents(this);
61
+ }
62
+
63
+ async getBalance(signer: string, tokenAddress: string): Promise<bigint> {
64
+ const token = new PublicKey(tokenAddress);
65
+ const publicKey = new PublicKey(signer);
66
+
67
+ let { balance } = await this.Tokens.getTokenBalance(publicKey, token);
68
+ if(token.equals(SolanaTokens.WSOL_ADDRESS)) {
69
+ const accountRentExemptCost = 1000000n;
70
+ balance = balance - accountRentExemptCost;
71
+ if(balance < 0n) balance = 0n;
72
+ }
73
+ this.logger.debug("getBalance(): token balance, token: "+token.toBase58()+" balance: "+balance.toString(10));
74
+ return balance;
75
+ }
76
+
77
+ isValidAddress(address: string): boolean {
78
+ return SolanaAddresses.isValidAddress(address);
79
+ }
80
+
81
+ getNativeCurrencyAddress(): string {
82
+ return this.Tokens.getNativeCurrencyAddress().toString();
83
+ }
84
+
85
+ txsTransfer(signer: string, token: string, amount: bigint, dstAddress: string, feeRate?: string): Promise<SolanaTx[]> {
86
+ return this.Tokens.txsTransfer(new PublicKey(signer), new PublicKey(token), amount, new PublicKey(dstAddress), feeRate);
87
+ }
88
+
89
+ async transfer(
90
+ signer: SolanaSigner,
91
+ token: string,
92
+ amount: bigint,
93
+ dstAddress: string,
94
+ txOptions?: TransactionConfirmationOptions
95
+ ): Promise<string> {
96
+ const txs = await this.Tokens.txsTransfer(signer.getPublicKey(), new PublicKey(token), amount, new PublicKey(dstAddress), txOptions?.feeRate);
97
+ const [txId] = await this.Transactions.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
98
+ return txId;
99
+ }
100
+
101
+
102
+ ////////////////////////////////////////////
103
+ //// Transactions
104
+ sendAndConfirm(
105
+ signer: SolanaSigner,
106
+ txs: SolanaTx[],
107
+ waitForConfirmation?: boolean,
108
+ abortSignal?: AbortSignal,
109
+ parallel?: boolean,
110
+ onBeforePublish?: (txId: string, rawTx: string) => Promise<void>
111
+ ): Promise<string[]> {
112
+ return this.Transactions.sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish);
113
+ }
114
+
115
+ serializeTx(tx: SolanaTx): Promise<string> {
116
+ return this.Transactions.serializeTx(tx);
117
+ }
118
+
119
+ deserializeTx(txData: string): Promise<SolanaTx> {
120
+ return this.Transactions.deserializeTx(txData);
121
+ }
122
+
123
+ getTxIdStatus(txId: string): Promise<"not_found" | "pending" | "success" | "reverted"> {
124
+ return this.Transactions.getTxIdStatus(txId);
125
+ }
126
+
127
+ getTxStatus(tx: string): Promise<"not_found" | "pending" | "success" | "reverted"> {
128
+ return this.Transactions.getTxStatus(tx);
129
+ }
130
+
131
+
132
+ ///////////////////////////////////
133
+ //// Callbacks & handlers
134
+ offBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): boolean {
135
+ return true;
136
+ }
137
+
138
+ onBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): void {}
139
+
140
+ onBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): void {
141
+ this.Transactions.onBeforeTxSigned(callback);
142
+ }
143
+
144
+ offBeforeTxSigned(callback: (tx: SolanaTx) => Promise<void>): boolean {
145
+ return this.Transactions.offBeforeTxSigned(callback);
146
+ }
147
+
148
+ onSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): void {
149
+ this.Transactions.onSendTransaction(callback);
150
+ }
151
+
152
+ offSendTransaction(callback: (tx: Buffer, options?: SendOptions) => Promise<string>): boolean {
153
+ return this.Transactions.offSendTransaction(callback);
154
+ }
155
+
156
+ isValidToken(tokenIdentifier: string): boolean {
157
+ try {
158
+ new PublicKey(tokenIdentifier);
159
+ return true;
160
+ } catch (e) {
161
+ return false;
162
+ }
163
+ }
164
+
165
+ randomAddress(): string {
166
+ return Keypair.generate().publicKey.toString();
167
+ }
168
+
169
+ randomSigner(): SolanaSigner {
170
+ const keypair = Keypair.generate();
171
+ const wallet = new SolanaKeypairWallet(keypair);
172
+ return new SolanaSigner(wallet, keypair);
173
+ }
174
+
175
175
  }
@@ -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
  }
@@ -1,21 +1,21 @@
1
- import {PublicKey} from "@solana/web3.js";
2
-
3
-
4
- export class SolanaAddresses {
5
-
6
- ///////////////////
7
- //// Address utils
8
- /**
9
- * Checks whether an address is a valid Solana address (base58 encoded ed25519 public key)
10
- *
11
- * @param address
12
- */
13
- static isValidAddress(address: string): boolean {
14
- try {
15
- return PublicKey.isOnCurve(address);
16
- } catch (e) {
17
- return false;
18
- }
19
- }
20
-
1
+ import {PublicKey} from "@solana/web3.js";
2
+
3
+
4
+ export class SolanaAddresses {
5
+
6
+ ///////////////////
7
+ //// Address utils
8
+ /**
9
+ * Checks whether an address is a valid Solana address (base58 encoded ed25519 public key)
10
+ *
11
+ * @param address
12
+ */
13
+ static isValidAddress(address: string): boolean {
14
+ try {
15
+ return PublicKey.isOnCurve(address);
16
+ } catch (e) {
17
+ return false;
18
+ }
19
+ }
20
+
21
21
  }