Skip to content

Commit 10aba1e

Browse files
committed
Add missing instructions to update metadata JS plan
1 parent 1964b56 commit 10aba1e

1 file changed

Lines changed: 55 additions & 8 deletions

File tree

clients/js/src/updateMetadata.ts

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ import {
1212
import {
1313
fetchMetadata,
1414
getAllocateInstruction,
15+
getCloseInstruction,
1516
getSetAuthorityInstruction,
1617
getSetDataInstruction,
18+
getTrimInstruction,
1719
} from './generated';
1820
import {
1921
calculateMaxChunkSize,
2022
getComputeUnitInstructions,
2123
getExtendedMetadataInput,
24+
getExtendInstructionPlan,
2225
getMetadataInstructionPlanExecutor,
2326
getWriteInstructionPlan,
2427
messageFitsInOneTransaction,
2528
PdaDetails,
29+
REALLOC_LIMIT,
2630
} from './internals';
2731
import { getAccountSize, MetadataInput, MetadataResponse } from './utils';
2832
import {
@@ -132,8 +136,6 @@ export function getUpdateMetadataInstructionPlanUsingInstructionData(
132136
);
133137
}
134138

135-
// TODO: Use extend instruction if sizeDifference > 10KB.
136-
137139
plan.instructions.push(
138140
getSetDataInstruction({
139141
...input,
@@ -143,7 +145,15 @@ export function getUpdateMetadataInstructionPlanUsingInstructionData(
143145
);
144146

145147
if (input.sizeDifference < 0) {
146-
// TODO: Trim to withdraw excess lamports.
148+
plan.instructions.push(
149+
getTrimInstruction({
150+
account: input.metadata,
151+
authority: input.authority,
152+
destination: input.payer.address,
153+
program: input.program,
154+
programData: input.isCanonical ? input.programData : undefined,
155+
})
156+
);
147157
}
148158

149159
return plan;
@@ -180,9 +190,6 @@ export function getUpdateMetadataInstructionPlanUsingBuffer(
180190
);
181191
}
182192

183-
// TODO: Use extend on metadata instruction if sizeDifference > 10KB.
184-
// TODO: Use extend on buffer.
185-
186193
initialMessage.instructions.push(
187194
getTransferSolInstruction({
188195
source: input.payer,
@@ -201,6 +208,30 @@ export function getUpdateMetadataInstructionPlanUsingBuffer(
201208
);
202209
mainPlan.plans.push(initialMessage);
203210

211+
if (input.sizeDifference > REALLOC_LIMIT) {
212+
mainPlan.plans.push(
213+
getExtendInstructionPlan({
214+
account: input.metadata,
215+
authority: input.authority,
216+
extraLength: Number(input.sizeDifference),
217+
program: input.program,
218+
programData: input.isCanonical ? input.programData : undefined,
219+
priorityFees: input.priorityFees,
220+
})
221+
);
222+
}
223+
224+
if (input.data.length > REALLOC_LIMIT) {
225+
mainPlan.plans.push(
226+
getExtendInstructionPlan({
227+
account: input.buffer.address,
228+
authority: input.authority,
229+
extraLength: input.data.length,
230+
priorityFees: input.priorityFees,
231+
})
232+
);
233+
}
234+
204235
let offset = 0;
205236
const writePlan: InstructionPlan = { kind: 'parallel', plans: [] };
206237
while (offset < input.data.length) {
@@ -234,11 +265,27 @@ export function getUpdateMetadataInstructionPlanUsingBuffer(
234265
};
235266

236267
if (input.closeBuffer) {
237-
// TODO: Close buffer account.
268+
finalizeMessage.instructions.push(
269+
getCloseInstruction({
270+
account: input.buffer.address,
271+
authority: input.authority,
272+
destination: input.payer.address,
273+
program: input.program,
274+
programData: input.isCanonical ? input.programData : undefined,
275+
})
276+
);
238277
}
239278

240279
if (input.sizeDifference < 0) {
241-
// TODO: Trim to withdraw excess lamports.
280+
finalizeMessage.instructions.push(
281+
getTrimInstruction({
282+
account: input.metadata,
283+
authority: input.authority,
284+
destination: input.payer.address,
285+
program: input.program,
286+
programData: input.isCanonical ? input.programData : undefined,
287+
})
288+
);
242289
}
243290

244291
mainPlan.plans.push(finalizeMessage);

0 commit comments

Comments
 (0)