1+ import { DasNftObject } from '@hooks/queries/digitalAssets'
12import { fetchNFTbyMint } from '@hooks/queries/nft'
23import { 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
66export 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