增加一个计算erc20转账价格的接口
This commit is contained in:
parent
286d24c5b0
commit
06313311c3
@ -1,5 +1,4 @@
|
||||
import { singleton } from 'decorators/singleton'
|
||||
import logger from 'logger/logger'
|
||||
import { TaskType } from 'models/RequestTask'
|
||||
import { ConfirmQueue } from 'queue/confirm.queue'
|
||||
import Web3 from 'web3'
|
||||
@ -9,6 +8,7 @@ import { ERC721Reactor } from './ERC721Reactor'
|
||||
import { HttpRetryProvider } from './HttpRetryProvider'
|
||||
import { WalletReactor } from './WalletReactor'
|
||||
import { DistributorReactor } from './DistributorReactor'
|
||||
import { fromTokenMinimalUnit } from 'utils/number.util'
|
||||
|
||||
@singleton
|
||||
export class BlockChain {
|
||||
@ -96,4 +96,11 @@ export class BlockChain {
|
||||
}
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
@ -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({
|
||||
address,
|
||||
to,
|
||||
|
16
src/controllers/token.controllers.ts
Normal file
16
src/controllers/token.controllers.ts
Normal 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
|
Loading…
x
Reference in New Issue
Block a user