Implement badge-hub queries & actions

This commit is contained in:
Serkan Reis 2023-02-01 15:55:52 +03:00
parent 5a00d329c1
commit 34ff4bf973
4 changed files with 359 additions and 575 deletions

View File

@ -1,3 +1,5 @@
import type { UseBadgeHubContractProps } from 'contracts/badge-hub'
import { useBadgeHubContract } from 'contracts/badge-hub'
import type { UseBaseFactoryContractProps } from 'contracts/baseFactory' import type { UseBaseFactoryContractProps } from 'contracts/baseFactory'
import { useBaseFactoryContract } from 'contracts/baseFactory' import { useBaseFactoryContract } from 'contracts/baseFactory'
import type { UseBaseMinterContractProps } from 'contracts/baseMinter' import type { UseBaseMinterContractProps } from 'contracts/baseMinter'
@ -25,6 +27,7 @@ export interface ContractsStore extends State {
whitelist: UseWhiteListContractProps | null whitelist: UseWhiteListContractProps | null
vendingFactory: UseVendingFactoryContractProps | null vendingFactory: UseVendingFactoryContractProps | null
baseFactory: UseBaseFactoryContractProps | null baseFactory: UseBaseFactoryContractProps | null
badgeHub: UseBadgeHubContractProps | null
} }
/** /**
@ -37,6 +40,7 @@ export const defaultValues: ContractsStore = {
whitelist: null, whitelist: null,
vendingFactory: null, vendingFactory: null,
baseFactory: null, baseFactory: null,
badgeHub: null,
} }
/** /**
@ -66,6 +70,7 @@ const ContractsSubscription: VFC = () => {
const whitelist = useWhiteListContract() const whitelist = useWhiteListContract()
const vendingFactory = useVendingFactoryContract() const vendingFactory = useVendingFactoryContract()
const baseFactory = useBaseFactoryContract() const baseFactory = useBaseFactoryContract()
const badgeHub = useBadgeHubContract()
useEffect(() => { useEffect(() => {
useContracts.setState({ useContracts.setState({
@ -75,8 +80,9 @@ const ContractsSubscription: VFC = () => {
whitelist, whitelist,
vendingFactory, vendingFactory,
baseFactory, baseFactory,
badgeHub,
}) })
}, [sg721, vendingMinter, baseMinter, whitelist, vendingFactory, baseFactory]) }, [sg721, vendingMinter, baseMinter, whitelist, vendingFactory, baseFactory, badgeHub])
return null return null
} }

View File

@ -1,10 +1,12 @@
import type { MsgExecuteContractEncodeObject, SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' /* eslint-disable eslint-comments/disable-enable-pair */
import { toUtf8 } from '@cosmjs/encoding' /* 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 type { Coin } from '@cosmjs/proto-signing'
import { coin } from '@cosmjs/proto-signing' import { coin } from '@cosmjs/proto-signing'
import type { logs } from '@cosmjs/stargate' import type { logs } from '@cosmjs/stargate'
import type { Timestamp } from '@stargazezone/types/contracts/minter/shared-types' import type { Timestamp } from '@stargazezone/types/contracts/minter/shared-types'
import { MsgExecuteContract } from 'cosmjs-types/cosmwasm/wasm/v1/tx'
export interface InstantiateResponse { export interface InstantiateResponse {
readonly contractAddress: string readonly contractAddress: string
@ -17,201 +19,185 @@ export interface MigrateResponse {
readonly logs: readonly logs.Log[] 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 { export interface BadgeHubInstance {
readonly contractAddress: string readonly contractAddress: string
//Query //Query
getConfig: () => Promise<any> getConfig: () => Promise<any>
getMintableNumTokens: () => Promise<any> getBadge: (id: number) => Promise<any>
getStartTime: () => Promise<any> getBadges: (start_after?: number, limit?: number) => Promise<any>
getMintPrice: () => Promise<any> getKey: (id: number, pubkey: string) => Promise<any>
getMintCount: (address: string) => Promise<any> getKeys: (id: number, start_after?: number, limit?: number) => Promise<any>
//Execute //Execute
mint: (senderAddress: string) => Promise<string> createBadge: (senderAddress: string, badge: Badge) => Promise<string>
purge: (senderAddress: string) => Promise<string> editBadge: (senderAddress: string, id: number, metadata: Metadata) => Promise<string>
updateMintPrice: (senderAddress: string, price: string) => Promise<string> addKeys: (senderAddress: string, id: number, keys: string[]) => Promise<string>
setWhitelist: (senderAddress: string, whitelist: string) => Promise<string> purgeKeys: (senderAddress: string, id: number, limit?: number) => Promise<string>
updateStartTime: (senderAddress: string, time: Timestamp) => Promise<string> purgeOwners: (senderAddress: string, id: number, limit?: number) => Promise<string>
updateStartTradingTime: (senderAddress: string, time?: Timestamp) => Promise<string> mintByMinter: (senderAddress: string, id: number, owners: string[]) => Promise<string>
updatePerAddressLimit: (senderAddress: string, perAddressLimit: number) => Promise<string> mintByKey: (senderAddress: string, id: number, owner: string, signature: string) => Promise<string>
mintTo: (senderAddress: string, recipient: string) => Promise<string> mintByKeys: (senderAddress: string, id: number, owner: string, pubkey: string, signature: string) => Promise<string>
mintFor: (senderAddress: string, recipient: string, tokenId: number) => Promise<string> setNft: (senderAddress: string, nft: string) => Promise<string>
batchMintFor: (senderAddress: string, recipient: string, tokenIds: string) => Promise<string>
batchMint: (senderAddress: string, recipient: string, batchNumber: number) => Promise<string>
shuffle: (senderAddress: string) => Promise<string>
withdraw: (senderAddress: string) => Promise<string>
airdrop: (senderAddress: string, recipients: string[]) => Promise<string>
burnRemaining: (senderAddress: string) => Promise<string>
} }
export interface BadgeHubMessages { export interface BadgeHubMessages {
mint: () => MintMessage createBadge: (badge: Badge) => CreateBadgeMessage
purge: () => PurgeMessage editBadge: (id: number, metadata: Metadata) => EditBadgeMessage
updateMintPrice: (price: string) => UpdateMintPriceMessage addKeys: (id: number, keys: string[]) => AddKeysMessage
setWhitelist: (whitelist: string) => SetWhitelistMessage purgeKeys: (id: number, limit?: number) => PurgeKeysMessage
updateStartTime: (time: Timestamp) => UpdateStartTimeMessage purgeOwners: (id: number, limit?: number) => PurgeOwnersMessage
updateStartTradingTime: (time: Timestamp) => UpdateStartTradingTimeMessage mintByMinter: (id: number, owners: string[]) => MintByMinterMessage
updatePerAddressLimit: (perAddressLimit: number) => UpdatePerAddressLimitMessage mintByKey: (id: number, owner: string, signature: string) => MintByKeyMessage
mintTo: (recipient: string) => MintToMessage mintByKeys: (id: number, owner: string, pubkey: string, signature: string) => MintByKeysMessage
mintFor: (recipient: string, tokenId: number) => MintForMessage setNft: (nft: string) => SetNftMessage
batchMintFor: (recipient: string, tokenIds: string) => BatchMintForMessage
batchMint: (recipient: string, batchNumber: number) => CustomMessage
shuffle: () => ShuffleMessage
withdraw: () => WithdrawMessage
airdrop: (recipients: string[]) => CustomMessage
burnRemaining: () => BurnRemainingMessage
} }
export interface MintMessage { export interface CreateBadgeMessage {
sender: string sender: string
contract: string contract: string
msg: { msg: {
mint: Record<string, never> create_badge: {
} manager: string
funds: Coin[] metadata: Metadata
} transferrable: boolean
rule: Rule
export interface PurgeMessage { expiry?: Timestamp
sender: string max_supply?: number
contract: string
msg: {
purge: Record<string, never>
}
funds: Coin[]
}
export interface UpdateMintPriceMessage {
sender: string
contract: string
msg: {
update_mint_price: {
price: string
} }
} }
funds: Coin[] funds: Coin[]
} }
export interface SetWhitelistMessage { export interface EditBadgeMessage {
sender: string sender: string
contract: string contract: string
msg: { msg: {
set_whitelist: { edit_badge: {
whitelist: string id: number
metadata: Metadata
} }
} }
funds: Coin[] funds: Coin[]
} }
export interface UpdateStartTimeMessage { export interface AddKeysMessage {
sender: string sender: string
contract: string contract: string
msg: { msg: {
update_start_time: string add_keys: {
} id: number
funds: Coin[] keys: string[]
}
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
} }
} }
funds: Coin[] funds: Coin[]
} }
export interface MintToMessage { export interface PurgeKeysMessage {
sender: string sender: string
contract: string contract: string
msg: { msg: {
mint_to: { purge_keys: {
recipient: string id: number
limit?: number
} }
} }
funds: Coin[] funds: Coin[]
} }
export interface MintForMessage { export interface PurgeOwnersMessage {
sender: string sender: string
contract: string contract: string
msg: { msg: {
mint_for: { purge_owners: {
recipient: string id: number
token_id: number limit?: number
} }
} }
funds: Coin[] funds: Coin[]
} }
export interface BatchMintForMessage {
sender: string
contract: string
msg: Record<string, unknown>[]
funds: Coin[]
}
export interface CustomMessage { export interface MintByMinterMessage {
sender: string
contract: string
msg: Record<string, unknown>[]
funds: Coin[]
}
export interface ShuffleMessage {
sender: string sender: string
contract: string contract: string
msg: { msg: {
shuffle: Record<string, never> mint_by_minter: {
id: number
owners: string[]
}
} }
funds: Coin[] funds: Coin[]
} }
export interface WithdrawMessage { export interface MintByKeyMessage {
sender: string sender: string
contract: string contract: string
msg: { msg: {
withdraw: Record<string, never> mint_by_key: {
id: number
owner: string
signature: string
}
} }
funds: Coin[] funds: Coin[]
} }
export interface BurnRemainingMessage { export interface MintByKeysMessage {
sender: string sender: string
contract: string contract: string
msg: { msg: {
burn_remaining: Record<string, never> mint_by_keys: {
id: number
owner: string
pubkey: string
signature: string
}
} }
funds: Coin[] funds: Coin[]
} }
export interface MintPriceMessage { export interface SetNftMessage {
public_price: { sender: string
denom: string contract: string
amount: string msg: {
} set_nft: {
airdrop_price: { nft: string
denom: string }
amount: string
}
whitelist_price?: {
denom: string
amount: string
}
current_price: {
denom: string
amount: string
} }
funds: Coin[]
} }
export interface BadgeHubContract { export interface BadgeHubContract {
@ -246,72 +232,65 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
return res return res
} }
const getMintableNumTokens = async (): Promise<any> => { const getBadge = async (id: number): Promise<any> => {
const res = await client.queryContractSmart(contractAddress, { const res = await client.queryContractSmart(contractAddress, {
mintable_num_tokens: {}, badge: { id },
}) })
return res return res
} }
const getStartTime = async (): Promise<any> => { const getBadges = async (start_after?: number, limit?: number): Promise<any> => {
const res = await client.queryContractSmart(contractAddress, { const res = await client.queryContractSmart(contractAddress, {
start_time: {}, badges: { start_after, limit },
}) })
return res return res
} }
const getMintPrice = async (): Promise<MintPriceMessage> => { const getKey = async (id: number, key: string): Promise<any> => {
const res = await client.queryContractSmart(contractAddress, { const res = await client.queryContractSmart(contractAddress, {
mint_price: {}, key: { id, key },
}) })
return res return res
} }
const getMintCount = async (address: string): Promise<any> => { const getKeys = async (id: number, start_after?: number, limit?: number): Promise<any> => {
const res = await client.queryContractSmart(contractAddress, { const res = await client.queryContractSmart(contractAddress, {
mint_count: { address }, keys: { id, start_after, limit },
}) })
return res return res
} }
//Execute //Execute
const mint = async (senderAddress: string): Promise<string> => { const createBadge = async (senderAddress: string, badge: Badge): Promise<string> => {
const price = (await getMintPrice()).public_price.amount
const res = await client.execute( const res = await client.execute(
senderAddress, senderAddress,
contractAddress, 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', 'auto',
'', '',
[coin(price, 'ustars')], [coin(315, 'ustars')],
) )
return res.transactionHash return res.transactionHash
} }
const purge = async (senderAddress: string): Promise<string> => { const editBadge = async (senderAddress: string, id: number, metadata: Metadata): Promise<string> => {
const res = await client.execute( const res = await client.execute(
senderAddress, senderAddress,
contractAddress, contractAddress,
{ {
purge: {}, edit_badge: {
}, id,
'auto', metadata,
'',
)
return res.transactionHash
}
const updateMintPrice = async (senderAddress: string, price: string): Promise<string> => {
const res = await client.execute(
senderAddress,
contractAddress,
{
update_mint_price: {
price: (Number(price) * 1000000).toString(),
}, },
}, },
'auto', 'auto',
@ -321,12 +300,15 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
return res.transactionHash return res.transactionHash
} }
const setWhitelist = async (senderAddress: string, whitelist: string): Promise<string> => { const addKeys = async (senderAddress: string, id: number, keys: string[]): Promise<string> => {
const res = await client.execute( const res = await client.execute(
senderAddress, senderAddress,
contractAddress, contractAddress,
{ {
set_whitelist: { whitelist }, add_keys: {
id,
keys,
},
}, },
'auto', 'auto',
'', '',
@ -335,12 +317,15 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
return res.transactionHash return res.transactionHash
} }
const updateStartTime = async (senderAddress: string, time: Timestamp): Promise<string> => { const purgeKeys = async (senderAddress: string, id: number, limit?: number): Promise<string> => {
const res = await client.execute( const res = await client.execute(
senderAddress, senderAddress,
contractAddress, contractAddress,
{ {
update_start_time: time, purge_keys: {
id,
limit,
},
}, },
'auto', 'auto',
'', '',
@ -349,12 +334,15 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
return res.transactionHash return res.transactionHash
} }
const updateStartTradingTime = async (senderAddress: string, time?: Timestamp): Promise<string> => { const purgeOwners = async (senderAddress: string, id: number, limit?: number): Promise<string> => {
const res = await client.execute( const res = await client.execute(
senderAddress, senderAddress,
contractAddress, contractAddress,
{ {
update_start_trading_time: time || null, purge_owners: {
id,
limit,
},
}, },
'auto', 'auto',
'', '',
@ -363,12 +351,15 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
return res.transactionHash return res.transactionHash
} }
const updatePerAddressLimit = async (senderAddress: string, perAddressLimit: number): Promise<string> => { const mintByMinter = async (senderAddress: string, id: number, owners: string[]): Promise<string> => {
const res = await client.execute( const res = await client.execute(
senderAddress, senderAddress,
contractAddress, contractAddress,
{ {
update_per_address_limit: { per_address_limit: perAddressLimit }, mint_by_minter: {
id,
owners,
},
}, },
'auto', 'auto',
'', '',
@ -377,12 +368,16 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
return res.transactionHash return res.transactionHash
} }
const mintTo = async (senderAddress: string, recipient: string): Promise<string> => { const mintByKey = async (senderAddress: string, id: number, owner: string, signature: string): Promise<string> => {
const res = await client.execute( const res = await client.execute(
senderAddress, senderAddress,
contractAddress, contractAddress,
{ {
mint_to: { recipient }, mint_by_key: {
id,
owner,
signature,
},
}, },
'auto', 'auto',
'', '',
@ -391,12 +386,23 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
return res.transactionHash return res.transactionHash
} }
const mintFor = async (senderAddress: string, recipient: string, tokenId: number): Promise<string> => { const mintByKeys = async (
senderAddress: string,
id: number,
owner: string,
pubkey: string,
signature: string,
): Promise<string> => {
const res = await client.execute( const res = await client.execute(
senderAddress, senderAddress,
contractAddress, contractAddress,
{ {
mint_for: { token_id: tokenId, recipient }, mint_by_keys: {
id,
owner,
pubkey,
signature,
},
}, },
'auto', 'auto',
'', '',
@ -405,130 +411,14 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
return res.transactionHash return res.transactionHash
} }
const batchMintFor = async (senderAddress: string, recipient: string, tokenIds: string): Promise<string> => { const setNft = async (senderAddress: string, nft: string): Promise<string> => {
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<string> => {
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<string> => {
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<string> => {
const res = await client.execute( const res = await client.execute(
senderAddress, senderAddress,
contractAddress, contractAddress,
{ {
shuffle: {}, set_nft: {
}, nft,
'auto', },
'',
[coin(500000000, 'ustars')],
)
return res.transactionHash
}
const withdraw = async (senderAddress: string): Promise<string> => {
const res = await client.execute(
senderAddress,
contractAddress,
{
withdraw: {},
},
'auto',
'',
)
return res.transactionHash
}
const burnRemaining = async (senderAddress: string): Promise<string> => {
const res = await client.execute(
senderAddress,
contractAddress,
{
burn_remaining: {},
}, },
'auto', 'auto',
'', '',
@ -540,25 +430,19 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
return { return {
contractAddress, contractAddress,
getConfig, getConfig,
getMintableNumTokens, getBadge,
getStartTime, getBadges,
getMintPrice, getKey,
getMintCount, getKeys,
mint, createBadge,
purge, editBadge,
updateMintPrice, addKeys,
setWhitelist, purgeKeys,
updateStartTime, purgeOwners,
updateStartTradingTime, mintByMinter,
updatePerAddressLimit, mintByKey,
mintTo, mintByKeys,
mintFor, setNft,
batchMintFor,
batchMint,
airdrop,
shuffle,
withdraw,
burnRemaining,
} }
} }
@ -581,9 +465,7 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
initMsg: Record<string, unknown>, initMsg: Record<string, unknown>,
label: string, label: string,
): Promise<InstantiateResponse> => { ): Promise<InstantiateResponse> => {
const result = await client.instantiate(senderAddress, codeId, initMsg, label, 'auto', { const result = await client.instantiate(senderAddress, codeId, initMsg, label, 'auto')
funds: [coin('3000000000', 'ustars')],
})
return { return {
contractAddress: result.contractAddress, contractAddress: result.contractAddress,
@ -593,215 +475,148 @@ export const badgeHub = (client: SigningCosmWasmClient, txSigner: string): Badge
} }
const messages = (contractAddress: string) => { const messages = (contractAddress: string) => {
const mint = (): MintMessage => { const createBadge = (badge: Badge): CreateBadgeMessage => {
return { return {
sender: txSigner, sender: txSigner,
contract: contractAddress, contract: contractAddress,
msg: { msg: {
mint: {}, create_badge: {
}, manager: badge.manager,
funds: [], metadata: badge.metadata,
} transferrable: badge.transferrable,
} rule: badge.rule,
expiry: badge.expiry,
const purge = (): PurgeMessage => { max_supply: badge.max_supply,
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(),
}, },
}, },
funds: [], funds: [],
} }
} }
const setWhitelist = (whitelist: string): SetWhitelistMessage => { const editBadge = (id: number, metadata: Metadata): EditBadgeMessage => {
return { return {
sender: txSigner, sender: txSigner,
contract: contractAddress, contract: contractAddress,
msg: { msg: {
set_whitelist: { edit_badge: {
whitelist, id,
metadata,
}, },
}, },
funds: [], funds: [],
} }
} }
const updateStartTime = (startTime: string): UpdateStartTimeMessage => { const addKeys = (id: number, keys: string[]): AddKeysMessage => {
return { return {
sender: txSigner, sender: txSigner,
contract: contractAddress, contract: contractAddress,
msg: { msg: {
update_start_time: startTime, add_keys: {
}, id,
funds: [], keys,
}
}
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,
}, },
}, },
funds: [], funds: [],
} }
} }
const mintTo = (recipient: string): MintToMessage => { const purgeKeys = (id: number, limit?: number): PurgeKeysMessage => {
return { return {
sender: txSigner, sender: txSigner,
contract: contractAddress, contract: contractAddress,
msg: { msg: {
mint_to: { purge_keys: {
recipient, id,
limit,
}, },
}, },
funds: [], funds: [],
} }
} }
const mintFor = (recipient: string, tokenId: number): MintForMessage => { const purgeOwners = (id: number, limit?: number): PurgeOwnersMessage => {
return { return {
sender: txSigner, sender: txSigner,
contract: contractAddress, contract: contractAddress,
msg: { msg: {
mint_for: { purge_owners: {
recipient, id,
token_id: tokenId, limit,
}, },
}, },
funds: [], funds: [],
} }
} }
const batchMintFor = (recipient: string, tokenIds: string): BatchMintForMessage => { const mintByMinter = (id: number, owners: string[]): MintByMinterMessage => {
const msg: Record<string, unknown>[] = []
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<string, unknown>[] = []
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<string, unknown>[] = []
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 => {
return { return {
sender: txSigner, sender: txSigner,
contract: contractAddress, contract: contractAddress,
msg: { msg: {
shuffle: {}, mint_by_minter: {
id,
owners,
},
}, },
funds: [], funds: [],
} }
} }
const withdraw = (): WithdrawMessage => { const mintByKey = (id: number, owner: string, signature: string): MintByKeyMessage => {
return { return {
sender: txSigner, sender: txSigner,
contract: contractAddress, contract: contractAddress,
msg: { msg: {
withdraw: {}, mint_by_key: {
id,
owner,
signature,
},
}, },
funds: [], funds: [],
} }
} }
const burnRemaining = (): BurnRemainingMessage => { const mintByKeys = (id: number, owner: string, pubkey: string, signature: string): MintByKeysMessage => {
return { return {
sender: txSigner, sender: txSigner,
contract: contractAddress, contract: contractAddress,
msg: { 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: [], funds: [],
} }
} }
return { return {
mint, createBadge,
purge, editBadge,
updateMintPrice, addKeys,
setWhitelist, purgeKeys,
updateStartTime, purgeOwners,
updateStartTradingTime, mintByMinter,
updatePerAddressLimit, mintByKey,
mintTo, mintByKeys,
mintFor, setNft,
batchMintFor,
batchMint,
airdrop,
shuffle,
withdraw,
burnRemaining,
} }
} }

View File

@ -1,21 +1,18 @@
import type { BadgeHubInstance } from '../index' import type { Badge, BadgeHubInstance, Metadata } from '../index'
import { useBadgeHubContract } from '../index' import { useBadgeHubContract } from '../index'
export type ExecuteType = typeof EXECUTE_TYPES[number] export type ExecuteType = typeof EXECUTE_TYPES[number]
export const EXECUTE_TYPES = [ export const EXECUTE_TYPES = [
'mint', 'create_badge',
'purge', 'edit_badge',
'update_mint_price', 'add_keys',
'set_whitelist', 'purge_keys',
'update_start_time', 'purge_owners',
'update_start_trading_time', 'mint_by_minter',
'update_per_address_limit', 'mint_by_key',
'mint_to', 'mint_by_keys',
'mint_for', 'set_nft',
'shuffle',
'withdraw',
'burn_remaining',
] as const ] as const
export interface ExecuteListItem { export interface ExecuteListItem {
@ -26,59 +23,49 @@ export interface ExecuteListItem {
export const EXECUTE_LIST: ExecuteListItem[] = [ export const EXECUTE_LIST: ExecuteListItem[] = [
{ {
id: 'mint', id: 'create_badge',
name: 'Mint', name: 'Create Badge',
description: `Mint new tokens for a given address`, description: `Create a new badge with the specified mint rule and metadata`,
}, },
{ {
id: 'purge', id: 'edit_badge',
name: 'Purge', name: 'Edit Badge',
description: `Purge`, description: `Edit the badge with the specified ID`,
}, },
{ {
id: 'update_mint_price', id: 'add_keys',
name: 'Update Mint Price', name: 'Add Keys',
description: `Update mint price`, description: `Add keys to the badge with the specified ID`,
}, },
{ {
id: 'set_whitelist', id: 'purge_keys',
name: 'Set Whitelist', name: 'Purge Keys',
description: `Set whitelist contract address`, description: `Purge keys from the badge with the specified ID`,
}, },
{ {
id: 'update_start_time', id: 'purge_owners',
name: 'Update Start Time', name: 'Purge Owners',
description: `Update start time for minting`, description: `Purge owners from the badge with the specified ID`,
}, },
{ {
id: 'update_start_trading_time', id: 'mint_by_minter',
name: 'Update Start Trading Time', name: 'Mint by Minter',
description: `Update start trading time for minting`, description: `Mint a new token by the minter with the specified ID`,
}, },
{ {
id: 'update_per_address_limit', id: 'mint_by_key',
name: 'Update Per Address Limit', name: 'Mint by Key',
description: `Update token per address limit`, description: `Mint a new token by the key with the specified ID`,
}, },
{ {
id: 'mint_to', id: 'mint_by_keys',
name: 'Mint To', name: 'Mint by Keys',
description: `Mint tokens to a given address`, description: `Mint a new token by the keys with the specified ID`,
}, },
{ {
id: 'mint_for', id: 'set_nft',
name: 'Mint For', name: 'Set NFT',
description: `Mint tokens for a given address with a given token ID`, description: `Set the Badge NFT contract address for the Badge Hub contract`,
},
{
id: 'shuffle',
name: 'Shuffle',
description: `Shuffle the token IDs`,
},
{
id: 'burn_remaining',
name: 'Burn Remaining',
description: `Burn remaining tokens`,
}, },
] ]
@ -89,25 +76,22 @@ export interface DispatchExecuteProps {
type Select<T extends ExecuteType> = T type Select<T extends ExecuteType> = T
/** @see {@link VendingMinterInstance} */ /** @see {@link BadgeHubInstance} */
export type DispatchExecuteArgs = { export type DispatchExecuteArgs = {
contract: string contract: string
messages?: BadgeHubInstance messages?: BadgeHubInstance
txSigner: string txSigner: string
} & ( } & (
| { type: undefined } | { type: undefined }
| { type: Select<'mint'> } | { type: Select<'create_badge'>; badge: Badge }
| { type: Select<'purge'> } | { type: Select<'edit_badge'>; id: number; metadata: Metadata }
| { type: Select<'update_mint_price'>; price: string } | { type: Select<'add_keys'>; id: number; keys: string[] }
| { type: Select<'set_whitelist'>; whitelist: string } | { type: Select<'purge_keys'>; id: number; limit?: number }
| { type: Select<'update_start_time'>; startTime: string } | { type: Select<'purge_owners'>; id: number; limit?: number }
| { type: Select<'update_start_trading_time'>; startTime?: string } | { type: Select<'mint_by_minter'>; id: number; owners: string[] }
| { type: Select<'update_per_address_limit'>; limit: number } | { type: Select<'mint_by_key'>; id: number; owner: string; signature: string }
| { type: Select<'mint_to'>; recipient: string } | { type: Select<'mint_by_keys'>; id: number; owner: string; pubkey: string; signature: string }
| { type: Select<'mint_for'>; recipient: string; tokenId: number } | { type: Select<'set_nft'>; nft: string }
| { type: Select<'shuffle'> }
| { type: Select<'withdraw'> }
| { type: Select<'burn_remaining'> }
) )
export const dispatchExecute = async (args: DispatchExecuteArgs) => { 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') throw new Error('cannot dispatch execute, messages is not defined')
} }
switch (args.type) { switch (args.type) {
case 'mint': { case 'create_badge': {
return messages.mint(txSigner) return messages.createBadge(txSigner, args.badge)
} }
case 'purge': { case 'edit_badge': {
return messages.purge(txSigner) return messages.editBadge(txSigner, args.id, args.metadata)
} }
case 'update_mint_price': { case 'add_keys': {
return messages.updateMintPrice(txSigner, args.price) return messages.addKeys(txSigner, args.id, args.keys)
} }
case 'set_whitelist': { case 'purge_keys': {
return messages.setWhitelist(txSigner, args.whitelist) return messages.purgeKeys(txSigner, args.id, args.limit)
} }
case 'update_start_time': { case 'purge_owners': {
return messages.updateStartTime(txSigner, args.startTime) return messages.purgeOwners(txSigner, args.id, args.limit)
} }
case 'update_start_trading_time': { case 'mint_by_minter': {
return messages.updateStartTradingTime(txSigner, args.startTime) return messages.mintByMinter(txSigner, args.id, args.owners)
} }
case 'update_per_address_limit': { case 'mint_by_key': {
return messages.updatePerAddressLimit(txSigner, args.limit) return messages.mintByKey(txSigner, args.id, args.owner, args.signature)
} }
case 'mint_to': { case 'mint_by_keys': {
return messages.mintTo(txSigner, args.recipient) return messages.mintByKeys(txSigner, args.id, args.owner, args.pubkey, args.signature)
} }
case 'mint_for': { case 'set_nft': {
return messages.mintFor(txSigner, args.recipient, args.tokenId) return messages.setNft(txSigner, args.nft)
}
case 'shuffle': {
return messages.shuffle(txSigner)
}
case 'withdraw': {
return messages.withdraw(txSigner)
}
case 'burn_remaining': {
return messages.burnRemaining(txSigner)
} }
default: { default: {
throw new Error('unknown execute type') throw new Error('unknown execute type')
@ -163,41 +138,32 @@ export const previewExecutePayload = (args: DispatchExecuteArgs) => {
const { messages } = useBadgeHubContract() const { messages } = useBadgeHubContract()
const { contract } = args const { contract } = args
switch (args.type) { switch (args.type) {
case 'mint': { case 'create_badge': {
return messages(contract)?.mint() return messages(contract)?.createBadge(args.badge)
} }
case 'purge': { case 'edit_badge': {
return messages(contract)?.purge() return messages(contract)?.editBadge(args.id, args.metadata)
} }
case 'update_mint_price': { case 'add_keys': {
return messages(contract)?.updateMintPrice(args.price) return messages(contract)?.addKeys(args.id, args.keys)
} }
case 'set_whitelist': { case 'purge_keys': {
return messages(contract)?.setWhitelist(args.whitelist) return messages(contract)?.purgeKeys(args.id, args.limit)
} }
case 'update_start_time': { case 'purge_owners': {
return messages(contract)?.updateStartTime(args.startTime) return messages(contract)?.purgeOwners(args.id, args.limit)
} }
case 'update_start_trading_time': { case 'mint_by_minter': {
return messages(contract)?.updateStartTradingTime(args.startTime as string) return messages(contract)?.mintByMinter(args.id, args.owners)
} }
case 'update_per_address_limit': { case 'mint_by_key': {
return messages(contract)?.updatePerAddressLimit(args.limit) return messages(contract)?.mintByKey(args.id, args.owner, args.signature)
} }
case 'mint_to': { case 'mint_by_keys': {
return messages(contract)?.mintTo(args.recipient) return messages(contract)?.mintByKeys(args.id, args.owner, args.pubkey, args.signature)
} }
case 'mint_for': { case 'set_nft': {
return messages(contract)?.mintFor(args.recipient, args.tokenId) return messages(contract)?.setNft(args.nft)
}
case 'shuffle': {
return messages(contract)?.shuffle()
}
case 'withdraw': {
return messages(contract)?.withdraw()
}
case 'burn_remaining': {
return messages(contract)?.burnRemaining()
} }
default: { default: {
return {} return {}

View File

@ -2,7 +2,7 @@ import type { BadgeHubInstance } from '../contract'
export type QueryType = typeof QUERY_TYPES[number] 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 { export interface QueryListItem {
id: QueryType id: QueryType
@ -12,39 +12,36 @@ export interface QueryListItem {
export const QUERY_LIST: QueryListItem[] = [ export const QUERY_LIST: QueryListItem[] = [
{ id: 'config', name: 'Config', description: 'View current config' }, { 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: 'getBadge', name: 'Query Badge', description: 'Query a badge by ID' },
{ id: 'start_time', name: 'Start Time', description: 'View the start time for minting' }, { id: 'getBadges', name: 'Query a list of Badges', description: 'Query the list of badges' },
{ id: 'mint_price', name: 'Mint Price', description: 'View the mint price' }, { 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' },
id: 'mint_count',
name: 'Total Minted Count',
description: 'View the total amount of minted tokens for an address',
},
] ]
export interface DispatchQueryProps { export interface DispatchQueryProps {
address: string id: number
pubkey: string
messages: BadgeHubInstance | undefined messages: BadgeHubInstance | undefined
type: QueryType type: QueryType
} }
export const dispatchQuery = (props: DispatchQueryProps) => { export const dispatchQuery = (props: DispatchQueryProps) => {
const { address, messages, type } = props const { id, pubkey, messages, type } = props
switch (type) { switch (type) {
case 'config': { case 'config': {
return messages?.getConfig() return messages?.getConfig()
} }
case 'mintable_num_tokens': { case 'getBadge': {
return messages?.getMintableNumTokens() return messages?.getBadge(id)
} }
case 'start_time': { case 'getBadges': {
return messages?.getStartTime() return messages?.getBadges()
} }
case 'mint_price': { case 'getKey': {
return messages?.getMintPrice() return messages?.getKey(id, pubkey)
} }
case 'mint_count': { case 'getKeys': {
return messages?.getMintCount(address) return messages?.getKeys(id)
} }
default: { default: {
throw new Error('unknown query type') throw new Error('unknown query type')