From 3691f3aa69bd2d839b8197fb39088d3c43d3f62e Mon Sep 17 00:00:00 2001 From: pygman Date: Mon, 25 Jul 2022 01:49:46 +0800 Subject: [PATCH] feat: add min wckb --- apps/ui/src/hooks/useMinWCKBQuery.ts | 18 +++++++++++ .../Bridge/Ethereum/BridgeOperation/index.tsx | 31 +++++++++++++------ packages/commons/src/rpc/axon-client.ts | 11 ++++++- packages/commons/src/rpc/client.ts | 4 +++ packages/commons/src/types/apiv1.ts | 2 ++ 5 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 apps/ui/src/hooks/useMinWCKBQuery.ts diff --git a/apps/ui/src/hooks/useMinWCKBQuery.ts b/apps/ui/src/hooks/useMinWCKBQuery.ts new file mode 100644 index 0000000..3930491 --- /dev/null +++ b/apps/ui/src/hooks/useMinWCKBQuery.ts @@ -0,0 +1,18 @@ +import { QueryObserverResult, useQuery } from 'react-query'; +import { ForceBridgeContainer } from 'containers/ForceBridgeContainer'; + +export function useMinWCKBQuery(): QueryObserverResult { + const { api } = ForceBridgeContainer.useContainer(); + + return useQuery( + ['getWCKBMin', {}], + () => { + return api.getWCKBMin(); + }, + { + enabled: true, + refetchInterval: false, + retry: false, + }, + ); +} diff --git a/apps/ui/src/views/Bridge/Ethereum/BridgeOperation/index.tsx b/apps/ui/src/views/Bridge/Ethereum/BridgeOperation/index.tsx index 160a155..fa211fb 100644 --- a/apps/ui/src/views/Bridge/Ethereum/BridgeOperation/index.tsx +++ b/apps/ui/src/views/Bridge/Ethereum/BridgeOperation/index.tsx @@ -1,9 +1,11 @@ import Icon from '@ant-design/icons'; -import { Button, Divider, Row, Spin, Typography } from 'antd'; +import { Button, Divider, Row, Spin, Typography, notification } from 'antd'; +import { ethers } from 'ethers'; import { useFormik } from 'formik'; import React, { useEffect, useMemo } from 'react'; import { useHistory, useLocation } from 'react-router-dom'; import styled from 'styled-components'; +import { useMinWCKBQuery } from '../../../../hooks/useMinWCKBQuery'; import { useAllowance } from '../hooks/useAllowance'; import { useApproveTransaction } from '../hooks/useApproveTransaction'; import { useChainId } from '../hooks/useChainId'; @@ -60,6 +62,7 @@ export const BridgeOperationForm: React.FC = () => { const history = useHistory(); const location = useLocation(); const { selectedAsset, setSelectedAsset } = useSelectBridgeAsset(); + const minWCKBQuery = useMinWCKBQuery(); const searchParams = useSearchParams(); const initRecipient = searchParams.get('recipient'); @@ -106,6 +109,13 @@ export const BridgeOperationForm: React.FC = () => { } else { const asset = direction === BridgeDirection.In ? selectedAsset.copy() : selectedAsset.shadow?.copy(); if (asset.info?.decimals == null) boom('asset info is not loaded'); + if (ethers.BigNumber.from(bridgeFromAmount).lt(ethers.BigNumber.from(minWCKBQuery.data))) { + notification.open({ + message: 'Bridge amount is too low', + description: `Bridge amount should be greater than ${minWCKBQuery.data}`, + }); + return; + } asset.amount = BeautyAmount.fromHumanize(bridgeFromAmount, asset.info.decimals).val.toString(); sendBridgeTransaction({ asset, recipient }).then(resetForm); @@ -204,14 +214,17 @@ export const BridgeOperationForm: React.FC = () => { } extra={ selectedAsset && ( - +
+ +
minWCKB:  {minWCKBQuery.data}
+
) } placeholder="0.0" diff --git a/packages/commons/src/rpc/axon-client.ts b/packages/commons/src/rpc/axon-client.ts index 94ae106..c1fe23c 100644 --- a/packages/commons/src/rpc/axon-client.ts +++ b/packages/commons/src/rpc/axon-client.ts @@ -5,7 +5,7 @@ import CrossChain from '../../src/abi/CrossChain.json'; import ERC20 from '../../src/abi/ERC20.json'; import { API, AssetType, NetworkBase, NetworkTypes, RequiredAsset } from '../types'; import { GetBalanceResponse } from '../types/apiv1'; -import { stringToUint8Array } from '../utils/index'; +import { stringToUint8Array } from '../utils'; export class AxonApiHandler implements API.ForceBridgeAPIV1 { provider: ethers.providers.JsonRpcProvider; @@ -73,6 +73,15 @@ export class AxonApiHandler implements API.ForceBridgeAPIV1 { return Promise.reject(new Error('not implementation')); } + getWCKBMin(): Promise { + // return Promise.resolve(Promise.resolve(this.client.request('getBridgeOutNervosBridgeFee', payload))); + return (async () => { + const minWCKB_amount = await this.crossChainContract.getWCKBMin(); + const minWCKB = minWCKB_amount.toString(); + return minWCKB; + })(); + } + async generateBridgeInNervosTransaction( payload: API.GenerateBridgeInTransactionPayload, ): Promise> { diff --git a/packages/commons/src/rpc/client.ts b/packages/commons/src/rpc/client.ts index a99b004..93fdabf 100644 --- a/packages/commons/src/rpc/client.ts +++ b/packages/commons/src/rpc/client.ts @@ -101,4 +101,8 @@ export class ForceBridgeAPIV1Handler implements API.ForceBridgeAPIV1 { async getBridgeConfig(): Promise { return this.client.request('getBridgeConfig'); } + + getWCKBMin(): Promise { + return Promise.reject(new Error('not implementation')); + } } diff --git a/packages/commons/src/types/apiv1.ts b/packages/commons/src/types/apiv1.ts index 7475eac..c5fa978 100644 --- a/packages/commons/src/types/apiv1.ts +++ b/packages/commons/src/types/apiv1.ts @@ -167,5 +167,7 @@ export interface ForceBridgeAPIV1 { // prettier-ignore getBridgeOutNervosBridgeFee: (payload: GetBridgeOutNervosBridgeFeePayload) => Promise; + getWCKBMin: () => Promise; + getBridgeConfig: () => Promise; }