Skip to content

Commit

Permalink
Add CosmWasmSigningClient
Browse files Browse the repository at this point in the history
  • Loading branch information
yanok87 committed Nov 10, 2023
1 parent 278b2e1 commit e889635
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
78 changes: 58 additions & 20 deletions explorer/src/components/Delegations/components/DelegateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,14 +67,15 @@ export const DelegateModal: FCWithChildren<{
}) => {
const [mixId, setMixId] = useState<number | undefined>();
const [identityKey, setIdentityKey] = useState<string | undefined>(initialIdentityKey);
const [amount, setAmount] = useState<string | undefined>(initialAmount);
const [amount, setAmount] = useState<DecCoin | undefined>();
const [isValidated, setValidated] = useState<boolean>(false);
const [errorAmount, setErrorAmount] = useState<string | undefined>();
// const [tokenPool, setTokenPool] = useState<TPoolOption>('balance');
const [errorIdentityKey, setErrorIdentityKey] = useState<string>();
const [mixIdError, setMixIdError] = useState<string>();
const [cosmWasmSignerClient, setCosmWasmSignerClient] = useState<any>();

const { fee, getFee, resetFeeState, feeError } = useGetFee();
// const { fee, getFee, resetFeeState, feeError } = useGetFee();

const {
username,
Expand All @@ -86,13 +87,24 @@ export const DelegateModal: FCWithChildren<{
getCosmWasmClient,
isWalletConnected,
getSigningCosmWasmClient,
estimateFee,
} = useChain('nyx');

const [balance, setBalance] = useState<{
status: 'loading' | 'success';
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();
Expand All @@ -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';
}
Expand All @@ -127,7 +139,7 @@ export const DelegateModal: FCWithChildren<{
newValidatedValue = false;
}

if (!amount?.length) {
if (!amount?.amount.length) {
newValidatedValue = false;
}

Expand All @@ -148,22 +160,48 @@ 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();

// console.log('SCWClient :>> ', SCWClient);
// };

const delegateToMixnode = async (
{
mixId,
}: {
mixId: number;
},
fee: number | StdFee | 'auto' = 'auto',
memo?: string,
_funds?: DecCoin[],
): Promise<ExecuteResult> => {
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) => {
Expand All @@ -180,7 +218,7 @@ export const DelegateModal: FCWithChildren<{
};

const handleAmountChanged = (newAmount: DecCoin) => {
setAmount(newAmount.amount);
setAmount(newAmount);

if (onAmountChanged) {
onAmountChanged(newAmount.amount);
Expand Down
1 change: 1 addition & 0 deletions explorer/src/components/TableToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const TableToolbar: FCWithChildren<TableToolBarProps> = ({
blocks: 10000,
},
};
if (nyx.apis) nyx.apis.rpc = [{ address: 'https://rpc.nymtech.net', provider: 'nym' }];
}
}
return chains;
Expand Down

0 comments on commit e889635

Please sign in to comment.