Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clients/js/src/updateMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export async function getUpdateMetadataInstructionPlan(
return await getUpdateMetadataInstructionPlanUsingNewBuffer(client, {
...input,
buffer: newBuffer,
closeBuffer: input.closeBuffer ?? true,
data,
metadata,
});
Expand Down
2 changes: 1 addition & 1 deletion clients/js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export type MetadataInput = {
/**
* When using a buffer, whether to close the buffer account after the operation.
* If an address is provided, it will be used as the destination for the close instruction.
* Defaults to `true`.
* Defaults to `true` for a buffer created to hold `data` and `false` for an existing `buffer`.
*/
closeBuffer?: Address | HasAddress | boolean;
/**
Expand Down
46 changes: 46 additions & 0 deletions clients/js/test/updateMetadata.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
address,
flattenTransactionPlanResult,
generateKeyPairSigner,
getUtf8Encoder,
isSolanaError,
Expand All @@ -12,13 +13,15 @@ import { expect, it } from 'vitest';

import {
AccountDiscriminator,
ALLOCATE_DISCRIMINATOR,
Compression,
DataSource,
Encoding,
findCanonicalPda,
findNonCanonicalPda,
Format,
Metadata,
PROGRAM_METADATA_PROGRAM_ADDRESS,
} from '../src';
import {
createDeployedProgram,
Expand Down Expand Up @@ -497,6 +500,49 @@ it('can close a new buffer after using it to update a new metadata account', asy
});
});

it('closes the buffer it creates for data larger than a transaction size by default', async () => {
// Given the following authority and deployed program.
const client = await createTestClient();
const authority = await generateKeyPairSignerWithSol(client);
const [program, programData] = await createDeployedProgram(client, authority);

// And the following existing canonical metadata account.
await client.programMetadata.createMetadata({
authority,
program,
programData,
seed: 'idl',
encoding: Encoding.Utf8,
compression: Compression.None,
dataSource: DataSource.Direct,
format: Format.Json,
data: getUtf8Encoder().encode('OLD'),
});

// When we update the metadata account with data that needs a buffer.
const result = await client.programMetadata.updateMetadata({
authority,
program,
programData,
seed: 'idl',
encoding: Encoding.Utf8,
compression: Compression.None,
dataSource: DataSource.Direct,
format: Format.Json,
data: getUtf8Encoder().encode('x'.repeat(3_000)),
});

// Then we expect the buffer created along the way to no longer exist.
const allocate = flattenTransactionPlanResult(result)
.flatMap(({ plannedMessage }) => plannedMessage.instructions)
.find(
({ data, programAddress }) =>
programAddress === PROGRAM_METADATA_PROGRAM_ADDRESS && data?.[0] === ALLOCATE_DISCRIMINATOR,
);
const bufferAccount = await client.programMetadata.accounts.buffer.fetchMaybe(allocate!.accounts![0].address);
expect(bufferAccount.exists).toBe(false);
});

it('can close an existing buffer after using it to update a new metadata account', async () => {
// Given the following authority and deployed program.
const client = await createTestClient();
Expand Down