Skip to content
Merged
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
7 changes: 6 additions & 1 deletion clients/js/src/cli/commands/create-buffer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { generateKeyPairSigner } from '@solana/kit';

import { fetchBuffer } from '../../generated';
import { fileArgument } from '../arguments';
import { logCommand } from '../logs';
import { GlobalOptions, setWriteOptions, WriteOptions } from '../options';
Expand All @@ -18,7 +19,11 @@ type Options = WriteOptions;
export async function doCreateBuffer(file: string | undefined, _: Options, cmd: CustomCommand) {
const options = cmd.optsWithGlobals() as GlobalOptions & Options;
const client = await getClient(options);
const [buffer, writeInput] = await Promise.all([generateKeyPairSigner(), getWriteInput(client, file, options)]);
const [buffer, sourceBuffer] = await Promise.all([
generateKeyPairSigner(),
options.buffer ? fetchBuffer(client.rpc, options.buffer) : Promise.resolve(undefined),
]);
const writeInput = getWriteInput(file, options, sourceBuffer);

logCommand(`Creating new buffer and setting authority...`, {
buffer: buffer.address,
Expand Down
21 changes: 10 additions & 11 deletions clients/js/src/cli/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Address } from '@solana/kit';
import picocolors from 'picocolors';

import { getCreateMetadataInstructionPlan } from '../../createMetadata';
import { fetchMaybeMetadata, Seed } from '../../generated';
import { Seed } from '../../generated';
import { decodeMaybeMetadataOrBuffer, fetchMetadataAndBuffer, isPreAllocatedBuffer } from '../../internals';
import { fileArgument, programArgument, seedArgument } from '../arguments';
import { logCommand, logErrorAndExit } from '../logs';
import { logCommand, logWarning } from '../logs';
import {
GlobalOptions,
nonCanonicalWriteOption,
NonCanonicalWriteOption,
setWriteOptions,
WriteOptions,
} from '../options';
import { CustomCommand, getClient, getPdaDetailsForWriting, getWriteInput } from '../utils';
import { CustomCommand, getClient, getPdaDetailsForWriting, getWriteInput, INTERRUPTED_CREATE_WARNING } from '../utils';

export function setCreateCommand(program: CustomCommand): void {
program
Expand All @@ -39,13 +39,12 @@ export async function doCreate(seed: Seed, program: Address, file: string | unde
authority: client.identity.address,
});

const [metadataAccount, writeInput] = await Promise.all([
fetchMaybeMetadata(client.rpc, metadata),
getWriteInput(client, file, options),
]);
const [encodedMetadata, buffer] = await fetchMetadataAndBuffer(client.rpc, metadata, options.buffer);
const metadataAccount = decodeMaybeMetadataOrBuffer(encodedMetadata);
const writeInput = getWriteInput(file, options, buffer);

if (metadataAccount.exists) {
logErrorAndExit(`Metadata account ${picocolors.bold(metadataAccount.address)} already exists.`);
if (isPreAllocatedBuffer(metadataAccount)) {
logWarning(INTERRUPTED_CREATE_WARNING);
}

await client.runOrExport(
Expand All @@ -56,7 +55,7 @@ export async function doCreate(seed: Seed, program: Address, file: string | unde
program,
programData,
seed,
metadata,
metadata: metadataAccount,
singleExtendPerTransaction: options.singleExtendPerTx,
}),
);
Expand Down
7 changes: 4 additions & 3 deletions clients/js/src/cli/commands/update-buffer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address } from '@solana/kit';

import { fetchMaybeBuffer } from '../../generated';
import { fetchBuffer, fetchMaybeBuffer } from '../../generated';
import { bufferArgument, fileArgument } from '../arguments';
import { logCommand, logErrorAndExit } from '../logs';
import { GlobalOptions, setWriteOptions, WriteOptions } from '../options';
Expand All @@ -22,10 +22,11 @@ export async function doUpdateBuffer(buffer: Address, file: string | undefined,
const client = await getClient(options);

logCommand(`Updating buffer...`, { buffer });
const [writeInput, bufferAccount] = await Promise.all([
getWriteInput(client, file, options),
const [bufferAccount, sourceBuffer] = await Promise.all([
fetchMaybeBuffer(client.rpc, buffer),
options.buffer ? fetchBuffer(client.rpc, options.buffer) : Promise.resolve(undefined),
]);
const writeInput = getWriteInput(file, options, sourceBuffer);

if (!bufferAccount.exists) {
logErrorAndExit(`Buffer account not found: "${buffer}"`);
Expand Down
10 changes: 5 additions & 5 deletions clients/js/src/cli/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Address } from '@solana/kit';
import picocolors from 'picocolors';

import { fetchMaybeMetadata, Seed } from '../../generated';
import { Seed } from '../../generated';
import { decodeMaybeMetadataOrBuffer, fetchMetadataAndBuffer } from '../../internals';
import { getUpdateMetadataInstructionPlan } from '../../updateMetadata';
import { fileArgument, programArgument, seedArgument } from '../arguments';
import { logCommand, logErrorAndExit } from '../logs';
Expand Down Expand Up @@ -39,10 +40,9 @@ export async function doWrite(seed: Seed, program: Address, file: string | undef
authority: client.identity.address,
});

const [metadataAccount, writeInput] = await Promise.all([
fetchMaybeMetadata(client.rpc, metadata),
getWriteInput(client, file, options),
]);
const [encodedMetadata, buffer] = await fetchMetadataAndBuffer(client.rpc, metadata, options.buffer);
const metadataAccount = decodeMaybeMetadataOrBuffer(encodedMetadata);
const writeInput = getWriteInput(file, options, buffer);

if (!metadataAccount.exists) {
logErrorAndExit(`Metadata account ${picocolors.bold(metadataAccount.address)} does not exist.`);
Expand Down
18 changes: 11 additions & 7 deletions clients/js/src/cli/commands/write.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Address } from '@solana/kit';

import { fetchMaybeMetadata, Seed } from '../../generated';
import { Seed } from '../../generated';
import { decodeMaybeMetadataOrBuffer, fetchMetadataAndBuffer, isPreAllocatedBuffer } from '../../internals';
import { getWriteMetadataInstructionPlan } from '../../writeMetadata';
import { fileArgument, programArgument, seedArgument } from '../arguments';
import { logCommand } from '../logs';
import { logCommand, logWarning } from '../logs';
import {
GlobalOptions,
nonCanonicalWriteOption,
NonCanonicalWriteOption,
setWriteOptions,
WriteOptions,
} from '../options';
import { CustomCommand, getClient, getPdaDetailsForWriting, getWriteInput } from '../utils';
import { CustomCommand, getClient, getPdaDetailsForWriting, getWriteInput, INTERRUPTED_CREATE_WARNING } from '../utils';

export function setWriteCommand(program: CustomCommand): void {
program
Expand All @@ -38,10 +39,13 @@ export async function doWrite(seed: Seed, program: Address, file: string | undef
authority: client.identity.address,
});

const [metadataAccount, writeInput] = await Promise.all([
fetchMaybeMetadata(client.rpc, metadata),
getWriteInput(client, file, options),
]);
const [encodedMetadata, buffer] = await fetchMetadataAndBuffer(client.rpc, metadata, options.buffer);
const metadataAccount = decodeMaybeMetadataOrBuffer(encodedMetadata);
const writeInput = getWriteInput(file, options, buffer);

if (isPreAllocatedBuffer(metadataAccount)) {
logWarning(INTERRUPTED_CREATE_WARNING);
}

await client.runOrExport(
getWriteMetadataInstructionPlan(client, {
Expand Down
32 changes: 21 additions & 11 deletions clients/js/src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import {
DataSource,
decodeMetadata,
Encoding,
fetchBuffer,
findMetadataPda,
Format,
SeedArgs,
Expand Down Expand Up @@ -445,18 +444,29 @@ async function getCanonicalPdaDetailsUnchecked(
return { isAuthorized: storedAuthority === identity, metadata, programData };
}

export async function getWriteInput(
client: Client,
/**
* Warning shown by the `create` and `write` commands when the metadata PDA
* holds a pre-allocated buffer left by an interrupted create, which they
* resume instead of starting over.
*/
export const INTERRUPTED_CREATE_WARNING =
'The metadata account holds a pre-allocated buffer left by an interrupted create. Resuming that create...';

/**
* Builds the write input shared by commands writing data from the CLI
* arguments and options. When the `--buffer` option is used, the fetched
* buffer account must be provided so commands can batch its fetch with other
* accounts.
*/
export function getWriteInput(
file: string | undefined,
options: WriteOptions,
): Promise<
PackedData & {
buffer?: Account<Buffer>;
format: Format;
closeBuffer?: Address | boolean;
}
> {
const buffer = options.buffer ? await fetchBuffer(client.rpc, options.buffer) : undefined;
buffer: Account<Buffer> | undefined,
): PackedData & {
buffer?: Account<Buffer>;
format: Format;
closeBuffer?: Address | boolean;
} {
return {
...getPackedData(file, options),
format: options.format ?? getFormatFromFile(file),
Expand Down
Loading
Loading