-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
88 lines (69 loc) · 2.91 KB
/
index.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
const {
Client,
AccountId,
PrivateKey,
ContractCreateFlow,
ContractExecuteTransaction,
ContractFunctionParameters,
ContractInfoQuery
} = require('@hashgraph/sdk');
const {
accountCreator,
tokenCreator
} = require('./utils');
require('dotenv').config({path: __dirname + '/../../.env'});
const fs = require('fs');
const operatorId = AccountId.fromString(process.env.ACCOUNT_ID);
const operatorKey = PrivateKey.fromString(process.env.PRIVATE_KEY);
const client = Client.forTestnet();
client.setOperator(operatorId, operatorKey);
const main = async () => {
const aliceKey = PrivateKey.generateED25519();
const aliceId = await accountCreator(aliceKey, 10, client);
const tokenId = await tokenCreator(aliceId, aliceKey, client);
console.log("The new token ID is " + tokenId);
const bytecode = fs.readFileSync('./binaries/TokenReceiver_sol_TokenReceiver.bin');
// Create contract using ContractCreateFlow
const createContract = new ContractCreateFlow()
.setGas(100000)
.setBytecode(bytecode)
const createSubmit = await createContract.execute(client);
const createRx = await createSubmit.getReceipt(client);
const contractId = createRx.contractId;
console.log("The new contract ID is " + contractId);
// Execute token associate
const tokenAssociate = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(1500000)
.setFunction("tokenAssociate",
new ContractFunctionParameters()
.addAddress(tokenId.toSolidityAddress())
);
const tokenAssociateTx = await tokenAssociate.execute(client);
const tokenAssociateRx = await tokenAssociateTx.getReceipt(client);
const tokenAssociateStatus = tokenAssociateRx.status;
console.log("Token associate transaction status: " + tokenAssociateStatus.toString());
// Execute token transfer
const tokenTransfer = new ContractExecuteTransaction()
.setContractId(contractId)
.setGas(1500000)
.setFunction("tokenTransfer",
new ContractFunctionParameters()
.addAddress(tokenId.toSolidityAddress())
.addAddress(aliceId.toSolidityAddress())
.addInt64(1000)
)
.freezeWith(client);
const tokenTransferSigned = await tokenTransfer.sign(aliceKey);
const tokenTransferTx = await tokenTransferSigned.execute(client);
const tokenTransferRx = await tokenTransferTx.getReceipt(client);
const tokenTransferStatus = tokenTransferRx.status;
console.log("Token transfer transaction status: " + tokenTransferStatus.toString());
//Create the query
const query = new ContractInfoQuery()
.setContractId(contractId);
const info = await query.execute(client);
const balance = info.tokenRelationships.get(tokenId).balance / 100;
console.log("The contract balance for token " + tokenId + " is: " + balance);
}
main();