Skip to content

Commit

Permalink
Adding update via candy machine and candy guard.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Dec 17, 2024
1 parent 1cfbe37 commit 96e062f
Show file tree
Hide file tree
Showing 14 changed files with 1,405 additions and 11 deletions.
2 changes: 1 addition & 1 deletion clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@
}
},
"packageManager": "[email protected]"
}
}
5 changes: 5 additions & 0 deletions clients/js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

284 changes: 284 additions & 0 deletions clients/js/src/generated/instructions/updateAssetV1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/metaplex-foundation/kinobi
*/

import {
Context,
Option,
OptionOrNullable,
Pda,
PublicKey,
Signer,
TransactionBuilder,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Serializer,
array,
bytes,
mapSerializer,
option,
string,
struct,
u32,
u8,
} from '@metaplex-foundation/umi/serializers';
import { findCandyGuardPda, findCandyMachineAuthorityPda } from '../../hooked';
import {
ResolvedAccount,
ResolvedAccountsWithIndices,
expectPublicKey,
getAccountMetasAndSigners,
} from '../shared';

// Accounts.
export type UpdateAssetV1InstructionAccounts = {
/** Candy Guard account. */
candyGuard?: PublicKey | Pda;
/**
* Candy Machine program account.
*
*/

candyMachineProgram?: PublicKey | Pda;
/** Candy machine account. */
candyMachine: PublicKey | Pda;
/**
* Candy Machine authority account.
*
*/

candyMachineAuthorityPda?: PublicKey | Pda;
/** Payer for the mint (SOL) fees. */
payer?: Signer;
/** Minter account for validation and non-SOL fees. */
minter?: Signer;
/** Optionally mint to different owner */
owner?: PublicKey | Pda;
/**
* Mint account of the NFT. The account will be initialized if necessary.
*
* Must be a signer if:
* * the nft_mint account does not exist.
*
*/

asset: PublicKey | Pda | Signer;
/**
* Mint account of the collection NFT.
*
*/

collection: PublicKey | Pda;
/**
* Token Metadata program.
*
* Token Metadata program.
*
*/

mplCoreProgram?: PublicKey | Pda;
/**
* SPL Token program.
* System program.
*/

systemProgram?: PublicKey | Pda;
/**
* Instructions sysvar account.
*
*/

sysvarInstructions?: PublicKey | Pda;
/**
* SlotHashes sysvar cluster data.
*
*/

recentSlothashes?: PublicKey | Pda;
};

// Data.
export type UpdateAssetV1InstructionData = {
discriminator: Array<number>;
updateArgs: Uint8Array;
group: Option<string>;
};

export type UpdateAssetV1InstructionDataArgs = {
updateArgs: Uint8Array;
group: OptionOrNullable<string>;
};

export function getUpdateAssetV1InstructionDataSerializer(): Serializer<
UpdateAssetV1InstructionDataArgs,
UpdateAssetV1InstructionData
> {
return mapSerializer<
UpdateAssetV1InstructionDataArgs,
any,
UpdateAssetV1InstructionData
>(
struct<UpdateAssetV1InstructionData>(
[
['discriminator', array(u8(), { size: 8 })],
['updateArgs', bytes({ size: u32() })],
['group', option(string())],
],
{ description: 'UpdateAssetV1InstructionData' }
),
(value) => ({
...value,
discriminator: [45, 97, 159, 66, 93, 39, 167, 157],
})
) as Serializer<
UpdateAssetV1InstructionDataArgs,
UpdateAssetV1InstructionData
>;
}

// Args.
export type UpdateAssetV1InstructionArgs = UpdateAssetV1InstructionDataArgs;

// Instruction.
export function updateAssetV1(
context: Pick<Context, 'eddsa' | 'identity' | 'payer' | 'programs'>,
input: UpdateAssetV1InstructionAccounts & UpdateAssetV1InstructionArgs
): TransactionBuilder {
// Program ID.
const programId = context.programs.getPublicKey(
'mplCoreCandyGuard',
'CMAGAKJ67e9hRZgfC5SFTbZH8MgEmtqazKXjmkaJjWTJ'
);

// Accounts.
const resolvedAccounts: ResolvedAccountsWithIndices = {
candyGuard: {
index: 0,
isWritable: false,
value: input.candyGuard ?? null,
},
candyMachineProgram: {
index: 1,
isWritable: false,
value: input.candyMachineProgram ?? null,
},
candyMachine: {
index: 2,
isWritable: true,
value: input.candyMachine ?? null,
},
candyMachineAuthorityPda: {
index: 3,
isWritable: true,
value: input.candyMachineAuthorityPda ?? null,
},
payer: { index: 4, isWritable: true, value: input.payer ?? null },
minter: { index: 5, isWritable: true, value: input.minter ?? null },
owner: { index: 6, isWritable: false, value: input.owner ?? null },
asset: { index: 7, isWritable: true, value: input.asset ?? null },
collection: { index: 8, isWritable: true, value: input.collection ?? null },
mplCoreProgram: {
index: 9,
isWritable: false,
value: input.mplCoreProgram ?? null,
},
systemProgram: {
index: 10,
isWritable: false,
value: input.systemProgram ?? null,
},
sysvarInstructions: {
index: 11,
isWritable: false,
value: input.sysvarInstructions ?? null,
},
recentSlothashes: {
index: 12,
isWritable: false,
value: input.recentSlothashes ?? null,
},
};

// Arguments.
const resolvedArgs: UpdateAssetV1InstructionArgs = { ...input };

// Default values.
if (!resolvedAccounts.candyGuard.value) {
resolvedAccounts.candyGuard.value = findCandyGuardPda(context, {
base: expectPublicKey(resolvedAccounts.candyMachine.value),
});
}
if (!resolvedAccounts.candyMachineProgram.value) {
resolvedAccounts.candyMachineProgram.value = context.programs.getPublicKey(
'mplCandyMachine',
'CMACYFENjoBMHzapRXyo1JZkVS6EtaDDzkjMrmQLvr4J'
);
resolvedAccounts.candyMachineProgram.isWritable = false;
}
if (!resolvedAccounts.candyMachineAuthorityPda.value) {
resolvedAccounts.candyMachineAuthorityPda.value =
findCandyMachineAuthorityPda(context, {
candyMachine: expectPublicKey(resolvedAccounts.candyMachine.value),
});
}
if (!resolvedAccounts.payer.value) {
resolvedAccounts.payer.value = context.payer;
}
if (!resolvedAccounts.minter.value) {
resolvedAccounts.minter.value = context.identity;
}
if (!resolvedAccounts.mplCoreProgram.value) {
resolvedAccounts.mplCoreProgram.value = context.programs.getPublicKey(
'mplCore',
'CoREENxT6tW1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d'
);
resolvedAccounts.mplCoreProgram.isWritable = false;
}
if (!resolvedAccounts.systemProgram.value) {
resolvedAccounts.systemProgram.value = context.programs.getPublicKey(
'splSystem',
'11111111111111111111111111111111'
);
resolvedAccounts.systemProgram.isWritable = false;
}
if (!resolvedAccounts.sysvarInstructions.value) {
resolvedAccounts.sysvarInstructions.value = publicKey(
'Sysvar1nstructions1111111111111111111111111'
);
}
if (!resolvedAccounts.recentSlothashes.value) {
resolvedAccounts.recentSlothashes.value = publicKey(
'SysvarS1otHashes111111111111111111111111111'
);
}

// Accounts in order.
const orderedAccounts: ResolvedAccount[] = Object.values(
resolvedAccounts
).sort((a, b) => a.index - b.index);

// Keys and Signers.
const [keys, signers] = getAccountMetasAndSigners(
orderedAccounts,
'programId',
programId
);

// Data.
const data = getUpdateAssetV1InstructionDataSerializer().serialize(
resolvedArgs as UpdateAssetV1InstructionDataArgs
);

// Bytes Created On Chain.
const bytesCreatedOnChain = 0;

return transactionBuilder([
{ instruction: { keys, programId, data }, signers, bytesCreatedOnChain },
]);
}
1 change: 1 addition & 0 deletions clients/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export * from './merkle';
export * from './mintV1';
export * from './plugin';
export * from './route';
export * from './updateAssetV1';
export * from './updateCandyGuard';
76 changes: 76 additions & 0 deletions clients/js/src/updateAssetV1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {
Option,
OptionOrNullable,
TransactionBuilder,
none,
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi';
import { DefaultGuardSetMintArgs } from './defaultGuards';
import {
UpdateAssetV1InstructionAccounts,
updateAssetV1 as baseUpdateAssetV1,
} from './generated/instructions/updateAssetV1';
import {
CandyGuardProgram,
GuardRepository,
GuardSetMintArgs,
MintContext,
parseGuardRemainingAccounts,
parseMintArgs,
} from './guards';
import { findCandyGuardPda } from './hooked';

export { UpdateAssetV1InstructionAccounts };

export type UpdateAssetV2InstructionData<MA extends GuardSetMintArgs> = {
discriminator: Array<number>;
updateArgs: MA;
group: Option<string>;
};

export type UpdateAssetV2InstructionDataArgs<MA extends GuardSetMintArgs> = {
updateArgs?: Partial<MA>;
group?: OptionOrNullable<string>;
};

export function updateAssetV1<MA extends GuardSetMintArgs = DefaultGuardSetMintArgs>(
context: Parameters<typeof baseUpdateAssetV1>[0] & {
coreGuards: GuardRepository;
},
input: UpdateAssetV1InstructionAccounts &
UpdateAssetV2InstructionDataArgs<
MA extends undefined ? DefaultGuardSetMintArgs : MA
>
): TransactionBuilder {
const { updateArgs = {}, group = none(), ...rest } = input;

// Parsing mint data.
const program = context.programs.get<CandyGuardProgram>('mplCoreCandyGuard');
const candyMachine = publicKey(input.candyMachine, false);
const mintContext: MintContext = {
minter: input.minter ?? context.identity,
payer: input.payer ?? context.payer,
asset: publicKey(input.asset, false),
candyMachine,
candyGuard: publicKey(
input.candyGuard ?? findCandyGuardPda(context, { base: candyMachine }),
false
),
};
const { data, remainingAccounts } = parseMintArgs<
MA extends undefined ? DefaultGuardSetMintArgs : MA
>(context, program, mintContext, updateArgs);

const ix = baseUpdateAssetV1(context, {
...rest,
updateArgs: data,
group,
}).items[0];

const [keys, signers] = parseGuardRemainingAccounts(remainingAccounts);
ix.instruction.keys.push(...keys);
ix.signers.push(...signers);

return transactionBuilder([ix]);
}
Loading

0 comments on commit 96e062f

Please sign in to comment.