diff --git a/src/wallet/BitgetWallet.js b/src/wallet/BitgetWallet.js index 2a2febc..b2998e2 100644 --- a/src/wallet/BitgetWallet.js +++ b/src/wallet/BitgetWallet.js @@ -1,8 +1,8 @@ import { providers } from "ethers" import { signLogin } from './utils.js' import { apiPreRecharge } from "./../utils/request.js" -import { switchEthereumChain } from './utils.js' - +import { switchEthereumChain, queryAllowance } from './utils.js' +import { FirebaseUtil } from "@/utils/firebase.util"; export class BitgetWallet{ constructor() { @@ -23,7 +23,7 @@ export class BitgetWallet{ params: [{ from, to, - data + data, }] }) // console.log(txHash) @@ -76,14 +76,25 @@ export class BitgetWallet{ throw new Error(errmsg) } if (!calls || calls.length == 0) { + new FirebaseUtil().uploadEvent('charge_nocall', data) throw new Error('no calls') } await this.checkAndChangeChain(data.net_id, provider) let resutls = [] - for (let i = 0; i < calls.length; i++) { - const { to, data } = calls[i].trans_req - let hash = await this.sendTransaction(provider, {from: data.account_address, to, data}) - resutls.push(hash) + let user = data.account_address + if (calls.length > 1) { + const contractAddress = calls[1].trans_req.to + // get calls[1].data last 32 bytes + const amount = BigInt(parseInt(calls[0].trans_req.data.slice(-64), 16)) + let allowance = await queryAllowance(provider, calls[0].trans_req.to, user, contractAddress) + if (allowance < amount) { + let hash = await this.sendTransaction(provider, {from: user, to: calls[0].trans_req.to, data: calls[0].trans_req.data}) + resutls.push(hash) + } + let hash2 = await this.sendTransaction(provider, {from: user, to: contractAddress, data: calls[1].trans_req.data}) + resutls.push(hash2) + } else { + throw new Error('no enough calls') } return resutls } diff --git a/src/wallet/utils.js b/src/wallet/utils.js index f758478..55a1b48 100644 --- a/src/wallet/utils.js +++ b/src/wallet/utils.js @@ -151,6 +151,11 @@ export function toHexChainId(chainId) { return '0x' + chainId.toString(16) } +export function queryAllowance(provider, token, owner, spender) { + const contract = new ethers.Contract(token, ['function allowance(address,address) view returns (uint256)'], provider); + return contract.allowance(owner, spender); +} + export const switchEthereumChain = async (provider, targetChainId) => { let chainCfg; for (const d of AllChains) {