-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreateFungibleToken.mjs
153 lines (127 loc) · 5.17 KB
/
createFungibleToken.mjs
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
152
153
import dotenv from 'dotenv';
import readlineSync from 'readline-sync';
dotenv.config();
import { AccountId, PrivateKey, Client, TokenCreateTransaction, TokenType, TokenSupplyType, AccountBalanceQuery, Hbar } from '@hashgraph/sdk';
// Configure accounts and client, and generate needed keys
const myAcctID = AccountId.fromString(process.env.MY_ACCOUNT_ID);
const myAcctPK = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
const tokenName = process.env.TOKEN_NAME;
const tokenSymbol = process.env.TOKEN_SYMBOL;
const tokenDecimal = Number(process.env.TOKEN_DECIMALS);
const tokenInitalSupply = Number(process.env.TOKEN_INITALSUPPLY);
const supplyKey = PrivateKey.generate();
const adminKey = PrivateKey.generate();
const pauseKey = PrivateKey.generate();
const freezeKey = PrivateKey.generate();
const wipeKey = PrivateKey.generate();
const feeScheduleKey = PrivateKey.generate();
function getArg(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
let customValue;
if (customIndex > -1) {
// Retrieve the value after --custom
customValue = process.argv[customIndex + 1];
}
return customValue;
}
function getArgFlag(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
if (customIndex > -1) {
return true;
}
return false;
}
async function main() {
if (getArgFlag('h')) {
console.log('Usage: node createFungibleToken.mjs [-mainnet] [-adminkey] [-freezekey] [-pausekey] [-maxsupply XXX]');
console.log(' -mainnet optional - defaults to testnet unless specified');
console.log(' -adminkey optional - add an admin key');
console.log(' -freezekey optional - add a freeze key');
console.log(' -pausekey optional - add an pause key');
console.log(' -wipekey optional - add a wip key');
console.log(' -feekey optional - add a fee schedule key');
console.log(' -supplykey optional - add a supply key (without no further minting can occur)');
console.log(' -maxsupply optional - if used please provide an integer, if omitted then infinite supply assumed');
return;
}
let client;
if (getArgFlag('mainnet')) {
console.log('using mainnet');
client = Client.forMainnet();
}
else {
console.log('Using testnet');
client = Client.forTestnet();
}
client.setOperator(myAcctID, myAcctPK);
// CREATE FUNGIBLE TOKEN (STABLECOIN)
const tokenCreateTx = new TokenCreateTransaction()
.setTokenName(tokenName)
.setTokenSymbol(tokenSymbol)
.setTokenType(TokenType.FungibleCommon)
.setDecimals(tokenDecimal)
.setInitialSupply(tokenInitalSupply)
.setTreasuryAccountId(myAcctID)
.setMaxTransactionFee(new Hbar(100));
if (getArgFlag('maxsupply')) {
tokenCreateTx.setSupplyType(TokenSupplyType.Finite);
const maxSupply = Number(getArg('maxsupply'));
if (Number.isInteger(maxSupply)) {
tokenCreateTx.setMaxSupply(maxSupply);
tokenCreateTx.setSupplyType(TokenSupplyType.Finite);
}
else {
console.log(maxSupply, 'must be an integer');
return;
}
}
else {
tokenCreateTx.setSupplyType(TokenSupplyType.Infinite);
}
const useFreeze = getArgFlag('freezekey');
const usePause = getArgFlag('pausekey');
const useAdmin = getArgFlag('adminkey');
const useWipe = getArgFlag('wipekey');
const useFee = getArgFlag('feekey');
const useSupply = getArgFlag('supplykey');
if (useFreeze) tokenCreateTx.setFreezeKey(freezeKey);
if (usePause) tokenCreateTx.setPauseKey(pauseKey);
if (useAdmin) tokenCreateTx.setAdminKey(adminKey);
if (useWipe) tokenCreateTx.setWipeKey(wipeKey);
if (useFee) tokenCreateTx.setFeeScheduleKey(feeScheduleKey);
if (useSupply) tokenCreateTx.setSupplyKey(supplyKey);
tokenCreateTx.freezeWith(client);
const tokenCreateSign = await tokenCreateTx.sign(myAcctPK);
console.log('Creating token...');
console.log(`- Token Name: ${tokenName}`);
console.log(`- Token Symbol: ${tokenSymbol}`);
console.log(`- Token Decimals: ${tokenDecimal}`);
console.log(`- Token Initial Supply: ${tokenInitalSupply}`);
console.log(`- Max Supply: ${getArg('maxsupply')}`);
console.log(`- WipeKey: ${useWipe}`);
console.log(`- FreezeKey: ${useFreeze}`);
console.log(`- PauseKey: ${usePause}`);
console.log(`- AdminKey: ${useAdmin}`);
console.log(`- FeeScheduleKey: ${useFee}`);
console.log(`- SupplyKey: ${useSupply}`);
const execute = readlineSync.keyInYNStrict('Do wish to execute the token creation?');
if (!execute) {
console.log('Exiting without creating token');
return;
}
if (useAdmin) await tokenCreateSign.sign(adminKey);
const tokenCreateSubmit = await tokenCreateSign.execute(client);
const tokenCreateRx = await tokenCreateSubmit.getReceipt(client);
const tokenId = tokenCreateRx.tokenId;
console.log(`- Created token with ID: ${tokenId} \n`);
console.log(`Supply Key: ${supplyKey}`);
if (useAdmin) console.log(`Admin Key: ${adminKey}`);
if (usePause) console.log(`Pause Key: ${pauseKey}`);
if (useFreeze) console.log(`Freeze Key: ${freezeKey}`);
if (useWipe) console.log(`Wipe Key: ${wipeKey}`);
if (useFee) console.log(`Fee Schedule Key: ${feeScheduleKey}`);
// BALANCE CHECK
const balanceCheckTx = await new AccountBalanceQuery().setAccountId(myAcctID).execute(client);
console.log(`- Treasury balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`);
}
main();