[Metaplex Candy Machine] #2. Candy Machine 관리하기
목차
[Metaplex Candy Machine] #2. Candy Machine 관리하기 이 글은 Metaplex Candy Machine 시리즈의 2번째 글입니다. 이전 포스팅에서 Candy Machine의 설정 값들에 대해 알아봤습니다. 이번 글에서는 이 설정 값들을 이용해 Candy Machine을 생성, 수정, 삭제하고 기존 Candy Machine을 조회하는 기능들에 대해 알아보겠습니다. Metaplex SDK는 새롭게 생성되는 Candy Machine의 Account를 Candy Guard의 Account와 연결시킵니다. 여기서는 Candy Machine에 대해서만 살펴볼 것이지만, Candy Machine은 항상 Collection NFT와 연관되어야 합니다. 아래는 새로운 Collection NFT와 Candy Machine을 만드는 예제 코드입니다. 위에서 언급했듯이 새로운 Candy Machine은 새로운 Candy Guard Account와 연결됩니다. 다만, Candy Guard를 제외하고 싶다면 아래처럼 withoutCandyGuard 속성을 사용해 주세요. 만들어진 Candy Machine Account는 아래와 같은 데이터를 갖고 있습니다. 아래는 Candy Machine 객체가 갖고 있는 코드 예시입니다. Candy Machine의 Address를 통해 이미 존재하는 Candy Machine을 조회할 수 있습니다. Candy Machine의 소유자는 Candy Machine의 Authority와 Mint Authority를 업데이트할 수 있습니다. Authority를 업데이트하기 위해서는 서명자로 현재 Authority 값과 새로운 Authority 값이 필요합니다. Mint Authority를 업데이트하기 위해서는 현재 Authority와 새로운 mint Authority를 서명자로 보내야 합니다. Create와 다르게 Update는 Candy Machine 객체를 반환하지 않기 때문에 업데이트된 객체가 필요하다면 refresh 해야 합니다. 아래는 mint Authority를 업데이트하는 코드 예시입니다. 마찬가지로 이전 포스팅에서 이야기했던 공통 속성들도 업데이트할 수 있습니다. 다만, 첫 NFT가 한 번 발행되고 나면, 이 속성들은 업데이트할 수 없습니다. Collection NFT 또한 업데이트 할 수 있는데, Collection NFT의 mint Account와 updateAuthority를 서명자로 제공해야 합니다. 이 역시 한 번 NFT가 발행되고 나면 바꿀 수 없습니다. Item 설정 또한 바꿀 수 있지만 몇 가지 제약 사항들이 있습니다. Candy Machine에서 모든 NFT가 발행되고 나면 안전하게 삭제할 수 있습니다. 삭제하면 Candy Machine Account의 Storage 비용을 돌려받을 수 있습니다. 위 코드는 Candy Machine Account 만 삭제할 뿐 연관된 Candy Guard Account는 삭제하지 않습니다. 둘 다 삭제하기 위해서는 아래 코드처럼 Candy Guard 주소 또한 필요합니다. 전체 목차:Metaplex Candy 관리하기
AutCreate Candy Machine
import { toBigNumber } from '@metaplex-foundation/js';
import { Keypair } from '@solana/web3.js';
// Create the Collection NFT.
const { nft: collectionNft } = await metaplex.nfts().create({
name: 'My Collection NFT',
uri: 'https://example.com/path/to/some/json/metadata.json',
sellerFeeBasisPoints: 0,
isCollection: true,
});
// Create the Candy Machine.
const { candyMachine } = await metaplex.candyMachines().create({
itemsAvailable: toBigNumber(5000),
sellerFeeBasisPoints: 333, // 3.33%
collection: {
address: collectionNft.address,
updateAuthority: metaplex.identity(),
},
});const { candyMachine } = await metaplex.candyMachines().create({
withoutCandyGuard: true,
// ...
});Candy Machine Account
const { candyMachine } = await metaplex.candyMachines().create({...});
candyMachine.address; // The public key of the Candy Machine account.
candyMachine.itemsAvailable; // Number of NFTs available.
candyMachine.itemsMinted; // Number of NFTs minted.
candyMachine.itemsRemaining; // Number of NFTs left to mint.
candyMachine.items[0].index; // The index of the first loaded item.
candyMachine.items[0].name; // The name of the first loaded item (with prefix).
candyMachine.items[0].uri; // The URI of the first loaded item (with prefix).
candyMachine.items[0].minted; // Whether the first item has been minted.Fetch Candy Mainche
import { PublicKey } from '@solana/web3.js';
const candyMachine = await metaplex
.candyMachines()
.findByAddress({ address: new PublicKey("Gjwc...thJS") });Update Authorities
import { Keypair } from '@solana/web3.js';
const currentAuthority = Keypair.generate();
const newAuthority = Keypair.generate();
const candyMachine = await metaplex
.candyMachines()
.findByAddress({...});
await metaplex.candyMachines().update({
candyMachine,
authority: currentAuthority,
newAuthority: newAuthority.address,
});
const updatedCandyMachine = await metaplex
.candyMachines()
.refresh(candyMachine);import { Keypair } from '@solana/web3.js';
const currentAuthority = Keypair.generate();
const newMintAuthority = Keypair.generate();
await metaplex.candyMachines().update({
candyMachine,
authority: currentAuthority,
newMintAuthority: newMintAuthority, // Notice this must be a Signer.
});Update Shared NFT Data
await metaplex.candyMachines().update({
candyMachine,
symbol: 'NEW',
sellerFeeBasisPoints: 100,
creators: [{ address: newCreator, share: 100 }],
});Update Collection
await metaplex.candyMachines().update({
candyMachine,
collection: {
address: newCollection.address,
updateAuthority: newCollectionAuthority,
},
});Update Item Settings
await metaplex.candyMachines().update({
candyMachine,
itemSettings: {
type: 'configLines',
prefixName: 'My New NFT #$ID+1$',
nameLength: 0,
prefixUri: 'https://arweave.net/',
uriLength: 43,
isSequential: true,
},
});Delete Candy Machine
await metaplex.candyMachines().delete({
candyMachine: candyMachine.address,
});await metaplex.candyMachines().delete({
candyMachine: candyMachine.address,
candyGuard: candyMachine.candyGuard.address,
});References