From 34ff4bf97308e85fafe7f642be4dfe39f45ebb4c Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Wed, 1 Feb 2023 15:55:52 +0300 Subject: [PATCH] Implement badge-hub queries & actions --- contexts/contracts.tsx | 8 +- contracts/badge-hub/contract.ts | 691 +++++++++--------------- contracts/badge-hub/messages/execute.ts | 200 +++---- contracts/badge-hub/messages/query.ts | 35 +- 4 files changed, 359 insertions(+), 575 deletions(-) diff --git a/contexts/contracts.tsx b/contexts/contracts.tsx index b0fbd06..1b410ad 100644 --- a/contexts/contracts.tsx +++ b/contexts/contracts.tsx @@ -1,3 +1,5 @@ +import type { UseBadgeHubContractProps } from 'contracts/badge-hub' +import { useBadgeHubContract } from 'contracts/badge-hub' import type { UseBaseFactoryContractProps } from 'contracts/baseFactory' import { useBaseFactoryContract } from 'contracts/baseFactory' import type { UseBaseMinterContractProps } from 'contracts/baseMinter' @@ -25,6 +27,7 @@ export interface ContractsStore extends State { whitelist: UseWhiteListContractProps | null vendingFactory: UseVendingFactoryContractProps | null baseFactory: UseBaseFactoryContractProps | null + badgeHub: UseBadgeHubContractProps | null } /** @@ -37,6 +40,7 @@ export const defaultValues: ContractsStore = { whitelist: null, vendingFactory: null, baseFactory: null, + badgeHub: null, } /** @@ -66,6 +70,7 @@ const ContractsSubscription: VFC = () => { const whitelist = useWhiteListContract() const vendingFactory = useVendingFactoryContract() const baseFactory = useBaseFactoryContract() + const badgeHub = useBadgeHubContract() useEffect(() => { useContracts.setState({ @@ -75,8 +80,9 @@ const ContractsSubscription: VFC = () => { whitelist, vendingFactory, baseFactory, + badgeHub, }) - }, [sg721, vendingMinter, baseMinter, whitelist, vendingFactory, baseFactory]) + }, [sg721, vendingMinter, baseMinter, whitelist, vendingFactory, baseFactory, badgeHub]) return null } diff --git a/contracts/badge-hub/contract.ts b/contracts/badge-hub/contract.ts index d0f1960..ee69626 100644 --- a/contracts/badge-hub/contract.ts +++ b/contracts/badge-hub/contract.ts @@ -1,10 +1,12 @@ -import type { MsgExecuteContractEncodeObject, SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' -import { toUtf8 } from '@cosmjs/encoding' +/* eslint-disable eslint-comments/disable-enable-pair */ +/* eslint-disable @typescript-eslint/no-unsafe-return */ +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ +/* eslint-disable camelcase */ +import type { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' import type { Coin } from '@cosmjs/proto-signing' import { coin } from '@cosmjs/proto-signing' import type { logs } from '@cosmjs/stargate' import type { Timestamp } from '@stargazezone/types/contracts/minter/shared-types' -import { MsgExecuteContract } from 'cosmjs-types/cosmwasm/wasm/v1/tx' export interface InstantiateResponse { readonly contractAddress: string @@ -17,201 +19,185 @@ export interface MigrateResponse { readonly logs: readonly logs.Log[] } +export interface Rule { + by_key?: string + by_minter?: string + by_keys?: string[] +} + +export interface Trait { + display_type?: string + trait_type: string + value: string +} + +export interface Metadata { + name?: string + image?: string + image_date?: string + external_url?: string + description?: string + attributes?: Trait[] + background_color?: string + animation_url?: string + youtube_url?: string +} + +export interface Badge { + manager: string + metadata: Metadata + transferrable: boolean + rule: Rule + expiry?: Timestamp + max_supply?: number +} + export interface BadgeHubInstance { readonly contractAddress: string //Query getConfig: () => Promise - getMintableNumTokens: () => Promise - getStartTime: () => Promise - getMintPrice: () => Promise - getMintCount: (address: string) => Promise + getBadge: (id: number) => Promise + getBadges: (start_after?: number, limit?: number) => Promise + getKey: (id: number, pubkey: string) => Promise + getKeys: (id: number, start_after?: number, limit?: number) => Promise //Execute - mint: (senderAddress: string) => Promise - purge: (senderAddress: string) => Promise - updateMintPrice: (senderAddress: string, price: string) => Promise - setWhitelist: (senderAddress: string, whitelist: string) => Promise - updateStartTime: (senderAddress: string, time: Timestamp) => Promise - updateStartTradingTime: (senderAddress: string, time?: Timestamp) => Promise - updatePerAddressLimit: (senderAddress: string, perAddressLimit: number) => Promise - mintTo: (senderAddress: string, recipient: string) => Promise - mintFor: (senderAddress: string, recipient: string, tokenId: number) => Promise - batchMintFor: (senderAddress: string, recipient: string, tokenIds: string) => Promise - batchMint: (senderAddress: string, recipient: string, batchNumber: number) => Promise - shuffle: (senderAddress: string) => Promise - withdraw: (senderAddress: string) => Promise - airdrop: (senderAddress: string, recipients: string[]) => Promise - burnRemaining: (senderAddress: string) => Promise + createBadge: (senderAddress: string, badge: Badge) => Promise + editBadge: (senderAddress: string, id: number, metadata: Metadata) => Promise + addKeys: (senderAddress: string, id: number, keys: string[]) => Promise + purgeKeys: (senderAddress: string, id: number, limit?: number) => Promise + purgeOwners: (senderAddress: string, id: number, limit?: number) => Promise + mintByMinter: (senderAddress: string, id: number, owners: string[]) => Promise + mintByKey: (senderAddress: string, id: number, owner: string, signature: string) => Promise + mintByKeys: (senderAddress: string, id: number, owner: string, pubkey: string, signature: string) => Promise + setNft: (senderAddress: string, nft: string) => Promise } export interface BadgeHubMessages { - mint: () => MintMessage - purge: () => PurgeMessage - updateMintPrice: (price: string) => UpdateMintPriceMessage - setWhitelist: (whitelist: string) => SetWhitelistMessage - updateStartTime: (time: Timestamp) => UpdateStartTimeMessage - updateStartTradingTime: (time: Timestamp) => UpdateStartTradingTimeMessage - updatePerAddressLimit: (perAddressLimit: number) => UpdatePerAddressLimitMessage - mintTo: (recipient: string) => MintToMessage - mintFor: (recipient: string, tokenId: number) => MintForMessage - batchMintFor: (recipient: string, tokenIds: string) => BatchMintForMessage - batchMint: (recipient: string, batchNumber: number) => CustomMessage - shuffle: () => ShuffleMessage - withdraw: () => WithdrawMessage - airdrop: (recipients: string[]) => CustomMessage - burnRemaining: () => BurnRemainingMessage + createBadge: (badge: Badge) => CreateBadgeMessage + editBadge: (id: number, metadata: Metadata) => EditBadgeMessage + addKeys: (id: number, keys: string[]) => AddKeysMessage + purgeKeys: (id: number, limit?: number) => PurgeKeysMessage + purgeOwners: (id: number, limit?: number) => PurgeOwnersMessage + mintByMinter: (id: number, owners: string[]) => MintByMinterMessage + mintByKey: (id: number, owner: string, signature: string) => MintByKeyMessage + mintByKeys: (id: number, owner: string, pubkey: string, signature: string) => MintByKeysMessage + setNft: (nft: string) => SetNftMessage } -export interface MintMessage { +export interface CreateBadgeMessage { sender: string contract: string msg: { - mint: Record - } - funds: Coin[] -} - -export interface PurgeMessage { - sender: string - contract: string - msg: { - purge: Record - } - funds: Coin[] -} - -export interface UpdateMintPriceMessage { - sender: string - contract: string - msg: { - update_mint_price: { - price: string + create_badge: { + manager: string + metadata: Metadata + transferrable: boolean + rule: Rule + expiry?: Timestamp + max_supply?: number } } funds: Coin[] } -export interface SetWhitelistMessage { +export interface EditBadgeMessage { sender: string contract: string msg: { - set_whitelist: { - whitelist: string + edit_badge: { + id: number + metadata: Metadata } } funds: Coin[] } -export interface UpdateStartTimeMessage { +export interface AddKeysMessage { sender: string contract: string msg: { - update_start_time: string - } - funds: Coin[] -} - -export interface UpdateStartTradingTimeMessage { - sender: string - contract: string - msg: { - update_start_trading_time: string - } - funds: Coin[] -} - -export interface UpdatePerAddressLimitMessage { - sender: string - contract: string - msg: { - update_per_address_limit: { - per_address_limit: number + add_keys: { + id: number + keys: string[] } } funds: Coin[] } -export interface MintToMessage { +export interface PurgeKeysMessage { sender: string contract: string msg: { - mint_to: { - recipient: string + purge_keys: { + id: number + limit?: number } } funds: Coin[] } -export interface MintForMessage { +export interface PurgeOwnersMessage { sender: string contract: string msg: { - mint_for: { - recipient: string - token_id: number + purge_owners: { + id: number + limit?: number } } funds: Coin[] } -export interface BatchMintForMessage { - sender: string - contract: string - msg: Record[] - funds: Coin[] -} -export interface CustomMessage { - sender: string - contract: string - msg: Record[] - funds: Coin[] -} - -export interface ShuffleMessage { +export interface MintByMinterMessage { sender: string contract: string msg: { - shuffle: Record + mint_by_minter: { + id: number + owners: string[] + } } funds: Coin[] } -export interface WithdrawMessage { +export interface MintByKeyMessage { sender: string contract: string msg: { - withdraw: Record + mint_by_key: { + id: number + owner: string + signature: string + } } funds: Coin[] } -export interface BurnRemainingMessage { +export interface MintByKeysMessage { sender: string contract: string msg: { - burn_remaining: Record + mint_by_keys: { + id: number + owner: string + pubkey: string + signature: string + } } funds: Coin[] } -export interface MintPriceMessage { - public_price: { - denom: string - amount: string - } - airdrop_price: { - denom: string - amount: string - } - whitelist_price?: { - denom: string - amount: string - } - current_price: { - denom: string - amount: string +export interface SetNftMessage { + sender: string + contract: string + msg: { + set_nft: { + nft: string + } } + funds: Coin[] } export interface BadgeHubContract { @@ -246,72 +232,65 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge return res } - const getMintableNumTokens = async (): Promise => { + const getBadge = async (id: number): Promise => { const res = await client.queryContractSmart(contractAddress, { - mintable_num_tokens: {}, + badge: { id }, }) return res } - const getStartTime = async (): Promise => { + const getBadges = async (start_after?: number, limit?: number): Promise => { const res = await client.queryContractSmart(contractAddress, { - start_time: {}, + badges: { start_after, limit }, }) return res } - const getMintPrice = async (): Promise => { + const getKey = async (id: number, key: string): Promise => { const res = await client.queryContractSmart(contractAddress, { - mint_price: {}, + key: { id, key }, }) return res } - const getMintCount = async (address: string): Promise => { + const getKeys = async (id: number, start_after?: number, limit?: number): Promise => { const res = await client.queryContractSmart(contractAddress, { - mint_count: { address }, + keys: { id, start_after, limit }, }) return res } //Execute - const mint = async (senderAddress: string): Promise => { - const price = (await getMintPrice()).public_price.amount + const createBadge = async (senderAddress: string, badge: Badge): Promise => { const res = await client.execute( senderAddress, contractAddress, { - mint: {}, + create_badge: { + manager: badge.manager, + metadata: badge.metadata, + transferrable: badge.transferrable, + rule: badge.rule, + expiry: badge.expiry, + max_supply: badge.max_supply, + }, }, 'auto', '', - [coin(price, 'ustars')], + [coin(315, 'ustars')], ) return res.transactionHash } - const purge = async (senderAddress: string): Promise => { + const editBadge = async (senderAddress: string, id: number, metadata: Metadata): Promise => { const res = await client.execute( senderAddress, contractAddress, { - purge: {}, - }, - 'auto', - '', - ) - - return res.transactionHash - } - - const updateMintPrice = async (senderAddress: string, price: string): Promise => { - const res = await client.execute( - senderAddress, - contractAddress, - { - update_mint_price: { - price: (Number(price) * 1000000).toString(), + edit_badge: { + id, + metadata, }, }, 'auto', @@ -321,12 +300,15 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge return res.transactionHash } - const setWhitelist = async (senderAddress: string, whitelist: string): Promise => { + const addKeys = async (senderAddress: string, id: number, keys: string[]): Promise => { const res = await client.execute( senderAddress, contractAddress, { - set_whitelist: { whitelist }, + add_keys: { + id, + keys, + }, }, 'auto', '', @@ -335,12 +317,15 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge return res.transactionHash } - const updateStartTime = async (senderAddress: string, time: Timestamp): Promise => { + const purgeKeys = async (senderAddress: string, id: number, limit?: number): Promise => { const res = await client.execute( senderAddress, contractAddress, { - update_start_time: time, + purge_keys: { + id, + limit, + }, }, 'auto', '', @@ -349,12 +334,15 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge return res.transactionHash } - const updateStartTradingTime = async (senderAddress: string, time?: Timestamp): Promise => { + const purgeOwners = async (senderAddress: string, id: number, limit?: number): Promise => { const res = await client.execute( senderAddress, contractAddress, { - update_start_trading_time: time || null, + purge_owners: { + id, + limit, + }, }, 'auto', '', @@ -363,12 +351,15 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge return res.transactionHash } - const updatePerAddressLimit = async (senderAddress: string, perAddressLimit: number): Promise => { + const mintByMinter = async (senderAddress: string, id: number, owners: string[]): Promise => { const res = await client.execute( senderAddress, contractAddress, { - update_per_address_limit: { per_address_limit: perAddressLimit }, + mint_by_minter: { + id, + owners, + }, }, 'auto', '', @@ -377,12 +368,16 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge return res.transactionHash } - const mintTo = async (senderAddress: string, recipient: string): Promise => { + const mintByKey = async (senderAddress: string, id: number, owner: string, signature: string): Promise => { const res = await client.execute( senderAddress, contractAddress, { - mint_to: { recipient }, + mint_by_key: { + id, + owner, + signature, + }, }, 'auto', '', @@ -391,12 +386,23 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge return res.transactionHash } - const mintFor = async (senderAddress: string, recipient: string, tokenId: number): Promise => { + const mintByKeys = async ( + senderAddress: string, + id: number, + owner: string, + pubkey: string, + signature: string, + ): Promise => { const res = await client.execute( senderAddress, contractAddress, { - mint_for: { token_id: tokenId, recipient }, + mint_by_keys: { + id, + owner, + pubkey, + signature, + }, }, 'auto', '', @@ -405,130 +411,14 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge return res.transactionHash } - const batchMintFor = async (senderAddress: string, recipient: string, tokenIds: string): Promise => { - const executeContractMsgs: MsgExecuteContractEncodeObject[] = [] - if (tokenIds.includes(':')) { - const [start, end] = tokenIds.split(':').map(Number) - for (let i = start; i <= end; i++) { - const msg = { - mint_for: { token_id: i, recipient }, - } - const executeContractMsg: MsgExecuteContractEncodeObject = { - typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract', - value: MsgExecuteContract.fromPartial({ - sender: txSigner, - contract: contractAddress, - msg: toUtf8(JSON.stringify(msg)), - }), - } - - executeContractMsgs.push(executeContractMsg) - } - } else { - const tokenNumbers = tokenIds.split(',').map(Number) - for (let i = 0; i < tokenNumbers.length; i++) { - const msg = { - mint_for: { token_id: tokenNumbers[i], recipient }, - } - const executeContractMsg: MsgExecuteContractEncodeObject = { - typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract', - value: MsgExecuteContract.fromPartial({ - sender: txSigner, - contract: contractAddress, - msg: toUtf8(JSON.stringify(msg)), - }), - } - - executeContractMsgs.push(executeContractMsg) - } - } - - const res = await client.signAndBroadcast(txSigner, executeContractMsgs, 'auto', 'batch mint for') - - return res.transactionHash - } - - const batchMint = async (senderAddress: string, recipient: string, batchNumber: number): Promise => { - const executeContractMsgs: MsgExecuteContractEncodeObject[] = [] - for (let i = 0; i < batchNumber; i++) { - const msg = { - mint_to: { recipient }, - } - const executeContractMsg: MsgExecuteContractEncodeObject = { - typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract', - value: MsgExecuteContract.fromPartial({ - sender: senderAddress, - contract: contractAddress, - msg: toUtf8(JSON.stringify(msg)), - }), - } - - executeContractMsgs.push(executeContractMsg) - } - - const res = await client.signAndBroadcast(senderAddress, executeContractMsgs, 'auto', 'batch mint') - - return res.transactionHash - } - - const airdrop = async (senderAddress: string, recipients: string[]): Promise => { - const executeContractMsgs: MsgExecuteContractEncodeObject[] = [] - for (let i = 0; i < recipients.length; i++) { - const msg = { - mint_to: { recipient: recipients[i] }, - } - const executeContractMsg: MsgExecuteContractEncodeObject = { - typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract', - value: MsgExecuteContract.fromPartial({ - sender: senderAddress, - contract: contractAddress, - msg: toUtf8(JSON.stringify(msg)), - }), - } - - executeContractMsgs.push(executeContractMsg) - } - - const res = await client.signAndBroadcast(senderAddress, executeContractMsgs, 'auto', 'airdrop') - - return res.transactionHash - } - - const shuffle = async (senderAddress: string): Promise => { + const setNft = async (senderAddress: string, nft: string): Promise => { const res = await client.execute( senderAddress, contractAddress, { - shuffle: {}, - }, - 'auto', - '', - [coin(500000000, 'ustars')], - ) - - return res.transactionHash - } - - const withdraw = async (senderAddress: string): Promise => { - const res = await client.execute( - senderAddress, - contractAddress, - { - withdraw: {}, - }, - 'auto', - '', - ) - - return res.transactionHash - } - - const burnRemaining = async (senderAddress: string): Promise => { - const res = await client.execute( - senderAddress, - contractAddress, - { - burn_remaining: {}, + set_nft: { + nft, + }, }, 'auto', '', @@ -540,25 +430,19 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge return { contractAddress, getConfig, - getMintableNumTokens, - getStartTime, - getMintPrice, - getMintCount, - mint, - purge, - updateMintPrice, - setWhitelist, - updateStartTime, - updateStartTradingTime, - updatePerAddressLimit, - mintTo, - mintFor, - batchMintFor, - batchMint, - airdrop, - shuffle, - withdraw, - burnRemaining, + getBadge, + getBadges, + getKey, + getKeys, + createBadge, + editBadge, + addKeys, + purgeKeys, + purgeOwners, + mintByMinter, + mintByKey, + mintByKeys, + setNft, } } @@ -581,9 +465,7 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge initMsg: Record, label: string, ): Promise => { - const result = await client.instantiate(senderAddress, codeId, initMsg, label, 'auto', { - funds: [coin('3000000000', 'ustars')], - }) + const result = await client.instantiate(senderAddress, codeId, initMsg, label, 'auto') return { contractAddress: result.contractAddress, @@ -593,215 +475,148 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge } const messages = (contractAddress: string) => { - const mint = (): MintMessage => { + const createBadge = (badge: Badge): CreateBadgeMessage => { return { sender: txSigner, contract: contractAddress, msg: { - mint: {}, - }, - funds: [], - } - } - - const purge = (): PurgeMessage => { - return { - sender: txSigner, - contract: contractAddress, - msg: { - purge: {}, - }, - funds: [], - } - } - - const updateMintPrice = (price: string): UpdateMintPriceMessage => { - return { - sender: txSigner, - contract: contractAddress, - msg: { - update_mint_price: { - price: (Number(price) * 1000000).toString(), + create_badge: { + manager: badge.manager, + metadata: badge.metadata, + transferrable: badge.transferrable, + rule: badge.rule, + expiry: badge.expiry, + max_supply: badge.max_supply, }, }, funds: [], } } - const setWhitelist = (whitelist: string): SetWhitelistMessage => { + const editBadge = (id: number, metadata: Metadata): EditBadgeMessage => { return { sender: txSigner, contract: contractAddress, msg: { - set_whitelist: { - whitelist, + edit_badge: { + id, + metadata, }, }, funds: [], } } - const updateStartTime = (startTime: string): UpdateStartTimeMessage => { + const addKeys = (id: number, keys: string[]): AddKeysMessage => { return { sender: txSigner, contract: contractAddress, msg: { - update_start_time: startTime, - }, - funds: [], - } - } - - const updateStartTradingTime = (startTime: string): UpdateStartTradingTimeMessage => { - return { - sender: txSigner, - contract: contractAddress, - msg: { - update_start_trading_time: startTime, - }, - funds: [], - } - } - - const updatePerAddressLimit = (limit: number): UpdatePerAddressLimitMessage => { - return { - sender: txSigner, - contract: contractAddress, - msg: { - update_per_address_limit: { - per_address_limit: limit, + add_keys: { + id, + keys, }, }, funds: [], } } - const mintTo = (recipient: string): MintToMessage => { + const purgeKeys = (id: number, limit?: number): PurgeKeysMessage => { return { sender: txSigner, contract: contractAddress, msg: { - mint_to: { - recipient, + purge_keys: { + id, + limit, }, }, funds: [], } } - const mintFor = (recipient: string, tokenId: number): MintForMessage => { + const purgeOwners = (id: number, limit?: number): PurgeOwnersMessage => { return { sender: txSigner, contract: contractAddress, msg: { - mint_for: { - recipient, - token_id: tokenId, + purge_owners: { + id, + limit, }, }, funds: [], } } - const batchMintFor = (recipient: string, tokenIds: string): BatchMintForMessage => { - const msg: Record[] = [] - if (tokenIds.includes(':')) { - const [start, end] = tokenIds.split(':').map(Number) - for (let i = start; i <= end; i++) { - msg.push({ - mint_for: { token_id: i.toString(), recipient }, - }) - } - } else { - const tokenNumbers = tokenIds.split(',').map(Number) - for (let i = 0; i < tokenNumbers.length; i++) { - msg.push({ mint_for: { token_id: tokenNumbers[i].toString(), recipient } }) - } - } - - return { - sender: txSigner, - contract: contractAddress, - msg, - funds: [], - } - } - - const batchMint = (recipient: string, batchNumber: number): CustomMessage => { - const msg: Record[] = [] - for (let i = 0; i < batchNumber; i++) { - msg.push({ mint_to: { recipient } }) - } - return { - sender: txSigner, - contract: contractAddress, - msg, - funds: [], - } - } - - const airdrop = (recipients: string[]): CustomMessage => { - const msg: Record[] = [] - for (let i = 0; i < recipients.length; i++) { - msg.push({ mint_to: { recipient: recipients[i] } }) - } - return { - sender: txSigner, - contract: contractAddress, - msg, - funds: [], - } - } - - const shuffle = (): ShuffleMessage => { + const mintByMinter = (id: number, owners: string[]): MintByMinterMessage => { return { sender: txSigner, contract: contractAddress, msg: { - shuffle: {}, + mint_by_minter: { + id, + owners, + }, }, funds: [], } } - const withdraw = (): WithdrawMessage => { + const mintByKey = (id: number, owner: string, signature: string): MintByKeyMessage => { return { sender: txSigner, contract: contractAddress, msg: { - withdraw: {}, + mint_by_key: { + id, + owner, + signature, + }, }, funds: [], } } - const burnRemaining = (): BurnRemainingMessage => { + const mintByKeys = (id: number, owner: string, pubkey: string, signature: string): MintByKeysMessage => { return { sender: txSigner, contract: contractAddress, msg: { - burn_remaining: {}, + mint_by_keys: { + id, + owner, + pubkey, + signature, + }, + }, + funds: [], + } + } + + const setNft = (nft: string): SetNftMessage => { + return { + sender: txSigner, + contract: contractAddress, + msg: { + set_nft: { + nft, + }, }, funds: [], } } return { - mint, - purge, - updateMintPrice, - setWhitelist, - updateStartTime, - updateStartTradingTime, - updatePerAddressLimit, - mintTo, - mintFor, - batchMintFor, - batchMint, - airdrop, - shuffle, - withdraw, - burnRemaining, + createBadge, + editBadge, + addKeys, + purgeKeys, + purgeOwners, + mintByMinter, + mintByKey, + mintByKeys, + setNft, } } diff --git a/contracts/badge-hub/messages/execute.ts b/contracts/badge-hub/messages/execute.ts index 8b65f7d..a3182ca 100644 --- a/contracts/badge-hub/messages/execute.ts +++ b/contracts/badge-hub/messages/execute.ts @@ -1,21 +1,18 @@ -import type { BadgeHubInstance } from '../index' +import type { Badge, BadgeHubInstance, Metadata } from '../index' import { useBadgeHubContract } from '../index' export type ExecuteType = typeof EXECUTE_TYPES[number] export const EXECUTE_TYPES = [ - 'mint', - 'purge', - 'update_mint_price', - 'set_whitelist', - 'update_start_time', - 'update_start_trading_time', - 'update_per_address_limit', - 'mint_to', - 'mint_for', - 'shuffle', - 'withdraw', - 'burn_remaining', + '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 ExecuteListItem { @@ -26,59 +23,49 @@ export interface ExecuteListItem { export const EXECUTE_LIST: ExecuteListItem[] = [ { - id: 'mint', - name: 'Mint', - description: `Mint new tokens for a given address`, + id: 'create_badge', + name: 'Create Badge', + description: `Create a new badge with the specified mint rule and metadata`, }, { - id: 'purge', - name: 'Purge', - description: `Purge`, + id: 'edit_badge', + name: 'Edit Badge', + description: `Edit the badge with the specified ID`, }, { - id: 'update_mint_price', - name: 'Update Mint Price', - description: `Update mint price`, + id: 'add_keys', + name: 'Add Keys', + description: `Add keys to the badge with the specified ID`, }, { - id: 'set_whitelist', - name: 'Set Whitelist', - description: `Set whitelist contract address`, + id: 'purge_keys', + name: 'Purge Keys', + description: `Purge keys from the badge with the specified ID`, }, { - id: 'update_start_time', - name: 'Update Start Time', - description: `Update start time for minting`, + id: 'purge_owners', + name: 'Purge Owners', + description: `Purge owners from the badge with the specified ID`, }, { - id: 'update_start_trading_time', - name: 'Update Start Trading Time', - description: `Update start trading time for minting`, + id: 'mint_by_minter', + name: 'Mint by Minter', + description: `Mint a new token by the minter with the specified ID`, }, { - id: 'update_per_address_limit', - name: 'Update Per Address Limit', - description: `Update token per address limit`, + id: 'mint_by_key', + name: 'Mint by Key', + description: `Mint a new token by the key with the specified ID`, }, { - id: 'mint_to', - name: 'Mint To', - description: `Mint tokens to a given address`, + id: 'mint_by_keys', + name: 'Mint by Keys', + description: `Mint a new token by the keys with the specified ID`, }, { - id: 'mint_for', - name: 'Mint For', - description: `Mint tokens for a given address with a given token ID`, - }, - { - id: 'shuffle', - name: 'Shuffle', - description: `Shuffle the token IDs`, - }, - { - id: 'burn_remaining', - name: 'Burn Remaining', - description: `Burn remaining tokens`, + id: 'set_nft', + name: 'Set NFT', + description: `Set the Badge NFT contract address for the Badge Hub contract`, }, ] @@ -89,25 +76,22 @@ export interface DispatchExecuteProps { type Select = T -/** @see {@link VendingMinterInstance} */ +/** @see {@link BadgeHubInstance} */ export type DispatchExecuteArgs = { contract: string messages?: BadgeHubInstance txSigner: string } & ( | { type: undefined } - | { type: Select<'mint'> } - | { type: Select<'purge'> } - | { type: Select<'update_mint_price'>; price: string } - | { type: Select<'set_whitelist'>; whitelist: string } - | { type: Select<'update_start_time'>; startTime: string } - | { type: Select<'update_start_trading_time'>; startTime?: string } - | { type: Select<'update_per_address_limit'>; limit: number } - | { type: Select<'mint_to'>; recipient: string } - | { type: Select<'mint_for'>; recipient: string; tokenId: number } - | { type: Select<'shuffle'> } - | { type: Select<'withdraw'> } - | { type: Select<'burn_remaining'> } + | { 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) => { @@ -116,41 +100,32 @@ export const dispatchExecute = async (args: DispatchExecuteArgs) => { throw new Error('cannot dispatch execute, messages is not defined') } switch (args.type) { - case 'mint': { - return messages.mint(txSigner) + case 'create_badge': { + return messages.createBadge(txSigner, args.badge) } - case 'purge': { - return messages.purge(txSigner) + case 'edit_badge': { + return messages.editBadge(txSigner, args.id, args.metadata) } - case 'update_mint_price': { - return messages.updateMintPrice(txSigner, args.price) + case 'add_keys': { + return messages.addKeys(txSigner, args.id, args.keys) } - case 'set_whitelist': { - return messages.setWhitelist(txSigner, args.whitelist) + case 'purge_keys': { + return messages.purgeKeys(txSigner, args.id, args.limit) } - case 'update_start_time': { - return messages.updateStartTime(txSigner, args.startTime) + case 'purge_owners': { + return messages.purgeOwners(txSigner, args.id, args.limit) } - case 'update_start_trading_time': { - return messages.updateStartTradingTime(txSigner, args.startTime) + case 'mint_by_minter': { + return messages.mintByMinter(txSigner, args.id, args.owners) } - case 'update_per_address_limit': { - return messages.updatePerAddressLimit(txSigner, args.limit) + case 'mint_by_key': { + return messages.mintByKey(txSigner, args.id, args.owner, args.signature) } - case 'mint_to': { - return messages.mintTo(txSigner, args.recipient) + case 'mint_by_keys': { + return messages.mintByKeys(txSigner, args.id, args.owner, args.pubkey, args.signature) } - case 'mint_for': { - return messages.mintFor(txSigner, args.recipient, args.tokenId) - } - case 'shuffle': { - return messages.shuffle(txSigner) - } - case 'withdraw': { - return messages.withdraw(txSigner) - } - case 'burn_remaining': { - return messages.burnRemaining(txSigner) + case 'set_nft': { + return messages.setNft(txSigner, args.nft) } default: { throw new Error('unknown execute type') @@ -163,41 +138,32 @@ export const previewExecutePayload = (args: DispatchExecuteArgs) => { const { messages } = useBadgeHubContract() const { contract } = args switch (args.type) { - case 'mint': { - return messages(contract)?.mint() + case 'create_badge': { + return messages(contract)?.createBadge(args.badge) } - case 'purge': { - return messages(contract)?.purge() + case 'edit_badge': { + return messages(contract)?.editBadge(args.id, args.metadata) } - case 'update_mint_price': { - return messages(contract)?.updateMintPrice(args.price) + case 'add_keys': { + return messages(contract)?.addKeys(args.id, args.keys) } - case 'set_whitelist': { - return messages(contract)?.setWhitelist(args.whitelist) + case 'purge_keys': { + return messages(contract)?.purgeKeys(args.id, args.limit) } - case 'update_start_time': { - return messages(contract)?.updateStartTime(args.startTime) + case 'purge_owners': { + return messages(contract)?.purgeOwners(args.id, args.limit) } - case 'update_start_trading_time': { - return messages(contract)?.updateStartTradingTime(args.startTime as string) + case 'mint_by_minter': { + return messages(contract)?.mintByMinter(args.id, args.owners) } - case 'update_per_address_limit': { - return messages(contract)?.updatePerAddressLimit(args.limit) + case 'mint_by_key': { + return messages(contract)?.mintByKey(args.id, args.owner, args.signature) } - case 'mint_to': { - return messages(contract)?.mintTo(args.recipient) + case 'mint_by_keys': { + return messages(contract)?.mintByKeys(args.id, args.owner, args.pubkey, args.signature) } - case 'mint_for': { - return messages(contract)?.mintFor(args.recipient, args.tokenId) - } - case 'shuffle': { - return messages(contract)?.shuffle() - } - case 'withdraw': { - return messages(contract)?.withdraw() - } - case 'burn_remaining': { - return messages(contract)?.burnRemaining() + case 'set_nft': { + return messages(contract)?.setNft(args.nft) } default: { return {} diff --git a/contracts/badge-hub/messages/query.ts b/contracts/badge-hub/messages/query.ts index eb411ef..8e5f0b4 100644 --- a/contracts/badge-hub/messages/query.ts +++ b/contracts/badge-hub/messages/query.ts @@ -2,7 +2,7 @@ import type { BadgeHubInstance } from '../contract' export type QueryType = typeof QUERY_TYPES[number] -export const QUERY_TYPES = ['config', 'mintable_num_tokens', 'start_time', 'mint_price', 'mint_count'] as const +export const QUERY_TYPES = ['config', 'getBadge', 'getBadges', 'getKey', 'getKeys'] as const export interface QueryListItem { id: QueryType @@ -12,39 +12,36 @@ export interface QueryListItem { export const QUERY_LIST: QueryListItem[] = [ { id: 'config', name: 'Config', description: 'View current config' }, - { id: 'mintable_num_tokens', name: 'Total Mintable Tokens', description: 'View the total amount of mintable tokens' }, - { id: 'start_time', name: 'Start Time', description: 'View the start time for minting' }, - { id: 'mint_price', name: 'Mint Price', description: 'View the mint price' }, - { - id: 'mint_count', - name: 'Total Minted Count', - description: 'View the total amount of minted tokens for an address', - }, + { id: 'getBadge', name: 'Query Badge', description: 'Query a badge by ID' }, + { id: 'getBadges', name: 'Query a list of Badges', description: 'Query the list of badges' }, + { id: 'getKey', name: 'Query Key', description: 'Query a key by ID' }, + { id: 'getKeys', name: 'Query a list of Keys', description: 'Query the list of keys' }, ] export interface DispatchQueryProps { - address: string + id: number + pubkey: string messages: BadgeHubInstance | undefined type: QueryType } export const dispatchQuery = (props: DispatchQueryProps) => { - const { address, messages, type } = props + const { id, pubkey, messages, type } = props switch (type) { case 'config': { return messages?.getConfig() } - case 'mintable_num_tokens': { - return messages?.getMintableNumTokens() + case 'getBadge': { + return messages?.getBadge(id) } - case 'start_time': { - return messages?.getStartTime() + case 'getBadges': { + return messages?.getBadges() } - case 'mint_price': { - return messages?.getMintPrice() + case 'getKey': { + return messages?.getKey(id, pubkey) } - case 'mint_count': { - return messages?.getMintCount(address) + case 'getKeys': { + return messages?.getKeys(id) } default: { throw new Error('unknown query type')