Skip to content

Commit

Permalink
Merge pull request #11 from metaplex-foundation/nhan/default-config-l…
Browse files Browse the repository at this point in the history
…ines

default config line settings if no config is provided and no hidden s…
  • Loading branch information
nhanphan authored Jul 8, 2024
2 parents ef83491 + 8d1e219 commit aa6cfe7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
35 changes: 30 additions & 5 deletions clients/js/src/createCandyMachine.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { createAccount } from '@metaplex-foundation/mpl-toolbox';
import {
Context,
isNone,
isOption,
none,
Signer,
transactionBuilder,
TransactionBuilder,
wrapNullable,
} from '@metaplex-foundation/umi';
import { initializeCandyMachine } from './generated';
import { getCandyMachineSize } from './hooked';
Expand All @@ -16,29 +19,51 @@ export type CreateCandyMachineInput = Omit<
candyMachine: Signer;
};

export const DEFAULT_CONFIG_LINE_SETTINGS = {
prefixName: '',
nameLength: 32,
prefixUri: '',
uriLength: 200,
isSequential: false,
};

export const createCandyMachine = async (
context: Parameters<typeof initializeCandyMachine>[0] & Pick<Context, 'rpc'>,
input: CreateCandyMachineInput
): Promise<TransactionBuilder> => {
const newInput = { ...input };

const hiddenSettings = isOption(input.hiddenSettings)
? input.hiddenSettings
: wrapNullable(input.hiddenSettings);

const configLineSettings = isOption(input.configLineSettings)
? input.configLineSettings
: wrapNullable(input.configLineSettings);

if (isNone(hiddenSettings) && isNone(configLineSettings)) {
newInput.configLineSettings = DEFAULT_CONFIG_LINE_SETTINGS;
}

const space = getCandyMachineSize(
input.itemsAvailable,
input.configLineSettings ?? none()
newInput.itemsAvailable,
newInput.configLineSettings ?? none()
);

const lamports = await context.rpc.getRent(space);
return transactionBuilder()
.add(
createAccount(context, {
newAccount: input.candyMachine,
newAccount: newInput.candyMachine,
lamports,
space,
programId: context.programs.get('mplCoreCandyMachineCore').publicKey,
})
)
.add(
initializeCandyMachine(context, {
...input,
candyMachine: input.candyMachine.publicKey,
...newInput,
candyMachine: newInput.candyMachine.publicKey,
})
);
};
32 changes: 27 additions & 5 deletions clients/js/test/createCandyMachineV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import {
transactionBuilder,
} from '@metaplex-foundation/umi';
import test from 'ava';
import { CandyMachine, createCandyMachine, fetchCandyMachine } from '../src';
import {
CandyMachine,
createCandyMachine,
DEFAULT_CONFIG_LINE_SETTINGS,
fetchCandyMachine,
} from '../src';
import { createCollection, createUmi, defaultCandyMachineData } from './_setup';

test('it can create a candy machine using config line settings', async (t) => {
Expand Down Expand Up @@ -109,14 +114,14 @@ test('it can create a candy machine using hidden settings', async (t) => {
});
});

test('it cannot create a candy machine without hidden or config line settings', async (t) => {
test('it can create a candy machine with defaulted config lines', async (t) => {
// Given an existing collection NFT.
const umi = await createUmi();
const collection = (await createCollection(umi)).publicKey;

// When we try to create a new candy machine without any settings.
const candyMachine = generateSigner(umi);
const promise = transactionBuilder()
await transactionBuilder()
.add(
await createCandyMachine(umi, {
...defaultCandyMachineData(umi),
Expand All @@ -128,8 +133,25 @@ test('it cannot create a candy machine without hidden or config line settings',
)
.sendAndConfirm(umi);

// Then we expect a program error.
await t.throwsAsync(promise, { message: /A raw constraint was violated/ });
const candyMachineAccount = await fetchCandyMachine(
umi,
candyMachine.publicKey
);

t.like(candyMachineAccount, <CandyMachine>{
publicKey: publicKey(candyMachine),
authority: publicKey(umi.identity),
mintAuthority: publicKey(umi.identity),
collectionMint: publicKey(collection),
itemsRedeemed: 0n,
data: {
itemsAvailable: 100n,
maxEditionSupply: 0n,
isMutable: true,
configLineSettings: some(DEFAULT_CONFIG_LINE_SETTINGS),
hiddenSettings: none(),
},
});
});

test('it can create a candy machine of Programmable NFTs', async (t) => {
Expand Down

0 comments on commit aa6cfe7

Please sign in to comment.