功能

转移资产

资产的所有者可以通过向 Token Metadata 程序发送 Transfer 指令将资产转移到另一个账户。此指令接受以下属性:

  • Authority:授权转移的签名者。通常,这是资产的所有者,但请注意,某些委托权限也可以代表所有者转移资产,如"委托权限"页面所述。
  • Token Owner:资产当前所有者的公钥。
  • Destination Owner:资产新所有者的公钥。
  • Token Standard:被转移资产的标准。此指令适用于所有代币标准,以提供统一的资产转移接口。话虽如此,值得注意的是,非可编程资产可以直接使用 SPL Token 程序的 Transfer 指令来转移。

以下是如何使用我们的 SDK 在 Token Metadata 上转移资产。

NFT 转移

1import { publicKey } from '@metaplex-foundation/umi';
2import {
3 transferV1,
4 TokenStandard,
5} from '@metaplex-foundation/mpl-token-metadata';
6
7// Assuming umi is set up with mplTokenMetadata plugin
8// See getting-started for full setup
9
10const mintAddress = publicKey('mintAddress...');
11const currentOwner = umi.identity; // Current token owner
12const destinationOwner = publicKey('destinationWallet...');
13
14// Transfer the NFT to a new owner
15await transferV1(umi, {
16 mint: mintAddress,
17 authority: currentOwner,
18 tokenOwner: currentOwner.publicKey,
19 destinationOwner,
20 tokenStandard: TokenStandard.NonFungible,
21}).sendAndConfirm(umi);
22
23console.log('NFT transferred to:', destinationOwner);

pNFT 转移

可编程 NFT(pNFT)可能有需要在转移过程中处理的额外授权规则。该指令将自动处理 Token Record 账户。

1import { publicKey } from '@metaplex-foundation/umi';
2import {
3 transferV1,
4 TokenStandard,
5} from '@metaplex-foundation/mpl-token-metadata';
6
7// Assuming umi is set up with mplTokenMetadata plugin
8
9const mintAddress = publicKey('mintAddress...');
10const currentOwner = umi.identity;
11const destinationOwner = publicKey('destinationWallet...');
12
13// Transfer the Programmable NFT to a new owner
14// Note: pNFTs require additional Token Record accounts handled automatically
15await transferV1(umi, {
16 mint: mintAddress,
17 authority: currentOwner,
18 tokenOwner: currentOwner.publicKey,
19 destinationOwner,
20 tokenStandard: TokenStandard.ProgrammableNonFungible,
21}).sendAndConfirm(umi);
22
23console.log('pNFT transferred to:', destinationOwner);

高级 pNFT 转移

对于具有复杂授权规则的 pNFT,您可能需要提供额外的参数。

1import { getMplTokenAuthRulesProgramId } from '@metaplex-foundation/mpl-candy-machine';
2import {
3 fetchDigitalAssetWithAssociatedToken,
4 findTokenRecordPda,
5 TokenStandard,
6 transferV1,
7} from '@metaplex-foundation/mpl-token-metadata';
8import { findAssociatedTokenPda } from '@metaplex-foundation/mpl-toolbox';
9import { publicKey, unwrapOptionRecursively } from '@metaplex-foundation/umi';
10import { base58 } from '@metaplex-foundation/umi/serializers';
11
12// The NFT Asset Mint ID
13const mintId = publicKey('11111111111111111111111111111111');
14
15// The destination wallet
16const destinationAddress = publicKey('22222222222222222222222222222222');
17
18// Fetch the pNFT Asset with the Token Account
19const assetWithToken = await fetchDigitalAssetWithAssociatedToken(
20 umi,
21 mintId,
22 umi.identity.publicKey
23);
24
25// Calculates the destination wallet's Token Account
26const destinationTokenAccount = findAssociatedTokenPda(umi, {
27 mint: mintId,
28 owner: destinationAddress,
29});
30
31// Calculates the destinations wallet's Token Record Account
32const destinationTokenRecord = findTokenRecordPda(umi, {
33 mint: mintId,
34 token: destinationTokenAccount[0],
35});
36
37// Transfer the pNFT
38const { signature } = await transferV1(umi, {
39 mint: mintId,
40 destinationOwner: destinationAddress,
41 destinationTokenRecord: destinationTokenRecord,
42 tokenRecord: assetWithToken.tokenRecord?.publicKey,
43 tokenStandard: TokenStandard.ProgrammableNonFungible,
44 // Check to see if the pNFT asset as auth rules.
45 authorizationRules:
46 unwrapOptionRecursively(assetWithToken.metadata.programmableConfig)
47 ?.ruleSet || undefined,
48 // Auth rules program ID
49 authorizationRulesProgram: getMplTokenAuthRulesProgramId(umi),
50 // Some pNFTs may require authorization data if set.
51 authorizationData: undefined,
52}).sendAndConfirm(umi);
53
54console.log('Signature:', base58.deserialize(signature));