-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnft_creation.js
90 lines (80 loc) · 2.84 KB
/
nft_creation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
console.clear();
require("dotenv").config();
const {
Client,
TokenCreateTransaction,
TokenType,
TokenSupplyType,
TokenInfoQuery,
AccountBalanceQuery,
PrivateKey,
TokenMintTransaction,
} = require("@hashgraph/sdk");
//Grab your Hedera testnet account ID and private key from your .env file
const myAccountId = process.env.MY_ACCOUNT_ID;
const myPrivateKey = PrivateKey.fromStringDer(process.env.MY_PRIVATE_KEY);
const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
async function createNFT() {
console.log("CreateNFT---------------------");
let tokenCreateTx = await new TokenCreateTransaction()
.setTokenName("MyNFT")
.setTokenSymbol("MNFT")
.setTokenType(TokenType.NonFungibleUnique)
.setInitialSupply(0)
.setTreasuryAccountId(myAccountId)
.setSupplyType(TokenSupplyType.Finite)
.setMaxSupply(4)
.setSupplyKey(myPrivateKey)
.setFreezeKey(myPrivateKey)
.setPauseKey(myPrivateKey)
.setAdminKey(myPrivateKey)
.setWipeKey(myPrivateKey)
.freezeWith(client);
let tokenCreateSign = await tokenCreateTx.sign(myPrivateKey);
let tokenCreateSubmit = await tokenCreateSign.execute(client);
let tokenCreateRx = await tokenCreateSubmit.getReceipt(client);
let tokenId = tokenCreateRx.tokenId;
console.log(`- Created token with ID: ${tokenId}`);
console.log("-----------------------------------");
return tokenId;
}
async function queryTokenInfo(tokenId) {
console.log("QueryTokenInfo---------------------");
const query = new TokenInfoQuery().setTokenId(tokenId);
const tokenInfo = await query.execute(client);
console.log(JSON.stringify(tokenInfo, null, 4));
console.log("-----------------------------------");
}
async function queryAccountBalance(accountId) {
console.log("QueryAccountBalance----------------");
const balanceQuery = new AccountBalanceQuery().setAccountId(accountId);
const accountBalance = await balanceQuery.execute(client);
console.log(JSON.stringify(accountBalance, null, 4));
console.log("-----------------------------------");
}
async function mintNFT(tokenId) {
console.log("MintNFT--------------------------");
// Mint new NFT
let mintTx = await new TokenMintTransaction()
.setTokenId(tokenId)
.setMetadata([
Buffer.from("ipfs://QmTzWcVfk88JRqjTpVwHzBeULRTNzHY7mnBSG42CpwHmPa"),
Buffer.from("secondToken"),
Buffer.from("thirdToken"),
Buffer.from("fourthToken"),
])
.execute(client);
let mintRx = await mintTx.getReceipt(client);
//Log the serial number
console.log(`- Created NFT ${tokenId} with serial: ${mintRx.serials} \n`);
console.log("-----------------------------------");
}
async function main() {
const tokenId = await createNFT();
await queryTokenInfo(tokenId);
await queryAccountBalance(myAccountId);
await mintNFT(tokenId);
await queryAccountBalance(myAccountId);
}
main();