功能

锁定资产

如"委托权限"页面所述,某些委托可以锁定和解锁资产,防止其所有者转移或销毁它们。锁定的资产还禁止所有者撤销委托的权限。这种锁定机制支持各种实用用例——例如质押——否则需要托管账户才能运行。

在下表中,我们列出了所有支持锁定资产的代币委托。您可以在各自的部分中了解更多关于这些委托以及如何批准/撤销它们的信息。

委托锁定/解锁转移销毁适用于
Standard除 pNFT 外的所有资产
Locked Transfer仅限 pNFT
Utility仅限 pNFT
Staking仅限 pNFT

假设我们在资产上有一个已批准的代币委托,现在让我们看看委托如何锁定和解锁它。

锁定资产

NFT

要锁定资产,委托可以使用 Token Metadata 程序的锁定指令。此指令接受以下属性:

  • Mint:资产的铸造账户地址。
  • Authority:授权锁定的签名者。这必须是委托权限。
  • Token Standard:被锁定资产的标准。请注意,Token Metadata 程序并不明确要求此参数,但我们的 SDK 需要它,以便它们可以为大多数其他参数提供适当的默认值。
1import { lockV1 } from '@metaplex-foundation/mpl-token-metadata';
2
3// Assuming umi, mint, and authority (delegate) are set up
4
5await lockV1(umi, {
6 mint,
7 authority,
8 tokenStandard: TokenStandard.NonFungible,
9}).sendAndConfirm(umi);
10
11console.log('NFT locked');

pNFT

1import {
2 fetchDigitalAssetWithAssociatedToken,
3 lockV1,
4 TokenStandard,
5} from '@metaplex-foundation/mpl-token-metadata';
6import { publicKey } from '@metaplex-foundation/umi';
7import { base58 } from '@metaplex-foundation/umi/serializers';
8
9// Assuming umi is set up with mplTokenMetadata plugin
10
11// Mint ID of the pNFT Asset
12const mintId = publicKey('11111111111111111111111111111111');
13
14// Fetch pNFT Asset with Token Accounts
15const assetWithToken = await fetchDigitalAssetWithAssociatedToken(
16 umi,
17 mintId,
18 umi.identity.publicKey
19);
20
21// Send lock instruction
22const { signature } = await lockV1(umi, {
23 // Mint ID of the pNFT Asset
24 mint: mintId,
25 // Update Authority or Delegate Authority
26 authority: umi.identity,
27 // Token Standard
28 tokenStandard: TokenStandard.ProgrammableNonFungible,
29 // Owner of the pNFT Asset
30 tokenOwner: assetWithToken.token.owner,
31 // Token Account of the pNFT Asset
32 token: assetWithToken.token.publicKey,
33 // Token Record of the pNFT Asset
34 tokenRecord: assetWithToken.tokenRecord?.publicKey,
35}).sendAndConfirm(umi);
36
37console.log('Signature: ', base58.deserialize(signature));

解锁资产

NFT

相反,委托可以使用 Token Metadata 程序的解锁指令来解锁资产。此指令接受与锁定指令相同的属性,并且可以以相同的方式使用。

1import { unlockV1 } from '@metaplex-foundation/mpl-token-metadata';
2
3// Assuming umi, mint, and authority (delegate) are set up
4
5await unlockV1(umi, {
6 mint,
7 authority,
8 tokenStandard: TokenStandard.NonFungible,
9}).sendAndConfirm(umi);
10
11console.log('NFT unlocked');

pNFT

1import {
2 fetchDigitalAssetWithAssociatedToken,
3 TokenStandard,
4 unlockV1,
5} from '@metaplex-foundation/mpl-token-metadata';
6import { publicKey } from '@metaplex-foundation/umi';
7import { base58 } from '@metaplex-foundation/umi/serializers';
8
9// Assuming umi is set up with mplTokenMetadata plugin
10
11// Mint pNFT ID of the Asset
12const mintId = publicKey('11111111111111111111111111111111');
13
14// Fetch the mint token accounts
15const assetWithToken = await fetchDigitalAssetWithAssociatedToken(
16 umi,
17 mintId,
18 umi.identity.publicKey
19);
20
21// Send unlock instruction
22const { signature } = await unlockV1(umi, {
23 // Mint ID of the pNFT Asset
24 mint: mintId,
25 // Update Authority or Delegate Authority
26 authority: umi.identity,
27 // Token Standard
28 tokenStandard: TokenStandard.ProgrammableNonFungible,
29 // Owner of the pNFT Assets
30 tokenOwner: assetWithToken.token.owner,
31 // Token Account of the pNFT Asset
32 token: assetWithToken.token.publicKey,
33 // Token Record of the pNFT Asset
34 tokenRecord: assetWithToken.tokenRecord?.publicKey,
35}).sendAndConfirm(umi);
36
37console.log('Signature: ', base58.deserialize(signature));