-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcloseEpochAccount.ts
299 lines (281 loc) · 9.71 KB
/
closeEpochAccount.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/kinobi-so/kinobi
*/
import {
combineCodec,
getStructDecoder,
getStructEncoder,
getU64Decoder,
getU64Encoder,
getU8Decoder,
getU8Encoder,
transformEncoder,
type Address,
type Codec,
type Decoder,
type Encoder,
type IAccountMeta,
type IInstruction,
type IInstructionWithAccounts,
type IInstructionWithData,
type ReadonlyAccount,
type WritableAccount,
} from '@solana/web3.js';
import { JITO_TIP_ROUTER_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
export const CLOSE_EPOCH_ACCOUNT_DISCRIMINATOR = 28;
export function getCloseEpochAccountDiscriminatorBytes() {
return getU8Encoder().encode(CLOSE_EPOCH_ACCOUNT_DISCRIMINATOR);
}
export type CloseEpochAccountInstruction<
TProgram extends string = typeof JITO_TIP_ROUTER_PROGRAM_ADDRESS,
TAccountEpochMarker extends string | IAccountMeta<string> = string,
TAccountEpochState extends string | IAccountMeta<string> = string,
TAccountConfig extends string | IAccountMeta<string> = string,
TAccountNcn extends string | IAccountMeta<string> = string,
TAccountAccountToClose extends string | IAccountMeta<string> = string,
TAccountAccountPayer extends string | IAccountMeta<string> = string,
TAccountDaoWallet extends string | IAccountMeta<string> = string,
TAccountSystemProgram extends
| string
| IAccountMeta<string> = '11111111111111111111111111111111',
TAccountReceiverToClose extends string | IAccountMeta<string> = string,
TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
> = IInstruction<TProgram> &
IInstructionWithData<Uint8Array> &
IInstructionWithAccounts<
[
TAccountEpochMarker extends string
? WritableAccount<TAccountEpochMarker>
: TAccountEpochMarker,
TAccountEpochState extends string
? WritableAccount<TAccountEpochState>
: TAccountEpochState,
TAccountConfig extends string
? ReadonlyAccount<TAccountConfig>
: TAccountConfig,
TAccountNcn extends string ? ReadonlyAccount<TAccountNcn> : TAccountNcn,
TAccountAccountToClose extends string
? WritableAccount<TAccountAccountToClose>
: TAccountAccountToClose,
TAccountAccountPayer extends string
? WritableAccount<TAccountAccountPayer>
: TAccountAccountPayer,
TAccountDaoWallet extends string
? WritableAccount<TAccountDaoWallet>
: TAccountDaoWallet,
TAccountSystemProgram extends string
? ReadonlyAccount<TAccountSystemProgram>
: TAccountSystemProgram,
TAccountReceiverToClose extends string
? WritableAccount<TAccountReceiverToClose>
: TAccountReceiverToClose,
...TRemainingAccounts,
]
>;
export type CloseEpochAccountInstructionData = {
discriminator: number;
epoch: bigint;
};
export type CloseEpochAccountInstructionDataArgs = { epoch: number | bigint };
export function getCloseEpochAccountInstructionDataEncoder(): Encoder<CloseEpochAccountInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU8Encoder()],
['epoch', getU64Encoder()],
]),
(value) => ({ ...value, discriminator: CLOSE_EPOCH_ACCOUNT_DISCRIMINATOR })
);
}
export function getCloseEpochAccountInstructionDataDecoder(): Decoder<CloseEpochAccountInstructionData> {
return getStructDecoder([
['discriminator', getU8Decoder()],
['epoch', getU64Decoder()],
]);
}
export function getCloseEpochAccountInstructionDataCodec(): Codec<
CloseEpochAccountInstructionDataArgs,
CloseEpochAccountInstructionData
> {
return combineCodec(
getCloseEpochAccountInstructionDataEncoder(),
getCloseEpochAccountInstructionDataDecoder()
);
}
export type CloseEpochAccountInput<
TAccountEpochMarker extends string = string,
TAccountEpochState extends string = string,
TAccountConfig extends string = string,
TAccountNcn extends string = string,
TAccountAccountToClose extends string = string,
TAccountAccountPayer extends string = string,
TAccountDaoWallet extends string = string,
TAccountSystemProgram extends string = string,
TAccountReceiverToClose extends string = string,
> = {
epochMarker: Address<TAccountEpochMarker>;
epochState: Address<TAccountEpochState>;
config: Address<TAccountConfig>;
ncn: Address<TAccountNcn>;
accountToClose: Address<TAccountAccountToClose>;
accountPayer: Address<TAccountAccountPayer>;
daoWallet: Address<TAccountDaoWallet>;
systemProgram?: Address<TAccountSystemProgram>;
receiverToClose?: Address<TAccountReceiverToClose>;
epoch: CloseEpochAccountInstructionDataArgs['epoch'];
};
export function getCloseEpochAccountInstruction<
TAccountEpochMarker extends string,
TAccountEpochState extends string,
TAccountConfig extends string,
TAccountNcn extends string,
TAccountAccountToClose extends string,
TAccountAccountPayer extends string,
TAccountDaoWallet extends string,
TAccountSystemProgram extends string,
TAccountReceiverToClose extends string,
TProgramAddress extends Address = typeof JITO_TIP_ROUTER_PROGRAM_ADDRESS,
>(
input: CloseEpochAccountInput<
TAccountEpochMarker,
TAccountEpochState,
TAccountConfig,
TAccountNcn,
TAccountAccountToClose,
TAccountAccountPayer,
TAccountDaoWallet,
TAccountSystemProgram,
TAccountReceiverToClose
>,
config?: { programAddress?: TProgramAddress }
): CloseEpochAccountInstruction<
TProgramAddress,
TAccountEpochMarker,
TAccountEpochState,
TAccountConfig,
TAccountNcn,
TAccountAccountToClose,
TAccountAccountPayer,
TAccountDaoWallet,
TAccountSystemProgram,
TAccountReceiverToClose
> {
// Program address.
const programAddress =
config?.programAddress ?? JITO_TIP_ROUTER_PROGRAM_ADDRESS;
// Original accounts.
const originalAccounts = {
epochMarker: { value: input.epochMarker ?? null, isWritable: true },
epochState: { value: input.epochState ?? null, isWritable: true },
config: { value: input.config ?? null, isWritable: false },
ncn: { value: input.ncn ?? null, isWritable: false },
accountToClose: { value: input.accountToClose ?? null, isWritable: true },
accountPayer: { value: input.accountPayer ?? null, isWritable: true },
daoWallet: { value: input.daoWallet ?? null, isWritable: true },
systemProgram: { value: input.systemProgram ?? null, isWritable: false },
receiverToClose: { value: input.receiverToClose ?? null, isWritable: true },
};
const accounts = originalAccounts as Record<
keyof typeof originalAccounts,
ResolvedAccount
>;
// Original args.
const args = { ...input };
// Resolve default values.
if (!accounts.systemProgram.value) {
accounts.systemProgram.value =
'11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>;
}
const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
const instruction = {
accounts: [
getAccountMeta(accounts.epochMarker),
getAccountMeta(accounts.epochState),
getAccountMeta(accounts.config),
getAccountMeta(accounts.ncn),
getAccountMeta(accounts.accountToClose),
getAccountMeta(accounts.accountPayer),
getAccountMeta(accounts.daoWallet),
getAccountMeta(accounts.systemProgram),
getAccountMeta(accounts.receiverToClose),
],
programAddress,
data: getCloseEpochAccountInstructionDataEncoder().encode(
args as CloseEpochAccountInstructionDataArgs
),
} as CloseEpochAccountInstruction<
TProgramAddress,
TAccountEpochMarker,
TAccountEpochState,
TAccountConfig,
TAccountNcn,
TAccountAccountToClose,
TAccountAccountPayer,
TAccountDaoWallet,
TAccountSystemProgram,
TAccountReceiverToClose
>;
return instruction;
}
export type ParsedCloseEpochAccountInstruction<
TProgram extends string = typeof JITO_TIP_ROUTER_PROGRAM_ADDRESS,
TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
> = {
programAddress: Address<TProgram>;
accounts: {
epochMarker: TAccountMetas[0];
epochState: TAccountMetas[1];
config: TAccountMetas[2];
ncn: TAccountMetas[3];
accountToClose: TAccountMetas[4];
accountPayer: TAccountMetas[5];
daoWallet: TAccountMetas[6];
systemProgram: TAccountMetas[7];
receiverToClose?: TAccountMetas[8] | undefined;
};
data: CloseEpochAccountInstructionData;
};
export function parseCloseEpochAccountInstruction<
TProgram extends string,
TAccountMetas extends readonly IAccountMeta[],
>(
instruction: IInstruction<TProgram> &
IInstructionWithAccounts<TAccountMetas> &
IInstructionWithData<Uint8Array>
): ParsedCloseEpochAccountInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 9) {
// TODO: Coded error.
throw new Error('Not enough accounts');
}
let accountIndex = 0;
const getNextAccount = () => {
const accountMeta = instruction.accounts![accountIndex]!;
accountIndex += 1;
return accountMeta;
};
const getNextOptionalAccount = () => {
const accountMeta = getNextAccount();
return accountMeta.address === JITO_TIP_ROUTER_PROGRAM_ADDRESS
? undefined
: accountMeta;
};
return {
programAddress: instruction.programAddress,
accounts: {
epochMarker: getNextAccount(),
epochState: getNextAccount(),
config: getNextAccount(),
ncn: getNextAccount(),
accountToClose: getNextAccount(),
accountPayer: getNextAccount(),
daoWallet: getNextAccount(),
systemProgram: getNextAccount(),
receiverToClose: getNextOptionalAccount(),
},
data: getCloseEpochAccountInstructionDataDecoder().decode(instruction.data),
};
}