Skip to content

Commit 3f5b83b

Browse files
authored
add mpl core transfer support (#235)
1 parent 69cf217 commit 3f5b83b

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

components/SendNft.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,14 @@ const SendNft = ({
151151
connection,
152152
fromOwner,
153153
toOwner,
154-
nftMint,
154+
nft,
155155
fromOwner,
156156
nativeTreasury,
157157
)
158158

159+
if (!transferIx)
160+
throw new Error('failed to create transfer instruction')
161+
159162
return {
160163
serializedInstruction: serializeInstructionToBase64(transferIx),
161164
isValid: true,

utils/metaplex.ts

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
import { DasNftObject } from '@hooks/queries/digitalAssets'
12
import { fetchNFTbyMint } from '@hooks/queries/nft'
23
import { Metaplex } from '@metaplex-foundation/js'
3-
import { TokenStandard } from '@metaplex-foundation/mpl-token-metadata'
4-
import { Connection, PublicKey } from '@solana/web3.js'
4+
import { Connection, PublicKey, TransactionInstruction } from '@solana/web3.js'
55

66
export const createIx_transferNft = async (
77
connection: Connection,
88
fromOwner: PublicKey,
99
toOwner: PublicKey,
10-
mint: PublicKey,
10+
nft: DasNftObject,
1111
authority: PublicKey,
12-
payer: PublicKey,
12+
payer: PublicKey
1313
) => {
1414
const metaplex = new Metaplex(
1515
connection,
@@ -22,6 +22,8 @@ export const createIx_transferNft = async (
2222
//metaplex.identity = () => ({ publicKey: fromOwner } as any) // you need to do this to set payer and authority. I love OOP!!
2323
// except the payer might not be the same person. great!
2424

25+
const mint = new PublicKey(nft.id);
26+
const MPL_CORE_PROGRAM_ID = new PublicKey('CoREENxT6tW1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d');
2527
try {
2628
const nft = await fetchNFTbyMint(connection, mint)
2729
if (!nft.result) throw 'failed to fetch nft'
@@ -46,24 +48,44 @@ export const createIx_transferNft = async (
4648
ix.keys[9].pubkey = authority
4749
ix.keys[10].pubkey = payer
4850
return ix
49-
} catch {
50-
const ix = metaplex
51-
.nfts()
52-
.builders()
53-
.transfer({
54-
nftOrSft: {
55-
address: mint,
56-
tokenStandard: TokenStandard.NonFungible,
57-
},
58-
authorizationDetails: undefined,
59-
toOwner,
60-
fromOwner,
61-
})
62-
.getInstructions()[0]
63-
64-
ix.keys[9].pubkey = authority
65-
ix.keys[10].pubkey = payer
66-
67-
return ix
51+
} catch (error) {
52+
if (nft.interface === 'MplCoreAsset') {
53+
// fallback for mpl core assets
54+
const ix: TransactionInstruction = {
55+
keys: [
56+
{ pubkey: mint, isSigner: false, isWritable: true },
57+
{
58+
pubkey: new PublicKey(nft.grouping.find((g) => g.group_key === 'collection')!.group_value),
59+
isSigner: false,
60+
isWritable: false,
61+
},
62+
{ pubkey: fromOwner,
63+
isSigner: true,
64+
isWritable: true,
65+
},
66+
{
67+
pubkey: MPL_CORE_PROGRAM_ID,
68+
isSigner: false,
69+
isWritable: false,
70+
},
71+
{ pubkey: toOwner, isSigner: false, isWritable: false },
72+
{
73+
pubkey: MPL_CORE_PROGRAM_ID,
74+
isSigner: false,
75+
isWritable: false,
76+
},
77+
{
78+
pubkey: MPL_CORE_PROGRAM_ID,
79+
isSigner: false,
80+
isWritable: false,
81+
},
82+
],
83+
programId: MPL_CORE_PROGRAM_ID,
84+
data: Buffer.from([14,0])
85+
}
86+
return ix;
87+
} else {
88+
throw new Error('failed to create transfer instruction')
89+
}
6890
}
6991
}

0 commit comments

Comments
 (0)