@atomiqlabs/chain-solana 12.0.12 → 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.
- package/LICENSE +201 -201
- package/dist/index.d.ts +29 -29
- package/dist/index.js +45 -45
- package/dist/solana/SolanaChainType.d.ts +11 -11
- package/dist/solana/SolanaChainType.js +2 -2
- package/dist/solana/SolanaChains.d.ts +20 -20
- package/dist/solana/SolanaChains.js +25 -25
- package/dist/solana/SolanaInitializer.d.ts +18 -18
- package/dist/solana/SolanaInitializer.js +63 -63
- package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +228 -228
- package/dist/solana/btcrelay/SolanaBtcRelay.js +441 -441
- package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +29 -29
- package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +34 -34
- package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +46 -46
- package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +78 -78
- package/dist/solana/btcrelay/program/programIdl.json +671 -671
- package/dist/solana/chain/SolanaAction.d.ts +26 -26
- package/dist/solana/chain/SolanaAction.js +86 -86
- package/dist/solana/chain/SolanaChainInterface.d.ts +65 -65
- package/dist/solana/chain/SolanaChainInterface.js +125 -125
- package/dist/solana/chain/SolanaModule.d.ts +14 -14
- package/dist/solana/chain/SolanaModule.js +13 -13
- package/dist/solana/chain/modules/SolanaAddresses.d.ts +8 -8
- package/dist/solana/chain/modules/SolanaAddresses.js +22 -22
- package/dist/solana/chain/modules/SolanaBlocks.d.ts +28 -28
- package/dist/solana/chain/modules/SolanaBlocks.js +72 -72
- package/dist/solana/chain/modules/SolanaEvents.d.ts +68 -68
- package/dist/solana/chain/modules/SolanaEvents.js +225 -225
- package/dist/solana/chain/modules/SolanaFees.d.ts +121 -121
- package/dist/solana/chain/modules/SolanaFees.js +379 -379
- package/dist/solana/chain/modules/SolanaSignatures.d.ts +23 -23
- package/dist/solana/chain/modules/SolanaSignatures.js +39 -39
- package/dist/solana/chain/modules/SolanaSlots.d.ts +31 -31
- package/dist/solana/chain/modules/SolanaSlots.js +68 -68
- package/dist/solana/chain/modules/SolanaTokens.d.ts +136 -136
- package/dist/solana/chain/modules/SolanaTokens.js +248 -248
- package/dist/solana/chain/modules/SolanaTransactions.d.ts +124 -124
- package/dist/solana/chain/modules/SolanaTransactions.js +323 -323
- package/dist/solana/events/SolanaChainEvents.d.ts +88 -88
- package/dist/solana/events/SolanaChainEvents.js +256 -256
- package/dist/solana/events/SolanaChainEventsBrowser.d.ts +75 -75
- package/dist/solana/events/SolanaChainEventsBrowser.js +172 -172
- package/dist/solana/program/SolanaProgramBase.d.ts +40 -40
- package/dist/solana/program/SolanaProgramBase.js +43 -43
- package/dist/solana/program/SolanaProgramModule.d.ts +8 -8
- package/dist/solana/program/SolanaProgramModule.js +11 -11
- package/dist/solana/program/modules/SolanaProgramEvents.d.ts +53 -53
- package/dist/solana/program/modules/SolanaProgramEvents.js +114 -114
- package/dist/solana/swaps/SolanaSwapData.d.ts +71 -71
- package/dist/solana/swaps/SolanaSwapData.js +292 -292
- package/dist/solana/swaps/SolanaSwapModule.d.ts +10 -10
- package/dist/solana/swaps/SolanaSwapModule.js +11 -11
- package/dist/solana/swaps/SolanaSwapProgram.d.ts +224 -224
- package/dist/solana/swaps/SolanaSwapProgram.js +570 -570
- package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -11
- package/dist/solana/swaps/SwapTypeEnum.js +42 -42
- package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +94 -94
- package/dist/solana/swaps/modules/SolanaDataAccount.js +231 -231
- package/dist/solana/swaps/modules/SolanaLpVault.d.ts +71 -71
- package/dist/solana/swaps/modules/SolanaLpVault.js +173 -173
- package/dist/solana/swaps/modules/SwapClaim.d.ts +129 -129
- package/dist/solana/swaps/modules/SwapClaim.js +291 -291
- package/dist/solana/swaps/modules/SwapInit.d.ts +217 -217
- package/dist/solana/swaps/modules/SwapInit.js +519 -519
- package/dist/solana/swaps/modules/SwapRefund.d.ts +82 -82
- package/dist/solana/swaps/modules/SwapRefund.js +262 -262
- package/dist/solana/swaps/programIdl.json +945 -945
- package/dist/solana/swaps/programTypes.d.ts +943 -943
- package/dist/solana/swaps/programTypes.js +945 -945
- package/dist/solana/wallet/SolanaKeypairWallet.d.ts +9 -9
- package/dist/solana/wallet/SolanaKeypairWallet.js +33 -33
- package/dist/solana/wallet/SolanaSigner.d.ts +11 -11
- package/dist/solana/wallet/SolanaSigner.js +17 -17
- package/dist/utils/Utils.d.ts +53 -53
- package/dist/utils/Utils.js +170 -170
- package/package.json +41 -41
- package/src/index.ts +36 -36
- package/src/solana/SolanaChainType.ts +27 -27
- package/src/solana/SolanaChains.ts +23 -23
- package/src/solana/SolanaInitializer.ts +102 -102
- package/src/solana/btcrelay/SolanaBtcRelay.ts +589 -589
- package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +57 -57
- package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +102 -102
- package/src/solana/btcrelay/program/programIdl.json +670 -670
- package/src/solana/chain/SolanaAction.ts +108 -108
- package/src/solana/chain/SolanaChainInterface.ts +192 -192
- package/src/solana/chain/SolanaModule.ts +20 -20
- package/src/solana/chain/modules/SolanaAddresses.ts +20 -20
- package/src/solana/chain/modules/SolanaBlocks.ts +78 -78
- package/src/solana/chain/modules/SolanaEvents.ts +256 -256
- package/src/solana/chain/modules/SolanaFees.ts +450 -450
- package/src/solana/chain/modules/SolanaSignatures.ts +39 -39
- package/src/solana/chain/modules/SolanaSlots.ts +82 -82
- package/src/solana/chain/modules/SolanaTokens.ts +307 -307
- package/src/solana/chain/modules/SolanaTransactions.ts +365 -365
- package/src/solana/events/SolanaChainEvents.ts +299 -299
- package/src/solana/events/SolanaChainEventsBrowser.ts +209 -209
- package/src/solana/program/SolanaProgramBase.ts +79 -79
- package/src/solana/program/SolanaProgramModule.ts +15 -15
- package/src/solana/program/modules/SolanaProgramEvents.ts +155 -155
- package/src/solana/swaps/SolanaSwapData.ts +430 -430
- package/src/solana/swaps/SolanaSwapModule.ts +16 -16
- package/src/solana/swaps/SolanaSwapProgram.ts +854 -854
- package/src/solana/swaps/SwapTypeEnum.ts +29 -29
- package/src/solana/swaps/modules/SolanaDataAccount.ts +307 -307
- package/src/solana/swaps/modules/SolanaLpVault.ts +215 -215
- package/src/solana/swaps/modules/SwapClaim.ts +389 -389
- package/src/solana/swaps/modules/SwapInit.ts +663 -663
- package/src/solana/swaps/modules/SwapRefund.ts +323 -323
- package/src/solana/swaps/programIdl.json +944 -944
- package/src/solana/swaps/programTypes.ts +1885 -1885
- package/src/solana/wallet/SolanaKeypairWallet.ts +36 -36
- package/src/solana/wallet/SolanaSigner.ts +24 -24
- package/src/utils/Utils.ts +180 -180
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import {Wallet} from "@coral-xyz/anchor/dist/cjs/provider";
|
|
2
|
-
import {Keypair, PublicKey, Transaction, VersionedTransaction} from "@solana/web3.js";
|
|
3
|
-
|
|
4
|
-
export class SolanaKeypairWallet implements Wallet {
|
|
5
|
-
|
|
6
|
-
readonly payer: Keypair;
|
|
7
|
-
|
|
8
|
-
constructor(payer: Keypair) {
|
|
9
|
-
this.payer = payer;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
get publicKey(): PublicKey {
|
|
13
|
-
return this.payer.publicKey;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
signAllTransactions<T extends Transaction | VersionedTransaction>(txs: T[]): Promise<T[]> {
|
|
17
|
-
txs.forEach((tx) => {
|
|
18
|
-
if(tx instanceof Transaction) {
|
|
19
|
-
tx.partialSign(this.payer);
|
|
20
|
-
} else if(tx instanceof VersionedTransaction) {
|
|
21
|
-
tx.sign([this.payer]);
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
return Promise.resolve(txs);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
signTransaction<T extends Transaction | VersionedTransaction>(tx: T): Promise<T> {
|
|
28
|
-
if(tx instanceof Transaction) {
|
|
29
|
-
tx.partialSign(this.payer);
|
|
30
|
-
} else if(tx instanceof VersionedTransaction) {
|
|
31
|
-
tx.sign([this.payer]);
|
|
32
|
-
}
|
|
33
|
-
return Promise.resolve(tx);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
}
|
|
1
|
+
import {Wallet} from "@coral-xyz/anchor/dist/cjs/provider";
|
|
2
|
+
import {Keypair, PublicKey, Transaction, VersionedTransaction} from "@solana/web3.js";
|
|
3
|
+
|
|
4
|
+
export class SolanaKeypairWallet implements Wallet {
|
|
5
|
+
|
|
6
|
+
readonly payer: Keypair;
|
|
7
|
+
|
|
8
|
+
constructor(payer: Keypair) {
|
|
9
|
+
this.payer = payer;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get publicKey(): PublicKey {
|
|
13
|
+
return this.payer.publicKey;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
signAllTransactions<T extends Transaction | VersionedTransaction>(txs: T[]): Promise<T[]> {
|
|
17
|
+
txs.forEach((tx) => {
|
|
18
|
+
if(tx instanceof Transaction) {
|
|
19
|
+
tx.partialSign(this.payer);
|
|
20
|
+
} else if(tx instanceof VersionedTransaction) {
|
|
21
|
+
tx.sign([this.payer]);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return Promise.resolve(txs);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
signTransaction<T extends Transaction | VersionedTransaction>(tx: T): Promise<T> {
|
|
28
|
+
if(tx instanceof Transaction) {
|
|
29
|
+
tx.partialSign(this.payer);
|
|
30
|
+
} else if(tx instanceof VersionedTransaction) {
|
|
31
|
+
tx.sign([this.payer]);
|
|
32
|
+
}
|
|
33
|
+
return Promise.resolve(tx);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
}
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { Wallet } from "@coral-xyz/anchor/dist/cjs/provider";
|
|
2
|
-
import {AbstractSigner} from "@atomiqlabs/base";
|
|
3
|
-
import {PublicKey, Signer} from "@solana/web3.js";
|
|
4
|
-
|
|
5
|
-
export class SolanaSigner implements AbstractSigner {
|
|
6
|
-
type = "AtomiqAbstractSigner" as const;
|
|
7
|
-
|
|
8
|
-
wallet: Wallet;
|
|
9
|
-
keypair?: Signer;
|
|
10
|
-
|
|
11
|
-
constructor(wallet: Wallet, keypair?: Signer) {
|
|
12
|
-
this.wallet = wallet;
|
|
13
|
-
this.keypair = keypair;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
getPublicKey(): PublicKey {
|
|
17
|
-
return this.wallet.publicKey;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
getAddress(): string {
|
|
21
|
-
return this.wallet.publicKey.toString();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
}
|
|
1
|
+
import { Wallet } from "@coral-xyz/anchor/dist/cjs/provider";
|
|
2
|
+
import {AbstractSigner} from "@atomiqlabs/base";
|
|
3
|
+
import {PublicKey, Signer} from "@solana/web3.js";
|
|
4
|
+
|
|
5
|
+
export class SolanaSigner implements AbstractSigner {
|
|
6
|
+
type = "AtomiqAbstractSigner" as const;
|
|
7
|
+
|
|
8
|
+
wallet: Wallet;
|
|
9
|
+
keypair?: Signer;
|
|
10
|
+
|
|
11
|
+
constructor(wallet: Wallet, keypair?: Signer) {
|
|
12
|
+
this.wallet = wallet;
|
|
13
|
+
this.keypair = keypair;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getPublicKey(): PublicKey {
|
|
17
|
+
return this.wallet.publicKey;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
getAddress(): string {
|
|
21
|
+
return this.wallet.publicKey.toString();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
}
|
package/src/utils/Utils.ts
CHANGED
|
@@ -1,180 +1,180 @@
|
|
|
1
|
-
import {ComputeBudgetProgram, PublicKey, Transaction} from "@solana/web3.js";
|
|
2
|
-
import * as BN from "bn.js";
|
|
3
|
-
import {Buffer} from "buffer";
|
|
4
|
-
import {sha256} from "@noble/hashes/sha2";
|
|
5
|
-
|
|
6
|
-
export function timeoutPromise(timeoutMillis: number, abortSignal?: AbortSignal): Promise<void> {
|
|
7
|
-
return new Promise((resolve, reject) => {
|
|
8
|
-
const timeout = setTimeout(resolve, timeoutMillis)
|
|
9
|
-
if(abortSignal!=null) abortSignal.addEventListener("abort", () => {
|
|
10
|
-
clearTimeout(timeout);
|
|
11
|
-
reject(new Error("Aborted"));
|
|
12
|
-
})
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function onceAsync<T>(executor: () => Promise<T>): () => Promise<T> {
|
|
17
|
-
let promise: Promise<T>;
|
|
18
|
-
|
|
19
|
-
return () => {
|
|
20
|
-
if(promise==null) {
|
|
21
|
-
promise = executor();
|
|
22
|
-
return promise;
|
|
23
|
-
} else {
|
|
24
|
-
return promise.catch(() => promise = executor());
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function getLogger(prefix: string) {
|
|
30
|
-
return {
|
|
31
|
-
debug: (msg, ...args) => global.atomiqLogLevel >= 3 && console.debug(prefix+msg, ...args),
|
|
32
|
-
info: (msg, ...args) => global.atomiqLogLevel >= 2 && console.info(prefix+msg, ...args),
|
|
33
|
-
warn: (msg, ...args) => (global.atomiqLogLevel==null || global.atomiqLogLevel >= 1) && console.warn(prefix+msg, ...args),
|
|
34
|
-
error: (msg, ...args) => (global.atomiqLogLevel==null || global.atomiqLogLevel >= 0) && console.error(prefix+msg, ...args)
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const logger = getLogger("Utils: ");
|
|
39
|
-
|
|
40
|
-
export async function tryWithRetries<T>(func: () => Promise<T>, retryPolicy?: {
|
|
41
|
-
maxRetries?: number, delay?: number, exponential?: boolean
|
|
42
|
-
}, errorAllowed?: (e: any) => boolean, abortSignal?: AbortSignal): Promise<T> {
|
|
43
|
-
retryPolicy = retryPolicy || {};
|
|
44
|
-
retryPolicy.maxRetries = retryPolicy.maxRetries || 5;
|
|
45
|
-
retryPolicy.delay = retryPolicy.delay || 500;
|
|
46
|
-
retryPolicy.exponential = retryPolicy.exponential==null ? true : retryPolicy.exponential;
|
|
47
|
-
|
|
48
|
-
let err = null;
|
|
49
|
-
|
|
50
|
-
for(let i=0;i<retryPolicy.maxRetries;i++) {
|
|
51
|
-
try {
|
|
52
|
-
const resp: T = await func();
|
|
53
|
-
return resp;
|
|
54
|
-
} catch (e) {
|
|
55
|
-
if(errorAllowed!=null && errorAllowed(e)) throw e;
|
|
56
|
-
err = e;
|
|
57
|
-
logger.error("tryWithRetries(): error on try number: "+i, e);
|
|
58
|
-
}
|
|
59
|
-
if(abortSignal!=null && abortSignal.aborted) throw new Error("Aborted");
|
|
60
|
-
if(i!==retryPolicy.maxRetries-1) {
|
|
61
|
-
await timeoutPromise(
|
|
62
|
-
retryPolicy.exponential ? retryPolicy.delay*Math.pow(2, i) : retryPolicy.delay,
|
|
63
|
-
abortSignal
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
throw err;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export class SolanaTxUtils {
|
|
72
|
-
// COMPACT ARRAY
|
|
73
|
-
private static LOW_VALUE = 127; // 0x7f
|
|
74
|
-
private static HIGH_VALUE = 16383; // 0x3fff
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Compact u16 array header size
|
|
78
|
-
* @param n elements in the compact array
|
|
79
|
-
* @returns size in bytes of array header
|
|
80
|
-
*/
|
|
81
|
-
private static compactHeader(n: number): number {
|
|
82
|
-
return (n <= SolanaTxUtils.LOW_VALUE ? 1 : n <= SolanaTxUtils.HIGH_VALUE ? 2 : 3);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Compact u16 array size
|
|
87
|
-
* @param n elements in the compact array
|
|
88
|
-
* @param size bytes per each element
|
|
89
|
-
* @returns size in bytes of array
|
|
90
|
-
*/
|
|
91
|
-
private static compactArraySize(n: number, size: number): number {
|
|
92
|
-
return SolanaTxUtils.compactHeader(n) + n * size;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Returns # number of non-compute budget related instructions
|
|
97
|
-
*
|
|
98
|
-
* @param tx
|
|
99
|
-
*/
|
|
100
|
-
public static getNonComputeBudgetIxs(tx: Transaction): number {
|
|
101
|
-
let counter = 0;
|
|
102
|
-
for(let ix of tx.instructions) {
|
|
103
|
-
if(!ix.programId.equals(ComputeBudgetProgram.programId)) counter++;
|
|
104
|
-
}
|
|
105
|
-
return counter;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* @param tx a solana transaction
|
|
110
|
-
* @param feePayer the publicKey of the signer
|
|
111
|
-
* @returns size in bytes of the transaction
|
|
112
|
-
*/
|
|
113
|
-
public static getTxSize(tx: Transaction, feePayer: PublicKey): number {
|
|
114
|
-
const feePayerPk = [feePayer.toBase58()];
|
|
115
|
-
|
|
116
|
-
const signers = new Set<string>(feePayerPk);
|
|
117
|
-
const accounts = new Set<string>(feePayerPk);
|
|
118
|
-
|
|
119
|
-
const ixsSize = tx.instructions.reduce((acc, ix) => {
|
|
120
|
-
ix.keys.forEach(({ pubkey, isSigner }) => {
|
|
121
|
-
const pk = pubkey.toBase58();
|
|
122
|
-
if (isSigner) signers.add(pk);
|
|
123
|
-
accounts.add(pk);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
accounts.add(ix.programId.toBase58());
|
|
127
|
-
|
|
128
|
-
const nIndexes = ix.keys.length;
|
|
129
|
-
const opaqueData = ix.data.length;
|
|
130
|
-
|
|
131
|
-
return (
|
|
132
|
-
acc +
|
|
133
|
-
1 + // PID index
|
|
134
|
-
SolanaTxUtils.compactArraySize(nIndexes, 1) +
|
|
135
|
-
SolanaTxUtils.compactArraySize(opaqueData, 1)
|
|
136
|
-
);
|
|
137
|
-
}, 0);
|
|
138
|
-
|
|
139
|
-
return (
|
|
140
|
-
SolanaTxUtils.compactArraySize(signers.size, 64) + // signatures
|
|
141
|
-
3 + // header
|
|
142
|
-
SolanaTxUtils.compactArraySize(accounts.size, 32) + // accounts
|
|
143
|
-
32 + // blockhash
|
|
144
|
-
SolanaTxUtils.compactHeader(tx.instructions.length) + // instructions
|
|
145
|
-
ixsSize
|
|
146
|
-
);
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function toClaimHash(paymentHash: string, nonce: bigint, confirmations: number): string {
|
|
151
|
-
return paymentHash+
|
|
152
|
-
nonce.toString(16).padStart(16, "0")+
|
|
153
|
-
confirmations.toString(16).padStart(4, "0");
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export function fromClaimHash(claimHash: string): {paymentHash: string, nonce: BN, confirmations: number} {
|
|
157
|
-
if(claimHash.length!==84) throw new Error("Claim hash invalid length: "+claimHash.length);
|
|
158
|
-
return {
|
|
159
|
-
paymentHash: claimHash.slice(0, 64),
|
|
160
|
-
nonce: new BN(claimHash.slice(64, 80), "hex"),
|
|
161
|
-
confirmations: parseInt(claimHash.slice(80, 84), 16)
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export function toEscrowHash(paymentHash: string, sequence: BN): string {
|
|
166
|
-
return Buffer.from(sha256(Buffer.concat([
|
|
167
|
-
Buffer.from(paymentHash, "hex"),
|
|
168
|
-
sequence.toArrayLike(Buffer, "be", 8)
|
|
169
|
-
]))).toString("hex");
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export function toBN(value: bigint): BN {
|
|
173
|
-
if(value==null) return null;
|
|
174
|
-
return new BN(value.toString(10));
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function toBigInt(value: BN): bigint {
|
|
178
|
-
if(value==null) return null;
|
|
179
|
-
return BigInt(value.toString(10));
|
|
180
|
-
}
|
|
1
|
+
import {ComputeBudgetProgram, PublicKey, Transaction} from "@solana/web3.js";
|
|
2
|
+
import * as BN from "bn.js";
|
|
3
|
+
import {Buffer} from "buffer";
|
|
4
|
+
import {sha256} from "@noble/hashes/sha2";
|
|
5
|
+
|
|
6
|
+
export function timeoutPromise(timeoutMillis: number, abortSignal?: AbortSignal): Promise<void> {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
const timeout = setTimeout(resolve, timeoutMillis)
|
|
9
|
+
if(abortSignal!=null) abortSignal.addEventListener("abort", () => {
|
|
10
|
+
clearTimeout(timeout);
|
|
11
|
+
reject(new Error("Aborted"));
|
|
12
|
+
})
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function onceAsync<T>(executor: () => Promise<T>): () => Promise<T> {
|
|
17
|
+
let promise: Promise<T>;
|
|
18
|
+
|
|
19
|
+
return () => {
|
|
20
|
+
if(promise==null) {
|
|
21
|
+
promise = executor();
|
|
22
|
+
return promise;
|
|
23
|
+
} else {
|
|
24
|
+
return promise.catch(() => promise = executor());
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function getLogger(prefix: string) {
|
|
30
|
+
return {
|
|
31
|
+
debug: (msg, ...args) => global.atomiqLogLevel >= 3 && console.debug(prefix+msg, ...args),
|
|
32
|
+
info: (msg, ...args) => global.atomiqLogLevel >= 2 && console.info(prefix+msg, ...args),
|
|
33
|
+
warn: (msg, ...args) => (global.atomiqLogLevel==null || global.atomiqLogLevel >= 1) && console.warn(prefix+msg, ...args),
|
|
34
|
+
error: (msg, ...args) => (global.atomiqLogLevel==null || global.atomiqLogLevel >= 0) && console.error(prefix+msg, ...args)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const logger = getLogger("Utils: ");
|
|
39
|
+
|
|
40
|
+
export async function tryWithRetries<T>(func: () => Promise<T>, retryPolicy?: {
|
|
41
|
+
maxRetries?: number, delay?: number, exponential?: boolean
|
|
42
|
+
}, errorAllowed?: (e: any) => boolean, abortSignal?: AbortSignal): Promise<T> {
|
|
43
|
+
retryPolicy = retryPolicy || {};
|
|
44
|
+
retryPolicy.maxRetries = retryPolicy.maxRetries || 5;
|
|
45
|
+
retryPolicy.delay = retryPolicy.delay || 500;
|
|
46
|
+
retryPolicy.exponential = retryPolicy.exponential==null ? true : retryPolicy.exponential;
|
|
47
|
+
|
|
48
|
+
let err = null;
|
|
49
|
+
|
|
50
|
+
for(let i=0;i<retryPolicy.maxRetries;i++) {
|
|
51
|
+
try {
|
|
52
|
+
const resp: T = await func();
|
|
53
|
+
return resp;
|
|
54
|
+
} catch (e) {
|
|
55
|
+
if(errorAllowed!=null && errorAllowed(e)) throw e;
|
|
56
|
+
err = e;
|
|
57
|
+
logger.error("tryWithRetries(): error on try number: "+i, e);
|
|
58
|
+
}
|
|
59
|
+
if(abortSignal!=null && abortSignal.aborted) throw new Error("Aborted");
|
|
60
|
+
if(i!==retryPolicy.maxRetries-1) {
|
|
61
|
+
await timeoutPromise(
|
|
62
|
+
retryPolicy.exponential ? retryPolicy.delay*Math.pow(2, i) : retryPolicy.delay,
|
|
63
|
+
abortSignal
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class SolanaTxUtils {
|
|
72
|
+
// COMPACT ARRAY
|
|
73
|
+
private static LOW_VALUE = 127; // 0x7f
|
|
74
|
+
private static HIGH_VALUE = 16383; // 0x3fff
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Compact u16 array header size
|
|
78
|
+
* @param n elements in the compact array
|
|
79
|
+
* @returns size in bytes of array header
|
|
80
|
+
*/
|
|
81
|
+
private static compactHeader(n: number): number {
|
|
82
|
+
return (n <= SolanaTxUtils.LOW_VALUE ? 1 : n <= SolanaTxUtils.HIGH_VALUE ? 2 : 3);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Compact u16 array size
|
|
87
|
+
* @param n elements in the compact array
|
|
88
|
+
* @param size bytes per each element
|
|
89
|
+
* @returns size in bytes of array
|
|
90
|
+
*/
|
|
91
|
+
private static compactArraySize(n: number, size: number): number {
|
|
92
|
+
return SolanaTxUtils.compactHeader(n) + n * size;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Returns # number of non-compute budget related instructions
|
|
97
|
+
*
|
|
98
|
+
* @param tx
|
|
99
|
+
*/
|
|
100
|
+
public static getNonComputeBudgetIxs(tx: Transaction): number {
|
|
101
|
+
let counter = 0;
|
|
102
|
+
for(let ix of tx.instructions) {
|
|
103
|
+
if(!ix.programId.equals(ComputeBudgetProgram.programId)) counter++;
|
|
104
|
+
}
|
|
105
|
+
return counter;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @param tx a solana transaction
|
|
110
|
+
* @param feePayer the publicKey of the signer
|
|
111
|
+
* @returns size in bytes of the transaction
|
|
112
|
+
*/
|
|
113
|
+
public static getTxSize(tx: Transaction, feePayer: PublicKey): number {
|
|
114
|
+
const feePayerPk = [feePayer.toBase58()];
|
|
115
|
+
|
|
116
|
+
const signers = new Set<string>(feePayerPk);
|
|
117
|
+
const accounts = new Set<string>(feePayerPk);
|
|
118
|
+
|
|
119
|
+
const ixsSize = tx.instructions.reduce((acc, ix) => {
|
|
120
|
+
ix.keys.forEach(({ pubkey, isSigner }) => {
|
|
121
|
+
const pk = pubkey.toBase58();
|
|
122
|
+
if (isSigner) signers.add(pk);
|
|
123
|
+
accounts.add(pk);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
accounts.add(ix.programId.toBase58());
|
|
127
|
+
|
|
128
|
+
const nIndexes = ix.keys.length;
|
|
129
|
+
const opaqueData = ix.data.length;
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
acc +
|
|
133
|
+
1 + // PID index
|
|
134
|
+
SolanaTxUtils.compactArraySize(nIndexes, 1) +
|
|
135
|
+
SolanaTxUtils.compactArraySize(opaqueData, 1)
|
|
136
|
+
);
|
|
137
|
+
}, 0);
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
SolanaTxUtils.compactArraySize(signers.size, 64) + // signatures
|
|
141
|
+
3 + // header
|
|
142
|
+
SolanaTxUtils.compactArraySize(accounts.size, 32) + // accounts
|
|
143
|
+
32 + // blockhash
|
|
144
|
+
SolanaTxUtils.compactHeader(tx.instructions.length) + // instructions
|
|
145
|
+
ixsSize
|
|
146
|
+
);
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function toClaimHash(paymentHash: string, nonce: bigint, confirmations: number): string {
|
|
151
|
+
return paymentHash+
|
|
152
|
+
nonce.toString(16).padStart(16, "0")+
|
|
153
|
+
confirmations.toString(16).padStart(4, "0");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function fromClaimHash(claimHash: string): {paymentHash: string, nonce: BN, confirmations: number} {
|
|
157
|
+
if(claimHash.length!==84) throw new Error("Claim hash invalid length: "+claimHash.length);
|
|
158
|
+
return {
|
|
159
|
+
paymentHash: claimHash.slice(0, 64),
|
|
160
|
+
nonce: new BN(claimHash.slice(64, 80), "hex"),
|
|
161
|
+
confirmations: parseInt(claimHash.slice(80, 84), 16)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function toEscrowHash(paymentHash: string, sequence: BN): string {
|
|
166
|
+
return Buffer.from(sha256(Buffer.concat([
|
|
167
|
+
Buffer.from(paymentHash, "hex"),
|
|
168
|
+
sequence.toArrayLike(Buffer, "be", 8)
|
|
169
|
+
]))).toString("hex");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function toBN(value: bigint): BN {
|
|
173
|
+
if(value==null) return null;
|
|
174
|
+
return new BN(value.toString(10));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function toBigInt(value: BN): bigint {
|
|
178
|
+
if(value==null) return null;
|
|
179
|
+
return BigInt(value.toString(10));
|
|
180
|
+
}
|