@hashgraph/hedera-local
Version:
Developer tooling for running Local Hedera Network (Consensus + Mirror Nodes).
253 lines • 11.4 kB
JavaScript
;
// SPDX-License-Identifier: Apache-2.0
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenUtils = void 0;
const sdk_1 = require("@hashgraph/sdk");
const IPrivateKey_1 = require("../configuration/types/IPrivateKey");
/**
* Provides utility methods for working with tokens.
*/
class TokenUtils {
/**
* Associates an account with the given tokens.
* @param accountId The account ID to associate.
* @param tokenIds The token IDs to associate.
* @param accountKey The account key to sign the transaction.
* @param client The client to use for associating the account with tokens.
* @returns {Promise<void>}
* A promise that resolves when the account is associated with the tokens.
*/
static associateAccountWithTokens(accountId, tokenIds, accountKey, client) {
return __awaiter(this, void 0, void 0, function* () {
const signTx = yield new sdk_1.TokenAssociateTransaction()
.setAccountId(accountId)
.setTokenIds(tokenIds)
.freezeWith(client)
.sign(accountKey);
const txResponse = yield signTx.execute(client);
yield txResponse.getReceipt(client);
});
}
/**
* Mints the given amount of tokens for the given token.
* @param tokenId The token ID to mint.
* @param CID The CID metadata for the minted tokens.
* @param supplyKey The supply key to sign the transaction.
* @param client The client to use for minting the tokens.
* @returns {TransactionReceipt} The receipt of the mint transaction.
*/
static mintToken(tokenId, CID, supplyKey, client) {
return __awaiter(this, void 0, void 0, function* () {
const transaction = new sdk_1.TokenMintTransaction()
.setTokenId(tokenId)
.setMetadata([Buffer.from(CID)])
.freezeWith(client);
const signTx = yield transaction.sign(supplyKey);
const txResponse = yield signTx.execute(client);
return txResponse.getReceipt(client);
});
}
/**
* Creates a token with the given properties.
* @param token The properties of the token to create.
* @param client The client to use for creating the token.
* @returns {TokenId} The ID of the created token.
*/
static createToken(token, client) {
return __awaiter(this, void 0, void 0, function* () {
const transaction = this.getTokenCreateTransaction(token);
let signTx = transaction.freezeWith(client);
if (token.adminKey) {
signTx = yield signTx.sign((0, IPrivateKey_1.getPrivateKey)(token.adminKey));
}
signTx = yield signTx.signWithOperator(client);
const txResponse = yield signTx.execute(client);
const receipt = yield txResponse.getReceipt(client);
return receipt.tokenId;
});
}
/**
* Returns the supply key for the given token.
*
* NOTE: The operator key will be used as a supply key by default,
* if the supply key is not provided in the properties
*
* @param token The properties of the token.
* @returns {PrivateKey} The supply key for the token.
*/
static getSupplyKey(token) {
// The operator key will be used as supply key if one is not provided
if (token.supplyKey) {
return (0, IPrivateKey_1.getPrivateKey)(token.supplyKey);
}
return sdk_1.PrivateKey.fromStringED25519(process.env.RELAY_OPERATOR_KEY_MAIN);
}
/**
* Returns the treasury account ID for the given token.
*
* NOTE: The operator ID will be used as a treasury account ID by default,
* if the treasury key is not provided in the properties
*
* @param token The properties of the token.
* @returns {AccountId} The treasury account ID for the token.
*/
static getTreasuryAccountId(token) {
// The operator key will be used as treasury key if one is not provided
if (token.treasuryKey) {
return (0, IPrivateKey_1.getPrivateKey)(token.treasuryKey).publicKey.toAccountId(0, 0);
}
return sdk_1.AccountId.fromString(process.env.RELAY_OPERATOR_ID_MAIN);
}
/**
* Creates a token create transaction with the given properties.
* @param token The properties of the token to create.
* @returns {TokenCreateTransaction} The token create transaction.
*/
static getTokenCreateTransaction(token) {
this.validateTokenProperties(token);
const transaction = new sdk_1.TokenCreateTransaction();
this.setRequiredProperties(transaction, token);
this.setKeyProperties(transaction, token);
this.setOptionalProperties(transaction, token);
return transaction;
}
/**
* Sets the required properties of the token create transaction.
* @param transaction The transaction to set the properties on.
* @param token The properties of the token to create.
*/
static setRequiredProperties(transaction, token) {
transaction.setTokenName(token.tokenName);
transaction.setTokenSymbol(token.tokenSymbol);
transaction.setTreasuryAccountId(this.getTreasuryAccountId(token));
transaction.setSupplyKey(this.getSupplyKey(token));
// If not provided, the TokenType is FUNGIBLE_COMMON by default
if (token.tokenType === sdk_1.TokenType.NonFungibleUnique.toString()) {
transaction.setTokenType(sdk_1.TokenType.NonFungibleUnique);
transaction.setInitialSupply(0);
}
else {
transaction.setTokenType(sdk_1.TokenType.FungibleCommon);
if (token.initialSupply) {
transaction.setInitialSupply(token.initialSupply);
}
if (token.decimals) {
transaction.setDecimals(token.decimals);
}
}
// If not provided, the TokenSupplyType is INFINITE by default
if (token.supplyType === sdk_1.TokenSupplyType.Finite.toString()) {
transaction.setSupplyType(sdk_1.TokenSupplyType.Finite);
if (token.maxSupply) {
transaction.setMaxSupply(token.maxSupply);
}
}
else {
transaction.setSupplyType(sdk_1.TokenSupplyType.Infinite);
}
}
/**
* Sets the key properties of the token create transaction.
* @param transaction The transaction to set the properties on.
* @param token The properties of the token to create.
*/
static setKeyProperties(transaction, token) {
if (token.adminKey) {
transaction.setAdminKey((0, IPrivateKey_1.getPrivateKey)(token.adminKey));
}
if (token.kycKey) {
transaction.setKycKey((0, IPrivateKey_1.getPrivateKey)(token.kycKey));
}
if (token.freezeKey) {
transaction.setFreezeKey((0, IPrivateKey_1.getPrivateKey)(token.freezeKey));
}
if (token.pauseKey) {
transaction.setPauseKey((0, IPrivateKey_1.getPrivateKey)(token.pauseKey));
}
if (token.wipeKey) {
transaction.setWipeKey((0, IPrivateKey_1.getPrivateKey)(token.wipeKey));
}
if (token.feeScheduleKey) {
transaction.setFeeScheduleKey((0, IPrivateKey_1.getPrivateKey)(token.feeScheduleKey));
}
}
/**
* Sets the optional properties of the token create transaction.
* @param transaction The transaction to set the properties on.
* @param token The properties of the token to create.
*/
static setOptionalProperties(transaction, token) {
if (token.freezeDefault !== undefined) {
transaction.setFreezeDefault(token.freezeDefault);
}
if (token.autoRenewAccountId) {
transaction.setAutoRenewAccountId(token.autoRenewAccountId);
}
if (token.expirationTime) {
transaction.setExpirationTime(new Date(token.expirationTime));
}
if (token.autoRenewPeriod) {
transaction.setAutoRenewPeriod(token.autoRenewPeriod);
}
if (token.tokenMemo) {
transaction.setTokenMemo(token.tokenMemo);
}
if (token.customFees) {
// TODO: Test this
transaction.setCustomFees(token.customFees.map(sdk_1.CustomFee._fromProtobuf));
}
}
static validateTokenProperties(token) {
this.assertTruthy(token.tokenName, 'Token name is required');
this.assertTruthy(token.tokenSymbol, 'Token symbol is required');
this.assertTruthy(token.tokenType, 'Token type is required');
this.assertTruthy(token.supplyType, 'Supply type is required');
// If the token type is NON_FUNGIBLE_UNIQUE,
// the initial supply must be 0 and decimals must be undefined
if (token.tokenType === sdk_1.TokenType.NonFungibleUnique.toString()) {
this.assertFalsy(token.initialSupply, 'Initial supply must be 0 or undefined for non-fungible tokens');
this.assertFalsy(token.decimals, 'Decimals must be 0 or undefined for non-fungible tokens');
}
else {
this.assertTruthy(token.initialSupply, 'Initial supply is required for fungible tokens');
this.assertTruthy(token.decimals, 'Decimals is required for fungible tokens');
}
// If the token supply type is FINITE, the max supply must be provided
if (token.supplyType === sdk_1.TokenSupplyType.Finite.toString()) {
this.assertTruthy(token.maxSupply, 'Max supply is required for finite supply tokens');
}
else {
this.assertFalsy(token.maxSupply, `Max supply must be undefined for infinite supply tokens, was ${token.maxSupply}`);
}
if (token.autoRenewPeriod) {
this.assertTruthy(token.autoRenewAccountId, 'Auto renew account ID is required for auto renew period');
this.assertInRange(token.autoRenewPeriod, 2592000, 8000000, 'Auto renew period must be between 30 days and 3 months');
}
}
static assertTruthy(condition, errorMessage) {
if (!condition) {
throw new Error(errorMessage);
}
}
static assertFalsy(condition, errorMessage) {
if (condition) {
throw new Error(errorMessage);
}
}
static assertInRange(value, min, max, errorMessage) {
if (value < min || value > max) {
throw new Error(errorMessage);
}
}
}
exports.TokenUtils = TokenUtils;
//# sourceMappingURL=TokenUtils.js.map