修改充值流程, approval在足够的情况下, 只调用一次

This commit is contained in:
CounterFire2023 2024-08-29 13:21:35 +08:00
parent a7b14fb769
commit 37089dca15
2 changed files with 23 additions and 7 deletions

View File

@ -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
}

View File

@ -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) {