-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathallocate.ts
More file actions
162 lines (145 loc) · 4.66 KB
/
allocate.ts
File metadata and controls
162 lines (145 loc) · 4.66 KB
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
/**
* This code was AUTOGENERATED using the codama library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun codama to update it.
*
* @see https://github.com/codama-idl/codama
*/
import {
combineCodec,
getStructDecoder,
getStructEncoder,
getU32Decoder,
getU32Encoder,
getU64Decoder,
getU64Encoder,
transformEncoder,
type AccountMeta,
type AccountSignerMeta,
type Address,
type FixedSizeCodec,
type FixedSizeDecoder,
type FixedSizeEncoder,
type Instruction,
type InstructionWithAccounts,
type InstructionWithData,
type ReadonlyUint8Array,
type TransactionSigner,
type WritableSignerAccount,
} from '@solana/kit';
import { SYSTEM_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
export const ALLOCATE_DISCRIMINATOR = 8;
export function getAllocateDiscriminatorBytes() {
return getU32Encoder().encode(ALLOCATE_DISCRIMINATOR);
}
export type AllocateInstruction<
TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
TAccountNewAccount extends string | AccountMeta<string> = string,
TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
InstructionWithData<ReadonlyUint8Array> &
InstructionWithAccounts<
[
TAccountNewAccount extends string
? WritableSignerAccount<TAccountNewAccount> &
AccountSignerMeta<TAccountNewAccount>
: TAccountNewAccount,
...TRemainingAccounts,
]
>;
export type AllocateInstructionData = { discriminator: number; space: bigint };
export type AllocateInstructionDataArgs = { space: number | bigint };
export function getAllocateInstructionDataEncoder(): FixedSizeEncoder<AllocateInstructionDataArgs> {
return transformEncoder(
getStructEncoder([
['discriminator', getU32Encoder()],
['space', getU64Encoder()],
]),
(value) => ({ ...value, discriminator: ALLOCATE_DISCRIMINATOR })
);
}
export function getAllocateInstructionDataDecoder(): FixedSizeDecoder<AllocateInstructionData> {
return getStructDecoder([
['discriminator', getU32Decoder()],
['space', getU64Decoder()],
]);
}
export function getAllocateInstructionDataCodec(): FixedSizeCodec<
AllocateInstructionDataArgs,
AllocateInstructionData
> {
return combineCodec(
getAllocateInstructionDataEncoder(),
getAllocateInstructionDataDecoder()
);
}
export type AllocateInput<TAccountNewAccount extends string = string> = {
newAccount: TransactionSigner<TAccountNewAccount>;
space: AllocateInstructionDataArgs['space'];
};
export function getAllocateInstruction<
TAccountNewAccount extends string,
TProgramAddress extends Address = typeof SYSTEM_PROGRAM_ADDRESS,
>(
input: AllocateInput<TAccountNewAccount>,
config?: { programAddress?: TProgramAddress }
): AllocateInstruction<TProgramAddress, TAccountNewAccount> {
// Program address.
const programAddress = config?.programAddress ?? SYSTEM_PROGRAM_ADDRESS;
// Original accounts.
const originalAccounts = {
newAccount: { value: input.newAccount ?? null, isWritable: true },
};
const accounts = originalAccounts as Record<
keyof typeof originalAccounts,
ResolvedAccount
>;
// Original args.
const args = { ...input };
const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
const instruction = {
accounts: [getAccountMeta(accounts.newAccount)],
programAddress,
data: getAllocateInstructionDataEncoder().encode(
args as AllocateInstructionDataArgs
),
} as AllocateInstruction<TProgramAddress, TAccountNewAccount>;
return instruction;
}
export type ParsedAllocateInstruction<
TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
programAddress: Address<TProgram>;
accounts: {
newAccount: TAccountMetas[0];
};
data: AllocateInstructionData;
};
export function parseAllocateInstruction<
TProgram extends string,
TAccountMetas extends readonly AccountMeta[],
>(
instruction: Instruction<TProgram> &
InstructionWithAccounts<TAccountMetas> &
InstructionWithData<ReadonlyUint8Array>
): ParsedAllocateInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 1) {
// TODO: Coded error.
throw new Error('Not enough accounts');
}
let accountIndex = 0;
const getNextAccount = () => {
const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
accountIndex += 1;
return accountMeta;
};
return {
programAddress: instruction.programAddress,
accounts: {
newAccount: getNextAccount(),
},
data: getAllocateInstructionDataDecoder().decode(instruction.data),
};
}