token-lib.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const {web3} = require("hardhat");
  2. const ierc20abi = require("../../abi/IERC20_ABI.json");
  3. module.exports = class TokenLib {
  4. async handleToken(pool, zero) {
  5. const token = {
  6. 'address': zero ? pool.token0 : pool.token1,
  7. 'symbol': zero ? pool.symbol0 : pool.symbol1,
  8. 'decimals': zero ? pool.decimals0 : pool.decimals1,
  9. 'name': 'xxxxxxxx'
  10. }
  11. let tokenObj = this.tokenInstance[token.address]
  12. if (!tokenObj) {
  13. tokenObj = new web3.eth.Contract(ierc20abi, token.address)
  14. this.tokenInstance[token.address] = tokenObj
  15. }
  16. token.name = await tokenObj.methods.name().call()
  17. token.name = token.name.replace(/[^A-Za-z0-9 ]+/g, '').substring(0, 37)
  18. return token
  19. }
  20. async saveToken(lp, type = 'token') {
  21. try {
  22. const token = await this.handleToken(lp, true)
  23. // token的hash不同时为lp的情况才更新
  24. if (!this.lpInstance[token.address.toLowerCase()]) {
  25. await history.appendOrUpdate(type, token.address, token)
  26. }
  27. } catch (e) {
  28. }
  29. try {
  30. const token = await this.handleToken(lp, false)
  31. // token的hash不同时为lp的情况才更新
  32. if (!this.lpInstance[token.address.toLowerCase()]) {
  33. await history.appendOrUpdate(type, token.address, token)
  34. }
  35. } catch (e) {
  36. }
  37. }
  38. }