/* eslint-disable eslint-comments/disable-enable-pair */ /* eslint-disable @typescript-eslint/no-unnecessary-condition */ /* eslint-disable no-nested-ternary */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable tailwindcss/classnames-order */ /* eslint-disable react/button-has-type */ import { GasPrice, SigningStargateClient } from '@cosmjs/stargate' import { Alert } from 'components/Alert' import { Conditional } from 'components/Conditional' import { ContractPageHeader } from 'components/ContractPageHeader' import { FormControl } from 'components/FormControl' import { TextInput } from 'components/forms/FormInput' import { useInputState } from 'components/forms/FormInput.hooks' import { InputDateTime } from 'components/InputDateTime' import { LinkTabs } from 'components/LinkTabs' import { authzLinkTabs } from 'components/LinkTabs.data' import { getConfig } from 'config' import type { Msg } from 'config/authz' import { AuthzGenericGrantMsg } from 'config/authz' import { useGlobalSettings } from 'contexts/globalSettings' import type { NextPage } from 'next' import { NextSeo } from 'next-seo' import { useState } from 'react' import toast from 'react-hot-toast' import { NETWORK } from 'utils/constants' import { withMetadata } from 'utils/layout' import { useWallet } from 'utils/wallet' export type AuthorizationMode = 'Grant' | 'Revoke' export type GrantAuthorizationType = 'Generic' | 'Send' | 'Execute Smart Contract' | 'Migrate Smart Contract' export type GenericAuthorizationType = | 'MsgDelegate' | 'MsgUndelegate' | 'MsgBeginRedelegate' | 'MsgWithdrawDelegatorReward' | 'MsgVote' | 'MsgSend' | 'MsgExecuteContract' | 'MsgMigrateContract' const Grant: NextPage = () => { const wallet = useWallet() const { timezone } = useGlobalSettings() const [authMode, setAuthMode] = useState('Grant') const [authType, setAuthType] = useState('Generic') const [genericAuthType, setGenericAuthType] = useState('MsgSend') const [expiration, setExpiration] = useState() const [transactionHash, setTransactionHash] = useState(undefined) const granteeAddressState = useInputState({ id: 'grantee-address', name: 'granteeAddress', title: 'Grantee Address', placeholder: 'stars1...', subtitle: 'The address to grant authorization to', }) const spendLimitDenomState = useInputState({ id: 'spend-limit-denom', name: 'spendLimitDenom', title: 'Spend Limit Denom', placeholder: `ustars`, subtitle: 'The spend limit denom', }) const messageToSign = () => { if (authType === 'Generic') { if (genericAuthType === 'MsgSend') { return AuthzGenericGrantMsg( wallet.address || '', granteeAddressState.value, '/cosmos.bank.v1beta1.MsgSend', (expiration?.getTime() as number) / 1000 || 0, ) } if (genericAuthType === 'MsgDelegate') { return AuthzGenericGrantMsg( wallet.address || '', granteeAddressState.value, '/cosmos.staking.v1beta1.MsgDelegate', (expiration?.getTime() as number) / 1000 || 0, ) } if (genericAuthType === 'MsgUndelegate') { return AuthzGenericGrantMsg( wallet.address || '', granteeAddressState.value, '/cosmos.staking.v1beta1.MsgUndelegate', (expiration?.getTime() as number) / 1000 || 0, ) } } } const handleSendMessage = async () => { try { if (!wallet.isWalletConnected) return toast.error('Please connect your wallet.') setTransactionHash(undefined) const offlineSigner = wallet.getOfflineSignerDirect() const stargateClient = await SigningStargateClient.connectWithSigner(getConfig(NETWORK).rpcUrl, offlineSigner, { gasPrice: GasPrice.fromString('0.025ustars'), }) const response = await stargateClient.signAndBroadcast(wallet.address || '', [messageToSign() as Msg], 'auto') setTransactionHash(response.transactionHash) toast.success(`${authType} authorization success.`, { style: { maxWidth: 'none' } }) } catch (error: any) { toast.error(error.message, { style: { maxWidth: 'none' } }) setTransactionHash(undefined) console.error('Error: ', error) } } return (
Authorization Type
Generic Authorization Type
date ? setExpiration( timezone === 'Local' ? date : new Date(date?.getTime() - new Date().getTimezoneOffset() * 60 * 1000), ) : setExpiration(undefined) } value={ timezone === 'Local' ? expiration : expiration ? new Date(expiration.getTime() + new Date().getTimezoneOffset() * 60 * 1000) : undefined } /> {transactionHash && ( {`Transaction Hash: ${transactionHash}`} )}
) } export default withMetadata(Grant, { center: false })