2022-07-25 08:29:52 +00:00
|
|
|
import type { MinterInstance } from 'contracts/minter'
|
|
|
|
import { useMinterContract } from 'contracts/minter'
|
|
|
|
import type { SG721Instance } from 'contracts/sg721'
|
|
|
|
import { useSG721Contract } from 'contracts/sg721'
|
|
|
|
|
|
|
|
export type ActionType = typeof ACTION_TYPES[number]
|
|
|
|
|
|
|
|
export const ACTION_TYPES = [
|
|
|
|
'mint_to',
|
|
|
|
'mint_for',
|
2022-08-10 07:25:23 +00:00
|
|
|
'batch_mint',
|
2022-07-25 08:29:52 +00:00
|
|
|
'set_whitelist',
|
|
|
|
'update_start_time',
|
|
|
|
'update_per_address_limit',
|
|
|
|
'withdraw',
|
|
|
|
'transfer',
|
2022-08-16 07:18:35 +00:00
|
|
|
'burn',
|
|
|
|
'batch_burn',
|
2022-07-25 08:29:52 +00:00
|
|
|
'shuffle',
|
|
|
|
] as const
|
|
|
|
|
|
|
|
export interface ActionListItem {
|
|
|
|
id: ActionType
|
|
|
|
name: string
|
|
|
|
description?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ACTION_LIST: ActionListItem[] = [
|
|
|
|
{
|
|
|
|
id: 'mint_to',
|
|
|
|
name: 'Mint To',
|
|
|
|
description: `Mint a token to a user`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'mint_for',
|
|
|
|
name: 'Mint For',
|
|
|
|
description: `Mint a token for a user with given token ID`,
|
|
|
|
},
|
2022-08-10 07:25:23 +00:00
|
|
|
{
|
|
|
|
id: 'batch_mint',
|
|
|
|
name: 'Batch Mint',
|
|
|
|
description: `Mint multiple tokens to a user with given token amount`,
|
|
|
|
},
|
2022-07-25 08:29:52 +00:00
|
|
|
{
|
|
|
|
id: 'set_whitelist',
|
|
|
|
name: 'Set Whitelist',
|
|
|
|
description: `Set whitelist contract address`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'update_start_time',
|
|
|
|
name: 'Update Start Time',
|
|
|
|
description: `Update start time for minting`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'update_per_address_limit',
|
|
|
|
name: 'Update Tokens Per Address Limit',
|
|
|
|
description: `Update token per address limit`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'withdraw',
|
|
|
|
name: 'Withdraw Tokens',
|
|
|
|
description: `Withdraw tokens from the contract`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'transfer',
|
|
|
|
name: 'Transfer Tokens',
|
|
|
|
description: `Transfer tokens from one address to another`,
|
|
|
|
},
|
2022-08-16 07:18:35 +00:00
|
|
|
{
|
|
|
|
id: 'burn',
|
|
|
|
name: 'Burn Token',
|
|
|
|
description: `Burn a specified token from the collection`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'batch_burn',
|
|
|
|
name: 'Batch Burn Tokens',
|
|
|
|
description: `Burn a list of tokens from the collection`,
|
|
|
|
},
|
2022-07-25 08:29:52 +00:00
|
|
|
{
|
|
|
|
id: 'shuffle',
|
|
|
|
name: 'Shuffle Tokens',
|
|
|
|
description: 'Shuffle the token IDs',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
export interface DispatchExecuteProps {
|
|
|
|
type: ActionType
|
|
|
|
[k: string]: unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
type Select<T extends ActionType> = T
|
|
|
|
|
|
|
|
/** @see {@link MinterInstance} */
|
|
|
|
export type DispatchExecuteArgs = {
|
|
|
|
minterContract: string
|
|
|
|
sg721Contract: string
|
|
|
|
minterMessages?: MinterInstance
|
|
|
|
sg721Messages?: SG721Instance
|
|
|
|
txSigner: string
|
|
|
|
} & (
|
|
|
|
| { type: undefined }
|
|
|
|
| { type: Select<'mint_to'>; recipient: string }
|
|
|
|
| { type: Select<'mint_for'>; recipient: string; tokenId: number }
|
2022-08-10 07:25:23 +00:00
|
|
|
| { type: Select<'batch_mint'>; recipient: string; batchNumber: number }
|
2022-07-25 08:29:52 +00:00
|
|
|
| { type: Select<'set_whitelist'>; whitelist: string }
|
|
|
|
| { type: Select<'update_start_time'>; startTime: string }
|
|
|
|
| { type: Select<'update_per_address_limit'>; limit: number }
|
|
|
|
| { type: Select<'shuffle'> }
|
|
|
|
| { type: Select<'withdraw'> }
|
|
|
|
| { type: Select<'transfer'>; recipient: string; tokenId: number }
|
2022-08-16 07:18:35 +00:00
|
|
|
| { type: Select<'burn'>; tokenId: number }
|
|
|
|
| { type: Select<'batch_burn'>; tokenIds: string }
|
2022-07-25 08:29:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
export const dispatchExecute = async (args: DispatchExecuteArgs) => {
|
|
|
|
const { minterMessages, sg721Messages, txSigner } = args
|
|
|
|
if (!minterMessages || !sg721Messages) {
|
|
|
|
throw new Error('Cannot execute actions')
|
|
|
|
}
|
|
|
|
switch (args.type) {
|
|
|
|
case 'mint_to': {
|
|
|
|
return minterMessages.mintTo(txSigner, args.recipient)
|
|
|
|
}
|
|
|
|
case 'mint_for': {
|
|
|
|
return minterMessages.mintFor(txSigner, args.recipient, args.tokenId)
|
|
|
|
}
|
2022-08-10 07:25:23 +00:00
|
|
|
case 'batch_mint': {
|
|
|
|
return minterMessages.batchMint(txSigner, args.recipient, args.batchNumber)
|
|
|
|
}
|
2022-07-25 08:29:52 +00:00
|
|
|
case 'set_whitelist': {
|
|
|
|
return minterMessages.setWhitelist(txSigner, args.whitelist)
|
|
|
|
}
|
|
|
|
case 'update_start_time': {
|
|
|
|
return minterMessages.updateStartTime(txSigner, args.startTime)
|
|
|
|
}
|
|
|
|
case 'update_per_address_limit': {
|
|
|
|
return minterMessages.updatePerAddressLimit(txSigner, args.limit)
|
|
|
|
}
|
|
|
|
case 'shuffle': {
|
|
|
|
return minterMessages.shuffle(txSigner)
|
|
|
|
}
|
|
|
|
case 'withdraw': {
|
|
|
|
return minterMessages.withdraw(txSigner)
|
|
|
|
}
|
|
|
|
case 'transfer': {
|
|
|
|
return sg721Messages.transferNft(args.recipient, args.tokenId.toString())
|
|
|
|
}
|
2022-08-16 07:18:35 +00:00
|
|
|
case 'burn': {
|
|
|
|
return sg721Messages.burn(args.tokenId.toString())
|
|
|
|
}
|
|
|
|
case 'batch_burn': {
|
|
|
|
return sg721Messages.batchBurn(args.tokenIds)
|
|
|
|
}
|
2022-07-25 08:29:52 +00:00
|
|
|
default: {
|
|
|
|
throw new Error('Unknown action')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const previewExecutePayload = (args: DispatchExecuteArgs) => {
|
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
|
const { messages: minterMessages } = useMinterContract()
|
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
|
const { messages: sg721Messages } = useSG721Contract()
|
|
|
|
const { minterContract, sg721Contract } = args
|
|
|
|
switch (args.type) {
|
|
|
|
case 'mint_to': {
|
2022-08-16 07:18:35 +00:00
|
|
|
return minterMessages(minterContract)?.mintTo(args.recipient)
|
2022-07-25 08:29:52 +00:00
|
|
|
}
|
|
|
|
case 'mint_for': {
|
2022-08-16 07:18:35 +00:00
|
|
|
return minterMessages(minterContract)?.mintFor(args.recipient, args.tokenId)
|
2022-07-25 08:29:52 +00:00
|
|
|
}
|
2022-08-10 07:25:23 +00:00
|
|
|
case 'batch_mint': {
|
2022-08-16 07:18:35 +00:00
|
|
|
return minterMessages(minterContract)?.batchMint(args.recipient, args.batchNumber)
|
2022-08-10 07:25:23 +00:00
|
|
|
}
|
2022-07-25 08:29:52 +00:00
|
|
|
case 'set_whitelist': {
|
2022-08-16 07:18:35 +00:00
|
|
|
return minterMessages(minterContract)?.setWhitelist(args.whitelist)
|
2022-07-25 08:29:52 +00:00
|
|
|
}
|
|
|
|
case 'update_start_time': {
|
2022-08-16 07:18:35 +00:00
|
|
|
return minterMessages(minterContract)?.updateStartTime(args.startTime)
|
2022-07-25 08:29:52 +00:00
|
|
|
}
|
|
|
|
case 'update_per_address_limit': {
|
2022-08-16 07:18:35 +00:00
|
|
|
return minterMessages(minterContract)?.updatePerAddressLimit(args.limit)
|
2022-07-25 08:29:52 +00:00
|
|
|
}
|
|
|
|
case 'shuffle': {
|
2022-08-16 07:18:35 +00:00
|
|
|
return minterMessages(minterContract)?.shuffle()
|
2022-07-25 08:29:52 +00:00
|
|
|
}
|
|
|
|
case 'withdraw': {
|
2022-08-16 07:18:35 +00:00
|
|
|
return minterMessages(minterContract)?.withdraw()
|
2022-07-25 08:29:52 +00:00
|
|
|
}
|
|
|
|
case 'transfer': {
|
|
|
|
return sg721Messages(sg721Contract)?.transferNft(args.recipient, args.tokenId.toString())
|
|
|
|
}
|
2022-08-16 07:18:35 +00:00
|
|
|
case 'burn': {
|
|
|
|
return sg721Messages(sg721Contract)?.burn(args.tokenId.toString())
|
|
|
|
}
|
|
|
|
case 'batch_burn': {
|
|
|
|
return sg721Messages(sg721Contract)?.batchBurn(args.tokenIds)
|
|
|
|
}
|
2022-07-25 08:29:52 +00:00
|
|
|
default: {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isEitherType = <T extends ActionType>(type: unknown, arr: T[]): type is T => {
|
|
|
|
return arr.some((val) => type === val)
|
|
|
|
}
|