From e8896352a196aa7cae97a5d6fa72acae2d8087de Mon Sep 17 00:00:00 2001 From: Yana Date: Fri, 10 Nov 2023 17:27:49 +0000 Subject: [PATCH] Add CosmWasmSigningClient --- .../Delegations/components/DelegateModal.tsx | 78 ++++++++++++++----- explorer/src/components/TableToolbar.tsx | 1 + 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/explorer/src/components/Delegations/components/DelegateModal.tsx b/explorer/src/components/Delegations/components/DelegateModal.tsx index 8a9e844017b..d01bdd6b990 100644 --- a/explorer/src/components/Delegations/components/DelegateModal.tsx +++ b/explorer/src/components/Delegations/components/DelegateModal.tsx @@ -2,23 +2,23 @@ import React, { useCallback, useContext, useState, useEffect, ChangeEvent } from import { Box, Typography, SxProps, TextField } from '@mui/material'; import { IdentityKeyFormField } from '@nymproject/react/mixnodes/IdentityKeyFormField'; import { CurrencyFormField } from '@nymproject/react/currency/CurrencyFormField'; -import { CurrencyDenom, FeeDetails, DecCoin, decimalToFloatApproximation } from '@nymproject/types'; -import { Console } from '../utils/console'; -import { useGetFee } from '../hooks/useGetFee'; -import { debounce } from 'lodash'; +import { CurrencyDenom, FeeDetails, DecCoin, decimalToFloatApproximation, Coin } from '@nymproject/types'; + import { SimpleModal } from './SimpleModal'; import { ModalListItem } from './ModalListItem'; -import { TPoolOption, checkTokenBalance, validateAmount, validateKey } from '../utils'; +import { TPoolOption, validateAmount } from '../utils'; import { useChain } from '@cosmos-kit/react'; +import { StdFee } from '@cosmjs/amino'; +import { ExecuteResult } from '@cosmjs/cosmwasm-stargate'; + import { uNYMtoNYM } from '../utils'; import { ErrorModal } from './ErrorModal'; -import { ConfirmTx } from './ConfirmTX'; -import { BalanceWarning } from './FeeWarning'; -import { getMixnodeStakeSaturation, simulateDelegateToMixnode, tryConvertIdentityToMixId } from '../requests'; const MIN_AMOUNT_TO_DELEGATE = 10; +const MIXNET_CONTRACT_ADDRESS = 'n17srjznxl9dvzdkpwpw24gg668wc73val88a6m5ajg6ankwvz9wtst0cznr'; +const sandboxContractAddress = 'n1xr3rq8yvd7qplsw5yx90ftsr2zdhg4e9z60h5duusgxpv72hud3sjkxkav'; export const DelegateModal: FCWithChildren<{ open: boolean; @@ -67,14 +67,15 @@ export const DelegateModal: FCWithChildren<{ }) => { const [mixId, setMixId] = useState(); const [identityKey, setIdentityKey] = useState(initialIdentityKey); - const [amount, setAmount] = useState(initialAmount); + const [amount, setAmount] = useState(); const [isValidated, setValidated] = useState(false); const [errorAmount, setErrorAmount] = useState(); // const [tokenPool, setTokenPool] = useState('balance'); const [errorIdentityKey, setErrorIdentityKey] = useState(); const [mixIdError, setMixIdError] = useState(); + const [cosmWasmSignerClient, setCosmWasmSignerClient] = useState(); - const { fee, getFee, resetFeeState, feeError } = useGetFee(); + // const { fee, getFee, resetFeeState, feeError } = useGetFee(); const { username, @@ -86,6 +87,7 @@ export const DelegateModal: FCWithChildren<{ getCosmWasmClient, isWalletConnected, getSigningCosmWasmClient, + estimateFee, } = useChain('nyx'); const [balance, setBalance] = useState<{ @@ -93,6 +95,16 @@ export const DelegateModal: FCWithChildren<{ data?: string; }>({ status: 'loading', data: undefined }); + useEffect(() => { + const getClient = async () => { + await getSigningCosmWasmClient() + .then((res) => setCosmWasmSignerClient(res)) + .catch((e) => console.log('e :>> ', e)); + }; + + getClient(); + }, []); + useEffect(() => { const getBalance = async (walletAddress: string) => { const account = await getCosmWasmClient(); @@ -117,7 +129,7 @@ export const DelegateModal: FCWithChildren<{ errorIdentityKeyMessage = 'Please enter a valid identity key'; } - if (amount && !(await validateAmount(amount, '0'))) { + if (amount && !(await validateAmount(amount.amount, '0'))) { newValidatedValue = false; errorAmountMessage = 'Please enter a valid amount'; } @@ -127,7 +139,7 @@ export const DelegateModal: FCWithChildren<{ newValidatedValue = false; } - if (!amount?.length) { + if (!amount?.amount.length) { newValidatedValue = false; } @@ -148,11 +160,11 @@ export const DelegateModal: FCWithChildren<{ setValidated(newValidatedValue); }; - const handleOk = async () => { - if (onOk && amount && identityKey && mixId) { - onOk(mixId, identityKey, { amount, denom }, 'balance', fee); - } - }; + // const handleOk = async () => { + // if (onOk && amount && identityKey && mixId) { + // onOk(mixId, identityKey, { amount }, 'balance', fee); + // } + // }; // const handleConfirm = async ({ mixId: id, value }: { mixId: number; value: DecCoin }) => { // const SCWClient = await getSigningCosmWasmClient(); @@ -160,10 +172,36 @@ export const DelegateModal: FCWithChildren<{ // console.log('SCWClient :>> ', SCWClient); // }; + const delegateToMixnode = async ( + { + mixId, + }: { + mixId: number; + }, + fee: number | StdFee | 'auto' = 'auto', + memo?: string, + _funds?: DecCoin[], + ): Promise => { + return await cosmWasmSignerClient.execute( + address, + MIXNET_CONTRACT_ADDRESS, + { + delegate_to_mixnode: { + mix_id: mixId, + }, + }, + fee, + memo, + _funds, + ); + }; + const handleConfirm = async () => { - const SCWClient = await getSigningCosmWasmClient(); + const memo: string = 'test delegation'; - console.log('SCWClient :>> ', SCWClient); + if (mixId && amount) { + await delegateToMixnode({ mixId }, 'auto', memo, [amount]); + } }; const handleIdentityKeyChanged = (newIdentityKey: string) => { @@ -180,7 +218,7 @@ export const DelegateModal: FCWithChildren<{ }; const handleAmountChanged = (newAmount: DecCoin) => { - setAmount(newAmount.amount); + setAmount(newAmount); if (onAmountChanged) { onAmountChanged(newAmount.amount); diff --git a/explorer/src/components/TableToolbar.tsx b/explorer/src/components/TableToolbar.tsx index f5aae9f38b2..eefe864db40 100644 --- a/explorer/src/components/TableToolbar.tsx +++ b/explorer/src/components/TableToolbar.tsx @@ -53,6 +53,7 @@ export const TableToolbar: FCWithChildren = ({ blocks: 10000, }, }; + if (nyx.apis) nyx.apis.rpc = [{ address: 'https://rpc.nymtech.net', provider: 'nym' }]; } } return chains;