-
Notifications
You must be signed in to change notification settings - Fork 33
feat(1595): added token create from file #1602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mmyslblocky
merged 16 commits into
main
from
feat/1595-implement-token-create-ft-from-file-batch-state-hook
Mar 16, 2026
+210
−11
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
cab40bd
feat(1585): implement account create batch state hook
mmyslblocky bb3f724
feat(1585): change async call
mmyslblocky c5b79d7
feat(1586): change async call
mmyslblocky 7d2adf2
feat(1585): removed kms from receipt, as we do not need operator for …
mmyslblocky 7d1bd4b
feat(1585): changes from review
mmyslblocky 85d53fb
feat(1585): deleted sort
mmyslblocky ec4ff74
feat(1585): PR remarks
mmyslblocky 444f6b0
feat(1585): deleted token file batch state hook from this PR
mmyslblocky 5e9f37b
Merge branch 'feat/1585-implement-account-create-batch-state-hook' of…
mmyslblocky 67129d8
feat(1585): removed types as they are in shared.types
mmyslblocky 90b195a
Merge branch 'main' into feat/1586-implement-topic-create-batch-state…
mmyslblocky 71f3c87
feat(1587): token create ft batch state hook
mmyslblocky a83a284
feat(1587): resolved conflicts
mmyslblocky 2b03ad7
feat(1595): added token create from file
mmyslblocky 626b5f7
feat(1595): conflict resolved
mmyslblocky a7e07cf
feat(1595): removed unused types
mmyslblocky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
133 changes: 133 additions & 0 deletions
133
src/plugins/token/hooks/batch-create-ft-from-file/handler.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import type { CommandHandlerArgs, CoreApi, Logger } from '@/core'; | ||
| import type { | ||
| HookResult, | ||
| PreOutputPreparationParams, | ||
| } from '@/core/hooks/types'; | ||
| import type { Credential } from '@/core/services/kms/kms-types.interface'; | ||
| import type { | ||
| BatchDataItem, | ||
| BatchExecuteTransactionResult, | ||
| TransactionResult, | ||
| } from '@/core/types/shared.types'; | ||
|
|
||
| import { StateError } from '@/core'; | ||
| import { AbstractHook } from '@/core/hooks/abstract-hook'; | ||
| import { AliasType } from '@/core/services/alias/alias-service.interface'; | ||
| import { composeKey } from '@/core/utils/key-composer'; | ||
| import { TOKEN_CREATE_FT_FROM_FILE_COMMAND_NAME } from '@/plugins/token/commands/create-ft-from-file'; | ||
| import { processTokenAssociations } from '@/plugins/token/utils/token-associations'; | ||
| import { buildTokenDataFromFile } from '@/plugins/token/utils/token-data-builders'; | ||
| import { ZustandTokenStateHelper } from '@/plugins/token/zustand-state-helper'; | ||
|
|
||
| import { CreateFtFromFileNormalizedParamsSchema } from './types'; | ||
|
|
||
| export class TokenCreateFtFromFileBatchStateHook extends AbstractHook { | ||
| override async preOutputPreparationHook( | ||
| args: CommandHandlerArgs, | ||
| params: PreOutputPreparationParams< | ||
| unknown, | ||
| unknown, | ||
| unknown, | ||
| BatchExecuteTransactionResult | ||
| >, | ||
| ): Promise<HookResult> { | ||
| const { api, logger } = args; | ||
| const batchData = params.executeTransactionResult.updatedBatchData; | ||
| if (!batchData.success) { | ||
| return Promise.resolve({ | ||
| breakFlow: false, | ||
| result: { | ||
| message: 'Batch transaction status failure', | ||
| }, | ||
| }); | ||
| } | ||
| for (const batchDataItem of [...batchData.transactions].filter( | ||
| (item) => item.command === TOKEN_CREATE_FT_FROM_FILE_COMMAND_NAME, | ||
| )) { | ||
| await this.saveToken(api, logger, batchDataItem); | ||
| } | ||
| return Promise.resolve({ | ||
| breakFlow: false, | ||
| result: { | ||
| message: 'success', | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| private async saveToken( | ||
| api: CoreApi, | ||
| logger: Logger, | ||
| batchDataItem: BatchDataItem, | ||
| ): Promise<void> { | ||
| const parseResult = CreateFtFromFileNormalizedParamsSchema.safeParse( | ||
| batchDataItem.normalizedParams, | ||
| ); | ||
| if (!parseResult.success) { | ||
| logger.warn( | ||
| `There was a problem with parsing data schema. The saving will not be done`, | ||
| ); | ||
| return; | ||
| } | ||
| const normalisedParams = parseResult.data; | ||
| const innerTransactionId = batchDataItem.transactionId; | ||
| if (!innerTransactionId) { | ||
| logger.warn( | ||
| `No transaction ID found for batch transaction ${batchDataItem.order}`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const innerTransactionResult: TransactionResult = | ||
| await api.receipt.getReceipt({ | ||
| transactionId: innerTransactionId, | ||
| }); | ||
|
|
||
| if (!innerTransactionResult.tokenId) { | ||
| throw new StateError( | ||
| 'Transaction completed but did not return a token ID', | ||
| { context: { transactionId: innerTransactionResult.transactionId } }, | ||
| ); | ||
| } | ||
|
|
||
| const tokenData = buildTokenDataFromFile( | ||
| innerTransactionResult, | ||
| normalisedParams.tokenDefinition, | ||
| normalisedParams.treasury.accountId, | ||
| normalisedParams.adminKey.publicKey, | ||
| normalisedParams.network, | ||
| { | ||
| supplyPublicKey: normalisedParams.supplyKey?.publicKey, | ||
| wipePublicKey: normalisedParams.wipeKey?.publicKey, | ||
| kycPublicKey: normalisedParams.kycKey?.publicKey, | ||
| freezePublicKey: normalisedParams.freezeKey?.publicKey, | ||
| pausePublicKey: normalisedParams.pauseKey?.publicKey, | ||
| feeSchedulePublicKey: normalisedParams.feeScheduleKey?.publicKey, | ||
| }, | ||
| ); | ||
|
|
||
| tokenData.associations = await processTokenAssociations( | ||
| innerTransactionResult.tokenId, | ||
| normalisedParams.tokenDefinition.associations as Credential[], | ||
| api, | ||
| logger, | ||
| normalisedParams.keyManager, | ||
| ); | ||
|
|
||
| const key = composeKey( | ||
| normalisedParams.network, | ||
| innerTransactionResult.tokenId, | ||
| ); | ||
| const tokenState = new ZustandTokenStateHelper(api.state, logger); | ||
| tokenState.saveToken(key, tokenData); | ||
| logger.info(' Token data saved to state'); | ||
|
|
||
| api.alias.register({ | ||
| alias: normalisedParams.tokenDefinition.name, | ||
| type: AliasType.Token, | ||
| network: normalisedParams.network, | ||
| entityId: innerTransactionResult.tokenId, | ||
| createdAt: innerTransactionResult.consensusTimestamp, | ||
| }); | ||
| logger.info(` Name registered: ${normalisedParams.tokenDefinition.name}`); | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { TokenCreateFtFromFileBatchStateHook } from './handler'; |
56 changes: 56 additions & 0 deletions
56
src/plugins/token/hooks/batch-create-ft-from-file/types.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| import { | ||
| HtsDecimalsSchema, | ||
| KeySchema, | ||
| MemoSchema, | ||
| NetworkSchema, | ||
| PrivateKeySchema, | ||
| PrivateKeyWithAccountIdSchema, | ||
| ResolvedAccountCredentialSchema, | ||
| ResolvedPublicKeySchema, | ||
| TinybarSchema, | ||
| TokenNameSchema, | ||
| TokenSymbolSchema, | ||
| TokenTypeSchema, | ||
| } from '@/core/schemas/common-schemas'; | ||
| import { keyManagerNameSchema } from '@/core/services/kms/kms-types.interface'; | ||
| import { TokenFileCustomFeeSchema } from '@/plugins/token/schema'; | ||
|
|
||
| export const FungibleTokenFileSchema = z.object({ | ||
| name: TokenNameSchema, | ||
| symbol: TokenSymbolSchema, | ||
| decimals: HtsDecimalsSchema, | ||
| supplyType: z.union([z.literal('finite'), z.literal('infinite')]), | ||
| initialSupply: TinybarSchema, | ||
| maxSupply: TinybarSchema, | ||
| treasuryKey: PrivateKeyWithAccountIdSchema, | ||
| adminKey: PrivateKeySchema, | ||
| supplyKey: KeySchema.optional(), | ||
| wipeKey: KeySchema.optional(), | ||
| kycKey: KeySchema.optional(), | ||
| freezeKey: KeySchema.optional(), | ||
| pauseKey: KeySchema.optional(), | ||
| feeScheduleKey: KeySchema.optional(), | ||
| associations: z.array(PrivateKeyWithAccountIdSchema).default([]), | ||
| customFees: z | ||
| .array(TokenFileCustomFeeSchema) | ||
| .max(10, 'Maximum 10 custom fees allowed per token') | ||
| .default([]), | ||
| memo: MemoSchema.default(''), | ||
| tokenType: TokenTypeSchema, | ||
| }); | ||
|
|
||
| export const CreateFtFromFileNormalizedParamsSchema = z.object({ | ||
| keyManager: keyManagerNameSchema, | ||
| tokenDefinition: FungibleTokenFileSchema, | ||
| network: NetworkSchema, | ||
| treasury: ResolvedAccountCredentialSchema, | ||
| adminKey: ResolvedPublicKeySchema, | ||
| supplyKey: ResolvedPublicKeySchema.optional(), | ||
| wipeKey: ResolvedPublicKeySchema.optional(), | ||
| kycKey: ResolvedPublicKeySchema.optional(), | ||
| freezeKey: ResolvedPublicKeySchema.optional(), | ||
| pauseKey: ResolvedPublicKeySchema.optional(), | ||
| feeScheduleKey: ResolvedPublicKeySchema.optional(), | ||
| }); |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import type { CommandHandlerArgs, CoreApi, Logger } from '@/core'; | ||
| import type { | ||
| HookResult, | ||
| PreOutputPreparationParams, | ||
| } from '@/core/hooks/types'; | ||
| import type { | ||
| BatchDataItem, | ||
| BatchExecuteTransactionResult, | ||
| TransactionResult, | ||
| } from '@/core/types/shared.types'; | ||
|
|
||
| import { StateError } from '@/core'; | ||
| import { AbstractHook } from '@/core/hooks/abstract-hook'; | ||
| import { AliasType } from '@/core/services/alias/alias-service.interface'; | ||
| import { composeKey } from '@/core/utils/key-composer'; | ||
| import { TOKEN_CREATE_FT_COMMAND_NAME } from '@/plugins/token/commands/create-ft'; | ||
| import { buildTokenData } from '@/plugins/token/utils/token-data-builders'; | ||
| import { ZustandTokenStateHelper } from '@/plugins/token/zustand-state-helper'; | ||
|
|
||
| import { CreateFtNormalizedParamsSchema } from './types'; | ||
|
|
||
| export class TokenCreateFtBatchStateHook extends AbstractHook { | ||
| override async preOutputPreparationHook( | ||
| args: CommandHandlerArgs, | ||
| params: PreOutputPreparationParams< | ||
| unknown, | ||
| unknown, | ||
| unknown, | ||
| BatchExecuteTransactionResult | ||
| >, | ||
| ): Promise<HookResult> { | ||
| const { api, logger } = args; | ||
| const batchData = params.executeTransactionResult.updatedBatchData; | ||
| await Promise.all( | ||
| [...batchData.transactions] | ||
| .filter((item) => item.command === TOKEN_CREATE_FT_COMMAND_NAME) | ||
| .map((batchDataItem) => this.saveCreateFt(api, logger, batchDataItem)), | ||
| ); | ||
| return Promise.resolve({ | ||
| breakFlow: false, | ||
| result: { | ||
| message: 'success', | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| private async saveCreateFt( | ||
| api: CoreApi, | ||
| logger: Logger, | ||
| batchDataItem: BatchDataItem, | ||
| ): Promise<void> { | ||
| const parseResult = CreateFtNormalizedParamsSchema.safeParse( | ||
| batchDataItem.normalizedParams, | ||
| ); | ||
| if (!parseResult.success) { | ||
| logger.warn( | ||
| `There was a problem with parsing data schema. The saving will not be done`, | ||
| ); | ||
| return; | ||
| } | ||
| const normalisedParams = parseResult.data; | ||
| const innerTransactionId = batchDataItem.transactionId; | ||
| if (!innerTransactionId) { | ||
| logger.warn( | ||
| `No transaction ID found for batch transaction ${batchDataItem.order}`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const innerTransactionResult: TransactionResult = | ||
| await api.receipt.getReceipt({ | ||
| transactionId: innerTransactionId, | ||
| }); | ||
|
|
||
| if (!innerTransactionResult.tokenId) { | ||
| throw new StateError( | ||
| 'Transaction completed but did not return a token ID', | ||
| { context: { transactionId: innerTransactionResult.transactionId } }, | ||
| ); | ||
| } | ||
|
|
||
| const tokenData = buildTokenData(innerTransactionResult, { | ||
| name: normalisedParams.name, | ||
| symbol: normalisedParams.symbol, | ||
| treasuryId: normalisedParams.treasury.accountId, | ||
| decimals: normalisedParams.decimals, | ||
| initialSupply: normalisedParams.initialSupply, | ||
| tokenType: normalisedParams.tokenType, | ||
| supplyType: normalisedParams.supplyType, | ||
| adminPublicKey: normalisedParams.admin.publicKey, | ||
| supplyPublicKey: normalisedParams.supply?.publicKey, | ||
| network: normalisedParams.network, | ||
| }); | ||
|
|
||
| const key = composeKey( | ||
| normalisedParams.network, | ||
| innerTransactionResult.tokenId, | ||
| ); | ||
| const tokenState = new ZustandTokenStateHelper(api.state, logger); | ||
| tokenState.saveToken(key, tokenData); | ||
| logger.info(' Token data saved to state'); | ||
|
|
||
| if (normalisedParams.alias) { | ||
| api.alias.register({ | ||
| alias: normalisedParams.alias, | ||
| type: AliasType.Token, | ||
| network: normalisedParams.network, | ||
| entityId: innerTransactionResult.tokenId, | ||
| createdAt: innerTransactionResult.consensusTimestamp, | ||
| }); | ||
| logger.info(` Name registered: ${normalisedParams.alias}`); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { TokenCreateFtBatchStateHook } from './handler'; |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| import { | ||
| NetworkSchema, | ||
| SupplyTypeSchema, | ||
| TinybarSchema, | ||
| } from '@/core/schemas/common-schemas'; | ||
| import { keyManagerNameSchema } from '@/core/services/kms/kms-types.interface'; | ||
| import { HederaTokenType } from '@/core/shared/constants'; | ||
|
|
||
| const ResolvedAccountCredentialSchema = z.object({ | ||
| keyRefId: z.string(), | ||
| accountId: z.string(), | ||
| publicKey: z.string(), | ||
| }); | ||
|
|
||
| const ResolvedPublicKeySchema = z.object({ | ||
| keyRefId: z.string(), | ||
| publicKey: z.string(), | ||
| }); | ||
|
|
||
| export const CreateFtNormalizedParamsSchema = z.object({ | ||
| name: z.string(), | ||
| symbol: z.string(), | ||
| decimals: z.number(), | ||
| initialSupply: TinybarSchema, | ||
| supplyType: SupplyTypeSchema, | ||
| alias: z.string().optional(), | ||
| memo: z.string().optional(), | ||
| tokenType: z.enum([ | ||
| HederaTokenType.NON_FUNGIBLE_TOKEN, | ||
| HederaTokenType.FUNGIBLE_COMMON, | ||
| ]), | ||
| network: NetworkSchema, | ||
| keyManager: keyManagerNameSchema, | ||
| treasury: ResolvedAccountCredentialSchema, | ||
| admin: ResolvedAccountCredentialSchema, | ||
| supply: ResolvedPublicKeySchema.optional(), | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.