import fs from'fs'import bs58 from'bs58'import{ createUmi }from'@metaplex-foundation/umi-bundle-defaults'import{ keypairIdentity, publicKey }from'@metaplex-foundation/umi'import{ mplHybrid,MPL_HYBRID_PROGRAM_ID, captureV1,}from'@metaplex-foundation/mpl-hybrid'import{ mplTokenMetadata }from'@metaplex-foundation/mpl-token-metadata'import{string, publicKey as publicKeySerializer,}from'@metaplex-foundation/umi/serializers'import{ fetchAssetsByCollection }from'@metaplex-foundation/mpl-core'constRPC='<INSERT RPC>'const umi =createUmi(RPC)// THIS IS USING A LOCAL KEYPAIRconst parsed_wallet =JSON.parse(fs.readFileSync('<PATH TO KEYPAIR>','utf-8'))const kp_wallet = umi.eddsa.createKeypairFromSecretKey(newUint8Array(parsed_wallet))umi.use(keypairIdentity(kp_wallet))umi.use(mplHybrid())umi.use(mplTokenMetadata())constCOLLECTION=publicKey('<INSERT COLLECTION ACCOUNT/NFT ADDRESS>')constTOKEN=publicKey('<INSERT TOKEN ADDRESS>')// THE TOKEN TO BE DISPENSEDconstFEE_WALLET=publicKey('<INSERT FEE WALLET>')constESCROW= umi.eddsa.findPda(MPL_HYBRID_PROGRAM_ID,[string({ size:'variable'}).serialize('escrow'),publicKeySerializer().serialize(COLLECTION),])const assetsByCollection =awaitfetchAssetsByCollection(umi,COLLECTION,{ skipDerivePlugins:false,})const escrowAssets = assetsByCollection.filter((a)=> a.owner ===publicKey(ESCROW))// WITH METADATA REROLLING, ANY ESCROW ASSET IS EFFECTIVELY THE SAME. WE CAN JUST USE THE FIRST ONE// WITHOUT METADATA REROLLING A USER COULD CHOOSE TO RECEIVE A SPECIFIC ASSETconst captureData ={ owner: umi.identity, payer: umi.identity, escrow:ESCROW, asset: escrowAssets[0].publicKey, collection:COLLECTION, feeProjectAccount:FEE_WALLET, token:TOKEN,}const captureTx =awaitcaptureV1(umi, captureData).sendAndConfirm(umi)console.log(bs58.encode(captureTx.signature))
Release: Swapping to fungibles
import{... releaseV1 }from'@metaplex-foundation/mpl-hybrid'...(see above code)const ownedAssets = assetsByCollection.filter(a => a.owner === umi.identity.publicKey )// TYPICALLY A USER WOULD CHOOSE A SPECIFIC ASSET THE WOULD LIKE TO SWAP TO TOKENSconst releaseData ={ owner: umi.identity, escrow:ESCROW, asset: ownedAssets[0].publicKey, collection:COLLECTION, feeProjectAccount:FEE_WALLET, token:TOKEN}const releaseTx =awaitreleaseV1(umi, releaseData).sendAndConfirm(umi)console.log(bs58.encode(releaseTx.signature))