Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add min wckb #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions apps/ui/src/hooks/useMinWCKBQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { QueryObserverResult, useQuery } from 'react-query';
import { ForceBridgeContainer } from 'containers/ForceBridgeContainer';

export function useMinWCKBQuery(): QueryObserverResult<string, Error> {
const { api } = ForceBridgeContainer.useContainer();

return useQuery(
['getWCKBMin', {}],
() => {
return api.getWCKBMin();
},
{
enabled: true,
refetchInterval: false,
retry: false,
},
);
}
31 changes: 22 additions & 9 deletions apps/ui/src/views/Bridge/Ethereum/BridgeOperation/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -204,14 +214,17 @@ export const BridgeOperationForm: React.FC = () => {
}
extra={
selectedAsset && (
<Button
type="link"
size="small"
onClick={() => setBridgeFromAmount(BeautyAmount.from(selectedAsset).humanize({ separator: false }))}
>
Max:&nbsp;
<HumanizeAmount asset={selectedAsset} humanize={{ decimalPlaces: 4 }} />
</Button>
<div>
<Button
type="link"
size="small"
onClick={() => setBridgeFromAmount(BeautyAmount.from(selectedAsset).humanize({ separator: false }))}
>
Max:&nbsp;
<HumanizeAmount asset={selectedAsset} humanize={{ decimalPlaces: 4 }} />
</Button>
<div>minWCKB:&nbsp; {minWCKBQuery.data}</div>
</div>
)
}
placeholder="0.0"
Expand Down
11 changes: 10 additions & 1 deletion packages/commons/src/rpc/axon-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,6 +73,15 @@ export class AxonApiHandler implements API.ForceBridgeAPIV1 {
return Promise.reject(new Error('not implementation'));
}

getWCKBMin(): Promise<string> {
// 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<T extends NetworkTypes>(
payload: API.GenerateBridgeInTransactionPayload,
): Promise<API.GenerateTransactionResponse<T>> {
Expand Down
4 changes: 4 additions & 0 deletions packages/commons/src/rpc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ export class ForceBridgeAPIV1Handler implements API.ForceBridgeAPIV1 {
async getBridgeConfig(): Promise<API.GetConfigResponse> {
return this.client.request('getBridgeConfig');
}

getWCKBMin(): Promise<string> {
return Promise.reject(new Error('not implementation'));
}
}
2 changes: 2 additions & 0 deletions packages/commons/src/types/apiv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,7 @@ export interface ForceBridgeAPIV1 {
// prettier-ignore
getBridgeOutNervosBridgeFee: (payload: GetBridgeOutNervosBridgeFeePayload) => Promise<GetBridgeOutNervosBridgeFeeResponse>;

getWCKBMin: () => Promise<string>;

getBridgeConfig: () => Promise<GetConfigResponse>;
}