1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { base16, base58 } from "npm:@scure/base";
const input = bytes(`
04 e9 e1 2f bc 84 e8 26 c9 32 cc e9 e2 64 0c ce
15 59 0c 1c 62 73 b0 92 57 08 ba 3b 85 20 b0 bc
06 9b 88 57 fe ab 81 84 fb 68 7f 63 46 18 c0 35
da c4 39 dc 1a eb 3b 55 98 a0 f0 00 00 00 00 01
a0 86 01 00 00 00 00 00 05 2e e1 83 38 96 96 9f
8c d1 cd 46 83 18 c5 98 c7 e0 58 96 07 4a 59 1c
2a e0 98 60 2f 16 80 00 d9 34 05 00 00 00 00 00
`);
export function publicKey(data: Uint8Array, offset: number) {
return base58.encode(data.subarray(offset, offset + 32));
}
export function u64(view: DataView, offset: number) {
return view.getBigUint64(offset, true);
}
export function parseSwapEvent(data: Uint8Array) {
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
return {
amm: publicKey(data, 0),
inputMint: publicKey(data, 32),
inputAmount: u64(view, 64),
outputMint: publicKey(data, 72),
outputAmount: u64(view, 104),
};
}
console.log(parseSwapEvent(input));
function bytes(str: string): Uint8Array {
return base16.decode(str.replaceAll(/\s/g, "").toUpperCase());
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 29, 2024