forked from cisc0f/hedera
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (105 loc) · 4.94 KB
/
index.js
File metadata and controls
137 lines (105 loc) · 4.94 KB
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
const {
AccountId,
PrivateKey,
Hbar,
Client,
TokenType,
AccountCreateTransaction,
CustomFractionalFee,
TransferTransaction,
TokenCreateTransaction,
AccountBalanceQuery,
} = require('@hashgraph/sdk');
require('dotenv').config({path: __dirname + '/../../.env'});
const operatorId = AccountId.fromString(process.env.ACCOUNT_ID);
const operatorKey = PrivateKey.fromString(process.env.PRIVATE_KEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
const accountCreator = async (initialBalance, privateKey) => {
const createAccount = new AccountCreateTransaction()
.setInitialBalance(initialBalance)
.setKey(privateKey.publicKey)
.setMaxAutomaticTokenAssociations(10)
.freezeWith(client);
const createAccountTx = await createAccount.execute(client);
const createAccountRx = await createAccountTx.getReceipt(client);
return createAccountRx.accountId;
}
async function main() {
// STEP 1: Create three accounts using the helper function
const accountKey1 = PrivateKey.generateED25519();
const accountId1 = await accountCreator(50, accountKey1);
const accountKey2 = PrivateKey.generateED25519();
const accountId2 = await accountCreator(50, accountKey2);
const accountKey3 = PrivateKey.generateED25519();
const accountId3 = await accountCreator(50, accountKey3);
console.log(`STEP 1 - Three accounts created: \n ${accountId1} \n ${accountId2} \n ${accountId3}\n`);
// STEP 2: Create Custom Fees and Fungible Token
const fee1 = new CustomFractionalFee()
.setFeeCollectorAccountId(accountId1)
.setNumerator(1)
.setDenominator(100)
.setAllCollectorsAreExempt(true);
const fee2 = new CustomFractionalFee()
.setFeeCollectorAccountId(accountId2)
.setNumerator(2)
.setDenominator(100)
.setAllCollectorsAreExempt(true);
const fee3 = new CustomFractionalFee()
.setFeeCollectorAccountId(accountId3)
.setNumerator(3)
.setDenominator(100)
.setAllCollectorsAreExempt(true);
const createToken = new TokenCreateTransaction()
.setTokenName("HIP-573 Token")
.setTokenSymbol("H573")
.setTokenType(TokenType.FungibleCommon)
.setTreasuryAccountId(operatorId)
.setAutoRenewAccountId(operatorId)
.setAdminKey(operatorKey)
.setFreezeKey(operatorKey)
.setWipeKey(operatorKey)
.setInitialSupply(100000000) // Total supply = 100000000 / 10 ^ 2
.setDecimals(2)
.setCustomFees([fee1, fee2, fee3])
.setMaxTransactionFee(new Hbar(40))
.freezeWith(client);
const createTokenSigned1 = await createToken.sign(accountKey1);
const createTokenSigned2 = await createTokenSigned1.sign(accountKey2);
const createTokenSigned3 = await createTokenSigned2.sign(accountKey3);
const createTokenTx = await createTokenSigned3.execute(client);
const createTokenRx = await createTokenTx.getReceipt(client);
const tokenId = createTokenRx.tokenId;
console.log(`STEP 2 - Token with custom fees created: ${tokenId}\n`);
// STEP 3: Send token from treasury to one account and from one account to another
const transferFromTreasuryTx = await new TransferTransaction()
.addTokenTransfer(tokenId, operatorId, -10000)
.addTokenTransfer(tokenId, accountId2, 10000)
.freezeWith(client)
.execute(client);
const transferFromTreasuryRx = await transferFromTreasuryTx.getReceipt(client);
const transferFromTreasuryStatus = transferFromTreasuryRx.status.toString();
console.log(`STEP 3 \nToken transfer from treasury to account 2: ${transferFromTreasuryStatus}`);
const transferFromAccount2 = await new TransferTransaction()
.addTokenTransfer(tokenId, accountId2, -10000)
.addTokenTransfer(tokenId, accountId1, 10000)
.freezeWith(client)
.sign(accountKey2);
const transferFromAccount2Tx = await transferFromAccount2.execute(client);
const transferFromAccount2Rx = await transferFromAccount2Tx.getReceipt(client);
console.log(`Transfer from account 2 to account 1: ${transferFromAccount2Rx.status.toString()}\n`);
// Check collectors account balance (methods will be deprecated soon, use axios and mirror node api)
const checkBalance1 = await new AccountBalanceQuery()
.setAccountId(accountId1)
.execute(client);
const balance1 = checkBalance1.tokens._map.get(tokenId.toString());
const checkBalance2 = await new AccountBalanceQuery()
.setAccountId(accountId2)
.execute(client);
const balance2 = checkBalance2.tokens._map.get(tokenId.toString());
const checkBalance3 = await new AccountBalanceQuery()
.setAccountId(accountId3)
.execute(client);
const balance3 = checkBalance3.tokens._map.get(tokenId.toString());
console.log(`Accounts Balance: \nAccount 1: ${balance1} \nAccount 2: ${balance2} \nAccount 3: ${balance3} \n`);
}
void main();