Skip to content

Commit 1eb2762

Browse files
authored
pinocchio/program: Add program IDL (#145)
* Add idl * Replace current IDL * Address review comments * Update generated client * Tweak formatting
1 parent 876ef4d commit 1eb2762

7 files changed

Lines changed: 897 additions & 11 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* This code was AUTOGENERATED using the Codama library.
3+
* Please DO NOT EDIT THIS FILE, instead use visitors
4+
* to add features, then rerun Codama to update it.
5+
*
6+
* @see https://github.com/codama-idl/codama
7+
*/
8+
9+
import {
10+
AccountRole,
11+
addDecoderSizePrefix,
12+
addEncoderSizePrefix,
13+
combineCodec,
14+
getArrayDecoder,
15+
getArrayEncoder,
16+
getBytesDecoder,
17+
getBytesEncoder,
18+
getStructDecoder,
19+
getStructEncoder,
20+
getU8Decoder,
21+
getU8Encoder,
22+
transformEncoder,
23+
type AccountMeta,
24+
type Address,
25+
type Codec,
26+
type Decoder,
27+
type Encoder,
28+
type Instruction,
29+
type InstructionWithAccounts,
30+
type InstructionWithData,
31+
type ReadonlyUint8Array,
32+
type TransactionSigner,
33+
} from '@solana/kit';
34+
import { TOKEN_PROGRAM_ADDRESS } from '../programs';
35+
36+
export const BATCH_DISCRIMINATOR = 255;
37+
38+
export function getBatchDiscriminatorBytes() {
39+
return getU8Encoder().encode(BATCH_DISCRIMINATOR);
40+
}
41+
42+
export type BatchInstruction<
43+
TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
44+
TRemainingAccounts extends readonly AccountMeta<string>[] = [],
45+
> = Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<TRemainingAccounts>;
46+
47+
export type BatchInstructionData = {
48+
discriminator: number;
49+
/** Instruction data for batch instructions. */
50+
data: Array<{ numberOfAccounts: number; instructionData: ReadonlyUint8Array }>;
51+
};
52+
53+
export type BatchInstructionDataArgs = {
54+
/** Instruction data for batch instructions. */
55+
data: Array<{ numberOfAccounts: number; instructionData: ReadonlyUint8Array }>;
56+
};
57+
58+
export function getBatchInstructionDataEncoder(): Encoder<BatchInstructionDataArgs> {
59+
return transformEncoder(
60+
getStructEncoder([
61+
['discriminator', getU8Encoder()],
62+
[
63+
'data',
64+
getArrayEncoder(
65+
getStructEncoder([
66+
['numberOfAccounts', getU8Encoder()],
67+
['instructionData', addEncoderSizePrefix(getBytesEncoder(), getU8Encoder())],
68+
]),
69+
{ size: 'remainder' },
70+
),
71+
],
72+
]),
73+
value => ({ ...value, discriminator: BATCH_DISCRIMINATOR }),
74+
);
75+
}
76+
77+
export function getBatchInstructionDataDecoder(): Decoder<BatchInstructionData> {
78+
return getStructDecoder([
79+
['discriminator', getU8Decoder()],
80+
[
81+
'data',
82+
getArrayDecoder(
83+
getStructDecoder([
84+
['numberOfAccounts', getU8Decoder()],
85+
['instructionData', addDecoderSizePrefix(getBytesDecoder(), getU8Decoder())],
86+
]),
87+
{ size: 'remainder' },
88+
),
89+
],
90+
]);
91+
}
92+
93+
export function getBatchInstructionDataCodec(): Codec<BatchInstructionDataArgs, BatchInstructionData> {
94+
return combineCodec(getBatchInstructionDataEncoder(), getBatchInstructionDataDecoder());
95+
}
96+
97+
export type BatchInput = {
98+
data: BatchInstructionDataArgs['data'];
99+
accounts?: Array<TransactionSigner>;
100+
};
101+
102+
export function getBatchInstruction<TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS>(
103+
input: BatchInput,
104+
config?: { programAddress?: TProgramAddress },
105+
): BatchInstruction<TProgramAddress> {
106+
// Program address.
107+
const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
108+
109+
// Original args.
110+
const args = { ...input };
111+
112+
// Remaining accounts.
113+
const remainingAccounts: AccountMeta[] = (args.accounts ?? []).map(signer => ({
114+
address: signer.address,
115+
role: AccountRole.READONLY_SIGNER,
116+
signer,
117+
}));
118+
119+
return Object.freeze({
120+
accounts: remainingAccounts,
121+
data: getBatchInstructionDataEncoder().encode(args as BatchInstructionDataArgs),
122+
programAddress,
123+
} as BatchInstruction<TProgramAddress>);
124+
}
125+
126+
export type ParsedBatchInstruction<TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS> = {
127+
programAddress: Address<TProgram>;
128+
data: BatchInstructionData;
129+
};
130+
131+
export function parseBatchInstruction<TProgram extends string>(
132+
instruction: Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array>,
133+
): ParsedBatchInstruction<TProgram> {
134+
return {
135+
programAddress: instruction.programAddress,
136+
data: getBatchInstructionDataDecoder().decode(instruction.data),
137+
};
138+
}

clients/js/src/generated/instructions/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
export * from './amountToUiAmount';
1010
export * from './approve';
1111
export * from './approveChecked';
12+
export * from './batch';
1213
export * from './burn';
1314
export * from './burnChecked';
1415
export * from './closeAccount';
@@ -34,3 +35,5 @@ export * from './thawAccount';
3435
export * from './transfer';
3536
export * from './transferChecked';
3637
export * from './uiAmountToAmount';
38+
export * from './unwrapLamports';
39+
export * from './withdrawExcessLamports';

clients/js/src/generated/instructions/syncNative.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
type Instruction,
2424
type InstructionWithAccounts,
2525
type InstructionWithData,
26+
type ReadonlyAccount,
2627
type ReadonlyUint8Array,
2728
type WritableAccount,
2829
} from '@solana/kit';
@@ -38,11 +39,18 @@ export function getSyncNativeDiscriminatorBytes() {
3839
export type SyncNativeInstruction<
3940
TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
4041
TAccountAccount extends string | AccountMeta<string> = string,
42+
TAccountRent extends string | AccountMeta<string> | undefined = undefined,
4143
TRemainingAccounts extends readonly AccountMeta<string>[] = [],
4244
> = Instruction<TProgram> &
4345
InstructionWithData<ReadonlyUint8Array> &
4446
InstructionWithAccounts<
45-
[TAccountAccount extends string ? WritableAccount<TAccountAccount> : TAccountAccount, ...TRemainingAccounts]
47+
[
48+
TAccountAccount extends string ? WritableAccount<TAccountAccount> : TAccountAccount,
49+
...(TAccountRent extends undefined
50+
? []
51+
: [TAccountRent extends string ? ReadonlyAccount<TAccountRent> : TAccountRent]),
52+
...TRemainingAccounts,
53+
]
4654
>;
4755

4856
export type SyncNativeInstructionData = { discriminator: number };
@@ -67,31 +75,45 @@ export function getSyncNativeInstructionDataCodec(): FixedSizeCodec<
6775
return combineCodec(getSyncNativeInstructionDataEncoder(), getSyncNativeInstructionDataDecoder());
6876
}
6977

70-
export type SyncNativeInput<TAccountAccount extends string = string> = {
78+
export type SyncNativeInput<TAccountAccount extends string = string, TAccountRent extends string = string> = {
7179
/** The native token account to sync with its underlying lamports. */
7280
account: Address<TAccountAccount>;
81+
/** Rent sysvar. */
82+
rent?: Address<TAccountRent>;
7383
};
7484

7585
export function getSyncNativeInstruction<
7686
TAccountAccount extends string,
87+
TAccountRent extends string,
7788
TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
7889
>(
79-
input: SyncNativeInput<TAccountAccount>,
90+
input: SyncNativeInput<TAccountAccount, TAccountRent>,
8091
config?: { programAddress?: TProgramAddress },
81-
): SyncNativeInstruction<TProgramAddress, TAccountAccount> {
92+
): SyncNativeInstruction<TProgramAddress, TAccountAccount, TAccountRent> {
8293
// Program address.
8394
const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
8495

8596
// Original accounts.
86-
const originalAccounts = { account: { value: input.account ?? null, isWritable: true } };
97+
const originalAccounts = {
98+
account: { value: input.account ?? null, isWritable: true },
99+
rent: { value: input.rent ?? null, isWritable: false },
100+
};
87101
const accounts = originalAccounts as Record<keyof typeof originalAccounts, ResolvedInstructionAccount>;
88102

89-
const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
103+
// Resolve default values.
104+
if (!accounts.rent.value) {
105+
accounts.rent.value =
106+
'SysvarRent111111111111111111111111111111111' as Address<'SysvarRent111111111111111111111111111111111'>;
107+
}
108+
109+
const getAccountMeta = getAccountMetaFactory(programAddress, 'omitted');
90110
return Object.freeze({
91-
accounts: [getAccountMeta('account', accounts.account)],
111+
accounts: [getAccountMeta('account', accounts.account), getAccountMeta('rent', accounts.rent)].filter(
112+
<T>(x: T | undefined): x is T => x !== undefined,
113+
),
92114
data: getSyncNativeInstructionDataEncoder().encode({}),
93115
programAddress,
94-
} as SyncNativeInstruction<TProgramAddress, TAccountAccount>);
116+
} as SyncNativeInstruction<TProgramAddress, TAccountAccount, TAccountRent>);
95117
}
96118

97119
export type ParsedSyncNativeInstruction<
@@ -102,6 +124,8 @@ export type ParsedSyncNativeInstruction<
102124
accounts: {
103125
/** The native token account to sync with its underlying lamports. */
104126
account: TAccountMetas[0];
127+
/** Rent sysvar. */
128+
rent?: TAccountMetas[1] | undefined;
105129
};
106130
data: SyncNativeInstructionData;
107131
};
@@ -123,9 +147,15 @@ export function parseSyncNativeInstruction<TProgram extends string, TAccountMeta
123147
accountIndex += 1;
124148
return accountMeta;
125149
};
150+
let optionalAccountsRemaining = instruction.accounts.length - 1;
151+
const getNextOptionalAccount = () => {
152+
if (optionalAccountsRemaining === 0) return undefined;
153+
optionalAccountsRemaining -= 1;
154+
return getNextAccount();
155+
};
126156
return {
127157
programAddress: instruction.programAddress,
128-
accounts: { account: getNextAccount() },
158+
accounts: { account: getNextAccount(), rent: getNextOptionalAccount() },
129159
data: getSyncNativeInstructionDataDecoder().decode(instruction.data),
130160
};
131161
}

0 commit comments

Comments
 (0)