Skip to content

Commit

Permalink
wip: new core mpl ix for collections
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanphan committed Mar 7, 2024
1 parent 8189c2a commit 8427151
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 167 deletions.
21 changes: 4 additions & 17 deletions clients/js/src/defaultGuards/freezeSolPayment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
findAssociatedTokenPda,
getSplSystemProgramId,
} from '@metaplex-foundation/mpl-toolbox';
import { PublicKey, Signer } from '@metaplex-foundation/umi';
Expand Down Expand Up @@ -43,19 +42,10 @@ export const freezeSolPaymentGuardManifest: GuardManifest<
candyGuard: mintContext.candyGuard,
});

// TODO actually freeze asset
const [nftAta] = findAssociatedTokenPda(context, {
mint: mintContext.asset,
owner: mintContext.minter.publicKey,
});
return {
data: new Uint8Array(),
remainingAccounts: [
{ publicKey: freezeEscrow, isWritable: true },
{ publicKey: nftAta, isWritable: false },
...(args.nftRuleSet
? [{ publicKey: args.nftRuleSet, isWritable: false }]
: []),
],
};
},
Expand All @@ -81,8 +71,6 @@ export type FreezeSolPaymentMintArgs = Omit<
FreezeSolPaymentArgs,
'lamports'
> & {
/** The ruleSet of the minted NFT, if any. */
nftRuleSet?: PublicKey;
};

/**
Expand Down Expand Up @@ -143,9 +131,8 @@ export type FreezeSolPaymentRouteArgsInitialize = Omit<
* routeArgs: {
* path: 'thaw',
* destination,
* nftMint,
* nftOwner,
* nftTokenStandard: candyMachine.tokenStandard,
* asset,
* collection,
* },
* });
* ```
Expand All @@ -161,7 +148,7 @@ export type FreezeSolPaymentRouteArgsThaw = Omit<
asset: PublicKey;

/** The owner address of the NFT to thaw. */
owner: PublicKey;
collection: PublicKey;
};

/**
Expand Down Expand Up @@ -227,7 +214,7 @@ const thawRouteInstruction: RouteParser<FreezeSolPaymentRouteArgsThaw> = (
const remainingAccounts: GuardRemainingAccount[] = [
{ publicKey: freezeEscrow, isWritable: true },
{ publicKey: args.asset, isWritable: true },
{ publicKey: args.owner, isWritable: false },
{ publicKey: args.collection, isWritable: false },
{ publicKey: getMplCoreProgramId(context), isWritable: false },
{ publicKey: getSplSystemProgramId(context), isWritable: false },
];
Expand Down
20 changes: 4 additions & 16 deletions clients/js/src/defaultGuards/freezeTokenPayment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ export const freezeTokenPaymentGuardManifest: GuardManifest<
candyGuard: mintContext.candyGuard,
});

// TODO actually freeze asset
const [nftAta] = findAssociatedTokenPda(context, {
mint: mintContext.asset,
owner: mintContext.minter.publicKey,
});
const [tokenAddress] = findAssociatedTokenPda(context, {
mint: args.mint,
owner: mintContext.minter.publicKey,
Expand All @@ -62,12 +57,8 @@ export const freezeTokenPaymentGuardManifest: GuardManifest<
data: new Uint8Array(),
remainingAccounts: [
{ publicKey: freezeEscrow, isWritable: true },
{ publicKey: nftAta, isWritable: false },
{ publicKey: tokenAddress, isWritable: true },
{ publicKey: freezeAta, isWritable: true },
...(args.nftRuleSet
? [{ publicKey: args.nftRuleSet, isWritable: false }]
: []),
],
};
},
Expand All @@ -93,8 +84,6 @@ export type FreezeTokenPaymentMintArgs = Omit<
FreezeTokenPaymentArgs,
'amount'
> & {
/** The ruleSet of the minted NFT, if any. */
nftRuleSet?: PublicKey;
};

/**
Expand Down Expand Up @@ -157,9 +146,8 @@ export type FreezeTokenPaymentRouteArgsInitialize = Omit<
* path: 'thaw',
* mint: tokenMint.publicKey,
* destinationAta,
* nftMint,
* nftOwner,
* nftTokenStandard: candyMachine.tokenStandard,
* asset,
* collection,
* },
* });
* ```
Expand All @@ -175,7 +163,7 @@ export type FreezeTokenPaymentRouteArgsThaw = Omit<
asset: PublicKey;

/** The owner address of the NFT to thaw. */
owner: PublicKey;
collection: PublicKey;
};

/**
Expand Down Expand Up @@ -250,7 +238,7 @@ const thawRouteInstruction: RouteParser<FreezeTokenPaymentRouteArgsThaw> = (
const remainingAccounts: GuardRemainingAccount[] = [
{ publicKey: freezeEscrow, isWritable: true },
{ publicKey: args.asset, isWritable: true },
{ publicKey: args.owner, isWritable: false },
{ publicKey: args.collection, isWritable: false },
{ publicKey: getMplCoreProgramId(context), isWritable: false },
{ publicKey: getSplSystemProgramId(context), isWritable: false },
];
Expand Down
13 changes: 13 additions & 0 deletions clients/js/src/generated/errors/mplCandyMachineCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,19 @@ export class CmInvalidAccountVersionError extends ProgramError {
codeToErrorMap.set(0x178d, CmInvalidAccountVersionError);
nameToErrorMap.set('InvalidAccountVersion', CmInvalidAccountVersionError);

/** IncorrectPluginAuthority: Invalid plugin authority */
export class CmIncorrectPluginAuthorityError extends ProgramError {
readonly name: string = 'IncorrectPluginAuthority';

readonly code: number = 0x178e; // 6030

constructor(program: Program, cause?: Error) {
super('Invalid plugin authority', program, cause);
}
}
codeToErrorMap.set(0x178e, CmIncorrectPluginAuthorityError);
nameToErrorMap.set('IncorrectPluginAuthority', CmIncorrectPluginAuthorityError);

/**
* Attempts to resolve a custom program error from the provided error code.
* @category Errors
Expand Down
2 changes: 1 addition & 1 deletion clients/js/test/_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const createCollection = async (
): Promise<Signer> => {
const mint = generateSigner(umi);
await baseCreateCollection(umi, {
collectionAddress: mint,
collection: mint,
...defaultAssetData(),
...input,
}).sendAndConfirm(umi);
Expand Down
24 changes: 12 additions & 12 deletions clients/js/test/defaultGuards/freezeSolPayment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ test('it transfers SOL to an escrow account and freezes the NFT', async (t) => {
t.is(isFrozen(asset), true, 'NFT is frozen');

// And cannot be thawed since not all NFTs have been minted.
const promise = thawNft(umi, candyMachine, destination, mint.publicKey);
const promise = thawNft(umi, candyMachine, destination, mint.publicKey, collection);
await t.throwsAsync(promise, { message: /ThawNotEnabled/ });

// And the treasury escrow received SOLs.
Expand Down Expand Up @@ -236,7 +236,7 @@ test('it can thaw an NFT once all NFTs are minted', async (t) => {
t.is(isFrozen(asset), true, 'NFT is frozen');

// When we thaw the NFT.
await thawNft(umi, candyMachine, destination, mint.publicKey);
await thawNft(umi, candyMachine, destination, mint.publicKey, collection);

// Then the NFT is thawed.
asset = await fetchAssetWithPlugins(umi, mint.publicKey);
Expand All @@ -259,7 +259,7 @@ test('it can unlock funds once all NFTs have been thawed', async (t) => {

// And given all NFTs have been minted and thawed.
const mint = await mintNft(umi, candyMachine, destination, collection);
await thawNft(umi, candyMachine, destination, mint.publicKey);
await thawNft(umi, candyMachine, destination, mint.publicKey, collection);

// When the authority unlocks the funds.
await transactionBuilder()
Expand Down Expand Up @@ -472,15 +472,15 @@ test('it can have multiple freeze escrow and reuse the same ones', async (t) =>
]);
};
await assertFrozenCounts(2, 1);
await thawNft(umi, cm, destinationAB, mintD.publicKey, 'GROUPA'); // Not frozen.
await thawNft(umi, cm, destinationAB, mintD.publicKey, collection, 'GROUPA'); // Not frozen.
await assertFrozenCounts(2, 1); // No change.
await thawNft(umi, cm, destinationAB, mintA.publicKey, 'GROUPA');
await thawNft(umi, cm, destinationAB, mintA.publicKey, collection, 'GROUPA');
await assertFrozenCounts(1, 1); // AB decreased.
await thawNft(umi, cm, destinationAB, mintA.publicKey, 'GROUPA'); // Already thawed.
await thawNft(umi, cm, destinationAB, mintA.publicKey, collection, 'GROUPA'); // Already thawed.
await assertFrozenCounts(1, 1); // No change.
await thawNft(umi, cm, destinationAB, mintB.publicKey, 'GROUPB');
await thawNft(umi, cm, destinationAB, mintB.publicKey, collection, 'GROUPB');
await assertFrozenCounts(0, 1); // AB decreased.
await thawNft(umi, cm, destinationC, mintC.publicKey, 'GROUPC');
await thawNft(umi, cm, destinationC, mintC.publicKey, collection, 'GROUPC');
await assertFrozenCounts(0, 0); // C decreased.

// And when the authority unlocks the funds of both freeze escrows.
Expand Down Expand Up @@ -683,7 +683,7 @@ test('it transfers SOL to an escrow account and locks the Programmable NFT', asy
t.is(isFrozen(asset), true);

// And cannot be thawed since not all NFTs have been minted.
const promise = thawNft(umi, candyMachine, destination, mint.publicKey);
const promise = thawNft(umi, candyMachine, destination, mint.publicKey, collection);
await t.throwsAsync(promise, { message: /ThawNotEnabled/ });

// And the treasury escrow received SOLs.
Expand Down Expand Up @@ -765,7 +765,7 @@ test('it can thaw a Programmable NFT once all NFTs are minted', async (t) => {
routeArgs: {
path: 'thaw',
asset: mint.publicKey,
owner: umi.identity.publicKey,
collection,
destination,
},
})
Expand Down Expand Up @@ -874,8 +874,8 @@ const thawNft = async (
candyMachine: PublicKey,
destination: PublicKey,
asset: PublicKey,
collection: PublicKey,
group?: string,
nftOwner?: PublicKey
) => {
await route(umi, {
candyMachine,
Expand All @@ -884,7 +884,7 @@ const thawNft = async (
routeArgs: {
path: 'thaw',
asset,
owner: nftOwner ?? umi.identity.publicKey,
collection,
destination,
},
}).sendAndConfirm(umi);
Expand Down
25 changes: 13 additions & 12 deletions clients/js/test/defaultGuards/freezeTokenPayment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ test('it transfers tokens to an escrow account and freezes the NFT', async (t) =

// And cannot be thawed since not all NFTs have been minted.
const cm = candyMachine;
const promise = thawNft(umi, cm, tokenMint, destinationAta, mint.publicKey);
const promise = thawNft(umi, cm, tokenMint, destinationAta, mint.publicKey, collection);
await t.throwsAsync(promise, { message: /ThawNotEnabled/ });

// And the treasury escrow received tokens.
Expand Down Expand Up @@ -234,7 +234,7 @@ test('it can thaw an NFT once all NFTs are minted', async (t) => {
t.is(isFrozen(asset), true);

// When we thaw the NFT.
await thawNft(umi, candyMachine, tokenMint, destinationAta, mint.publicKey);
await thawNft(umi, candyMachine, tokenMint, destinationAta, mint.publicKey, collection);

// Then the NFT is thawed.
asset = await fetchAssetWithPlugins(umi, mint.publicKey);
Expand Down Expand Up @@ -275,7 +275,7 @@ test('it can unlock funds once all NFTs have been thawed', async (t) => {
destinationAta,
collection
);
await thawNft(umi, candyMachine, tokenMint, destinationAta, mint.publicKey);
await thawNft(umi, candyMachine, tokenMint, destinationAta, mint.publicKey, collection);

// When the authority unlocks the funds.
await transactionBuilder()
Expand Down Expand Up @@ -531,15 +531,15 @@ test('it can have multiple freeze escrow and reuse the same ones', async (t) =>
]);
};
await assertFrozenCounts(2, 1);
await thawNft(umi, cm, mintAB, destinationAtaAB, nftD.publicKey, 'GROUPA'); // Not frozen.
await thawNft(umi, cm, mintAB, destinationAtaAB, nftD.publicKey, collection, 'GROUPA'); // Not frozen.
await assertFrozenCounts(2, 1); // No change.
await thawNft(umi, cm, mintAB, destinationAtaAB, nftA.publicKey, 'GROUPA');
await thawNft(umi, cm, mintAB, destinationAtaAB, nftA.publicKey, collection, 'GROUPA');
await assertFrozenCounts(1, 1); // AB decreased.
await thawNft(umi, cm, mintAB, destinationAtaAB, nftA.publicKey, 'GROUPA'); // Already thawed.
await thawNft(umi, cm, mintAB, destinationAtaAB, nftA.publicKey, collection, 'GROUPA'); // Already thawed.
await assertFrozenCounts(1, 1); // No change.
await thawNft(umi, cm, mintAB, destinationAtaAB, nftB.publicKey, 'GROUPB');
await thawNft(umi, cm, mintAB, destinationAtaAB, nftB.publicKey, collection, 'GROUPB');
await assertFrozenCounts(0, 1); // AB decreased.
await thawNft(umi, cm, mintC, destinationAtaC, nftC.publicKey, 'GROUPC');
await thawNft(umi, cm, mintC, destinationAtaC, nftC.publicKey, collection, 'GROUPC');
await assertFrozenCounts(0, 0); // C decreased.

// And when the authority unlocks the funds of both freeze escrows.
Expand Down Expand Up @@ -809,7 +809,8 @@ test('it transfers tokens to an escrow account and locks the Programmable NFT',
candyMachine,
tokenMint,
destinationAta,
mint.publicKey
mint.publicKey,
collection
);
await t.throwsAsync(promise, { message: /ThawNotEnabled/ });

Expand Down Expand Up @@ -896,7 +897,7 @@ test('it can thaw a Programmable NFT once all NFTs are minted', async (t) => {
routeArgs: {
path: 'thaw',
asset: mint.publicKey,
owner: umi.identity.publicKey,
collection,
mint: tokenMint.publicKey,
destinationAta,
},
Expand Down Expand Up @@ -1029,8 +1030,8 @@ const thawNft = async (
tokenMint: PublicKey | Signer,
destinationAta: PublicKey,
asset: PublicKey,
collection: PublicKey,
group?: string,
nftOwner?: PublicKey
) => {
await route(umi, {
candyMachine,
Expand All @@ -1039,7 +1040,7 @@ const thawNft = async (
routeArgs: {
path: 'thaw',
asset,
owner: nftOwner ?? umi.identity.publicKey,
collection,
mint: publicKey(tokenMint),
destinationAta,
},
Expand Down
1 change: 1 addition & 0 deletions clients/js/test/setCollectionV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test('it can update the collection of a candy machine v2', async (t) => {
const collectionB = await createCollection(umi, {
updateAuthority: collectionUpdateAuthorityB.publicKey,
});

await setCollectionV2(umi, {
candyMachine: candyMachine.publicKey,
collection: collectionA.publicKey,
Expand Down
3 changes: 2 additions & 1 deletion configs/program-scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
OUTPUT="./programs/.bin"
# saves external programs binaries to the output directory
source ${SCRIPT_DIR}/dump.sh ${OUTPUT}
# TODO remove this
# source ${SCRIPT_DIR}/dump.sh ${OUTPUT}

# FIXME TODO Remove this
cp ~/src/mpl-core/programs/.bin/mpl_core_program.so ${OUTPUT}/mpl_core_program.so
Expand Down
5 changes: 5 additions & 0 deletions idls/candy_machine_core.json
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,11 @@
"code": 6029,
"name": "InvalidAccountVersion",
"msg": "Invalid account version"
},
{
"code": 6030,
"name": "IncorrectPluginAuthority",
"msg": "Invalid plugin authority"
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 8427151

Please sign in to comment.