Created
August 22, 2022 10:58
-
-
Save michielmulders/5fa87e6fe442fe81233a9acec4e6ee9b to your computer and use it in GitHub Desktop.
Hedera Utils for account and token creation using Hedera JS SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
TokenCreateTransaction, | |
Hbar, | |
AccountCreateTransaction, | |
PrivateKey | |
} = require("@hashgraph/sdk"); | |
/* | |
* @return {AccountId} accountId | |
*/ | |
async function accountCreator(pvKey, initialBalance, client) { | |
const response = await new AccountCreateTransaction() | |
.setInitialBalance(new Hbar(initialBalance)) | |
.setKey(pvKey.publicKey) | |
.execute(client); | |
const receipt = await response.getReceipt(client); | |
return receipt.accountId; | |
} | |
/* | |
* @return {TokenId} tokenId | |
* sets adminKey, treasuryId, treasuryKey | |
*/ | |
async function tokenCreator(adminKey, treasuryId, treasuryKey, client) { | |
//Create the transaction and freeze for manual signing | |
const createToken = await new TokenCreateTransaction() | |
.setTokenName("USD Bar") | |
.setTokenSymbol("USDB") | |
.setTreasuryAccountId(treasuryId) | |
.setInitialSupply(10000) | |
.setDecimals(2) | |
.setAdminKey(adminKey.publicKey) | |
.setMaxTransactionFee(new Hbar(20)) //Change the default max transaction fee | |
.freezeWith(client); | |
const createTokenTx = await (await createToken.sign(adminKey)).sign(treasuryKey); | |
const createTokenRx = await createTokenTx.execute(client); | |
const createTokenReceipt = await createTokenRx.getReceipt(client); | |
const tokenId = createTokenReceipt.tokenId; | |
return tokenId; | |
} | |
/* | |
* @return {Object} tokenInfo | |
* @return {TokenId} tokenInfo.tokenId | |
* @return {Key} tokenInfo.adminKey | |
* | |
* sets treasuryId, treasuryKey | |
* generates an admin key and returns it | |
*/ | |
async function tokenCreatorWithAdminKey(treasuryId, treasuryKey, client) { | |
const adminKey = PrivateKey.generateED25519(); | |
//Create the transaction and freeze for manual signing | |
const createToken = await new TokenCreateTransaction() | |
.setTokenName("USD Bar") | |
.setTokenSymbol("USDB") | |
.setTreasuryAccountId(treasuryId) | |
.setInitialSupply(10000) | |
.setDecimals(2) | |
.setAdminKey(adminKey.publicKey) | |
.setMaxTransactionFee(new Hbar(20)) //Change the default max transaction fee | |
.freezeWith(client); | |
const createTokenTx = await (await createToken.sign(adminKey)).sign(treasuryKey); | |
const createTokenRx = await createTokenTx.execute(client); | |
const createTokenReceipt = await createTokenRx.getReceipt(client); | |
const tokenId = createTokenReceipt.tokenId; | |
return { tokenId, adminKey }; | |
} | |
/* | |
* @return {TokenId} tokenId | |
* sets token name, symbol, supply, decimals, adminKey, treasuryId, treasuryKey | |
*/ | |
async function tokenCreatorFull(tokenName, tokenSymbol, initialSupply, decimals, adminKey, treasuryId, treasuryKey, client) { | |
//Create the transaction and freeze for manual signing | |
const createToken = await new TokenCreateTransaction() | |
.setTokenName(tokenName) | |
.setTokenSymbol(tokenSymbol) | |
.setTreasuryAccountId(treasuryId) | |
.setInitialSupply(initialSupply) | |
.setDecimals(decimals) | |
.setAdminKey(adminKey.publicKey) | |
.setMaxTransactionFee(new Hbar(20)) //Change the default max transaction fee | |
.freezeWith(client); | |
const createTokenTx = await (await createToken.sign(adminKey)).sign(treasuryKey); | |
const createTokenRx = await createTokenTx.execute(client); | |
const createTokenReceipt = await createTokenRx.getReceipt(client); | |
const tokenId = createTokenReceipt.tokenId; | |
return tokenId; | |
} | |
module.exports = { | |
accountCreator, | |
tokenCreator, | |
tokenCreatorFull, | |
tokenCreatorWithAdminKey | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment