From 3c2478cd38fb09ef444ded7fdad86a7811b3fbae Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Tue, 28 Feb 2023 17:00:44 +0300 Subject: [PATCH] Enable Mint Rule: By Keys & By Minter for badge creation using the dashboard --- pages/contracts/badgeHub/execute.tsx | 172 +++++++++++++++++++++++++-- 1 file changed, 159 insertions(+), 13 deletions(-) diff --git a/pages/contracts/badgeHub/execute.tsx b/pages/contracts/badgeHub/execute.tsx index e4d6780..e078be8 100644 --- a/pages/contracts/badgeHub/execute.tsx +++ b/pages/contracts/badgeHub/execute.tsx @@ -1,11 +1,14 @@ /* eslint-disable eslint-comments/disable-enable-pair */ +/* eslint-disable no-nested-ternary */ /* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ import { toUtf8 } from '@cosmjs/encoding' +import clsx from 'clsx' import { Alert } from 'components/Alert' +import type { MintRule } from 'components/badges/creation/ImageUploadDetails' import { Button } from 'components/Button' import { Conditional } from 'components/Conditional' import { ContractPageHeader } from 'components/ContractPageHeader' @@ -66,6 +69,7 @@ const BadgeHubExecutePage: NextPage = () => { const [createdBadgeId, setCreatedBadgeId] = useState(null) const [createdBadgeKey, setCreatedBadgeKey] = useState(undefined) const [resolvedOwnerAddress, setResolvedOwnerAddress] = useState('') + const [resolvedMinterAddress, setResolvedMinterAddress] = useState('') const [signature, setSignature] = useState('') const [ownerList, setOwnerList] = useState([]) const [editFee, setEditFee] = useState(undefined) @@ -73,6 +77,7 @@ const BadgeHubExecutePage: NextPage = () => { const qrRef = useRef(null) const [numberOfKeys, setNumberOfKeys] = useState(0) const [keyPairs, setKeyPairs] = useState<{ publicKey: string; privateKey: string }[]>([]) + const [mintRule, setMintRule] = useState('by_key') const comboboxState = useExecuteComboboxState() const type = comboboxState.value?.id @@ -216,6 +221,14 @@ const BadgeHubExecutePage: NextPage = () => { subtitle: 'Number of keys/owners to execute the action for (0 for all)', }) + const designatedMinterState = useInputState({ + id: 'designatedMinter', + name: 'designatedMinter', + title: 'Minter Address', + subtitle: 'The address of the designated minter for this badge', + defaultValue: wallet.address, + }) + const showBadgeField = type === 'create_badge' const showMetadataField = isEitherType(type, ['create_badge', 'edit_badge']) const showIdField = isEitherType(type, [ @@ -258,9 +271,16 @@ const BadgeHubExecutePage: NextPage = () => { youtube_url: youtubeUrlState.value || undefined, }, transferrable, - rule: { - by_key: keyState.value, - }, + rule: + mintRule === 'by_key' + ? { + by_key: keyState.value, + } + : mintRule === 'by_minter' + ? { + by_minter: resolvedMinterAddress, + } + : 'by_keys', expiry: timestamp ? timestamp.getTime() * 1000000 : undefined, max_supply: maxSupplyState.value || undefined, }, @@ -284,7 +304,7 @@ const BadgeHubExecutePage: NextPage = () => { youtube_url: youtubeUrlState.value || undefined, }, id: badgeIdState.value, - owner: ownerState.value, + owner: resolvedOwnerAddress, pubkey: pubkeyState.value, signature, keys: keyPairs.map((keyPair) => keyPair.publicKey), @@ -368,6 +388,7 @@ const BadgeHubExecutePage: NextPage = () => { if (txHash) { setLastTx(txHash.split(':')[0]) setCreatedBadgeId(txHash.split(':')[1]) + badgeIdState.onChange(!isNaN(Number(txHash.split(':')[1])) ? Number(txHash.split(':')[1]) : 1) } } }, @@ -493,6 +514,15 @@ const BadgeHubExecutePage: NextPage = () => { void resolveOwnerAddress() }, [ownerState.value]) + const resolveMinterAddress = async () => { + await resolveAddress(designatedMinterState.value.trim(), wallet).then((resolvedAddress) => { + setResolvedMinterAddress(resolvedAddress) + }) + } + useEffect(() => { + void resolveMinterAddress() + }, [designatedMinterState.value]) + const resolveManagerAddress = async () => { await resolveAddress(managerState.value.trim(), wallet).then((resolvedAddress) => { setBadge({ @@ -517,9 +547,16 @@ const BadgeHubExecutePage: NextPage = () => { youtube_url: youtubeUrlState.value || undefined, }, transferrable, - rule: { - by_key: keyState.value, - }, + rule: + mintRule === 'by_key' + ? { + by_key: keyState.value, + } + : mintRule === 'by_minter' + ? { + by_minter: resolvedMinterAddress, + } + : 'by_keys', expiry: timestamp ? timestamp.getTime() * 1000000 : undefined, max_supply: maxSupplyState.value || undefined, }) @@ -553,9 +590,16 @@ const BadgeHubExecutePage: NextPage = () => { youtube_url: youtubeUrlState.value || undefined, }, transferrable, - rule: { - by_key: keyState.value, - }, + rule: + mintRule === 'by_key' + ? { + by_key: keyState.value, + } + : mintRule === 'by_minter' + ? { + by_minter: resolvedMinterAddress, + } + : 'by_keys', expiry: timestamp ? timestamp.getTime() * 1000000 : undefined, max_supply: maxSupplyState.value || undefined, }) @@ -586,7 +630,7 @@ const BadgeHubExecutePage: NextPage = () => { /> - {showBadgeField && createdBadgeId && createdBadgeKey && ( + {showBadgeField && createdBadgeId && createdBadgeKey && mintRule === 'by_key' && (
@@ -643,14 +687,116 @@ const BadgeHubExecutePage: NextPage = () => {
)} + + {showBadgeField && createdBadgeId && mintRule === 'by_keys' && ( + +
+ Badge ID:{` ${createdBadgeId as string}`} +
+
+
+ + You may select Message Type {'>'} Add Keys to add whitelisted keys authorized to mint a badge. + +
+
+
+
+ )} + + {showBadgeField && createdBadgeId && mintRule === 'by_minter' && ( + +
+ Badge successfully created with ID:{` ${createdBadgeId as string}`} +
+ Designated Minter Address: {` ${resolvedMinterAddress}`} +
+
+
+ + You may select Message Type {'>'} Mint by Minter to mint badges using the designated minter wallet. + +
+
+
+
+ )} +
+ +
+
+
+ Mint Rule: +
+ { + setMintRule('by_key') + setCreatedBadgeId(null) + }} + type="radio" + /> + +
+
+ { + setMintRule('by_keys') + setCreatedBadgeId(null) + }} + type="radio" + /> + +
+
+ { + setMintRule('by_minter') + setCreatedBadgeId(null) + }} + type="radio" + /> + +
+
+
+
+
{showIdField && } {showBadgeField && } - {showBadgeField && } - {showBadgeField && } + {showBadgeField && mintRule === 'by_key' && } + {showBadgeField && mintRule === 'by_key' && } + {showBadgeField && mintRule === 'by_minter' && } {showMetadataField && (
Metadata