Skip to content

Commit

Permalink
export Long and add test and example
Browse files Browse the repository at this point in the history
Signed-off-by: svetoslav-nikol0v <svetoslav.nikolov@limechain.tech>
  • Loading branch information
svetoslav-nikol0v committed Jan 31, 2024
1 parent cb01e23 commit 1fac9db
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
68 changes: 68 additions & 0 deletions examples/mint-big-number-of-units-of-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
AccountId,
Client,
PrivateKey,
TokenCreateTransaction,
TokenType,
TokenMintTransaction,
TokenInfoQuery,
Long,
Status
} from "@hashgraph/sdk";
import dotenv from "dotenv";

dotenv.config();

async function main() {
const operatorKey = PrivateKey.fromStringECDSA(process.env.OPERATOR_KEY)
const operatorId = AccountId.fromString(process.env.OPERATOR_ID)

const client = Client.forName(process.env.HEDERA_NETWORK).setOperator(
operatorId,
operatorKey
);

let tokenCreate = await new TokenCreateTransaction()
.setTokenName("Token")
.setTokenSymbol("T")
.setTokenType(TokenType.FungibleCommon)
.setDecimals(8)
.setTreasuryAccountId(operatorId)
.setSupplyKey(operatorKey)
.execute(client);

let tokenCreateReceipt = await tokenCreate.getReceipt(client);
const tokenId = tokenCreateReceipt.tokenId;
console.log(`TokenId is ${tokenId.toString()}.`);

// If the number of tokens that should be minted is bigger
// than Number.MAX_SAFE_INTEGER it should be passed as a Long number
const amount = Long.fromValue("25817858423044461");
console.log(`Token balance will be set to ${amount}.`);

let tokenMint = await new TokenMintTransaction()
.setTokenId(tokenId)
.setAmount(amount)
.execute(client);

const tokenMintReciept = await tokenMint.getReceipt(client);
if (tokenMintReciept.status === Status.Success) {
console.log("Token has been minted!");
} else {
console.error('Token mint transaction failed.');
}

let tokenInfo = await new TokenInfoQuery()
.setTokenId(tokenId)
.execute(client);

if (tokenInfo) {
console.log(`Token Balance = ${tokenInfo.totalSupply.toString()}`);
} else {
console.error('Token query failed.');
}

client.close();
}

void main();
1 change: 1 addition & 0 deletions src/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export { default as Wallet } from "./Wallet.js";
export { default as Logger } from "./logger/Logger.js";
export { default as LogLevel } from "./logger/LogLevel.js";
export { EntityIdHelper };
export { default as Long } from "long";

export { default as StatusError } from "./StatusError.js";
export { default as PrecheckStatusError } from "./PrecheckStatusError.js";
Expand Down
9 changes: 9 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,12 @@ export function arrayEqual(array1, array2) {

return true;
}

/**
* @description Function that delays an execution for a given time (in milliseconds)
* @param {number} ms
* @returns {Promise<void>}
*/
export function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
38 changes: 38 additions & 0 deletions test/integration/TokenMintIntegrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
TokenSupplyType,
TokenType,
Transaction,
TokenInfoQuery,
Long,
} from "../../src/exports.js";
import { wait } from "../../src/util.js";
import IntegrationTestEnv from "./client/NodeIntegrationTestEnv.js";

describe("TokenMint", function () {
Expand Down Expand Up @@ -221,6 +224,41 @@ describe("TokenMint", function () {
}
});

it("should retrieve the correct token balance", async function () {
this.timeout(120000);

const operatorId = env.operatorId;
const operatorKey = env.operatorKey.publicKey;

const tokenCreateTransaction = await new TokenCreateTransaction()
.setTokenName("Token")
.setTokenSymbol("T")
.setTokenType(TokenType.FungibleCommon)
.setDecimals(8)
.setTreasuryAccountId(operatorId)
.setSupplyKey(operatorKey)
.execute(env.client);

const tokenCreateTransactionReceipt =
await tokenCreateTransaction.getReceipt(env.client);
const tokenId = tokenCreateTransactionReceipt.tokenId;

const amount = Long.fromValue("25817858423044461");

await new TokenMintTransaction()
.setTokenId(tokenId)
.setAmount(amount)
.execute(env.client);

await wait(5000);

const tokenInfo = await new TokenInfoQuery()
.setTokenId(tokenId)
.execute(env.client);

expect(tokenInfo.totalSupply.toString()).to.be.equal(amount.toString());
});

after(async function () {
await env.close();
});
Expand Down

0 comments on commit 1fac9db

Please sign in to comment.