From 16a0f037e071e6e65d6476da27d3cbc4bc7c0577 Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Sun, 19 Feb 2023 13:42:07 +0300 Subject: [PATCH] Init actions.ts for Badge Actions --- components/badges/actions/actions.ts | 273 +++++++++++++++++++++++++++ 1 file changed, 273 insertions(+) create mode 100644 components/badges/actions/actions.ts diff --git a/components/badges/actions/actions.ts b/components/badges/actions/actions.ts new file mode 100644 index 0000000..f4ae7c1 --- /dev/null +++ b/components/badges/actions/actions.ts @@ -0,0 +1,273 @@ +import type { Badge, BadgeHubInstance, Metadata } from 'contracts/badgeHub' +import { useBadgeHubContract } from 'contracts/badgeHub' + +export type ActionType = typeof ACTION_TYPES[number] + +export const ACTION_TYPES = [ + 'create_badge', + 'edit_badge', + 'add_keys', + 'purge_keys', + 'purge_owners', + 'mint_by_minter', + 'mint_by_key', + 'mint_by_keys', + 'set_nft', +] as const + +export interface ActionListItem { + id: ActionType + name: string + description?: string +} + +export const BY_KEY_ACTION_LIST: ActionListItem[] = [ + { + id: 'create_badge', + name: 'Create Badge', + description: `Create a new badge with the specified mint rule and metadata`, + }, + { + id: 'edit_badge', + name: 'Edit Badge', + description: `Edit the badge with the specified ID`, + }, + { + id: 'add_keys', + name: 'Add Keys', + description: `Add keys to the badge with the specified ID`, + }, + { + id: 'purge_keys', + name: 'Purge Keys', + description: `Purge keys from the badge with the specified ID`, + }, + { + id: 'purge_owners', + name: 'Purge Owners', + description: `Purge owners from the badge with the specified ID`, + }, + { + id: 'mint_by_minter', + name: 'Mint by Minter', + description: `Mint a new token by the minter with the specified ID`, + }, + { + id: 'mint_by_key', + name: 'Mint by Key', + description: `Mint a new token by the key with the specified ID`, + }, + { + id: 'mint_by_keys', + name: 'Mint by Keys', + description: `Mint a new token by the keys with the specified ID`, + }, + { + id: 'set_nft', + name: 'Set NFT', + description: `Set the Badge NFT contract address for the Badge Hub contract`, + }, +] + +export const BY_KEYS_ACTION_LIST: ActionListItem[] = [ + { + id: 'create_badge', + name: 'Create Badge', + description: `Create a new badge with the specified mint rule and metadata`, + }, + { + id: 'edit_badge', + name: 'Edit Badge', + description: `Edit the badge with the specified ID`, + }, + { + id: 'add_keys', + name: 'Add Keys', + description: `Add keys to the badge with the specified ID`, + }, + { + id: 'purge_keys', + name: 'Purge Keys', + description: `Purge keys from the badge with the specified ID`, + }, + { + id: 'purge_owners', + name: 'Purge Owners', + description: `Purge owners from the badge with the specified ID`, + }, + { + id: 'mint_by_minter', + name: 'Mint by Minter', + description: `Mint a new token by the minter with the specified ID`, + }, + { + id: 'mint_by_key', + name: 'Mint by Key', + description: `Mint a new token by the key with the specified ID`, + }, + { + id: 'mint_by_keys', + name: 'Mint by Keys', + description: `Mint a new token by the keys with the specified ID`, + }, + { + id: 'set_nft', + name: 'Set NFT', + description: `Set the Badge NFT contract address for the Badge Hub contract`, + }, +] + +export const BY_MINTER_ACTION_LIST: ActionListItem[] = [ + { + id: 'create_badge', + name: 'Create Badge', + description: `Create a new badge with the specified mint rule and metadata`, + }, + { + id: 'edit_badge', + name: 'Edit Badge', + description: `Edit the badge with the specified ID`, + }, + { + id: 'add_keys', + name: 'Add Keys', + description: `Add keys to the badge with the specified ID`, + }, + { + id: 'purge_keys', + name: 'Purge Keys', + description: `Purge keys from the badge with the specified ID`, + }, + { + id: 'purge_owners', + name: 'Purge Owners', + description: `Purge owners from the badge with the specified ID`, + }, + { + id: 'mint_by_minter', + name: 'Mint by Minter', + description: `Mint a new token by the minter with the specified ID`, + }, + { + id: 'mint_by_key', + name: 'Mint by Key', + description: `Mint a new token by the key with the specified ID`, + }, + { + id: 'mint_by_keys', + name: 'Mint by Keys', + description: `Mint a new token by the keys with the specified ID`, + }, + { + id: 'set_nft', + name: 'Set NFT', + description: `Set the Badge NFT contract address for the Badge Hub contract`, + }, +] + +export interface DispatchExecuteProps { + type: ActionType + [k: string]: unknown +} + +type Select = T + +/** @see {@link BadgeHubInstance}*/ +export type DispatchExecuteArgs = { + badgeHubContract: string + badgeHubMessages?: BadgeHubInstance + txSigner: string +} & ( + | { type: undefined } + | { type: Select<'create_badge'>; badge: Badge } + | { type: Select<'edit_badge'>; id: number; metadata: Metadata } + | { type: Select<'add_keys'>; id: number; keys: string[] } + | { type: Select<'purge_keys'>; id: number; limit?: number } + | { type: Select<'purge_owners'>; id: number; limit?: number } + | { type: Select<'mint_by_minter'>; id: number; owners: string[] } + | { type: Select<'mint_by_key'>; id: number; owner: string; signature: string } + | { type: Select<'mint_by_keys'>; id: number; owner: string; pubkey: string; signature: string } + | { type: Select<'set_nft'>; nft: string } +) + +export const dispatchExecute = async (args: DispatchExecuteArgs) => { + const { badgeHubMessages, txSigner } = args + if (!badgeHubMessages) { + throw new Error('Cannot execute actions') + } + switch (args.type) { + case 'create_badge': { + return badgeHubMessages.createBadge(txSigner, args.badge) + } + case 'edit_badge': { + return badgeHubMessages.editBadge(txSigner, args.id, args.metadata) + } + case 'add_keys': { + return badgeHubMessages.addKeys(txSigner, args.id, args.keys) + } + case 'purge_keys': { + return badgeHubMessages.purgeKeys(txSigner, args.id, args.limit) + } + case 'purge_owners': { + return badgeHubMessages.purgeOwners(txSigner, args.id, args.limit) + } + case 'mint_by_minter': { + return badgeHubMessages.mintByMinter(txSigner, args.id, args.owners) + } + case 'mint_by_key': { + return badgeHubMessages.mintByKey(txSigner, args.id, args.owner, args.signature) + } + case 'mint_by_keys': { + return badgeHubMessages.mintByKeys(txSigner, args.id, args.owner, args.pubkey, args.signature) + } + case 'set_nft': { + return badgeHubMessages.setNft(txSigner, args.nft) + } + default: { + throw new Error('Unknown action') + } + } +} + +export const previewExecutePayload = (args: DispatchExecuteArgs) => { + // eslint-disable-next-line react-hooks/rules-of-hooks + const { messages: badgeHubMessages } = useBadgeHubContract() + + const { badgeHubContract } = args + switch (args.type) { + case 'create_badge': { + return badgeHubMessages(badgeHubContract)?.createBadge(args.badge) + } + case 'edit_badge': { + return badgeHubMessages(badgeHubContract)?.editBadge(args.id, args.metadata) + } + case 'add_keys': { + return badgeHubMessages(badgeHubContract)?.addKeys(args.id, args.keys) + } + case 'purge_keys': { + return badgeHubMessages(badgeHubContract)?.purgeKeys(args.id, args.limit) + } + case 'purge_owners': { + return badgeHubMessages(badgeHubContract)?.purgeOwners(args.id, args.limit) + } + case 'mint_by_minter': { + return badgeHubMessages(badgeHubContract)?.mintByMinter(args.id, args.owners) + } + case 'mint_by_key': { + return badgeHubMessages(badgeHubContract)?.mintByKey(args.id, args.owner, args.signature) + } + case 'mint_by_keys': { + return badgeHubMessages(badgeHubContract)?.mintByKeys(args.id, args.owner, args.pubkey, args.signature) + } + case 'set_nft': { + return badgeHubMessages(badgeHubContract)?.setNft(args.nft) + } + default: { + return {} + } + } +} + +export const isEitherType = (type: unknown, arr: T[]): type is T => { + return arr.some((val) => type === val) +}