Skip to content

Commit bd2ab59

Browse files
authored
Fix byte prefix of write instruction in generated clients (#13)
1 parent 37b619f commit bd2ab59

File tree

12 files changed

+269
-56
lines changed

12 files changed

+269
-56
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/js/_setup.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { getCreateAccountInstruction } from '@solana-program/system';
2+
import {
3+
BaseTransactionMessage,
4+
Commitment,
5+
Instruction,
6+
Rpc,
7+
RpcSubscriptions,
8+
SolanaRpcApi,
9+
SolanaRpcSubscriptionsApi,
10+
TransactionMessageWithBlockhashLifetime,
11+
TransactionMessageWithFeePayer,
12+
TransactionSigner,
13+
airdropFactory,
14+
appendTransactionMessageInstructions,
15+
assertIsSendableTransaction,
16+
assertIsTransactionWithBlockhashLifetime,
17+
createSolanaRpc,
18+
createSolanaRpcSubscriptions,
19+
createTransactionMessage,
20+
generateKeyPairSigner,
21+
getSignatureFromTransaction,
22+
lamports,
23+
pipe,
24+
sendAndConfirmTransactionFactory,
25+
setTransactionMessageFeePayerSigner,
26+
setTransactionMessageLifetimeUsingBlockhash,
27+
signTransactionMessageWithSigners,
28+
} from '@solana/kit';
29+
import { getInitializeBufferInstruction, LOADER_V3_PROGRAM_ADDRESS } from './src';
30+
31+
export const BUFFER_HEADER_SIZE = 37n;
32+
33+
type Client = {
34+
rpc: Rpc<SolanaRpcApi>;
35+
rpcSubscriptions: RpcSubscriptions<SolanaRpcSubscriptionsApi>;
36+
};
37+
38+
export const createDefaultSolanaClient = (): Client => {
39+
const rpc = createSolanaRpc('http://127.0.0.1:8899');
40+
const rpcSubscriptions = createSolanaRpcSubscriptions('ws://127.0.0.1:8900');
41+
return { rpc, rpcSubscriptions };
42+
};
43+
44+
export const generateKeyPairSignerWithSol = async (client: Client, putativeLamports: bigint = 1_000_000_000n) => {
45+
const signer = await generateKeyPairSigner();
46+
await airdropFactory(client)({
47+
recipientAddress: signer.address,
48+
lamports: lamports(putativeLamports),
49+
commitment: 'confirmed',
50+
});
51+
return signer;
52+
};
53+
54+
export const createDefaultTransactionMessage = async (
55+
client: Client,
56+
feePayer: TransactionSigner,
57+
instructions?: Instruction[],
58+
) => {
59+
const { value: latestBlockhash } = await client.rpc.getLatestBlockhash().send();
60+
return pipe(
61+
createTransactionMessage({ version: 0 }),
62+
tx => setTransactionMessageFeePayerSigner(feePayer, tx),
63+
tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
64+
tx => (instructions ? appendTransactionMessageInstructions(instructions, tx) : tx),
65+
);
66+
};
67+
68+
export const signAndSendTransaction = async (
69+
client: Client,
70+
transactionMessage: BaseTransactionMessage &
71+
TransactionMessageWithFeePayer &
72+
TransactionMessageWithBlockhashLifetime,
73+
commitment: Commitment = 'confirmed',
74+
) => {
75+
const signedTransaction = await signTransactionMessageWithSigners(transactionMessage);
76+
const signature = getSignatureFromTransaction(signedTransaction);
77+
assertIsSendableTransaction(signedTransaction);
78+
assertIsTransactionWithBlockhashLifetime(signedTransaction);
79+
await sendAndConfirmTransactionFactory(client)(signedTransaction, {
80+
commitment,
81+
});
82+
return signature;
83+
};
84+
85+
export async function getCreateBufferInstructions(
86+
client: Client,
87+
input: {
88+
payer: TransactionSigner;
89+
buffer: TransactionSigner;
90+
dataLength: number | bigint;
91+
},
92+
) {
93+
const bufferSize = BUFFER_HEADER_SIZE + BigInt(input.dataLength);
94+
const bufferLamports = await client.rpc.getMinimumBalanceForRentExemption(bufferSize).send();
95+
return [
96+
getCreateAccountInstruction({
97+
payer: input.payer,
98+
newAccount: input.buffer,
99+
lamports: bufferLamports,
100+
space: bufferSize,
101+
programAddress: LOADER_V3_PROGRAM_ADDRESS,
102+
}),
103+
getInitializeBufferInstruction({
104+
sourceAccount: input.buffer.address,
105+
bufferAuthority: input.payer.address,
106+
}),
107+
];
108+
}

clients/js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@solana/kit": "^5.0"
3939
},
4040
"devDependencies": {
41+
"@solana-program/system": "^0.10.0",
4142
"@solana/eslint-config-solana": "^6.0.0",
4243
"@solana/kit": "^5.0",
4344
"@types/node": "^24",

clients/js/pnpm-lock.yaml

Lines changed: 30 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)