Skip to content

Commit b7c36e8

Browse files
committed
fix a bunch of errors with the sdk
1 parent 700520e commit b7c36e8

File tree

14 files changed

+2843
-7635
lines changed

14 files changed

+2843
-7635
lines changed

sdk/src/accounts.ts

Lines changed: 222 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { Connection, PublicKey } from '@solana/web3.js';
22
import BN from 'bn.js';
3-
import { EXT_PROGRAM_ID, PROGRAM_ID } from '.';
4-
import { getProgram, getExtProgram } from './idl';
3+
import { BorshAccountsCoder, Idl } from '@coral-xyz/anchor';
4+
import { IdlDefinedFields } from '@coral-xyz/anchor/dist/cjs/idl';
5+
6+
enum YieldVariant {
7+
NoYield = 0,
8+
ScaledUi = 1,
9+
Crank = 2,
10+
}
511

612
export interface EarnManagerData {
713
isActive: boolean;
@@ -13,49 +19,237 @@ export interface EarnManagerData {
1319

1420
export interface GlobalAccountData {
1521
admin: PublicKey;
16-
earnAuthority?: PublicKey;
17-
mint: PublicKey;
18-
underlyingMint?: PublicKey;
22+
extMint: PublicKey;
23+
mMint: PublicKey;
24+
variant: YieldVariant;
25+
wrapAuthorities: PublicKey[];
26+
27+
// crank fields
1928
index?: BN;
2029
timestamp?: BN;
21-
maxYield?: BN;
22-
claimComplete?: boolean;
30+
earnAuthority?: PublicKey;
2331
}
2432

2533
export interface EarnerData {
26-
user: PublicKey;
2734
lastClaimIndex: BN;
2835
lastClaimTimestamp: BN;
2936
bump: number;
37+
user: PublicKey;
3038
userTokenAccount: PublicKey;
31-
earnManager?: PublicKey | null;
32-
recipientTokenAccount?: PublicKey | null;
39+
earnManager: PublicKey;
40+
recipientTokenAccount: PublicKey | null;
3341
}
3442

35-
export async function loadGlobal(connection: Connection, program = PROGRAM_ID): Promise<GlobalAccountData> {
43+
export async function loadGlobal(connection: Connection, program: PublicKey): Promise<GlobalAccountData> {
3644
const [globalAccount] = PublicKey.findProgramAddressSync([Buffer.from('global')], program);
3745

38-
// $M
39-
if (program.equals(PROGRAM_ID)) {
40-
return await getProgram(connection).account.global.fetch(globalAccount);
41-
}
46+
// global account will differ depending on the program features
47+
const globalData = (await connection.getAccountInfo(globalAccount))!.data;
48+
49+
const yieldType = globalData[140];
50+
const decoder = yieldVariantsDecoder(yieldType);
51+
const global = decoder.decode('ExtGlobalV2', globalData);
4252

43-
// wrapped $M
44-
if (program.equals(EXT_PROGRAM_ID)) {
45-
const extGlobal = await getExtProgram(connection).account.extGlobal.fetch(globalAccount);
46-
return {
47-
...extGlobal,
48-
mint: extGlobal.extMint,
49-
underlyingMint: extGlobal.mMint,
50-
};
53+
return {
54+
admin: global.admin,
55+
extMint: global.extMint,
56+
mMint: global.mMint,
57+
variant: global.yieldConfig.yieldVariant,
58+
wrapAuthorities: global.wrapAuthorities,
59+
index: global.yieldConfig.index,
60+
timestamp: global.yieldConfig.ts,
61+
earnAuthority: global.yieldConfig.earnAuthority,
62+
};
63+
}
64+
65+
function yieldVariantsDecoder(variant: number) {
66+
return new BorshAccountsCoder(extensionIDL(variant));
67+
}
68+
69+
function extensionIDL(variant: number): Idl {
70+
if (variant >= 3) {
71+
throw new Error('Invalid yield variant, must be 0, 1, or 2');
5172
}
5273

53-
// $M extension (global account will differ depending on the program features)
54-
const globalData = (await connection.getAccountInfo(globalAccount))!.data;
74+
const yieldVariants: IdlDefinedFields[] = [
75+
[
76+
{
77+
name: 'yield_variant',
78+
type: {
79+
defined: {
80+
name: 'YieldVariant',
81+
},
82+
},
83+
},
84+
],
85+
[
86+
{
87+
name: 'yield_variant',
88+
type: {
89+
defined: {
90+
name: 'YieldVariant',
91+
},
92+
},
93+
},
94+
{
95+
name: 'fee_bps',
96+
type: {
97+
defined: {
98+
name: 'u64',
99+
},
100+
},
101+
},
102+
{
103+
name: 'last_m_index',
104+
type: {
105+
defined: {
106+
name: 'u64',
107+
},
108+
},
109+
},
110+
{
111+
name: 'last_ext_index',
112+
type: {
113+
defined: {
114+
name: 'u64',
115+
},
116+
},
117+
},
118+
],
119+
[
120+
{
121+
name: 'yield_variant',
122+
type: {
123+
defined: {
124+
name: 'YieldVariant',
125+
},
126+
},
127+
},
128+
{
129+
name: 'earn_authority',
130+
type: {
131+
defined: {
132+
name: 'pubkey',
133+
},
134+
},
135+
},
136+
{
137+
name: 'index',
138+
type: {
139+
defined: {
140+
name: 'u64',
141+
},
142+
},
143+
},
144+
{
145+
name: 'ts',
146+
type: {
147+
defined: {
148+
name: 'u64',
149+
},
150+
},
151+
},
152+
],
153+
];
55154

56155
return {
57-
admin: new PublicKey(globalData.subarray(8, 40)),
58-
mint: new PublicKey(globalData.subarray(40, 72)),
59-
underlyingMint: new PublicKey(globalData.subarray(72, 104)),
156+
address: '',
157+
instructions: [],
158+
metadata: {
159+
name: '',
160+
version: '',
161+
spec: '',
162+
},
163+
accounts: [
164+
{
165+
name: 'ExtGlobalV2',
166+
discriminator: [116, 209, 219, 83, 70, 143, 55, 127],
167+
},
168+
],
169+
types: [
170+
{
171+
name: 'ExtGlobalV2',
172+
type: {
173+
kind: 'struct',
174+
fields: [
175+
{
176+
name: 'admin',
177+
type: 'pubkey',
178+
},
179+
{
180+
name: 'pending_admin',
181+
type: {
182+
option: 'pubkey',
183+
},
184+
},
185+
{
186+
name: 'ext_mint',
187+
type: 'pubkey',
188+
},
189+
{
190+
name: 'm_mint',
191+
type: 'pubkey',
192+
},
193+
{
194+
name: 'm_earn_global_account',
195+
type: 'pubkey',
196+
},
197+
{
198+
name: 'bump',
199+
type: 'u8',
200+
},
201+
{
202+
name: 'm_vault_bump',
203+
type: 'u8',
204+
},
205+
{
206+
name: 'ext_mint_authority_bump',
207+
type: 'u8',
208+
},
209+
{
210+
name: 'yield_config',
211+
type: {
212+
defined: {
213+
name: 'YieldConfig',
214+
},
215+
},
216+
},
217+
{
218+
name: 'wrap_authorities',
219+
type: {
220+
vec: 'pubkey',
221+
},
222+
},
223+
],
224+
},
225+
},
226+
{
227+
name: 'YieldConfig',
228+
type: {
229+
kind: 'struct',
230+
fields: yieldVariants[variant],
231+
},
232+
},
233+
{
234+
name: 'YieldVariant',
235+
repr: {
236+
kind: 'rust',
237+
},
238+
type: {
239+
kind: 'enum',
240+
variants: [
241+
{
242+
name: 'NoYield',
243+
},
244+
{
245+
name: 'ScaledUi',
246+
},
247+
{
248+
name: 'Crank',
249+
},
250+
],
251+
},
252+
},
253+
],
60254
};
61255
}

0 commit comments

Comments
 (0)