功能
锁定资产
如"委托权限"页面所述,某些委托可以锁定和解锁资产,防止其所有者转移或销毁它们。锁定的资产还禁止所有者撤销委托的权限。这种锁定机制支持各种实用用例——例如质押——否则需要托管账户才能运行。
在下表中,我们列出了所有支持锁定资产的代币委托。您可以在各自的部分中了解更多关于这些委托以及如何批准/撤销它们的信息。
| 委托 | 锁定/解锁 | 转移 | 销毁 | 适用于 |
|---|---|---|---|---|
| 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');
1import {
2 getLockV1InstructionAsync,
3 TokenStandard,
4} from '@metaplex-foundation/mpl-token-metadata-kit';
5
6// Assuming rpc, rpcSubscriptions, sendAndConfirm, authority (delegate), and tokenOwner are set up
7
8const mintAddress = 'mintAddress...'; // The NFT mint address
9
10// Lock an NFT (freeze) - requires approved delegate authority
11const lockIx = await getLockV1InstructionAsync({
12 mint: mintAddress,
13 authority,
14 payer: authority,
15 tokenOwner,
16 tokenStandard: TokenStandard.NonFungible,
17});
18
19await sendAndConfirm({
20 instructions: [lockIx],
21 payer: authority,
22});
23
24console.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));
1import {
2 getLockV1InstructionAsync,
3 fetchDigitalAssetWithAssociatedToken,
4 TokenStandard,
5} from '@metaplex-foundation/mpl-token-metadata-kit';
6
7// Assuming rpc, rpcSubscriptions, sendAndConfirm, and authority (delegate) are set up
8
9const mintAddress = 'mintAddress...'; // The pNFT mint address
10
11// Fetch pNFT Asset with Token Accounts
12const assetWithToken = await fetchDigitalAssetWithAssociatedToken(
13 rpc,
14 mintAddress,
15 authority.address
16);
17
18// Lock a pNFT - requires approved delegate authority
19const lockIx = await getLockV1InstructionAsync({
20 mint: mintAddress,
21 authority,
22 payer: authority,
23 tokenOwner: assetWithToken.token.owner,
24 token: assetWithToken.token.address,
25 tokenRecord: assetWithToken.tokenRecord?.address,
26 tokenStandard: TokenStandard.ProgrammableNonFungible,
27});
28
29await sendAndConfirm({
30 instructions: [lockIx],
31 payer: authority,
32});
33
34console.log('pNFT locked');
解锁资产
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');
1import {
2 getUnlockV1InstructionAsync,
3 TokenStandard,
4} from '@metaplex-foundation/mpl-token-metadata-kit';
5
6// Assuming rpc, rpcSubscriptions, sendAndConfirm, authority (delegate), and tokenOwner are set up
7
8const mintAddress = 'mintAddress...'; // The NFT mint address
9
10// Unlock an NFT (thaw) - requires approved delegate authority
11const unlockIx = await getUnlockV1InstructionAsync({
12 mint: mintAddress,
13 authority,
14 payer: authority,
15 tokenOwner,
16 tokenStandard: TokenStandard.NonFungible,
17});
18
19await sendAndConfirm({
20 instructions: [unlockIx],
21 payer: authority,
22});
23
24console.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));
1import {
2 getUnlockV1InstructionAsync,
3 fetchDigitalAssetWithAssociatedToken,
4 TokenStandard,
5} from '@metaplex-foundation/mpl-token-metadata-kit';
6
7// Assuming rpc, rpcSubscriptions, sendAndConfirm, and authority (delegate) are set up
8
9const mintAddress = 'mintAddress...'; // The pNFT mint address
10
11// Fetch pNFT Asset with Token Accounts
12const assetWithToken = await fetchDigitalAssetWithAssociatedToken(
13 rpc,
14 mintAddress,
15 authority.address
16);
17
18// Unlock a pNFT - requires approved delegate authority
19const unlockIx = await getUnlockV1InstructionAsync({
20 mint: mintAddress,
21 authority,
22 payer: authority,
23 tokenOwner: assetWithToken.token.owner,
24 token: assetWithToken.token.address,
25 tokenRecord: assetWithToken.tokenRecord?.address,
26 tokenStandard: TokenStandard.ProgrammableNonFungible,
27});
28
29await sendAndConfirm({
30 instructions: [unlockIx],
31 payer: authority,
32});
33
34console.log('pNFT unlocked');
