diff --git a/components/SendNft.tsx b/components/SendNft.tsx index eaea6b4c6..3382d9067 100644 --- a/components/SendNft.tsx +++ b/components/SendNft.tsx @@ -151,11 +151,14 @@ const SendNft = ({ connection, fromOwner, toOwner, - nftMint, + nft, fromOwner, nativeTreasury, ) + if (!transferIx) + throw new Error('failed to create transfer instruction') + return { serializedInstruction: serializeInstructionToBase64(transferIx), isValid: true, diff --git a/utils/metaplex.ts b/utils/metaplex.ts index c77258b02..ba98deb75 100644 --- a/utils/metaplex.ts +++ b/utils/metaplex.ts @@ -1,15 +1,15 @@ +import { DasNftObject } from '@hooks/queries/digitalAssets' import { fetchNFTbyMint } from '@hooks/queries/nft' import { Metaplex } from '@metaplex-foundation/js' -import { TokenStandard } from '@metaplex-foundation/mpl-token-metadata' -import { Connection, PublicKey } from '@solana/web3.js' +import { Connection, PublicKey, TransactionInstruction } from '@solana/web3.js' export const createIx_transferNft = async ( connection: Connection, fromOwner: PublicKey, toOwner: PublicKey, - mint: PublicKey, + nft: DasNftObject, authority: PublicKey, - payer: PublicKey, + payer: PublicKey ) => { const metaplex = new Metaplex( connection, @@ -22,6 +22,8 @@ export const createIx_transferNft = async ( //metaplex.identity = () => ({ publicKey: fromOwner } as any) // you need to do this to set payer and authority. I love OOP!! // except the payer might not be the same person. great! + const mint = new PublicKey(nft.id); + const MPL_CORE_PROGRAM_ID = new PublicKey('CoREENxT6tW1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d'); try { const nft = await fetchNFTbyMint(connection, mint) if (!nft.result) throw 'failed to fetch nft' @@ -46,24 +48,44 @@ export const createIx_transferNft = async ( ix.keys[9].pubkey = authority ix.keys[10].pubkey = payer return ix - } catch { - const ix = metaplex - .nfts() - .builders() - .transfer({ - nftOrSft: { - address: mint, - tokenStandard: TokenStandard.NonFungible, - }, - authorizationDetails: undefined, - toOwner, - fromOwner, - }) - .getInstructions()[0] - - ix.keys[9].pubkey = authority - ix.keys[10].pubkey = payer - - return ix + } catch (error) { + if (nft.interface === 'MplCoreAsset') { + // fallback for mpl core assets + const ix: TransactionInstruction = { + keys: [ + { pubkey: mint, isSigner: false, isWritable: true }, + { + pubkey: new PublicKey(nft.grouping.find((g) => g.group_key === 'collection')!.group_value), + isSigner: false, + isWritable: false, + }, + { pubkey: fromOwner, + isSigner: true, + isWritable: true, + }, + { + pubkey: MPL_CORE_PROGRAM_ID, + isSigner: false, + isWritable: false, + }, + { pubkey: toOwner, isSigner: false, isWritable: false }, + { + pubkey: MPL_CORE_PROGRAM_ID, + isSigner: false, + isWritable: false, + }, + { + pubkey: MPL_CORE_PROGRAM_ID, + isSigner: false, + isWritable: false, + }, + ], + programId: MPL_CORE_PROGRAM_ID, + data: Buffer.from([14,0]) + } + return ix; + } else { + throw new Error('failed to create transfer instruction') + } } }