Skip to content

Commit e520611

Browse files
committed
finish nft transaction
1 parent 260086e commit e520611

File tree

1 file changed

+138
-0
lines changed
  • i18n/zh-CN/docusaurus-plugin-content-docs/current/introduction/getting-started/transaction

1 file changed

+138
-0
lines changed

i18n/zh-CN/docusaurus-plugin-content-docs/current/introduction/getting-started/transaction/nft_transaction_sdk.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,141 @@ sidebar_position: 6
33
---
44

55
# 使用SDK构建NFT交易
6+
7+
本文介绍使用MetaContract SDK构建NFT(Non-Fungible Token)交易的方法。
8+
9+
> 使用之前请确保已经完成环境配置和测试币领取,请参考[搭建一个MVC项目](../enviroment/setup-project)
10+
11+
本文基于上述已经配置好的环境和密钥进行操作。
12+
13+
## 代码示例
14+
15+
`src/index.ts`文件替换为以下内容:
16+
17+
```typescript
18+
import {Mnemonic} from "meta-contract/dist/mvc";
19+
import {promises as fs} from 'fs';
20+
import {API_NET, API_TARGET, NftManager, Wallet} from "meta-contract";
21+
22+
const FILE_PATH = 'mnemonic-seed.txt'
23+
const WALLET_PATH = "m/44'/10001'/0'/0/0"
24+
let seed = '';
25+
const generateMnemonic = async (): Promise<string> => {
26+
console.log("Generating random mnemonic seed: <%s>, use your own if you already have it", Mnemonic.fromRandom().toString())
27+
const mnemonic = Mnemonic.fromRandom().toString();
28+
await fs.writeFile(FILE_PATH, mnemonic, 'utf8');
29+
return mnemonic;
30+
};
31+
32+
const readMnemonic = async (): Promise<string> => {
33+
return await fs.readFile(FILE_PATH, 'utf8');
34+
};
35+
36+
// Detect if the mnomonic seed file exists, if not, generate a new one
37+
// Then create a wallet with the seed with path WALLET_PATH
38+
const setupMyWalletAndOperateNft = async (): Promise<void> => {
39+
try {
40+
await fs.access(FILE_PATH);
41+
const mnemonic = await readMnemonic();
42+
console.log('Mnemonic seed exists: <%s>, will use this one.', mnemonic);
43+
seed = mnemonic;
44+
} catch (error) {
45+
const mnemonic = await generateMnemonic();
46+
console.log('Mnemonic seed not detected, generating new one. <%s> , please keep it safe for future use.', mnemonic);
47+
seed = mnemonic;
48+
}
49+
console.log('Creating wallet with seed: <%s> and path <%s>', seed, WALLET_PATH);
50+
let mnemonicParsed = Mnemonic.fromString(seed);
51+
let privateKey = mnemonicParsed.toHDPrivateKey("", API_NET.TEST).deriveChild(WALLET_PATH);
52+
let wallet = new Wallet(privateKey.privateKey.toWIF(), API_NET.TEST, 1);
53+
console.log("Your private key %s", privateKey.privateKey.toWIF());
54+
console.log("Your address %s", privateKey.privateKey.toAddress(API_NET.TEST).toString());
55+
console.log("Your balance %s satoshis", await wallet.getBalance());
56+
57+
// create an NftManager instance for the following operations
58+
const nftManager = new NftManager({
59+
network: API_NET.TEST,
60+
apiTarget: API_TARGET.MVC,
61+
purse: privateKey.privateKey.toWIF(),
62+
feeb: 1,
63+
})
64+
const totalSupply = "10";
65+
// genesis the NFT with totalSupply
66+
const genesisResult = await nftManager.genesis({totalSupply: totalSupply,version:1})
67+
console.log("Created NFT with sensibleId: %s,codehash %s, genesis: %s, totalSupply is : ", genesisResult.sensibleId, genesisResult.codehash, genesisResult.genesis, totalSupply);
68+
const sensibleId = genesisResult.sensibleId;
69+
const genesis = genesisResult.genesis;
70+
const codehash = genesisResult.codehash;
71+
72+
// mint one NFT
73+
// The metaTxid is the transaction that you bury your NFT metadata in, if you don't have one, just leave it empty
74+
const metaTxId = ''
75+
const metaOutputIndex = 0
76+
const {txid} = await nftManager.mint({
77+
version: 1,
78+
sensibleId: sensibleId!,
79+
metaTxId,
80+
metaOutputIndex,
81+
})
82+
console.log("Minted NFT with txid: %s", txid);
83+
84+
// sleep for 1 seconds to wait for the mint transaction
85+
await new Promise(resolve => setTimeout(resolve, 1000));
86+
87+
// mint Another NFT
88+
const {txid: txid2} = await nftManager.mint({
89+
version: 1,
90+
sensibleId: sensibleId!,
91+
metaTxId,
92+
metaOutputIndex,
93+
})
94+
console.log("Minted Another NFT with txid: %s", txid2);
95+
96+
// sleep for 1 seconds to wait for the mint transaction
97+
await new Promise(resolve => setTimeout(resolve, 1000));
98+
99+
// transfer the first NFT to faucet address
100+
let transferResp = await nftManager.transfer({
101+
tokenIndex: "0",
102+
genesis: genesis!,
103+
codehash: codehash!,
104+
senderWif: privateKey.privateKey.toWIF(),
105+
receiverAddress: "mktGt8zJo5qYmXc97SVvCLPvtLExymgBKF"
106+
})
107+
console.log("Transferred NFT to %s with txid: %s", "mktGt8zJo5qYmXc97SVvCLPvtLExymgBKF", transferResp.txid);
108+
109+
};
110+
111+
112+
setupMyWalletAndOperateNft().catch(console.error);
113+
```
114+
115+
这个示例程序创建了一个NFT集合,然后铸造两个NFT,最后将第一个NFT转移到水龙头地址。
116+
117+
程序的功能介绍:
118+
1. 初始化钱包并打印出地址和余额。
119+
2. 创建一个NFT集合。
120+
3. 铸造两个NFT。
121+
4. 将第一个NFT转移到水龙头地址。
122+
5. 打印出交易ID。
123+
124+
## 运行程序
125+
126+
运行以下命令,执行程序:
127+
128+
```bash
129+
npx tsc
130+
node src/index.js
131+
```
132+
133+
如果一切正常,你会看到类似以下输出:
134+
135+
```text
136+
......
137+
Your address mmGruHTY1ivexPzmvjz8AxrhhWWE8BbgN9
138+
Your balance 9267082 satoshis
139+
Created NFT with sensibleId: 91776d7db885b9e8c42c4ea4ae24316dea55574bcd017d7e34aeac159645a8ab00000000,codehash e205939ad9956673ce7da9fbd40514b30f66dc35, genesis: 8cbca577f02bc27d9c9f9cfee2d01a37f9ad4230, totalSupply is : 10
140+
Minted NFT with txid: f4d3347a479e9fcb66aab971d8da6b592d83dcc95dd7e1a385a15bc48e8286d2
141+
Minted Another NFT with txid: b8312ceb8a4baa5aa09bbcbfa0bd262f6c1975f437985e1b99e1ede1f925b47d
142+
Transferred NFT to mktGt8zJo5qYmXc97SVvCLPvtLExymgBKF with txid: 468fe1f009562fb7a1ae16cb335c42fbaf75c5d84a21a28b5bf145b393645831
143+
```

0 commit comments

Comments
 (0)