Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion components/SendNft.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
68 changes: 45 additions & 23 deletions utils/metaplex.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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'
Expand All @@ -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')
}
}
}
Loading