-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateTokenExample.js
151 lines (123 loc) · 4.76 KB
/
createTokenExample.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env node
const {
Client,
PrivateKey,
AccountCreateTransaction,
AccountBalanceQuery,
Hbar,
TransferTransaction,
TokenCreateTransaction,
TokenType,
TokenSupplyType,
TokenAssociationTransaction,
TokenAssociateTransaction,
} = require('@hashgraph/sdk');
const dotenv = require('dotenv');
dotenv.config({ path: '.env' });
async function environmentSetup() {
const myAccountId = process.env.OPERATOR_ACCOUNT_ID;
// using the DER private key
const myPrivateKey = process.env.OPERATOR_ACCOUNT_PRIVATE_KEY_DER;
if (!myAccountId || !myPrivateKey) {
throw new Error('Environment variables not found');
} else {
console.log('Account found successfully');
console.log('Account ID', myAccountId);
console.log('Private Key', myPrivateKey);
}
// Now we have a valid operator
const client = Client.forTestnet();
client.setOperator(myAccountId, myPrivateKey);
// setting hbar limits
client.setDefaultMaxTransactionFee(new Hbar(100));
client.setDefaultMaxQueryPayment(new Hbar(50));
// create new keys
const newAccountPrivateKey = PrivateKey.generateED25519();
const newAccountPublicKey = newAccountPrivateKey.publicKey;
// creating new account with 1000 tinybars
const newAccount = await new AccountCreateTransaction()
.setKey(newAccountPublicKey)
.setInitialBalance(Hbar.fromTinybars(1000))
.execute(client);
// get new account ID
const getReceipt = await newAccount.getReceipt(client);
const newAccoundId = getReceipt.accountId;
// log account ID
console.log('The new account ID is: ' + newAccoundId);
// New code for this file
// CREATE FUNGIBLE TOKEN (STABLECOIN)
const supplyKey = PrivateKey.generate();
let tokenCreateTx = await new TokenCreateTransaction()
.setTokenName('THETCOIN')
.setTokenSymbol('THETTY')
.setTokenType(TokenType.FungibleCommon)
.setDecimals(2)
.setInitialSupply(10000)
.setTreasuryAccountId(myAccountId)
.setSupplyType(TokenSupplyType.Infinite)
.setSupplyKey(supplyKey)
.freezeWith(client);
//SIGN WITH TREASURY KEY
let tokenCreateSign = await tokenCreateTx.sign(
PrivateKey.fromStringDer(myPrivateKey), // need to conver to string. apparently "PrivateKey.fromString()" is deprecated
);
PrivateKey.fromStringDer;
//SUBMIT THE TRANSACTION
let tokenCreateSubmit = await tokenCreateSign.execute(client);
//GET THE TRANSACTION RECEIPT
let tokenCreateRx = await tokenCreateSubmit.getReceipt(client);
//GET THE TOKEN ID
let tokenId = tokenCreateRx.tokenId;
//LOG THE TOKEN ID TO THE CONSOLE
console.log(`- Created token with ID: ${tokenId} \n`);
// associate token with new account
const transaction = await new TokenAssociateTransaction()
.setAccountId(newAccoundId)
.setTokenIds([tokenId])
.freezeWith(client);
const signTx = await transaction.sign(newAccountPrivateKey);
const txResponse = await signTx.execute(client);
const associationRecepit = await txResponse.getReceipt(client);
const transactionStatus = associationRecepit.status;
console.log('Association transaction Status: ' + transactionStatus);
// before transfer, lets do a balance query:
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(myAccountId)
.execute(client);
console.log(
`My Account Balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`,
);
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(newAccoundId)
.execute(client);
console.log(
`My Account Balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`,
);
// send token from my account to new accound
const transferTransaction = await new TransferTransaction()
.addTokenTransfer(tokenId, myAccountId, -100) // from
.addTokenTransfer(tokenId, newAccoundId, 100) // to
.freezeWith(client);
const transferSign = await transferTransaction.sign(
PrivateKey.fromStringDer(myPrivateKey),
); // signing with my account bc I am sending the toke
const transferTxResponse = await transferSign.execute(client);
const transferReceipt = await transferTxResponse.getReceipt(client);
const transferStatus = transferReceipt.status;
console.log('Transfer Status: ' + transferStatus);
// after transfer, lets do a balance query:
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(myAccountId)
.execute(client);
console.log(
`My Account Balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`,
);
var balanceCheckTx = await new AccountBalanceQuery()
.setAccountId(newAccoundId)
.execute(client);
console.log(
`My Account Balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`,
);
client.close();
}
environmentSetup();