Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 2x 8x 8x 8x 8x 11x 8x 3x 10x 7x 3x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 3x 3x 5x 5x 5x 5x 5x 8x 5x 5x 5x 3x 8x 2x 8x 6x 8x 6x 8x 6x 8x 6x 8x 6x 8x 6x 8x 6x 8x 6x 8x 6x 8x 3x 8x 6x 8x 8x 8x 8x 8x 3x 3x 5x 5x 8x 5x 3x 8x 6x 6x 53x 9x 6x | // SPDX-License-Identifier: Apache-2.0
import {
AccountId,
Client,
CustomFee,
PrivateKey,
TokenAssociateTransaction,
TokenCreateTransaction,
TokenId,
TokenMintTransaction,
TokenSupplyType,
TokenType,
TransactionReceipt
} from '@hashgraph/sdk';
import { ITokenProps } from '../configuration/types/ITokenProps';
import { getPrivateKey } from '../configuration/types/IPrivateKey';
/**
* Provides utility methods for working with tokens.
*/
export 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.
*/
public static async associateAccountWithTokens(accountId: AccountId,
tokenIds: TokenId[],
accountKey: PrivateKey,
client: Client): Promise<void> {
const signTx = await new TokenAssociateTransaction()
.setAccountId(accountId)
.setTokenIds(tokenIds)
.freezeWith(client)
.sign(accountKey);
const txResponse = await signTx.execute(client);
await 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.
*/
public static async mintToken(tokenId: TokenId,
CID: string,
supplyKey: PrivateKey,
client: Client): Promise<TransactionReceipt> {
const transaction = new TokenMintTransaction()
.setTokenId(tokenId)
.setMetadata([Buffer.from(CID)])
.freezeWith(client);
const signTx = await transaction.sign(supplyKey);
const txResponse = await 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.
*/
public static async createToken(token: ITokenProps, client: Client): Promise<TokenId> {
const transaction = this.getTokenCreateTransaction(token);
let signTx: TokenCreateTransaction = transaction.freezeWith(client);
if (token.adminKey) {
signTx = await signTx.sign(getPrivateKey(token.adminKey));
}
signTx = await signTx.signWithOperator(client);
const txResponse = await signTx.execute(client);
const receipt = await 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.
*/
public static getSupplyKey(token: ITokenProps): PrivateKey {
// The operator key will be used as supply key if one is not provided
if (token.supplyKey) {
return getPrivateKey(token.supplyKey);
}
return 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.
*/
public static getTreasuryAccountId(token: ITokenProps): AccountId {
// The operator key will be used as treasury key if one is not provided
if (token.treasuryKey) {
return getPrivateKey(token.treasuryKey).publicKey.toAccountId(0, 0);
}
return 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.
*/
private static getTokenCreateTransaction(token: ITokenProps): TokenCreateTransaction {
this.validateTokenProperties(token);
const transaction = new 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.
*/
private static setRequiredProperties(transaction: TokenCreateTransaction, token: ITokenProps): void {
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 === TokenType.NonFungibleUnique.toString()) {
transaction.setTokenType(TokenType.NonFungibleUnique);
transaction.setInitialSupply(0);
} else {
transaction.setTokenType(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 === TokenSupplyType.Finite.toString()) {
transaction.setSupplyType(TokenSupplyType.Finite);
if (token.maxSupply) {
transaction.setMaxSupply(token.maxSupply);
}
} else {
transaction.setSupplyType(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.
*/
private static setKeyProperties(transaction: TokenCreateTransaction, token: ITokenProps): void {
if (token.adminKey) {
transaction.setAdminKey(getPrivateKey(token.adminKey));
}
if (token.kycKey) {
transaction.setKycKey(getPrivateKey(token.kycKey));
}
if (token.freezeKey) {
transaction.setFreezeKey(getPrivateKey(token.freezeKey));
}
if (token.pauseKey) {
transaction.setPauseKey(getPrivateKey(token.pauseKey));
}
if (token.wipeKey) {
transaction.setWipeKey(getPrivateKey(token.wipeKey));
}
if (token.feeScheduleKey) {
transaction.setFeeScheduleKey(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.
*/
private static setOptionalProperties(transaction: TokenCreateTransaction, token: ITokenProps): void {
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(CustomFee._fromProtobuf));
}
}
private static validateTokenProperties(token: ITokenProps): void {
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 === 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 === 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, 2_592_000, 8_000_000, 'Auto renew period must be between 30 days and 3 months');
}
}
private static assertTruthy(condition: unknown, errorMessage: string): void {
Iif (!condition) {
throw new Error(errorMessage);
}
}
private static assertFalsy(condition: unknown, errorMessage: string): void {
Iif (condition) {
throw new Error(errorMessage);
}
}
private static assertInRange(value: number, min: number, max: number, errorMessage: string): void {
Iif (value < min || value > max) {
throw new Error(errorMessage);
}
}
}
|