const {web3} = require("hardhat"); const ierc20abi = require("../../abi/IERC20_ABI.json"); const BaseModel = require("../../model/base-model"); module.exports = class TokenLib { static LEVEL = { NOT: 'not', DISCARD: 'discard', INIT: 'init', NORMAL: 'normal', TOP: 'top' } constructor(web3, chain) { this.web3 = web3 this.chain = chain this.tokenContractMap = {} this.tokenModel = new BaseModel(this.chain.id, BaseModel.MODULES.TOKEN) } getTokenContract(tokenAddress) { if (!this.tokenContractMap[tokenAddress]) { this.tokenContractMap[tokenAddress] = new this.web3.eth.Contract(ierc20abi, tokenAddress) } return this.tokenContractMap[tokenAddress] } async parseToken(lp, isZero) { const token = { 'hash': isZero ? lp.token0 : lp.token1, 'symbol': isZero ? lp.symbol0 : lp.symbol1, 'decimals': isZero ? lp.decimals0 : lp.decimals1 } let tokenContract = this.getTokenContract(token.hash) token.name = await tokenContract.methods.name().call() token.name = this.getEffectiveName(token.name) token.level = TokenLib.LEVEL.INIT // 链信息 token.chainId = this.chain.id return token } getEffectiveSymbol(originSymbol) { return originSymbol.replace(/[^A-Za-z0-9]+/g, '').substring(0, 10) } getEffectiveName(originName) { return originName.replace(/[^A-Za-z0-9]+/g, '').substring(0, 37) } async saveToken(token) { const saveRst = await this.tokenModel.appendOrUpdate(token) if (!saveRst.state) throw Error(saveRst.msg) } }