diff --git a/clients/js/src/updateMetadata.ts b/clients/js/src/updateMetadata.ts index 3f1e82f..4c4809d 100644 --- a/clients/js/src/updateMetadata.ts +++ b/clients/js/src/updateMetadata.ts @@ -116,6 +116,7 @@ export async function getUpdateMetadataInstructionPlan( return await getUpdateMetadataInstructionPlanUsingNewBuffer(client, { ...input, buffer: newBuffer, + closeBuffer: input.closeBuffer ?? true, data, metadata, }); diff --git a/clients/js/src/utils.ts b/clients/js/src/utils.ts index 443f73b..446f942 100644 --- a/clients/js/src/utils.ts +++ b/clients/js/src/utils.ts @@ -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; /** diff --git a/clients/js/test/updateMetadata.test.ts b/clients/js/test/updateMetadata.test.ts index 39a150f..5305f56 100644 --- a/clients/js/test/updateMetadata.test.ts +++ b/clients/js/test/updateMetadata.test.ts @@ -1,5 +1,6 @@ import { address, + flattenTransactionPlanResult, generateKeyPairSigner, getUtf8Encoder, isSolanaError, @@ -12,6 +13,7 @@ import { expect, it } from 'vitest'; import { AccountDiscriminator, + ALLOCATE_DISCRIMINATOR, Compression, DataSource, Encoding, @@ -19,6 +21,7 @@ import { findNonCanonicalPda, Format, Metadata, + PROGRAM_METADATA_PROGRAM_ADDRESS, } from '../src'; import { createDeployedProgram, @@ -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();