增加一个计算erc20转账价格的接口

This commit is contained in:
zhl 2023-05-22 11:00:20 +08:00
parent 286d24c5b0
commit 06313311c3
3 changed files with 30 additions and 1 deletions

View File

@ -1,5 +1,4 @@
import { singleton } from 'decorators/singleton' import { singleton } from 'decorators/singleton'
import logger from 'logger/logger'
import { TaskType } from 'models/RequestTask' import { TaskType } from 'models/RequestTask'
import { ConfirmQueue } from 'queue/confirm.queue' import { ConfirmQueue } from 'queue/confirm.queue'
import Web3 from 'web3' import Web3 from 'web3'
@ -9,6 +8,7 @@ import { ERC721Reactor } from './ERC721Reactor'
import { HttpRetryProvider } from './HttpRetryProvider' import { HttpRetryProvider } from './HttpRetryProvider'
import { WalletReactor } from './WalletReactor' import { WalletReactor } from './WalletReactor'
import { DistributorReactor } from './DistributorReactor' import { DistributorReactor } from './DistributorReactor'
import { fromTokenMinimalUnit } from 'utils/number.util'
@singleton @singleton
export class BlockChain { export class BlockChain {
@ -96,4 +96,11 @@ export class BlockChain {
} }
return abi return abi
} }
public async generateGasShow(gas: any) {
let price = await this.web3.eth.getGasPrice()
let ehtBN = this.web3.utils.toBN(price).mul(this.web3.utils.toBN(gas))
let eth = fromTokenMinimalUnit(ehtBN, 18)
return { gas, price, eth }
}
} }

View File

@ -135,6 +135,12 @@ export class ERC20Reactor {
}) })
} }
async calcTransferGas({ address }: { address: string }) {
const contract = new this.web3.eth.Contract(abiFt, address, { from: this.account.address })
let gas = await contract.methods.transferFrom(this.account.address, this.account.address, 0).estimateGas()
return gas
}
async mint({ async mint({
address, address,
to, to,

View File

@ -0,0 +1,16 @@
import BaseController from 'common/base.controller'
import { role, router } from 'decorators/router'
import { BlockChain } from 'chain/BlockChain'
import { ZError } from 'common/ZError'
class TokenController extends BaseController {
@role('anon')
@router('post /chain/estimate_transfer_gas')
async calcGasPrice(req, res) {
const { address } = req.params
let gas = await new BlockChain().erc20Reactor.calcTransferGas({ address })
let data = await new BlockChain().generateGasShow(gas)
return data
}
}
export default TokenController