@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.
- package/LICENSE +201 -201
- package/dist/index.d.ts +29 -29
- package/dist/index.js +45 -45
- package/dist/solana/SolanaChainType.d.ts +10 -10
- 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 +58 -58
- package/dist/solana/chain/SolanaChainInterface.js +112 -112
- 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 +25 -25
- package/dist/solana/chain/modules/SolanaEvents.js +58 -58
- 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 +332 -332
- package/dist/solana/events/SolanaChainEvents.d.ts +88 -88
- package/dist/solana/events/SolanaChainEvents.js +256 -256
- package/dist/solana/events/SolanaChainEventsBrowser.d.ts +85 -85
- package/dist/solana/events/SolanaChainEventsBrowser.js +194 -194
- 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 +59 -59
- package/dist/solana/program/modules/SolanaProgramEvents.js +103 -103
- package/dist/solana/swaps/SolanaSwapData.d.ts +59 -59
- package/dist/solana/swaps/SolanaSwapData.js +267 -267
- package/dist/solana/swaps/SolanaSwapModule.d.ts +10 -10
- package/dist/solana/swaps/SolanaSwapModule.js +11 -11
- package/dist/solana/swaps/SolanaSwapProgram.d.ts +202 -202
- package/dist/solana/swaps/SolanaSwapProgram.js +470 -463
- 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 +252 -252
- 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 +10 -10
- package/dist/solana/wallet/SolanaSigner.js +16 -16
- 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 +25 -25
- package/src/solana/SolanaChains.ts +23 -23
- package/src/solana/SolanaInitializer.ts +102 -102
- package/src/solana/btcrelay/SolanaBtcRelay.ts +589 -588
- 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 +174 -174
- 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 +56 -56
- 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 +370 -370
- package/src/solana/events/SolanaChainEvents.ts +299 -299
- package/src/solana/events/SolanaChainEventsBrowser.ts +256 -256
- package/src/solana/program/SolanaProgramBase.ts +79 -79
- package/src/solana/program/SolanaProgramModule.ts +15 -15
- package/src/solana/program/modules/SolanaProgramEvents.ts +140 -140
- package/src/solana/swaps/SolanaSwapData.ts +379 -379
- package/src/solana/swaps/SolanaSwapModule.ts +16 -16
- package/src/solana/swaps/SolanaSwapProgram.ts +697 -692
- 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 +312 -312
- 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 +23 -23
- package/src/utils/Utils.ts +180 -180
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
import {AnchorProvider, Idl, Program} from "@coral-xyz/anchor";
|
|
2
|
-
import {SolanaFees} from "../chain/modules/SolanaFees";
|
|
3
|
-
import {SolanaChainInterface, SolanaRetryPolicy} from "../chain/SolanaChainInterface";
|
|
4
|
-
import {SolanaProgramEvents} from "./modules/SolanaProgramEvents";
|
|
5
|
-
import {Connection, Keypair, PublicKey} from "@solana/web3.js";
|
|
6
|
-
import {sha256} from "@noble/hashes/sha2";
|
|
7
|
-
import {Buffer} from "buffer";
|
|
8
|
-
import {SolanaKeypairWallet} from "../wallet/SolanaKeypairWallet";
|
|
9
|
-
import {getLogger} from "../../utils/Utils";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Base class providing program specific utilities
|
|
13
|
-
*/
|
|
14
|
-
export class SolanaProgramBase<T extends Idl> {
|
|
15
|
-
|
|
16
|
-
protected readonly logger = getLogger(this.constructor.name+": ");
|
|
17
|
-
|
|
18
|
-
program: Program<T>;
|
|
19
|
-
|
|
20
|
-
public readonly Events: SolanaProgramEvents<T>;
|
|
21
|
-
public readonly Chain: SolanaChainInterface;
|
|
22
|
-
|
|
23
|
-
constructor(
|
|
24
|
-
chainInterface: SolanaChainInterface,
|
|
25
|
-
programIdl: any,
|
|
26
|
-
programAddress?: string
|
|
27
|
-
) {
|
|
28
|
-
this.Chain = chainInterface;
|
|
29
|
-
|
|
30
|
-
this.program = new Program<T>(
|
|
31
|
-
programIdl as any,
|
|
32
|
-
programAddress || programIdl.metadata.address,
|
|
33
|
-
new AnchorProvider(chainInterface.connection, new SolanaKeypairWallet(Keypair.generate()), {})
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
this.Events = new SolanaProgramEvents(chainInterface, this);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Derives static PDA address from the seed
|
|
41
|
-
*
|
|
42
|
-
* @param seed
|
|
43
|
-
*/
|
|
44
|
-
public pda(seed: string): PublicKey;
|
|
45
|
-
/**
|
|
46
|
-
* Returns a function for deriving a dynamic PDA address from seed + dynamic arguments
|
|
47
|
-
*
|
|
48
|
-
* @param seed
|
|
49
|
-
* @param func function translating the function argument to Buffer[]
|
|
50
|
-
*/
|
|
51
|
-
public pda<T extends Array<any>>(seed: string, func: (...args: T) => Buffer[]): (...args: T) => PublicKey;
|
|
52
|
-
public pda<T extends Array<any>>(seed: string, func?: (...args: T) => Buffer[]): PublicKey | ((...args: T) => PublicKey) {
|
|
53
|
-
if(func==null) {
|
|
54
|
-
return PublicKey.findProgramAddressSync(
|
|
55
|
-
[Buffer.from(seed)],
|
|
56
|
-
this.program.programId
|
|
57
|
-
)[0];
|
|
58
|
-
}
|
|
59
|
-
return (...args: T) => {
|
|
60
|
-
const res = func(...args);
|
|
61
|
-
return PublicKey.findProgramAddressSync(
|
|
62
|
-
[Buffer.from(seed)].concat(res),
|
|
63
|
-
this.program.programId
|
|
64
|
-
)[0]
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Returns a function for deriving a dynamic deterministic keypair from dynamic arguments
|
|
69
|
-
*
|
|
70
|
-
* @param func function translating the function argument to Buffer[] to be used for deriving the keypair
|
|
71
|
-
*/
|
|
72
|
-
public keypair<T extends Array<any>>(func: (...args: T) => Buffer[]): (...args: T) => Keypair {
|
|
73
|
-
return (...args: T) => {
|
|
74
|
-
const res = func(...args);
|
|
75
|
-
const buff = sha256(Buffer.concat(res));
|
|
76
|
-
return Keypair.fromSeed(buff);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
1
|
+
import {AnchorProvider, Idl, Program} from "@coral-xyz/anchor";
|
|
2
|
+
import {SolanaFees} from "../chain/modules/SolanaFees";
|
|
3
|
+
import {SolanaChainInterface, SolanaRetryPolicy} from "../chain/SolanaChainInterface";
|
|
4
|
+
import {SolanaProgramEvents} from "./modules/SolanaProgramEvents";
|
|
5
|
+
import {Connection, Keypair, PublicKey} from "@solana/web3.js";
|
|
6
|
+
import {sha256} from "@noble/hashes/sha2";
|
|
7
|
+
import {Buffer} from "buffer";
|
|
8
|
+
import {SolanaKeypairWallet} from "../wallet/SolanaKeypairWallet";
|
|
9
|
+
import {getLogger} from "../../utils/Utils";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Base class providing program specific utilities
|
|
13
|
+
*/
|
|
14
|
+
export class SolanaProgramBase<T extends Idl> {
|
|
15
|
+
|
|
16
|
+
protected readonly logger = getLogger(this.constructor.name+": ");
|
|
17
|
+
|
|
18
|
+
program: Program<T>;
|
|
19
|
+
|
|
20
|
+
public readonly Events: SolanaProgramEvents<T>;
|
|
21
|
+
public readonly Chain: SolanaChainInterface;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
chainInterface: SolanaChainInterface,
|
|
25
|
+
programIdl: any,
|
|
26
|
+
programAddress?: string
|
|
27
|
+
) {
|
|
28
|
+
this.Chain = chainInterface;
|
|
29
|
+
|
|
30
|
+
this.program = new Program<T>(
|
|
31
|
+
programIdl as any,
|
|
32
|
+
programAddress || programIdl.metadata.address,
|
|
33
|
+
new AnchorProvider(chainInterface.connection, new SolanaKeypairWallet(Keypair.generate()), {})
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
this.Events = new SolanaProgramEvents(chainInterface, this);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Derives static PDA address from the seed
|
|
41
|
+
*
|
|
42
|
+
* @param seed
|
|
43
|
+
*/
|
|
44
|
+
public pda(seed: string): PublicKey;
|
|
45
|
+
/**
|
|
46
|
+
* Returns a function for deriving a dynamic PDA address from seed + dynamic arguments
|
|
47
|
+
*
|
|
48
|
+
* @param seed
|
|
49
|
+
* @param func function translating the function argument to Buffer[]
|
|
50
|
+
*/
|
|
51
|
+
public pda<T extends Array<any>>(seed: string, func: (...args: T) => Buffer[]): (...args: T) => PublicKey;
|
|
52
|
+
public pda<T extends Array<any>>(seed: string, func?: (...args: T) => Buffer[]): PublicKey | ((...args: T) => PublicKey) {
|
|
53
|
+
if(func==null) {
|
|
54
|
+
return PublicKey.findProgramAddressSync(
|
|
55
|
+
[Buffer.from(seed)],
|
|
56
|
+
this.program.programId
|
|
57
|
+
)[0];
|
|
58
|
+
}
|
|
59
|
+
return (...args: T) => {
|
|
60
|
+
const res = func(...args);
|
|
61
|
+
return PublicKey.findProgramAddressSync(
|
|
62
|
+
[Buffer.from(seed)].concat(res),
|
|
63
|
+
this.program.programId
|
|
64
|
+
)[0]
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Returns a function for deriving a dynamic deterministic keypair from dynamic arguments
|
|
69
|
+
*
|
|
70
|
+
* @param func function translating the function argument to Buffer[] to be used for deriving the keypair
|
|
71
|
+
*/
|
|
72
|
+
public keypair<T extends Array<any>>(func: (...args: T) => Buffer[]): (...args: T) => Keypair {
|
|
73
|
+
return (...args: T) => {
|
|
74
|
+
const res = func(...args);
|
|
75
|
+
const buff = sha256(Buffer.concat(res));
|
|
76
|
+
return Keypair.fromSeed(buff);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
80
|
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {SolanaModule} from "../chain/SolanaModule";
|
|
2
|
-
import {Idl} from "@coral-xyz/anchor";
|
|
3
|
-
import {SolanaProgramBase} from "./SolanaProgramBase";
|
|
4
|
-
import {SolanaChainInterface} from "../chain/SolanaChainInterface";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export class SolanaProgramModule<IDL extends Idl> extends SolanaModule {
|
|
8
|
-
|
|
9
|
-
protected readonly program: SolanaProgramBase<IDL>;
|
|
10
|
-
|
|
11
|
-
constructor(chainInterface: SolanaChainInterface, program: SolanaProgramBase<IDL>) {
|
|
12
|
-
super(chainInterface);
|
|
13
|
-
this.program = program;
|
|
14
|
-
}
|
|
15
|
-
|
|
1
|
+
import {SolanaModule} from "../chain/SolanaModule";
|
|
2
|
+
import {Idl} from "@coral-xyz/anchor";
|
|
3
|
+
import {SolanaProgramBase} from "./SolanaProgramBase";
|
|
4
|
+
import {SolanaChainInterface} from "../chain/SolanaChainInterface";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export class SolanaProgramModule<IDL extends Idl> extends SolanaModule {
|
|
8
|
+
|
|
9
|
+
protected readonly program: SolanaProgramBase<IDL>;
|
|
10
|
+
|
|
11
|
+
constructor(chainInterface: SolanaChainInterface, program: SolanaProgramBase<IDL>) {
|
|
12
|
+
super(chainInterface);
|
|
13
|
+
this.program = program;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
16
|
}
|
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
import {SolanaEvents} from "../../chain/modules/SolanaEvents";
|
|
2
|
-
import {BorshCoder, DecodeType, Event, EventParser, Idl, IdlTypes, Instruction} from "@coral-xyz/anchor";
|
|
3
|
-
import {IdlField, IdlInstruction} from "@coral-xyz/anchor/dist/cjs/idl";
|
|
4
|
-
import {ConfirmedSignatureInfo, ParsedMessage, PartiallyDecodedInstruction, PublicKey} from "@solana/web3.js";
|
|
5
|
-
import {SolanaProgramBase} from "../SolanaProgramBase";
|
|
6
|
-
import {SolanaChainInterface} from "../../chain/SolanaChainInterface";
|
|
7
|
-
|
|
8
|
-
type DecodedFieldOrNull<D, Defined> = D extends IdlField ? DecodeType<D["type"], Defined> : unknown;
|
|
9
|
-
type ArgsTuple<A extends IdlField[], Defined> = {
|
|
10
|
-
[K in A[number]["name"]]: DecodedFieldOrNull<Extract<A[number], { name: K }>, Defined>
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type InstructionWithAccounts<IDL extends Idl> = SingleInstructionWithAccounts<IDL["instructions"][number], IDL>;
|
|
14
|
-
|
|
15
|
-
export type SingleInstructionWithAccounts<I extends IdlInstruction, IDL extends Idl> = {
|
|
16
|
-
name: I["name"],
|
|
17
|
-
accounts: {
|
|
18
|
-
[key in I["accounts"][number]["name"]]: PublicKey
|
|
19
|
-
},
|
|
20
|
-
data: ArgsTuple<I["args"], IdlTypes<IDL>>
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type ProgramEvent<IDL extends Idl> = Event<IDL["events"][number], Record<string, any>>;
|
|
24
|
-
|
|
25
|
-
export class SolanaProgramEvents<IDL extends Idl> extends SolanaEvents {
|
|
26
|
-
|
|
27
|
-
private readonly programCoder: BorshCoder;
|
|
28
|
-
private readonly eventParser: EventParser;
|
|
29
|
-
private readonly program: SolanaProgramBase<IDL>;
|
|
30
|
-
private readonly nameMappedInstructions: {[name: string]: IdlInstruction};
|
|
31
|
-
|
|
32
|
-
constructor(chain: SolanaChainInterface, program: SolanaProgramBase<IDL>) {
|
|
33
|
-
super(chain);
|
|
34
|
-
this.program = program;
|
|
35
|
-
this.programCoder = new BorshCoder(program.program.idl);
|
|
36
|
-
this.eventParser = new EventParser(program.program.programId, this.programCoder);
|
|
37
|
-
this.nameMappedInstructions = {};
|
|
38
|
-
for(let ix of program.program.idl.instructions) {
|
|
39
|
-
this.nameMappedInstructions[ix.name] = ix;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Gets events from specific transaction as specified by signature, events are ordered from newest to oldest
|
|
45
|
-
*
|
|
46
|
-
* @param signature
|
|
47
|
-
* @private
|
|
48
|
-
*/
|
|
49
|
-
private async getEvents(signature: string): Promise<ProgramEvent<IDL>[]> {
|
|
50
|
-
const tx = await this.connection.getTransaction(signature, {
|
|
51
|
-
commitment: "confirmed",
|
|
52
|
-
maxSupportedTransactionVersion: 0
|
|
53
|
-
});
|
|
54
|
-
if(tx.meta.err) return [];
|
|
55
|
-
|
|
56
|
-
const events = this.parseLogs(tx.meta.logMessages);
|
|
57
|
-
events.reverse();
|
|
58
|
-
|
|
59
|
-
return events;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Runs a search backwards in time, processing the events for a specific topic public key
|
|
64
|
-
*
|
|
65
|
-
* @param topicKey
|
|
66
|
-
* @param processor called for every event, should return a value if the correct event was found, or null
|
|
67
|
-
* if the search should continue
|
|
68
|
-
* @param abortSignal
|
|
69
|
-
* @param logBatchSize how many signatures should be fetched in one getSignaturesForAddress call
|
|
70
|
-
*/
|
|
71
|
-
public findInEvents<T>(
|
|
72
|
-
topicKey: PublicKey,
|
|
73
|
-
processor: (event: ProgramEvent<IDL>, info: ConfirmedSignatureInfo) => Promise<T>,
|
|
74
|
-
abortSignal?: AbortSignal,
|
|
75
|
-
logBatchSize?: number
|
|
76
|
-
): Promise<T> {
|
|
77
|
-
return this.findInSignatures<T>(topicKey, async (signatures: ConfirmedSignatureInfo[]) => {
|
|
78
|
-
for(let data of signatures) {
|
|
79
|
-
for(let event of await this.getEvents(data.signature)) {
|
|
80
|
-
if(abortSignal!=null) abortSignal.throwIfAborted();
|
|
81
|
-
const result: T = await processor(event, data);
|
|
82
|
-
if(result!=null) return result;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}, abortSignal, logBatchSize);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Decodes the instructions for this program from the transaction, leaves null in the returned instructions array
|
|
90
|
-
* for every instruction that doesn't correspond to this program (as those are impossible to parse)
|
|
91
|
-
*
|
|
92
|
-
* @param transactionMessage
|
|
93
|
-
*/
|
|
94
|
-
public decodeInstructions(transactionMessage: ParsedMessage): InstructionWithAccounts<IDL>[] {
|
|
95
|
-
const instructions: InstructionWithAccounts<IDL>[] = [];
|
|
96
|
-
|
|
97
|
-
for(let _ix of transactionMessage.instructions) {
|
|
98
|
-
if(!_ix.programId.equals(this.program.program.programId)) {
|
|
99
|
-
instructions.push(null);
|
|
100
|
-
continue;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const ix: PartiallyDecodedInstruction = _ix as PartiallyDecodedInstruction;
|
|
104
|
-
if(ix.data==null) continue;
|
|
105
|
-
|
|
106
|
-
const parsedIx: Instruction = this.programCoder.instruction.decode(ix.data, 'base58');
|
|
107
|
-
const accountsData = this.nameMappedInstructions[parsedIx.name];
|
|
108
|
-
let accounts: {[name: string]: PublicKey};
|
|
109
|
-
if(accountsData!=null && accountsData.accounts!=null) {
|
|
110
|
-
accounts = {};
|
|
111
|
-
for(let i=0;i<accountsData.accounts.length;i++) {
|
|
112
|
-
accounts[accountsData.accounts[i].name] = ix.accounts[i];
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
instructions.push({
|
|
116
|
-
name: parsedIx.name,
|
|
117
|
-
data: parsedIx.data as any,
|
|
118
|
-
accounts: accounts as any
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return instructions;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Parses program event related to this program from transaction logs
|
|
127
|
-
*
|
|
128
|
-
* @param logs
|
|
129
|
-
*/
|
|
130
|
-
public parseLogs(logs: string[]): ProgramEvent<IDL>[] {
|
|
131
|
-
const eventsGenerator = this.eventParser.parseLogs(logs);
|
|
132
|
-
|
|
133
|
-
const events: ProgramEvent<IDL>[] = [];
|
|
134
|
-
for(let log of eventsGenerator) {
|
|
135
|
-
events.push(log as ProgramEvent<IDL>);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return events;
|
|
139
|
-
}
|
|
140
|
-
|
|
1
|
+
import {SolanaEvents} from "../../chain/modules/SolanaEvents";
|
|
2
|
+
import {BorshCoder, DecodeType, Event, EventParser, Idl, IdlTypes, Instruction} from "@coral-xyz/anchor";
|
|
3
|
+
import {IdlField, IdlInstruction} from "@coral-xyz/anchor/dist/cjs/idl";
|
|
4
|
+
import {ConfirmedSignatureInfo, ParsedMessage, PartiallyDecodedInstruction, PublicKey} from "@solana/web3.js";
|
|
5
|
+
import {SolanaProgramBase} from "../SolanaProgramBase";
|
|
6
|
+
import {SolanaChainInterface} from "../../chain/SolanaChainInterface";
|
|
7
|
+
|
|
8
|
+
type DecodedFieldOrNull<D, Defined> = D extends IdlField ? DecodeType<D["type"], Defined> : unknown;
|
|
9
|
+
type ArgsTuple<A extends IdlField[], Defined> = {
|
|
10
|
+
[K in A[number]["name"]]: DecodedFieldOrNull<Extract<A[number], { name: K }>, Defined>
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type InstructionWithAccounts<IDL extends Idl> = SingleInstructionWithAccounts<IDL["instructions"][number], IDL>;
|
|
14
|
+
|
|
15
|
+
export type SingleInstructionWithAccounts<I extends IdlInstruction, IDL extends Idl> = {
|
|
16
|
+
name: I["name"],
|
|
17
|
+
accounts: {
|
|
18
|
+
[key in I["accounts"][number]["name"]]: PublicKey
|
|
19
|
+
},
|
|
20
|
+
data: ArgsTuple<I["args"], IdlTypes<IDL>>
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type ProgramEvent<IDL extends Idl> = Event<IDL["events"][number], Record<string, any>>;
|
|
24
|
+
|
|
25
|
+
export class SolanaProgramEvents<IDL extends Idl> extends SolanaEvents {
|
|
26
|
+
|
|
27
|
+
private readonly programCoder: BorshCoder;
|
|
28
|
+
private readonly eventParser: EventParser;
|
|
29
|
+
private readonly program: SolanaProgramBase<IDL>;
|
|
30
|
+
private readonly nameMappedInstructions: {[name: string]: IdlInstruction};
|
|
31
|
+
|
|
32
|
+
constructor(chain: SolanaChainInterface, program: SolanaProgramBase<IDL>) {
|
|
33
|
+
super(chain);
|
|
34
|
+
this.program = program;
|
|
35
|
+
this.programCoder = new BorshCoder(program.program.idl);
|
|
36
|
+
this.eventParser = new EventParser(program.program.programId, this.programCoder);
|
|
37
|
+
this.nameMappedInstructions = {};
|
|
38
|
+
for(let ix of program.program.idl.instructions) {
|
|
39
|
+
this.nameMappedInstructions[ix.name] = ix;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Gets events from specific transaction as specified by signature, events are ordered from newest to oldest
|
|
45
|
+
*
|
|
46
|
+
* @param signature
|
|
47
|
+
* @private
|
|
48
|
+
*/
|
|
49
|
+
private async getEvents(signature: string): Promise<ProgramEvent<IDL>[]> {
|
|
50
|
+
const tx = await this.connection.getTransaction(signature, {
|
|
51
|
+
commitment: "confirmed",
|
|
52
|
+
maxSupportedTransactionVersion: 0
|
|
53
|
+
});
|
|
54
|
+
if(tx.meta.err) return [];
|
|
55
|
+
|
|
56
|
+
const events = this.parseLogs(tx.meta.logMessages);
|
|
57
|
+
events.reverse();
|
|
58
|
+
|
|
59
|
+
return events;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Runs a search backwards in time, processing the events for a specific topic public key
|
|
64
|
+
*
|
|
65
|
+
* @param topicKey
|
|
66
|
+
* @param processor called for every event, should return a value if the correct event was found, or null
|
|
67
|
+
* if the search should continue
|
|
68
|
+
* @param abortSignal
|
|
69
|
+
* @param logBatchSize how many signatures should be fetched in one getSignaturesForAddress call
|
|
70
|
+
*/
|
|
71
|
+
public findInEvents<T>(
|
|
72
|
+
topicKey: PublicKey,
|
|
73
|
+
processor: (event: ProgramEvent<IDL>, info: ConfirmedSignatureInfo) => Promise<T>,
|
|
74
|
+
abortSignal?: AbortSignal,
|
|
75
|
+
logBatchSize?: number
|
|
76
|
+
): Promise<T> {
|
|
77
|
+
return this.findInSignatures<T>(topicKey, async (signatures: ConfirmedSignatureInfo[]) => {
|
|
78
|
+
for(let data of signatures) {
|
|
79
|
+
for(let event of await this.getEvents(data.signature)) {
|
|
80
|
+
if(abortSignal!=null) abortSignal.throwIfAborted();
|
|
81
|
+
const result: T = await processor(event, data);
|
|
82
|
+
if(result!=null) return result;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}, abortSignal, logBatchSize);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Decodes the instructions for this program from the transaction, leaves null in the returned instructions array
|
|
90
|
+
* for every instruction that doesn't correspond to this program (as those are impossible to parse)
|
|
91
|
+
*
|
|
92
|
+
* @param transactionMessage
|
|
93
|
+
*/
|
|
94
|
+
public decodeInstructions(transactionMessage: ParsedMessage): InstructionWithAccounts<IDL>[] {
|
|
95
|
+
const instructions: InstructionWithAccounts<IDL>[] = [];
|
|
96
|
+
|
|
97
|
+
for(let _ix of transactionMessage.instructions) {
|
|
98
|
+
if(!_ix.programId.equals(this.program.program.programId)) {
|
|
99
|
+
instructions.push(null);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const ix: PartiallyDecodedInstruction = _ix as PartiallyDecodedInstruction;
|
|
104
|
+
if(ix.data==null) continue;
|
|
105
|
+
|
|
106
|
+
const parsedIx: Instruction = this.programCoder.instruction.decode(ix.data, 'base58');
|
|
107
|
+
const accountsData = this.nameMappedInstructions[parsedIx.name];
|
|
108
|
+
let accounts: {[name: string]: PublicKey};
|
|
109
|
+
if(accountsData!=null && accountsData.accounts!=null) {
|
|
110
|
+
accounts = {};
|
|
111
|
+
for(let i=0;i<accountsData.accounts.length;i++) {
|
|
112
|
+
accounts[accountsData.accounts[i].name] = ix.accounts[i];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
instructions.push({
|
|
116
|
+
name: parsedIx.name,
|
|
117
|
+
data: parsedIx.data as any,
|
|
118
|
+
accounts: accounts as any
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return instructions;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Parses program event related to this program from transaction logs
|
|
127
|
+
*
|
|
128
|
+
* @param logs
|
|
129
|
+
*/
|
|
130
|
+
public parseLogs(logs: string[]): ProgramEvent<IDL>[] {
|
|
131
|
+
const eventsGenerator = this.eventParser.parseLogs(logs);
|
|
132
|
+
|
|
133
|
+
const events: ProgramEvent<IDL>[] = [];
|
|
134
|
+
for(let log of eventsGenerator) {
|
|
135
|
+
events.push(log as ProgramEvent<IDL>);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return events;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
141
|
}
|