-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnft_transfer.js
65 lines (56 loc) · 2.17 KB
/
nft_transfer.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
console.clear();
require("dotenv").config();
const {
PrivateKey,
Client,
TransferTransaction,
TokenNftInfoQuery,
TokenAssociateTransaction,
TokenId,
NftId,
} = 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 secondAccountId = process.env.SECOND_ACCOUNT_ID;
const secondPrivateKey = PrivateKey.fromStringDer(
process.env.SECOND_PRIVATE_KEY
);
const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
async function transferNFT(senderId, receiverId, nftId) {
let tokenTransferTx = await new TransferTransaction()
.addNftTransfer(nftId.tokenId, nftId.serial, senderId, receiverId)
.execute(client);
let tokenTransferRx = await tokenTransferTx.getReceipt(client);
console.log(`\n- NFT transfer : ${tokenTransferRx.status.toString()} \n`);
}
async function nftInfoQuery(nftId) {
console.log("NftInfoQuery----------------");
//Returns the info for the specified NFT ID
const nftInfo = await new TokenNftInfoQuery().setNftId(nftId).execute(client);
console.log(JSON.stringify(nftInfo, null, 4));
console.log("-----------------------------------");
}
async function associateToken(tokenId, accountId, accountPrivateKey) {
console.log("AssociateToken----------------");
let associateTokenTx = await new TokenAssociateTransaction()
.setAccountId(accountId)
.setTokenIds([tokenId])
.freezeWith(client);
let associationSign = await associateTokenTx.sign(accountPrivateKey);
let associationSubmit = await associationSign.execute(client);
let receipt = await associationSubmit.getReceipt(client);
console.log("Associate Token: ", receipt.status.toString());
console.log("-----------------------------------");
}
async function main() {
const tokenId = "0.0.6767588";
const nftId = new NftId(TokenId.fromString(tokenId), 2);
console.log(nftId);
await nftInfoQuery(nftId);
// await associateToken(tokenId, secondAccountId, secondPrivateKey);
await transferNFT(myAccountId, secondAccountId, nftId);
await nftInfoQuery(nftId);
}
main();