/
index.ts
131 lines (115 loc) · 3.1 KB
/
index.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { u64 } from "@saberhq/token-utils";
import { PublicKey } from "@solana/web3.js";
import type { SwapTokenInfo } from "../instructions/swap.js";
import type { Fees } from "./fees.js";
import { decodeFees } from "./fees.js";
import { StableSwapLayout } from "./layout.js";
export * from "./fees.js";
export * from "./layout.js";
/**
* State of a StableSwap, read from the swap account.
*/
export interface StableSwapState {
/**
* Whether or not the swap is initialized.
*/
isInitialized: boolean;
/**
* Whether or not the swap is paused.
*/
isPaused: boolean;
/**
* Nonce used to generate the swap authority.
*/
nonce: number;
/**
* Mint account for pool token
*/
poolTokenMint: PublicKey;
/**
* Admin account
*/
adminAccount: PublicKey;
tokenA: SwapTokenInfo;
tokenB: SwapTokenInfo;
/**
* Initial amplification coefficient (A)
*/
initialAmpFactor: u64;
/**
* Target amplification coefficient (A)
*/
targetAmpFactor: u64;
/**
* Ramp A start timestamp
*/
startRampTimestamp: number;
/**
* Ramp A start timestamp
*/
stopRampTimestamp: number;
/**
* When the future admin can no longer become the admin, if applicable.
*/
futureAdminDeadline: number;
/**
* The next admin.
*/
futureAdminAccount: PublicKey;
/**
* Fee schedule
*/
fees: Fees;
}
/**
* Decodes the Swap account.
* @param data
* @returns
*/
export const decodeSwap = (data: Buffer): StableSwapState => {
const stableSwapData = StableSwapLayout.decode(data);
if (!stableSwapData.isInitialized) {
throw new Error(`Invalid token swap state`);
}
const adminAccount = new PublicKey(stableSwapData.adminAccount);
const adminFeeAccountA = new PublicKey(stableSwapData.adminFeeAccountA);
const adminFeeAccountB = new PublicKey(stableSwapData.adminFeeAccountB);
const tokenAccountA = new PublicKey(stableSwapData.tokenAccountA);
const tokenAccountB = new PublicKey(stableSwapData.tokenAccountB);
const poolTokenMint = new PublicKey(stableSwapData.tokenPool);
const mintA = new PublicKey(stableSwapData.mintA);
const mintB = new PublicKey(stableSwapData.mintB);
const initialAmpFactor = u64.fromBuffer(
Buffer.from(stableSwapData.initialAmpFactor)
);
const targetAmpFactor = u64.fromBuffer(
Buffer.from(stableSwapData.targetAmpFactor)
);
const startRampTimestamp = stableSwapData.startRampTs;
const stopRampTimestamp = stableSwapData.stopRampTs;
const fees = decodeFees(stableSwapData.fees);
return {
isInitialized: !!stableSwapData.isInitialized,
isPaused: !!stableSwapData.isPaused,
nonce: stableSwapData.nonce,
futureAdminDeadline: stableSwapData.futureAdminDeadline,
futureAdminAccount: new PublicKey(stableSwapData.futureAdminAccount),
adminAccount,
tokenA: {
adminFeeAccount: adminFeeAccountA,
reserve: tokenAccountA,
mint: mintA,
},
tokenB: {
adminFeeAccount: adminFeeAccountB,
reserve: tokenAccountB,
mint: mintB,
},
poolTokenMint,
initialAmpFactor,
targetAmpFactor,
startRampTimestamp,
stopRampTimestamp,
fees,
};
};