Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plugins/batch/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const batchPluginManifest: PluginManifest = {
'token-create-ft-from-file-batch-state',
'token-create-nft-batch-state',
'token-create-nft-from-file-batch-state',
'token-associate-batch-state',
],
options: [
{
Expand Down
83 changes: 83 additions & 0 deletions src/plugins/token/hooks/batch-associate/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { CommandHandlerArgs, CoreApi, Logger } from '@/core';
import type {
HookResult,
PreOutputPreparationParams,
} from '@/core/hooks/types';
import type {
BatchDataItem,
BatchExecuteTransactionResult,
} from '@/core/types/shared.types';

import { AbstractHook } from '@/core/hooks/abstract-hook';
import { TOKEN_ASSOCIATE_COMMAND_NAME } from '@/plugins/token/commands/associate';
import { saveAssociationToState } from '@/plugins/token/utils/token-associations';
import { ZustandTokenStateHelper } from '@/plugins/token/zustand-state-helper';

import { AssociateNormalizedParamsSchema } from './types';

export class TokenAssociateBatchStateHook 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_ASSOCIATE_COMMAND_NAME,
)) {
this.saveAssociations(api, logger, batchDataItem);
}
return Promise.resolve({
breakFlow: false,
result: {
message: 'success',
},
});
}

private saveAssociations(
api: CoreApi,
logger: Logger,
batchDataItem: BatchDataItem,
): void {
const parseResult = AssociateNormalizedParamsSchema.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;

if (normalisedParams.alreadyAssociated) {
logger.debug(
`Skipping already associated token ${normalisedParams.tokenId} for account ${normalisedParams.account.accountId}`,
);
return;
}

const tokenState = new ZustandTokenStateHelper(api.state, logger);
saveAssociationToState(
tokenState,
normalisedParams.tokenId,
normalisedParams.account.accountId,
normalisedParams.network,
logger,
);
}
}
1 change: 1 addition & 0 deletions src/plugins/token/hooks/batch-associate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { TokenAssociateBatchStateHook } from './handler';
18 changes: 18 additions & 0 deletions src/plugins/token/hooks/batch-associate/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { z } from 'zod';

import { NetworkSchema } from '@/core/schemas/common-schemas';
import { keyManagerNameSchema } from '@/core/services/kms/kms-types.interface';

const ResolvedAccountCredentialSchema = z.object({
keyRefId: z.string(),
accountId: z.string(),
publicKey: z.string(),
});

export const AssociateNormalizedParamsSchema = z.object({
network: NetworkSchema,
tokenId: z.string(),
account: ResolvedAccountCredentialSchema,
keyManager: keyManagerNameSchema,
alreadyAssociated: z.boolean(),
});
6 changes: 6 additions & 0 deletions src/plugins/token/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import {
tokenView,
TokenViewOutputSchema,
} from './commands/view';
import { TokenAssociateBatchStateHook } from './hooks/batch-associate';
import { TokenCreateFtBatchStateHook } from './hooks/batch-create-ft';
import { TokenCreateFtFromFileBatchStateHook } from './hooks/batch-create-ft-from-file';
import { TokenCreateNftBatchStateHook } from './hooks/batch-create-nft';
Expand Down Expand Up @@ -103,6 +104,11 @@ export const tokenPluginManifest: PluginManifest = {
hook: new TokenCreateNftFromFileBatchStateHook(),
options: [],
},
{
name: 'token-associate-batch-state',
hook: new TokenAssociateBatchStateHook(),
options: [],
},
],
commands: [
{
Expand Down
Loading