Skip to content

Commit e72cd94

Browse files
committed
Update write instruction in JS client
1 parent 6257b0d commit e72cd94

7 files changed

Lines changed: 39 additions & 6 deletions

File tree

clients/js/src/createMetadata.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export function getCreateMetadataInstructionPlanUsingBuffer(
132132
getWriteInstructionPlan({
133133
buffer: input.metadata,
134134
authority: input.authority,
135+
offset,
135136
data: input.data.slice(offset, offset + input.chunkSize),
136137
priorityFees: input.priorityFees,
137138
})

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
getBytesEncoder,
1313
getStructDecoder,
1414
getStructEncoder,
15+
getU32Decoder,
16+
getU32Encoder,
1517
getU8Decoder,
1618
getU8Encoder,
1719
transformEncoder,
@@ -60,15 +62,22 @@ export type WriteInstruction<
6062

6163
export type WriteInstructionData = {
6264
discriminator: number;
65+
/** The offset to write to. */
66+
offset: number;
6367
data: ReadonlyUint8Array;
6468
};
6569

66-
export type WriteInstructionDataArgs = { data: ReadonlyUint8Array };
70+
export type WriteInstructionDataArgs = {
71+
/** The offset to write to. */
72+
offset: number;
73+
data: ReadonlyUint8Array;
74+
};
6775

6876
export function getWriteInstructionDataEncoder(): Encoder<WriteInstructionDataArgs> {
6977
return transformEncoder(
7078
getStructEncoder([
7179
['discriminator', getU8Encoder()],
80+
['offset', getU32Encoder()],
7281
['data', getBytesEncoder()],
7382
]),
7483
(value) => ({ ...value, discriminator: WRITE_DISCRIMINATOR })
@@ -78,6 +87,7 @@ export function getWriteInstructionDataEncoder(): Encoder<WriteInstructionDataAr
7887
export function getWriteInstructionDataDecoder(): Decoder<WriteInstructionData> {
7988
return getStructDecoder([
8089
['discriminator', getU8Decoder()],
90+
['offset', getU32Decoder()],
8191
['data', getBytesDecoder()],
8292
]);
8393
}
@@ -100,6 +110,7 @@ export type WriteInput<
100110
buffer: Address<TAccountBuffer>;
101111
/** The authority of the buffer. */
102112
authority: TransactionSigner<TAccountAuthority>;
113+
offset: WriteInstructionDataArgs['offset'];
103114
data: WriteInstructionDataArgs['data'];
104115
};
105116

clients/js/src/internals.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ export function calculateMaxChunkSize(
162162
priorityFees?: MicroLamports;
163163
}
164164
) {
165-
const plan = getWriteInstructionPlan({ ...input, data: new Uint8Array(0) });
165+
const plan = getWriteInstructionPlan({
166+
...input,
167+
offset: 0,
168+
data: new Uint8Array(0),
169+
});
166170
const message = getTransactionMessageFromPlan(defaultMessage, plan);
167171
return getRemainingTransactionSpaceFromMessage(message);
168172
}
@@ -193,6 +197,7 @@ function getTransactionSizeFromMessage(
193197
export function getWriteInstructionPlan(input: {
194198
buffer: Address;
195199
authority: TransactionSigner;
200+
offset: number;
196201
data: ReadonlyUint8Array;
197202
priorityFees?: MicroLamports;
198203
}): MessageInstructionPlan {
@@ -203,7 +208,7 @@ export function getWriteInstructionPlan(input: {
203208
computeUnitPrice: input.priorityFees,
204209
computeUnitLimit: 'simulated',
205210
}),
206-
getWriteInstruction(input),
211+
getWriteInstruction({ ...input }),
207212
],
208213
};
209214
}

clients/js/src/updateMetadata.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ export function getUpdateMetadataInstructionPlanUsingBuffer(
209209
getWriteInstructionPlan({
210210
buffer: input.buffer.address,
211211
authority: input.buffer,
212+
offset,
212213
data: input.data.slice(offset, offset + input.chunkSize),
213214
priorityFees: input.priorityFees,
214215
})

clients/js/test/_setup.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ export async function createBuffer(
220220
});
221221
const instructions: IInstruction[] = [preFundIx, allocateIx];
222222
if (data) {
223-
instructions.push(getWriteInstruction({ buffer, authority, data }));
223+
instructions.push(
224+
getWriteInstruction({ buffer, authority, offset: 0, data })
225+
);
224226
}
225227
await pipe(
226228
defaultTransaction,

clients/js/test/write.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test('it writes to canonical PDA buffers', async (t) => {
4141
});
4242

4343
// When we write some data to the buffer account.
44-
const writeIx = getWriteInstruction({ buffer, authority, data });
44+
const writeIx = getWriteInstruction({ buffer, authority, offset: 0, data });
4545
await pipe(
4646
await createDefaultTransaction(client, authority),
4747
(tx) => appendTransactionMessageInstruction(writeIx, tx),
@@ -74,7 +74,7 @@ test('it writes to non-canonical PDA buffers', async (t) => {
7474
});
7575

7676
// When we write some data to the buffer account.
77-
const writeIx = getWriteInstruction({ buffer, authority, data });
77+
const writeIx = getWriteInstruction({ buffer, authority, offset: 0, data });
7878
await pipe(
7979
await createDefaultTransaction(client, authority),
8080
(tx) => appendTransactionMessageInstruction(writeIx, tx),
@@ -106,6 +106,7 @@ test('it writes to keypair buffers', async (t) => {
106106
const writeIx = getWriteInstruction({
107107
buffer: buffer.address,
108108
authority: buffer,
109+
offset: 0,
109110
data,
110111
});
111112
await pipe(
@@ -139,11 +140,13 @@ test('it appends to the end of buffers when doing multiple writes', async (t) =>
139140
const firstWriteIx = getWriteInstruction({
140141
buffer: buffer.address,
141142
authority: buffer,
143+
offset: 0,
142144
data: dataChunk1,
143145
});
144146
const secondWriteIx = getWriteInstruction({
145147
buffer: buffer.address,
146148
authority: buffer,
149+
offset: dataChunk1.length,
147150
data: dataChunk2,
148151
});
149152
await pipe(

program/idl.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,16 @@
326326
"defaultValueStrategy": "omitted",
327327
"defaultValue": { "kind": "numberValueNode", "number": 0 }
328328
},
329+
{
330+
"kind": "instructionArgumentNode",
331+
"name": "offset",
332+
"docs": ["The offset to write to."],
333+
"type": {
334+
"kind": "numberTypeNode",
335+
"format": "u32",
336+
"endian": "le"
337+
}
338+
},
329339
{
330340
"kind": "instructionArgumentNode",
331341
"name": "data",

0 commit comments

Comments
 (0)