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 24, 2024
2 parents fcc8642 + 9b51d75 commit b2e17da
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
1 change: 1 addition & 0 deletions _raw/locales/zh-CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@
"revokeOneByOne": "一一撤销",
"resume": "继续",
"pause": "暂停",
"useGasAccount": "Gas余额不足,你的GasAccount账户将支付这笔Gas费",
"gasNotEnough": "Gas 不足,无法提交",
"submitTxFailed": "提交失败",
"gasTooHigh": "煤气费高",
Expand Down
79 changes: 53 additions & 26 deletions src/ui/utils/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,45 @@ export enum FailedCode {

type ProgressStatus = 'building' | 'builded' | 'signed' | 'submitted';

const checkEnoughUseGasAccount = async ({
gasAccount,
wallet,
transaction,
currentAccountType,
}: {
transaction: Tx;
currentAccountType: string;
wallet: WalletControllerType;
gasAccount?: {
sig: string | undefined;
accountId: string | undefined;
};
}) => {
let gasAccountCanPay: boolean = false;

// 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],
});
} catch (e) {
gasAccountVerfiyPass = false;
}
gasAccountCanPay =
gasAccountVerfiyPass &&
currentAccountType !== KEYRING_TYPE.WalletConnectKeyring &&
currentAccountType !== KEYRING_TYPE.WatchAddressKeyring &&
!!gasAccountCost?.balance_is_enough &&
!gasAccountCost.chain_not_support &&
!!gasAccountCost.is_gas_account;

return gasAccountCanPay;
};

/**
* send transaction without rpcFlow
* @param tx
Expand All @@ -48,6 +87,9 @@ type ProgressStatus = 'building' | 'builded' | 'signed' | 'submitted';
* @param lowGasDeadline low gas deadline
* @param isGasLess is gas less
* @param isGasAccount is gas account
* @param gasAccount gas account { sig, account }
* @param autoUseGasAccount when gas balance is low , auto use gas account for gasfee
* @param onUseGasAccount use gas account callback
*/
export const sendTransaction = async ({
tx,
Expand Down Expand Up @@ -229,33 +271,18 @@ export const sendTransaction = async ({
let failedCode;
let canUseGasAccount: boolean = false;
if (isGasNotEnough) {
// native gas not enough check gasAccount
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;

const gasAccountCanPay = await checkEnoughUseGasAccount({
gasAccount,
currentAccountType,
wallet,
transaction: {
...transaction,
gas: gasLimit,
gasPrice: intToHex(normalGas.price),
},
});
if (gasAccountCanPay) {
onUseGasAccount?.();
canUseGasAccount = true;
Expand Down

0 comments on commit b2e17da

Please sign in to comment.