Merge pull request #70 from public-awesome/release/v1.x-develop

migrate option
This commit is contained in:
Jorge Hernandez 2022-11-25 06:07:52 -06:00 committed by GitHub
commit 9eab06738b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 288 additions and 7 deletions

View File

@ -34,6 +34,11 @@ export const minterLinkTabs: LinkTabProps[] = [
description: `Execute Minter contract actions`,
href: '/contracts/minter/execute',
},
{
title: 'Migrate',
description: `Migrate Minter contract`,
href: '/contracts/minter/migrate',
},
]
export const whitelistLinkTabs: LinkTabProps[] = [

View File

@ -12,6 +12,11 @@ export interface InstantiateResponse {
readonly logs: readonly logs.Log[]
}
export interface MigrateResponse {
readonly transactionHash: string
readonly logs: readonly logs.Log[]
}
export interface RoyalityInfo {
payment_address: string
share: string
@ -38,6 +43,8 @@ export interface MinterInstance {
shuffle: (senderAddress: string) => Promise<string>
withdraw: (senderAddress: string) => Promise<string>
airdrop: (senderAddress: string, recipients: string[]) => Promise<string>
updateDiscountPrice: (senderAddress: string, price: string) => Promise<string>
removeDiscountPrice: (senderAddress: string) => Promise<string>
}
export interface MinterMessages {
@ -51,6 +58,26 @@ export interface MinterMessages {
shuffle: () => ShuffleMessage
withdraw: () => WithdrawMessage
airdrop: (recipients: string[]) => CustomMessage
updateDiscountPrice: (price: string) => UpdateDiscountPriceMessage
removeDiscountPrice: () => RemoveDiscountPriceMessage
}
export interface UpdateDiscountPriceMessage {
sender: string
contract: string
msg: {
update_discount_price: Record<string, unknown>
}
funds: Coin[]
}
export interface RemoveDiscountPriceMessage {
sender: string
contract: string
msg: {
remove_discount_price: Record<string, never>
}
funds: Coin[]
}
export interface MintMessage {
@ -151,6 +178,13 @@ export interface MinterContract {
funds?: Coin[],
) => Promise<InstantiateResponse>
migrate: (
senderAddress: string,
contractAddress: string,
codeId: number,
migrateMsg: Record<string, unknown>,
) => Promise<MigrateResponse>
use: (contractAddress: string) => MinterInstance
messages: (contractAddress: string) => MinterMessages
@ -354,6 +388,35 @@ export const minter = (client: SigningCosmWasmClient, txSigner: string): MinterC
return res.transactionHash
}
const updateDiscountPrice = async (senderAddress: string, price: string): Promise<string> => {
const res = await client.execute(
senderAddress,
contractAddress,
{
update_discount_price: {
price,
},
},
'auto',
'',
)
return res.transactionHash
}
const removeDiscountPrice = async (senderAddress: string): Promise<string> => {
const res = await client.execute(
senderAddress,
contractAddress,
{
remove_discount_price: {},
},
'auto',
'',
)
return res.transactionHash
}
return {
contractAddress,
getConfig,
@ -371,6 +434,21 @@ export const minter = (client: SigningCosmWasmClient, txSigner: string): MinterC
airdrop,
shuffle,
withdraw,
updateDiscountPrice,
removeDiscountPrice,
}
}
const migrate = async (
senderAddress: string,
contractAddress: string,
codeId: number,
migrateMsg: Record<string, unknown>,
): Promise<MigrateResponse> => {
const result = await client.migrate(senderAddress, contractAddress, codeId, migrateMsg, 'auto')
return {
transactionHash: result.transactionHash,
logs: result.logs,
}
}
@ -514,6 +592,28 @@ export const minter = (client: SigningCosmWasmClient, txSigner: string): MinterC
funds: [],
}
}
const updateDiscountPrice = (price: string): UpdateDiscountPriceMessage => {
return {
sender: txSigner,
contract: contractAddress,
msg: {
update_discount_price: {
price,
},
},
funds: [],
}
}
const removeDiscountPrice = (): RemoveDiscountPriceMessage => {
return {
sender: txSigner,
contract: contractAddress,
msg: {
remove_discount_price: {},
},
funds: [],
}
}
return {
mint,
@ -526,8 +626,10 @@ export const minter = (client: SigningCosmWasmClient, txSigner: string): MinterC
airdrop,
shuffle,
withdraw,
updateDiscountPrice,
removeDiscountPrice,
}
}
return { use, instantiate, messages }
return { use, instantiate, messages, migrate }
}

View File

@ -12,6 +12,8 @@ export const EXECUTE_TYPES = [
'mint_for',
'shuffle',
'withdraw',
'update_discount_price',
'remove_discount_price',
] as const
export interface ExecuteListItem {
@ -56,6 +58,16 @@ export const EXECUTE_LIST: ExecuteListItem[] = [
name: 'Shuffle',
description: `Shuffle the token IDs`,
},
{
id: 'update_discount_price',
name: 'Update Discount Price',
description: `Updates discount price`,
},
{
id: 'remove_discount_price',
name: 'Remove Discount Price',
description: `Removes discount price`,
},
]
export interface DispatchExecuteProps {
@ -80,6 +92,8 @@ export type DispatchExecuteArgs = {
| { type: Select<'mint_for'>; recipient: string; tokenId: number }
| { type: Select<'shuffle'> }
| { type: Select<'withdraw'> }
| { type: Select<'update_discount_price'>; price: string }
| { type: Select<'remove_discount_price'> }
)
export const dispatchExecute = async (args: DispatchExecuteArgs) => {
@ -112,6 +126,12 @@ export const dispatchExecute = async (args: DispatchExecuteArgs) => {
case 'withdraw': {
return messages.withdraw(txSigner)
}
case 'update_discount_price': {
return messages.updateDiscountPrice(txSigner, args.price === '' ? '0' : args.price)
}
case 'remove_discount_price': {
return messages.removeDiscountPrice(txSigner)
}
default: {
throw new Error('unknown execute type')
}
@ -147,6 +167,12 @@ export const previewExecutePayload = (args: DispatchExecuteArgs) => {
case 'withdraw': {
return messages(contract)?.withdraw()
}
case 'update_discount_price': {
return messages(contract)?.updateDiscountPrice(args.price === '' ? '0' : args.price)
}
case 'remove_discount_price': {
return messages(contract)?.removeDiscountPrice()
}
default: {
return {}
}

View File

@ -3,7 +3,7 @@ import type { logs } from '@cosmjs/stargate'
import { useWallet } from 'contexts/wallet'
import { useCallback, useEffect, useState } from 'react'
import type { MinterContract, MinterInstance, MinterMessages } from './contract'
import type { MigrateResponse, MinterContract, MinterInstance, MinterMessages } from './contract'
import { minter as initContract } from './contract'
/*export interface InstantiateResponse {
@ -32,6 +32,7 @@ export interface UseMinterContractProps {
admin?: string,
funds?: Coin[],
) => Promise<InstantiateResponse>
migrate: (contractAddress: string, codeId: number, migrateMsg: Record<string, unknown>) => Promise<MigrateResponse>
use: (customAddress: string) => MinterInstance | undefined
updateContractAddress: (contractAddress: string) => void
getContractAddress: () => string | undefined
@ -70,6 +71,20 @@ export function useMinterContract(): UseMinterContractProps {
[minter, wallet],
)
const migrate = useCallback(
(contractAddress: string, codeId: number, migrateMsg: Record<string, unknown>): Promise<MigrateResponse> => {
return new Promise((resolve, reject) => {
if (!minter) {
reject(new Error('Contract is not initialized.'))
return
}
console.log(wallet.address, contractAddress, codeId)
minter.migrate(wallet.address, contractAddress, codeId, migrateMsg).then(resolve).catch(reject)
})
},
[minter, wallet],
)
const use = useCallback(
(customAddress = ''): MinterInstance | undefined => {
return minter?.use(address || customAddress)
@ -94,5 +109,6 @@ export function useMinterContract(): UseMinterContractProps {
updateContractAddress,
getContractAddress,
messages,
migrate,
}
}

View File

@ -1,6 +1,6 @@
{
"name": "stargaze-studio",
"version": "0.1.0",
"version": "0.1.1",
"workspaces": [
"packages/*"
],

View File

@ -84,7 +84,7 @@ const MinterExecutePage: NextPage = () => {
const showLimitField = type === 'update_per_address_limit'
const showTokenIdField = type === 'mint_for'
const showRecipientField = isEitherType(type, ['mint_to', 'mint_for'])
const showPriceField = type === 'mint'
const showPriceField = isEitherType(type, ['mint', 'update_discount_price'])
const messages = useMemo(() => contract?.use(contractState.value), [contract, wallet.address, contractState.value])
const payload: DispatchExecuteArgs = {

View File

@ -0,0 +1,132 @@
import { Button } from 'components/Button'
import { ContractPageHeader } from 'components/ContractPageHeader'
import { useExecuteComboboxState } from 'components/contracts/minter/ExecuteCombobox.hooks'
import { FormControl } from 'components/FormControl'
import { AddressInput, NumberInput } from 'components/forms/FormInput'
import { useInputState, useNumberInputState } from 'components/forms/FormInput.hooks'
import { JsonPreview } from 'components/JsonPreview'
import { LinkTabs } from 'components/LinkTabs'
import { minterLinkTabs } from 'components/LinkTabs.data'
import { TransactionHash } from 'components/TransactionHash'
import { useContracts } from 'contexts/contracts'
import { useWallet } from 'contexts/wallet'
import type { MigrateResponse } from 'contracts/minter'
import type { NextPage } from 'next'
import { useRouter } from 'next/router'
import { NextSeo } from 'next-seo'
import type { FormEvent } from 'react'
import { useEffect, useState } from 'react'
import { toast } from 'react-hot-toast'
import { FaArrowRight } from 'react-icons/fa'
import { useMutation } from 'react-query'
import { withMetadata } from 'utils/layout'
import { links } from 'utils/links'
const MinterMigratePage: NextPage = () => {
const { minter: contract } = useContracts()
const wallet = useWallet()
const [lastTx, setLastTx] = useState('')
const comboboxState = useExecuteComboboxState()
const type = comboboxState.value?.id
const codeIdState = useNumberInputState({
id: 'code-id',
name: 'code-id',
title: 'Code ID',
subtitle: 'Code ID of the New Minter',
placeholder: '1',
})
const contractState = useInputState({
id: 'contract-address',
name: 'contract-address',
title: 'Minter Address',
subtitle: 'Address of the Minter contract',
})
const contractAddress = contractState.value
const { data, isLoading, mutate } = useMutation(
async (event: FormEvent): Promise<MigrateResponse | null> => {
event.preventDefault()
if (!contract) {
throw new Error('Smart contract connection failed')
}
if (!wallet.initialized) {
throw new Error('Please connect your wallet.')
}
const migrateMsg = {}
return toast.promise(contract.migrate(contractAddress, codeIdState.value, migrateMsg), {
error: `Migration failed!`,
loading: 'Executing message...',
success: (tx) => {
if (tx) {
setLastTx(tx.transactionHash)
}
return `Transaction success!`
},
})
},
{
onError: (error) => {
toast.error(String(error))
},
},
)
const router = useRouter()
useEffect(() => {
if (contractAddress.length > 0) {
void router.replace({ query: { contractAddress } })
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [contractAddress])
useEffect(() => {
const initial = new URL(document.URL).searchParams.get('contractAddress')
if (initial && initial.length > 0) contractState.onChange(initial)
}, [])
return (
<section className="py-6 px-12 space-y-4">
<NextSeo title="Execute Minter Contract" />
<ContractPageHeader
description="Minter contract facilitates primary market vending machine style minting."
link={links.Documentation}
title="Minter Contract"
/>
<LinkTabs activeIndex={3} data={minterLinkTabs} />
<form className="grid grid-cols-2 p-4 space-x-8" onSubmit={mutate}>
<div className="space-y-8">
<AddressInput {...contractState} />
<NumberInput isRequired {...codeIdState} />
</div>
<div className="space-y-8">
<div className="relative">
<Button className="absolute top-0 right-0" isLoading={isLoading} rightIcon={<FaArrowRight />} type="submit">
Execute
</Button>
<FormControl subtitle="View execution transaction hash" title="Transaction Hash">
<TransactionHash hash={lastTx} />
</FormControl>
</div>
<FormControl subtitle="View current message to be sent" title="Payload Preview">
<JsonPreview
content={{
sender: wallet.address,
contract: contractAddress,
code_id: codeIdState.value,
msg: {},
}}
isCopyable
/>
</FormControl>
</div>
</form>
</section>
)
}
export default withMetadata(MinterMigratePage, { center: false })

View File

@ -3557,9 +3557,9 @@ camelcase@^6.2.0:
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==
caniuse-lite@^1.0.30001283, caniuse-lite@^1.0.30001332:
version "1.0.30001332"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001332.tgz"
integrity sha512-10T30NYOEQtN6C11YGg411yebhvpnC6Z102+B95eAsN0oB6KUs01ivE8u+G6FMIRtIrVlYXhL+LUwQ3/hXwDWw==
version "1.0.30001434"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001434.tgz"
integrity sha512-aOBHrLmTQw//WFa2rcF1If9fa3ypkC1wzqqiKHgfdrXTWcU8C4gKVZT77eQAPWN1APys3+uQ0Df07rKauXGEYA==
carbites@^1.0.6:
version "1.0.6"