Match list items with contract helpers

This commit is contained in:
Serkan Reis 2023-10-07 10:41:10 +03:00
parent 9fcd5b82f4
commit ca8f5cca58
2 changed files with 226 additions and 0 deletions

View File

@ -0,0 +1,162 @@
import type { RoyaltyRegistryInstance } from '../index'
import { useRoyaltyRegistryContract } from '../index'
export type ExecuteType = typeof EXECUTE_TYPES[number]
export const EXECUTE_TYPES = [
'initialize_collection_royalty',
'set_collection_royalty_default',
'update_collection_royalty_default',
'set_collection_royalty_protocol',
'update_collection_royalty_protocol',
] as const
export interface ExecuteListItem {
id: ExecuteType
name: string
description?: string
}
export const EXECUTE_LIST: ExecuteListItem[] = [
{
id: 'initialize_collection_royalty',
name: 'Initialize Collection Royalty',
description: 'Initialize collection royalty',
},
{
id: 'set_collection_royalty_default',
name: 'Set Collection Royalty Default',
description: 'Set collection royalty default',
},
{
id: 'update_collection_royalty_default',
name: 'Update Collection Royalty Default',
description: 'Update collection royalty default',
},
{
id: 'set_collection_royalty_protocol',
name: 'Set Collection Royalty Protocol',
description: 'Set collection royalty protocol',
},
{
id: 'update_collection_royalty_protocol',
name: 'Update Collection Royalty Protocol',
description: 'Update collection royalty protocol',
},
]
export interface DispatchExecuteProps {
type: ExecuteType
[k: string]: unknown
}
type Select<T extends ExecuteType> = T
/** @see {@link RoyaltyRegistryInstance} */
export type DispatchExecuteArgs = {
contract: string
messages?: RoyaltyRegistryInstance
} & (
| { type: Select<'initialize_collection_royalty'>; collection: string }
| { type: Select<'set_collection_royalty_default'>; collection: string; recipient: string; royalty: number }
| {
type: Select<'update_collection_royalty_default'>
collection: string
recipient: string
shareDelta: number
decrement: boolean
}
| {
type: Select<'set_collection_royalty_protocol'>
collection: string
protocol: string
recipient: string
royalty: number
}
| {
type: Select<'update_collection_royalty_protocol'>
collection: string
protocol: string
recipient: string
shareDelta: number
decrement: boolean
}
)
export const dispatchExecute = async (args: DispatchExecuteArgs) => {
const { messages } = args
if (!messages) {
throw new Error('Cannot dispatch execute, messages are not defined')
}
switch (args.type) {
case 'initialize_collection_royalty': {
return messages.initializeCollectionRoyalty(args.collection)
}
case 'set_collection_royalty_default': {
return messages.setCollectionRoyaltyDefault(args.collection, args.recipient, args.royalty)
}
case 'update_collection_royalty_default': {
return messages.updateCollectionRoyaltyDefault(args.collection, args.recipient, args.shareDelta, args.decrement)
}
case 'set_collection_royalty_protocol': {
return messages.setCollectionRoyaltyProtocol(args.collection, args.protocol, args.recipient, args.royalty)
}
case 'update_collection_royalty_protocol': {
return messages.updateCollectionRoyaltyProtocol(
args.collection,
args.protocol,
args.recipient,
args.shareDelta,
args.decrement,
)
}
default: {
throw new Error('Unknown execution type')
}
}
}
export const previewExecutePayload = (args: DispatchExecuteArgs) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { messages } = useRoyaltyRegistryContract()
const { contract } = args
switch (args.type) {
case 'initialize_collection_royalty': {
return messages(contract)?.initializeCollectionRoyalty(args.collection)
}
case 'set_collection_royalty_default': {
return messages(contract)?.setCollectionRoyaltyDefault(args.collection, args.recipient, args.royalty)
}
case 'update_collection_royalty_default': {
return messages(contract)?.updateCollectionRoyaltyDefault(
args.collection,
args.recipient,
args.shareDelta,
args.decrement,
)
}
case 'set_collection_royalty_protocol': {
return messages(contract)?.setCollectionRoyaltyProtocol(
args.collection,
args.protocol,
args.recipient,
args.royalty,
)
}
case 'update_collection_royalty_protocol': {
return messages(contract)?.updateCollectionRoyaltyProtocol(
args.collection,
args.protocol,
args.recipient,
args.shareDelta,
args.decrement,
)
}
default: {
return {}
}
}
}
export const isEitherType = <T extends ExecuteType>(type: unknown, arr: T[]): type is T => {
return arr.some((val) => type === val)
}

View File

@ -0,0 +1,64 @@
import type { RoyaltyRegistryInstance } from '../contract'
export type QueryType = typeof QUERY_TYPES[number]
export const QUERY_TYPES = [
'config',
'collection_royalty_default',
'collection_royalty_protocol',
'royalty_payment',
] as const
export interface QueryListItem {
id: QueryType
name: string
description?: string
}
export const QUERY_LIST: QueryListItem[] = [
{ id: 'config', name: 'Query Config', description: 'View the contract config' },
{
id: 'collection_royalty_default',
name: 'Query Collection Royalty Details',
description: 'View the collection royalty details',
},
{
id: 'collection_royalty_protocol',
name: 'Query Collection Royalty Protocol',
description: 'View the collection royalty protocol',
},
{
id: 'royalty_payment',
name: 'Query Royalty Payment',
description: 'View the royalty payment',
},
]
/*
//Query
config: () => Promise<string>
collectionRoyaltyDefault: (collection: string) => Promise<string>
collectionRoyaltyProtocol: (collection: string, protocol: string) => Promise<string>
// RoyaltyProtocolByCollection: (collection: string, queryOptions: QqueryOptions) => Promise<string>
royaltyPayment: (collection: string, protocol?: string) => Promise<string>
*/
export interface DispatchQueryProps {
messages: RoyaltyRegistryInstance | undefined
type: QueryType
collection: string
protocol: string
}
export const dispatchQuery = (props: DispatchQueryProps) => {
const { messages, type, collection, protocol } = props
switch (type) {
case 'config':
return messages?.config()
case 'collection_royalty_default':
return messages?.collectionRoyaltyDefault(collection)
case 'collection_royalty_protocol':
return messages?.collectionRoyaltyProtocol(collection, protocol)
default: {
throw new Error('unknown query type')
}
}
}