@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.
- 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 -567
- 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 -849
- 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,209 +1,209 @@
|
|
|
1
|
-
import {ChainEvents, ClaimEvent, EventListener, InitializeEvent, RefundEvent, SwapEvent} from "@atomiqlabs/base";
|
|
2
|
-
import {InitInstruction, SolanaSwapData} from "../swaps/SolanaSwapData";
|
|
3
|
-
import {IdlEvents} from "@coral-xyz/anchor";
|
|
4
|
-
import {SolanaSwapProgram} from "../swaps/SolanaSwapProgram";
|
|
5
|
-
import {
|
|
6
|
-
getLogger,
|
|
7
|
-
onceAsync, toEscrowHash, tryWithRetries
|
|
8
|
-
} from "../../utils/Utils";
|
|
9
|
-
import {Connection, ParsedTransactionWithMeta} from "@solana/web3.js";
|
|
10
|
-
import {SwapTypeEnum} from "../swaps/SwapTypeEnum";
|
|
11
|
-
import {
|
|
12
|
-
InstructionWithAccounts,
|
|
13
|
-
ProgramEvent
|
|
14
|
-
} from "../program/modules/SolanaProgramEvents";
|
|
15
|
-
import {SwapProgram} from "../swaps/programTypes";
|
|
16
|
-
import {Buffer} from "buffer";
|
|
17
|
-
|
|
18
|
-
export type EventObject = {
|
|
19
|
-
events: ProgramEvent<SwapProgram>[],
|
|
20
|
-
instructions: InstructionWithAccounts<SwapProgram>[],
|
|
21
|
-
blockTime: number,
|
|
22
|
-
signature: string
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Solana on-chain event handler for front-end systems without access to fs, uses pure WS to subscribe, might lose
|
|
27
|
-
* out on some events if the network is unreliable, front-end systems should take this into consideration and not
|
|
28
|
-
* rely purely on events
|
|
29
|
-
*/
|
|
30
|
-
export class SolanaChainEventsBrowser implements ChainEvents<SolanaSwapData> {
|
|
31
|
-
|
|
32
|
-
protected readonly listeners: EventListener<SolanaSwapData>[] = [];
|
|
33
|
-
protected readonly connection: Connection;
|
|
34
|
-
protected readonly solanaSwapProgram: SolanaSwapProgram;
|
|
35
|
-
protected eventListeners: number[] = [];
|
|
36
|
-
protected readonly logger = getLogger("SolanaChainEventsBrowser: ");
|
|
37
|
-
|
|
38
|
-
constructor(connection: Connection, solanaSwapContract: SolanaSwapProgram) {
|
|
39
|
-
this.connection = connection;
|
|
40
|
-
this.solanaSwapProgram = solanaSwapContract;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Fetches and parses transaction instructions
|
|
45
|
-
*
|
|
46
|
-
* @private
|
|
47
|
-
* @returns {Promise<InstructionWithAccounts<SwapProgram>[]>} array of parsed instructions
|
|
48
|
-
*/
|
|
49
|
-
private async getTransactionInstructions(signature: string): Promise<InstructionWithAccounts<SwapProgram>[]> {
|
|
50
|
-
const transaction = await tryWithRetries<ParsedTransactionWithMeta>(async () => {
|
|
51
|
-
const res = await this.connection.getParsedTransaction(signature, {
|
|
52
|
-
commitment: "confirmed",
|
|
53
|
-
maxSupportedTransactionVersion: 0
|
|
54
|
-
});
|
|
55
|
-
if(res==null) throw new Error("Transaction not found!");
|
|
56
|
-
return res;
|
|
57
|
-
});
|
|
58
|
-
if(transaction==null) return null;
|
|
59
|
-
if(transaction.meta.err!=null) return null;
|
|
60
|
-
return this.solanaSwapProgram.Events.decodeInstructions(transaction.transaction.message);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Returns async getter for fetching on-demand initialize event swap data
|
|
65
|
-
*
|
|
66
|
-
* @param eventObject
|
|
67
|
-
* @param txoHash
|
|
68
|
-
* @private
|
|
69
|
-
* @returns {() => Promise<SolanaSwapData>} getter to be passed to InitializeEvent constructor
|
|
70
|
-
*/
|
|
71
|
-
private getSwapDataGetter(eventObject: EventObject, txoHash: string): () => Promise<SolanaSwapData> {
|
|
72
|
-
return async () => {
|
|
73
|
-
if(eventObject.instructions==null) eventObject.instructions = await this.getTransactionInstructions(eventObject.signature);
|
|
74
|
-
if(eventObject.instructions==null) return null;
|
|
75
|
-
|
|
76
|
-
const initIx = eventObject.instructions.find(
|
|
77
|
-
ix => ix!=null && (ix.name === "offererInitializePayIn" || ix.name === "offererInitialize")
|
|
78
|
-
) as InitInstruction;
|
|
79
|
-
if(initIx == null) return null;
|
|
80
|
-
|
|
81
|
-
return SolanaSwapData.fromInstruction(initIx, txoHash);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
protected parseInitializeEvent(data: IdlEvents<SwapProgram>["InitializeEvent"], eventObject: EventObject): InitializeEvent<SolanaSwapData> {
|
|
86
|
-
const paymentHash: string = Buffer.from(data.hash).toString("hex");
|
|
87
|
-
const txoHash: string = Buffer.from(data.txoHash).toString("hex");
|
|
88
|
-
const escrowHash = toEscrowHash(paymentHash, data.sequence);
|
|
89
|
-
this.logger.debug("InitializeEvent paymentHash: "+paymentHash+" sequence: "+data.sequence.toString(10)+
|
|
90
|
-
" txoHash: "+txoHash+" escrowHash: "+escrowHash);
|
|
91
|
-
return new InitializeEvent<SolanaSwapData>(
|
|
92
|
-
escrowHash,
|
|
93
|
-
SwapTypeEnum.toChainSwapType(data.kind),
|
|
94
|
-
onceAsync<SolanaSwapData>(this.getSwapDataGetter(eventObject, txoHash))
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
protected parseRefundEvent(data: IdlEvents<SwapProgram>["RefundEvent"]): RefundEvent<SolanaSwapData> {
|
|
99
|
-
const paymentHash: string = Buffer.from(data.hash).toString("hex");
|
|
100
|
-
const escrowHash = toEscrowHash(paymentHash, data.sequence);
|
|
101
|
-
this.logger.debug("RefundEvent paymentHash: "+paymentHash+" sequence: "+data.sequence.toString(10)+
|
|
102
|
-
" escrowHash: "+escrowHash);
|
|
103
|
-
return new RefundEvent<SolanaSwapData>(escrowHash);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
protected parseClaimEvent(data: IdlEvents<SwapProgram>["ClaimEvent"]): ClaimEvent<SolanaSwapData> {
|
|
107
|
-
const secret: string = Buffer.from(data.secret).toString("hex");
|
|
108
|
-
const paymentHash: string = Buffer.from(data.hash).toString("hex");
|
|
109
|
-
const escrowHash = toEscrowHash(paymentHash, data.sequence);
|
|
110
|
-
this.logger.debug("ClaimEvent paymentHash: "+paymentHash+" sequence: "+data.sequence.toString(10)+
|
|
111
|
-
" secret: "+secret+" escrowHash: "+escrowHash);
|
|
112
|
-
return new ClaimEvent<SolanaSwapData>(escrowHash, secret);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Processes event as received from the chain, parses it & calls event listeners
|
|
117
|
-
*
|
|
118
|
-
* @param eventObject
|
|
119
|
-
* @protected
|
|
120
|
-
*/
|
|
121
|
-
protected async processEvent(eventObject : EventObject) {
|
|
122
|
-
let parsedEvents: SwapEvent<SolanaSwapData>[] = eventObject.events.map(event => {
|
|
123
|
-
let parsedEvent: SwapEvent<SolanaSwapData>;
|
|
124
|
-
switch(event.name) {
|
|
125
|
-
case "ClaimEvent":
|
|
126
|
-
parsedEvent = this.parseClaimEvent(event.data);
|
|
127
|
-
break;
|
|
128
|
-
case "RefundEvent":
|
|
129
|
-
parsedEvent = this.parseRefundEvent(event.data);
|
|
130
|
-
break;
|
|
131
|
-
case "InitializeEvent":
|
|
132
|
-
parsedEvent = this.parseInitializeEvent(event.data, eventObject);
|
|
133
|
-
break;
|
|
134
|
-
}
|
|
135
|
-
(parsedEvent as any).meta = {
|
|
136
|
-
blockTime: eventObject.blockTime,
|
|
137
|
-
timestamp: eventObject.blockTime,
|
|
138
|
-
txId: eventObject.signature
|
|
139
|
-
};
|
|
140
|
-
return parsedEvent;
|
|
141
|
-
}).filter(parsedEvent => parsedEvent!=null);
|
|
142
|
-
|
|
143
|
-
for(let listener of this.listeners) {
|
|
144
|
-
await listener(parsedEvents);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Returns websocket event handler for specific event type
|
|
150
|
-
*
|
|
151
|
-
* @param name
|
|
152
|
-
* @protected
|
|
153
|
-
* @returns event handler to be passed to program's addEventListener function
|
|
154
|
-
*/
|
|
155
|
-
protected getWsEventHandler<E extends "InitializeEvent" | "RefundEvent" | "ClaimEvent">(
|
|
156
|
-
name: E
|
|
157
|
-
): (data: IdlEvents<SwapProgram>[E], slotNumber: number, signature: string) => void {
|
|
158
|
-
return (data: IdlEvents<SwapProgram>[E], slotNumber: number, signature: string) => {
|
|
159
|
-
this.logger.debug("wsEventHandler: Process signature: ", signature);
|
|
160
|
-
|
|
161
|
-
this.processEvent({
|
|
162
|
-
events: [{name, data: data as any}],
|
|
163
|
-
instructions: null, //Instructions will be fetched on-demand if required
|
|
164
|
-
blockTime: Math.floor(Date.now()/1000),
|
|
165
|
-
signature
|
|
166
|
-
}).then(() => true).catch(e => {
|
|
167
|
-
this.logger.error("wsEventHandler: Error when processing signature: "+signature, e);
|
|
168
|
-
return false;
|
|
169
|
-
});
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Sets up event handlers listening for swap events over websocket
|
|
175
|
-
*
|
|
176
|
-
* @protected
|
|
177
|
-
*/
|
|
178
|
-
protected setupWebsocket() {
|
|
179
|
-
const program = this.solanaSwapProgram.program;
|
|
180
|
-
this.eventListeners.push(program.addEventListener<"InitializeEvent">("InitializeEvent", this.getWsEventHandler("InitializeEvent")));
|
|
181
|
-
this.eventListeners.push(program.addEventListener<"ClaimEvent">("ClaimEvent", this.getWsEventHandler("ClaimEvent")));
|
|
182
|
-
this.eventListeners.push(program.addEventListener<"RefundEvent">("RefundEvent", this.getWsEventHandler("RefundEvent")));
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
init(): Promise<void> {
|
|
186
|
-
this.setupWebsocket();
|
|
187
|
-
return Promise.resolve();
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
async stop(): Promise<void> {
|
|
191
|
-
for(let num of this.eventListeners) {
|
|
192
|
-
await this.solanaSwapProgram.program.removeEventListener(num);
|
|
193
|
-
}
|
|
194
|
-
this.eventListeners = [];
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
registerListener(cbk: EventListener<SolanaSwapData>): void {
|
|
198
|
-
this.listeners.push(cbk);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
unregisterListener(cbk: EventListener<SolanaSwapData>): boolean {
|
|
202
|
-
const index = this.listeners.indexOf(cbk);
|
|
203
|
-
if(index>=0) {
|
|
204
|
-
this.listeners.splice(index, 1);
|
|
205
|
-
return true;
|
|
206
|
-
}
|
|
207
|
-
return false;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
1
|
+
import {ChainEvents, ClaimEvent, EventListener, InitializeEvent, RefundEvent, SwapEvent} from "@atomiqlabs/base";
|
|
2
|
+
import {InitInstruction, SolanaSwapData} from "../swaps/SolanaSwapData";
|
|
3
|
+
import {IdlEvents} from "@coral-xyz/anchor";
|
|
4
|
+
import {SolanaSwapProgram} from "../swaps/SolanaSwapProgram";
|
|
5
|
+
import {
|
|
6
|
+
getLogger,
|
|
7
|
+
onceAsync, toEscrowHash, tryWithRetries
|
|
8
|
+
} from "../../utils/Utils";
|
|
9
|
+
import {Connection, ParsedTransactionWithMeta} from "@solana/web3.js";
|
|
10
|
+
import {SwapTypeEnum} from "../swaps/SwapTypeEnum";
|
|
11
|
+
import {
|
|
12
|
+
InstructionWithAccounts,
|
|
13
|
+
ProgramEvent
|
|
14
|
+
} from "../program/modules/SolanaProgramEvents";
|
|
15
|
+
import {SwapProgram} from "../swaps/programTypes";
|
|
16
|
+
import {Buffer} from "buffer";
|
|
17
|
+
|
|
18
|
+
export type EventObject = {
|
|
19
|
+
events: ProgramEvent<SwapProgram>[],
|
|
20
|
+
instructions: InstructionWithAccounts<SwapProgram>[],
|
|
21
|
+
blockTime: number,
|
|
22
|
+
signature: string
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Solana on-chain event handler for front-end systems without access to fs, uses pure WS to subscribe, might lose
|
|
27
|
+
* out on some events if the network is unreliable, front-end systems should take this into consideration and not
|
|
28
|
+
* rely purely on events
|
|
29
|
+
*/
|
|
30
|
+
export class SolanaChainEventsBrowser implements ChainEvents<SolanaSwapData> {
|
|
31
|
+
|
|
32
|
+
protected readonly listeners: EventListener<SolanaSwapData>[] = [];
|
|
33
|
+
protected readonly connection: Connection;
|
|
34
|
+
protected readonly solanaSwapProgram: SolanaSwapProgram;
|
|
35
|
+
protected eventListeners: number[] = [];
|
|
36
|
+
protected readonly logger = getLogger("SolanaChainEventsBrowser: ");
|
|
37
|
+
|
|
38
|
+
constructor(connection: Connection, solanaSwapContract: SolanaSwapProgram) {
|
|
39
|
+
this.connection = connection;
|
|
40
|
+
this.solanaSwapProgram = solanaSwapContract;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Fetches and parses transaction instructions
|
|
45
|
+
*
|
|
46
|
+
* @private
|
|
47
|
+
* @returns {Promise<InstructionWithAccounts<SwapProgram>[]>} array of parsed instructions
|
|
48
|
+
*/
|
|
49
|
+
private async getTransactionInstructions(signature: string): Promise<InstructionWithAccounts<SwapProgram>[]> {
|
|
50
|
+
const transaction = await tryWithRetries<ParsedTransactionWithMeta>(async () => {
|
|
51
|
+
const res = await this.connection.getParsedTransaction(signature, {
|
|
52
|
+
commitment: "confirmed",
|
|
53
|
+
maxSupportedTransactionVersion: 0
|
|
54
|
+
});
|
|
55
|
+
if(res==null) throw new Error("Transaction not found!");
|
|
56
|
+
return res;
|
|
57
|
+
});
|
|
58
|
+
if(transaction==null) return null;
|
|
59
|
+
if(transaction.meta.err!=null) return null;
|
|
60
|
+
return this.solanaSwapProgram.Events.decodeInstructions(transaction.transaction.message);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns async getter for fetching on-demand initialize event swap data
|
|
65
|
+
*
|
|
66
|
+
* @param eventObject
|
|
67
|
+
* @param txoHash
|
|
68
|
+
* @private
|
|
69
|
+
* @returns {() => Promise<SolanaSwapData>} getter to be passed to InitializeEvent constructor
|
|
70
|
+
*/
|
|
71
|
+
private getSwapDataGetter(eventObject: EventObject, txoHash: string): () => Promise<SolanaSwapData> {
|
|
72
|
+
return async () => {
|
|
73
|
+
if(eventObject.instructions==null) eventObject.instructions = await this.getTransactionInstructions(eventObject.signature);
|
|
74
|
+
if(eventObject.instructions==null) return null;
|
|
75
|
+
|
|
76
|
+
const initIx = eventObject.instructions.find(
|
|
77
|
+
ix => ix!=null && (ix.name === "offererInitializePayIn" || ix.name === "offererInitialize")
|
|
78
|
+
) as InitInstruction;
|
|
79
|
+
if(initIx == null) return null;
|
|
80
|
+
|
|
81
|
+
return SolanaSwapData.fromInstruction(initIx, txoHash);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
protected parseInitializeEvent(data: IdlEvents<SwapProgram>["InitializeEvent"], eventObject: EventObject): InitializeEvent<SolanaSwapData> {
|
|
86
|
+
const paymentHash: string = Buffer.from(data.hash).toString("hex");
|
|
87
|
+
const txoHash: string = Buffer.from(data.txoHash).toString("hex");
|
|
88
|
+
const escrowHash = toEscrowHash(paymentHash, data.sequence);
|
|
89
|
+
this.logger.debug("InitializeEvent paymentHash: "+paymentHash+" sequence: "+data.sequence.toString(10)+
|
|
90
|
+
" txoHash: "+txoHash+" escrowHash: "+escrowHash);
|
|
91
|
+
return new InitializeEvent<SolanaSwapData>(
|
|
92
|
+
escrowHash,
|
|
93
|
+
SwapTypeEnum.toChainSwapType(data.kind),
|
|
94
|
+
onceAsync<SolanaSwapData>(this.getSwapDataGetter(eventObject, txoHash))
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
protected parseRefundEvent(data: IdlEvents<SwapProgram>["RefundEvent"]): RefundEvent<SolanaSwapData> {
|
|
99
|
+
const paymentHash: string = Buffer.from(data.hash).toString("hex");
|
|
100
|
+
const escrowHash = toEscrowHash(paymentHash, data.sequence);
|
|
101
|
+
this.logger.debug("RefundEvent paymentHash: "+paymentHash+" sequence: "+data.sequence.toString(10)+
|
|
102
|
+
" escrowHash: "+escrowHash);
|
|
103
|
+
return new RefundEvent<SolanaSwapData>(escrowHash);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
protected parseClaimEvent(data: IdlEvents<SwapProgram>["ClaimEvent"]): ClaimEvent<SolanaSwapData> {
|
|
107
|
+
const secret: string = Buffer.from(data.secret).toString("hex");
|
|
108
|
+
const paymentHash: string = Buffer.from(data.hash).toString("hex");
|
|
109
|
+
const escrowHash = toEscrowHash(paymentHash, data.sequence);
|
|
110
|
+
this.logger.debug("ClaimEvent paymentHash: "+paymentHash+" sequence: "+data.sequence.toString(10)+
|
|
111
|
+
" secret: "+secret+" escrowHash: "+escrowHash);
|
|
112
|
+
return new ClaimEvent<SolanaSwapData>(escrowHash, secret);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Processes event as received from the chain, parses it & calls event listeners
|
|
117
|
+
*
|
|
118
|
+
* @param eventObject
|
|
119
|
+
* @protected
|
|
120
|
+
*/
|
|
121
|
+
protected async processEvent(eventObject : EventObject) {
|
|
122
|
+
let parsedEvents: SwapEvent<SolanaSwapData>[] = eventObject.events.map(event => {
|
|
123
|
+
let parsedEvent: SwapEvent<SolanaSwapData>;
|
|
124
|
+
switch(event.name) {
|
|
125
|
+
case "ClaimEvent":
|
|
126
|
+
parsedEvent = this.parseClaimEvent(event.data);
|
|
127
|
+
break;
|
|
128
|
+
case "RefundEvent":
|
|
129
|
+
parsedEvent = this.parseRefundEvent(event.data);
|
|
130
|
+
break;
|
|
131
|
+
case "InitializeEvent":
|
|
132
|
+
parsedEvent = this.parseInitializeEvent(event.data, eventObject);
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
(parsedEvent as any).meta = {
|
|
136
|
+
blockTime: eventObject.blockTime,
|
|
137
|
+
timestamp: eventObject.blockTime,
|
|
138
|
+
txId: eventObject.signature
|
|
139
|
+
};
|
|
140
|
+
return parsedEvent;
|
|
141
|
+
}).filter(parsedEvent => parsedEvent!=null);
|
|
142
|
+
|
|
143
|
+
for(let listener of this.listeners) {
|
|
144
|
+
await listener(parsedEvents);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Returns websocket event handler for specific event type
|
|
150
|
+
*
|
|
151
|
+
* @param name
|
|
152
|
+
* @protected
|
|
153
|
+
* @returns event handler to be passed to program's addEventListener function
|
|
154
|
+
*/
|
|
155
|
+
protected getWsEventHandler<E extends "InitializeEvent" | "RefundEvent" | "ClaimEvent">(
|
|
156
|
+
name: E
|
|
157
|
+
): (data: IdlEvents<SwapProgram>[E], slotNumber: number, signature: string) => void {
|
|
158
|
+
return (data: IdlEvents<SwapProgram>[E], slotNumber: number, signature: string) => {
|
|
159
|
+
this.logger.debug("wsEventHandler: Process signature: ", signature);
|
|
160
|
+
|
|
161
|
+
this.processEvent({
|
|
162
|
+
events: [{name, data: data as any}],
|
|
163
|
+
instructions: null, //Instructions will be fetched on-demand if required
|
|
164
|
+
blockTime: Math.floor(Date.now()/1000),
|
|
165
|
+
signature
|
|
166
|
+
}).then(() => true).catch(e => {
|
|
167
|
+
this.logger.error("wsEventHandler: Error when processing signature: "+signature, e);
|
|
168
|
+
return false;
|
|
169
|
+
});
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Sets up event handlers listening for swap events over websocket
|
|
175
|
+
*
|
|
176
|
+
* @protected
|
|
177
|
+
*/
|
|
178
|
+
protected setupWebsocket() {
|
|
179
|
+
const program = this.solanaSwapProgram.program;
|
|
180
|
+
this.eventListeners.push(program.addEventListener<"InitializeEvent">("InitializeEvent", this.getWsEventHandler("InitializeEvent")));
|
|
181
|
+
this.eventListeners.push(program.addEventListener<"ClaimEvent">("ClaimEvent", this.getWsEventHandler("ClaimEvent")));
|
|
182
|
+
this.eventListeners.push(program.addEventListener<"RefundEvent">("RefundEvent", this.getWsEventHandler("RefundEvent")));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
init(): Promise<void> {
|
|
186
|
+
this.setupWebsocket();
|
|
187
|
+
return Promise.resolve();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async stop(): Promise<void> {
|
|
191
|
+
for(let num of this.eventListeners) {
|
|
192
|
+
await this.solanaSwapProgram.program.removeEventListener(num);
|
|
193
|
+
}
|
|
194
|
+
this.eventListeners = [];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
registerListener(cbk: EventListener<SolanaSwapData>): void {
|
|
198
|
+
this.listeners.push(cbk);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
unregisterListener(cbk: EventListener<SolanaSwapData>): boolean {
|
|
202
|
+
const index = this.listeners.indexOf(cbk);
|
|
203
|
+
if(index>=0) {
|
|
204
|
+
this.listeners.splice(index, 1);
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -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
|
}
|