Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/revoke-gas-account' into tm…
Browse files Browse the repository at this point in the history
…p/20241025
  • Loading branch information
kim12322222 committed Oct 23, 2024
2 parents 326e982 + aead27d commit f5032d4
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 14 deletions.
1 change: 1 addition & 0 deletions _raw/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,7 @@
"stillRevoke": "Still Revoke",
"paused": "Paused",
"waitInQueue": "Wait in queue",
"useGasAccount": "Your gas balance is low. Your GasAccount will cover the gas fees.",
"revokeWithLedger": "Start Revoke with Ledger",
"connectLedger": "Connect Ledger",
"ledgerSended": "Please sign the request on Ledger ({{current}}/{{total}})",
Expand Down
77 changes: 64 additions & 13 deletions src/ui/utils/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
EVENTS,
INTERNAL_REQUEST_ORIGIN,
INTERNAL_REQUEST_SESSION,
KEYRING_TYPE,
} from '@/constant';
import { intToHex, WalletControllerType } from '@/ui/utils';
import { findChain, isTestnet } from '@/utils/chain';
Expand Down Expand Up @@ -56,20 +57,29 @@ export const sendTransaction = async ({
lowGasDeadline,
isGasLess,
isGasAccount,
gasAccount,
autoUseGasAccount,
waitCompleted = true,
pushType = 'default',
ignoreGasNotEnoughCheck,
onUseGasAccount,
}: {
tx: Tx;
chainServerId: string;
wallet: WalletControllerType;
ignoreGasCheck?: boolean;
ignoreGasNotEnoughCheck?: boolean;
onProgress?: (status: ProgressStatus) => void;
onUseGasAccount?: () => void;
gasLevel?: GasLevel;
lowGasDeadline?: number;
isGasLess?: boolean;
isGasAccount?: boolean;
gasAccount?: {
sig: string | undefined;
accountId: string | undefined;
};
autoUseGasAccount?: boolean;
waitCompleted?: boolean;
pushType?: TxPushType;
}) => {
Expand All @@ -78,7 +88,10 @@ export const sendTransaction = async ({
serverId: chainServerId,
})!;
const support1559 = chain.eip['1559'];
const { address } = (await wallet.getCurrentAccount())!;
const {
address,
type: currentAccountType,
} = (await wallet.getCurrentAccount())!;
const recommendNonce = await wallet.getRecommendNonce({
from: tx.from,
chainId: chain.id,
Expand Down Expand Up @@ -186,9 +199,57 @@ export const sendTransaction = async ({
? (await Browser.storage.local.get('DEBUG_OTHER_CHAIN_GAS_USD_LIMIT'))
.DEBUG_OTHER_CHAIN_GAS_USD_LIMIT || 5
: 5;

// generate tx with gas
const transaction: Tx = {
from: tx.from,
to: tx.to,
data: tx.data,
nonce: recommendNonce,
value: tx.value,
chainId: tx.chainId,
gas: gasLimit,
};

let failedCode;
let canUseGasAccount: boolean = false;
if (isGasNotEnough) {
failedCode = FailedCode.GasNotEnough;
if (autoUseGasAccount && gasAccount?.sig && gasAccount?.accountId) {
// native gas not enough check gasAccount
let gasAccountVerfiyPass = true;
let gasAccountCost;
try {
gasAccountCost = await wallet.openapi.checkGasAccountTxs({
sig: gasAccount?.sig || '',
account_id: gasAccount?.accountId || '',
tx_list: [
{
...transaction,
gas: gasLimit,
gasPrice: intToHex(normalGas.price),
},
],
});
} catch (e) {
gasAccountVerfiyPass = false;
}
const gasAccountCanPay =
gasAccountVerfiyPass &&
currentAccountType !== KEYRING_TYPE.WalletConnectKeyring &&
currentAccountType !== KEYRING_TYPE.WatchAddressKeyring &&
!!gasAccountCost?.balance_is_enough &&
!gasAccountCost.chain_not_support &&
!!gasAccountCost.is_gas_account;

if (gasAccountCanPay) {
onUseGasAccount?.();
canUseGasAccount = true;
} else {
failedCode = FailedCode.GasNotEnough;
}
} else {
failedCode = FailedCode.GasNotEnough;
}
} else if (
!ignoreGasCheck &&
// eth gas > $20
Expand All @@ -208,16 +269,6 @@ export const sendTransaction = async ({
};
}

// generate tx with gas
const transaction: Tx = {
from: tx.from,
to: tx.to,
data: tx.data,
nonce: recommendNonce,
value: tx.value,
chainId: tx.chainId,
gas: gasLimit,
};
const maxPriorityFee = calcMaxPriorityFee([], normalGas, chain.id, true);
const maxFeePerGas = intToHex(Math.round(normalGas.price));

Expand Down Expand Up @@ -336,7 +387,7 @@ export const sendTransaction = async ({
logId: logId,
lowGasDeadline,
isGasLess,
isGasAccount,
isGasAccount: autoUseGasAccount ? canUseGasAccount : isGasAccount,
pushType,
},
pushed: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const GasRow: React.FC<Props> = ({ record }) => {
return null;
}

if (record.$status?.status === 'pending' && record.$status?.isGasAccount) {
return null;
}

if (!record.$status || record.$status.status === 'pending') {
return <div>-</div>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const HashRow: React.FC<Props> = ({ record }) => {
return null;
}

if (record.$status?.status === 'pending' && record.$status?.isGasAccount) {
return null;
}

if (!record.$status || record.$status.status === 'pending') {
return <div>-</div>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export const StatusRow: React.FC<Props> = ({
{(record.$status?.status === 'pending' || isStillRevoke) && (
<>
<LoadingSVG className="text-blue-light" />
{record.$status?.status === 'pending' &&
record.$status?.isGasAccount && (
<span className="text-r-blue-default">
{t('page.approvals.revokeModal.useGasAccount')}
</span>
)}
{isStillRevoke && (
<span className="text-r-neutral-foot">
{t('page.approvals.revokeModal.waitInQueue')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React from 'react';
import { findIndexRevokeList } from '../../utils';
import i18n from '@/i18n';
import { FailedCode, sendTransaction } from '@/ui/utils/sendTransaction';
import { useGasAccountSign } from '@/ui/views/GasAccount/hooks';
export { FailedCode } from '@/ui/utils/sendTransaction';

async function buildTx(
Expand Down Expand Up @@ -71,6 +72,7 @@ export type AssetApprovalSpenderWithStatus = AssetApprovalSpender & {
$status?:
| {
status: 'pending';
isGasAccount?: boolean;
}
| {
status: 'fail';
Expand Down Expand Up @@ -134,6 +136,7 @@ const cloneAssetApprovalSpender = (item: AssetApprovalSpender) => {

export const useBatchRevokeTask = () => {
const wallet = useWallet();
const gasAccount = useGasAccountSign();
const queueRef = React.useRef(
new PQueue({ concurrency: 1, autoStart: true })
);
Expand Down Expand Up @@ -170,21 +173,30 @@ export const useBatchRevokeTask = () => {

cloneItem.$status!.status = 'pending';
setList((prev) => updateAssetApprovalSpender(prev, cloneItem));

try {
const tx = await buildTx(wallet, revokeItem);
const result = await sendTransaction({
tx,
ignoreGasCheck,
wallet,
chainServerId: revokeItem.chainServerId,
gasAccount: gasAccount,
autoUseGasAccount: true,
onProgress: (status) => {
if (status === 'builded') {
setTxStatus('sended');
} else if (status === 'signed') {
setTxStatus('signed');
}
},
onUseGasAccount: () => {
// update status
cloneItem.$status = {
status: 'pending',
isGasAccount: true,
};
setList((prev) => updateAssetApprovalSpender(prev, cloneItem));
},
});
// update status
cloneItem.$status = {
Expand Down

0 comments on commit f5032d4

Please sign in to comment.