Skip to content

Commit

Permalink
Revert "feat: udate gasMarket api (#2653)" (#2666)
Browse files Browse the repository at this point in the history
This reverts commit 1da1db0.
  • Loading branch information
heisenberg-2077 authored Dec 7, 2024
1 parent d4e35cc commit 5ebb302
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 165 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@rabby-wallet/gnosis-sdk": "1.3.9",
"@rabby-wallet/page-provider": "0.4.2",
"@rabby-wallet/rabby-action": "0.1.8",
"@rabby-wallet/rabby-api": "0.8.7",
"@rabby-wallet/rabby-api": "0.8.5",
"@rabby-wallet/rabby-security-engine": "2.0.7",
"@rabby-wallet/rabby-swap": "0.0.42",
"@rabby-wallet/widgets": "1.0.9",
Expand Down
5 changes: 1 addition & 4 deletions src/ui/utils/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ export const sendTransaction = async ({
// get gas
let normalGas = gasLevel;
if (!normalGas) {
const gasMarket = await wallet.openapi.gasMarket({
chainId: chainServerId,
tx,
});
const gasMarket = await wallet.openapi.gasMarket(chainServerId);
normalGas = gasMarket.find((item) => item.level === 'normal')!;
}

Expand Down
10 changes: 4 additions & 6 deletions src/ui/views/Approval/components/MiniSignTx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,10 @@ export const MiniSignTx = ({
chain: Chain,
custom?: number
): Promise<GasLevel[]> => {
const list = await wallet.openapi.gasMarket({
chainId: chain.serverId,
customGas: custom && custom > 0 ? custom : undefined,
tx: txs[0],
});
const list = await wallet.openapi.gasMarket(
chain.serverId,
custom && custom > 0 ? custom : undefined
);
setGasList(list);
return list;
};
Expand Down Expand Up @@ -851,7 +850,6 @@ export const MiniSignTx = ({
className={clsx(task.status !== 'idle' && 'pointer-events-none')}
>
<GasSelectorHeader
tx={txs[0]}
gasAccountCost={gasAccountCost}
gasMethod={gasMethod}
onChangeGasMethod={setGasMethod}
Expand Down
1 change: 0 additions & 1 deletion src/ui/views/Approval/components/SignTestnetTx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,6 @@ export const SignTestnetTx = ({ params, origin }: SignTxProps) => {
<FooterBar
Header={
<GasSelectorHeader
tx={tx}
disabled={false}
isReady={isReady}
gasLimit={gasLimit}
Expand Down
10 changes: 4 additions & 6 deletions src/ui/views/Approval/components/SignTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1339,11 +1339,10 @@ const SignTx = ({ params, origin }: SignTxProps) => {
chain: Chain,
custom?: number
): Promise<GasLevel[]> => {
const list = await wallet.openapi.gasMarket({
chainId: chain.serverId,
customGas: custom && custom > 0 ? custom : undefined,
tx,
});
const list = await wallet.openapi.gasMarket(
chain.serverId,
custom && custom > 0 ? custom : undefined
);
setGasList(list);
return list;
};
Expand Down Expand Up @@ -1997,7 +1996,6 @@ const SignTx = ({ params, origin }: SignTxProps) => {
<FooterBar
Header={
<GasSelectorHeader
tx={tx}
gasAccountCost={gasAccountCost}
gasMethod={gasMethod}
onChangeGasMethod={setGasMethod}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Input, Skeleton, Tooltip } from 'antd';
import { matomoRequestEvent } from '@/utils/matomo-request';
import { ValidateStatus } from 'antd/lib/form/FormItem';
import { GasLevel, Tx, TxPushType } from 'background/service/openapi';
import { GasLevel, TxPushType } from 'background/service/openapi';
import BigNumber from 'bignumber.js';
import clsx from 'clsx';
import {
Expand Down Expand Up @@ -58,7 +58,6 @@ export interface GasSelectorResponse extends GasLevel {
}

interface GasSelectorProps {
tx: Tx;
gasLimit: string | undefined;
gas: {
gasCostUsd: number | string | BigNumber;
Expand Down Expand Up @@ -299,7 +298,6 @@ const GasSelectorHeader = ({
gasMethod,
gasAccountCost,
onChangeGasMethod,
tx,
}: GasSelectorProps) => {
const wallet = useWallet();
const dispatch = useRabbyDispatch();
Expand Down Expand Up @@ -352,11 +350,10 @@ const GasSelectorHeader = ({

const loadCustomGasData = useCallback(
async (custom?: number): Promise<GasLevel> => {
const list = await wallet.openapi.gasMarket({
chainId: chain.serverId,
customGas: custom && custom > 0 ? custom : undefined,
tx,
});
const list = await wallet.openapi.gasMarket(
chain.serverId,
custom && custom > 0 ? custom : undefined
);
return list.find((item) => item.level === 'custom')!;
},
[]
Expand Down
4 changes: 1 addition & 3 deletions src/ui/views/Dashboard/components/GasPriceBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ export const GasPriceBar: React.FC<Props> = ({ currentConnectedSiteChain }) => {
? await wallet.getCustomTestnetGasMarket({
chainId: chain?.id,
})
: await wallet.openapi.gasMarket({
chainId: currentConnectedSiteChainNativeToken,
});
: await wallet.openapi.gasMarket(currentConnectedSiteChainNativeToken);
const selectedGasPice = marketGas.find((item) => item.level === 'slow')
?.price;
if (selectedGasPice) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export const useDbkChainBridge = ({
);

const fetchGasPrice = useMemoizedFn(async (serverId: string) => {
const marketGas = await wallet.openapi.gasMarket({ chainId: serverId });
const marketGas = await wallet.openapi.gasMarket(serverId);
const selectedGasPice = marketGas.find((item) => item.level === 'normal');
return selectedGasPice;
});
Expand Down
177 changes: 71 additions & 106 deletions src/ui/views/SendToken/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { query2obj } from 'ui/utils/url';
import { formatTokenAmount, splitNumberByStep } from 'ui/utils/number';
import AccountCard from '../Approval/components/AccountCard';
import TokenAmountInput from 'ui/component/TokenAmountInput';
import { GasLevel, TokenItem, Tx } from 'background/service/openapi';
import { GasLevel, TokenItem } from 'background/service/openapi';
import { PageHeader, AddressViewer } from 'ui/component';
import ContactEditModal from 'ui/component/Contact/EditModal';
import ContactListModal from 'ui/component/Contact/ListModal';
Expand Down Expand Up @@ -461,109 +461,21 @@ const SendToken = () => {
const isNativeToken =
!!chainItem && currentToken?.id === chainItem.nativeTokenAddress;

const { addressType } = useCheckAddressType(formSnapshot.to, chainItem);

const {
isShowMessageDataForToken,
isShowMessageDataForContract,
} = useMemo(() => {
return {
isShowMessageDataForToken: isNativeToken && addressType === 'EOA',
isShowMessageDataForContract: isNativeToken && addressType === 'CONTRACT',
};
}, [isNativeToken, addressType]);

const getParams = React.useCallback(
({
to,
amount,
messageDataForSendToEoa,
messageDataForContractCall,
}: FormSendToken) => {
const chain = findChain({
serverId: currentToken.chain,
})!;
const sendValue = new BigNumber(amount || 0)
.multipliedBy(10 ** currentToken.decimals)
.decimalPlaces(0, BigNumber.ROUND_DOWN);
const dataInput = [
{
name: 'transfer',
type: 'function',
inputs: [
{
type: 'address',
name: 'to',
},
{
type: 'uint256',
name: 'value',
},
] as any[],
} as const,
[
to || '0x0000000000000000000000000000000000000000',
sendValue.toFixed(0),
] as any[],
] as const;
const params: Record<string, any> = {
chainId: chain.id,
from: currentAccount!.address,
to: currentToken.id,
value: '0x0',
data: abiCoder.encodeFunctionCall(dataInput[0], dataInput[1]),
isSend: true,
};
if (safeInfo?.nonce != null) {
params.nonce = safeInfo.nonce;
}
if (isNativeToken) {
params.to = to;
delete params.data;

if (isShowMessageDataForToken && messageDataForSendToEoa) {
const encodedValue = formatTxInputDataOnERC20(messageDataForSendToEoa)
.hexData;

params.data = encodedValue;
} else if (isShowMessageDataForContract && messageDataForContractCall) {
params.data = messageDataForContractCall;
}

params.value = `0x${sendValue.toString(16)}`;
}

return params;
},
[
currentAccount,
currentToken.chain,
currentToken.decimals,
currentToken.id,
isNativeToken,
isShowMessageDataForContract,
isShowMessageDataForToken,
safeInfo,
]
);

const fetchGasList = useCallback(async () => {
const values = form.getFieldsValue();
const params = getParams(values) as Tx;

const list: GasLevel[] = chainItem?.isTestnet
? await wallet.getCustomTestnetGasMarket({ chainId: chainItem.id })
: await wallet.openapi.gasMarket({
chainId: chainItem?.serverId || '',
tx: params,
});
: await wallet.openapi.gasMarket(chainItem?.serverId || '');
return list;
}, [chainItem, form, getParams, wallet]);
}, [wallet, chainItem]);

const [{ value: gasList }, loadGasList] = useAsyncFn(() => {
return fetchGasList();
}, [fetchGasList]);

useEffect(() => {
loadGasList();
}, [loadGasList]);

useDebounce(
async () => {
const targetChain = findChainByEnum(chain)!;
Expand All @@ -573,17 +485,32 @@ const SendToken = () => {
gasPriceMap[targetChain.enum].expireAt > Date.now()
) {
gasList = gasPriceMap[targetChain.enum].list;
} else {
gasList = await fetchGasList();
setGasPriceMap({
...gasPriceMap,
[targetChain.enum]: {
list: gasList,
expireAt: Date.now() + 300000, // cache gasList for 5 mins
},
});
}
},
500,
[chain]
);

useEffect(() => {
if (clickedMax) {
loadGasList();
}
}, [clickedMax, loadGasList]);
const { addressType } = useCheckAddressType(formSnapshot.to, chainItem);

const {
isShowMessageDataForToken,
isShowMessageDataForContract,
} = useMemo(() => {
return {
isShowMessageDataForToken: isNativeToken && addressType === 'EOA',
isShowMessageDataForContract: isNativeToken && addressType === 'CONTRACT',
};
}, [isNativeToken, addressType]);

const handleSubmit = async ({
to,
Expand All @@ -595,13 +522,51 @@ const SendToken = () => {
const chain = findChain({
serverId: currentToken.chain,
})!;
const params = getParams({
to,
amount,
messageDataForSendToEoa,
messageDataForContractCall,
});
const sendValue = new BigNumber(amount)
.multipliedBy(10 ** currentToken.decimals)
.decimalPlaces(0, BigNumber.ROUND_DOWN);
const dataInput = [
{
name: 'transfer',
type: 'function',
inputs: [
{
type: 'address',
name: 'to',
},
{
type: 'uint256',
name: 'value',
},
] as any[],
} as const,
[to, sendValue.toFixed(0)] as any[],
] as const;
const params: Record<string, any> = {
chainId: chain.id,
from: currentAccount!.address,
to: currentToken.id,
value: '0x0',
data: abiCoder.encodeFunctionCall(dataInput[0], dataInput[1]),
isSend: true,
};
if (safeInfo?.nonce != null) {
params.nonce = safeInfo.nonce;
}
if (isNativeToken) {
params.to = to;
delete params.data;

if (isShowMessageDataForToken && messageDataForSendToEoa) {
const encodedValue = formatTxInputDataOnERC20(messageDataForSendToEoa)
.hexData;

params.data = encodedValue;
} else if (isShowMessageDataForContract && messageDataForContractCall) {
params.data = messageDataForContractCall;
}

params.value = `0x${sendValue.toString(16)}`;
// L2 has extra validation fee so we can not set gasLimit as 21000 when send native token
const couldSpecifyIntrinsicGas = !CAN_NOT_SPECIFY_INTRINSIC_GAS_CHAINS.includes(
chain.enum
Expand Down
16 changes: 4 additions & 12 deletions src/ui/views/Swap/hooks/quote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,7 @@ export const useQuoteMethods = () => {
const lastTimeGas: ChainGas | null = await walletController.getLastTimeGasSelection(
CHAINS[chain].id
);
const gasMarket = await walletOpenapi.gasMarket({
chainId: CHAINS[chain].serverId,
tx: {
...quote.tx,
nonce,
chainId: CHAINS[chain].id,
gas: '0x0',
},
});
const gasMarket = await walletOpenapi.gasMarket(CHAINS[chain].serverId);

let gasPrice = 0;
if (lastTimeGas?.lastTimeSelect === 'gasPrice' && lastTimeGas.gasPrice) {
Expand Down Expand Up @@ -329,9 +321,9 @@ export const useQuoteMethods = () => {
try {
let gasPrice: number;
if (isOpenOcean) {
const gasMarket = await walletOpenapi.gasMarket({
chainId: CHAINS[chain].serverId,
});
const gasMarket = await walletOpenapi.gasMarket(
CHAINS[chain].serverId
);
gasPrice = gasMarket?.[1]?.price;
}
stats.report('swapRequestQuote', {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/views/Swap/hooks/token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export const useTokenPair = (userAddress: string) => {
const { value: gasList } = useAsync(() => {
gasPriceRef.current = undefined;
setGasLevel('normal');
return wallet.openapi.gasMarket({ chainId: CHAINS[chain].serverId });
return wallet.openapi.gasMarket(CHAINS[chain].serverId);
}, [chain]);

const [reserveGasOpen, setReserveGasOpen] = useState(false);
Expand Down
Loading

0 comments on commit 5ebb302

Please sign in to comment.