Ver código fonte

基本结构整理-8:web3接口封装,基本ierc20功能实现

龚成明 2 anos atrás
pai
commit
2b1b8ad94d

Diferenças do arquivo suprimidas por serem muito extensas
+ 0 - 0
src/config/config.js


+ 8 - 4
src/libs/web3/simple-ierc20.js

@@ -1,18 +1,22 @@
-const SimpleWeb3 = require('simple-web3')
+const SimpleWeb3 = require('./simple-web3')
 const Config = require('../../config/config')
 
 class IERC20 {}
 
-IERC20.batchInit = async function (ierc20TokenAddressList) {
+IERC20.batchInit = function (ierc20TokenAddressList) {
   IERC20.contractMap = {}
 
-  ierc20TokenAddressList.map(function (tokenHash) {
+  ierc20TokenAddressList.forEach(function (tokenHash) {
     IERC20.contractMap[tokenHash] = new SimpleWeb3.web3.eth.Contract(Config.BASE_ABI, tokenHash)
   })
 }
 
 IERC20.getTokenName = async function (tokenHash) {
-  return await IERC20.contractMap[tokenHash].methods.getName(tokenHash)
+  return await IERC20.contractMap[tokenHash].methods.name().call()
+}
+
+IERC20.getTokenSymbol = async function (tokenHash) {
+  return await IERC20.contractMap[tokenHash].methods.symbol().call()
 }
 
 module.exports = IERC20

+ 7 - 0
src/libs/web3/test/ierc20-test.js

@@ -1,7 +1,14 @@
 const logger = require('../../../kit/logger-kit')
+const IERC20 = require('../simple-ierc20')
+const Config = require('../../../config/config')
 
 async function main() {
+  IERC20.batchInit(Config.tokenHashList)
 
+  for (const tokenHash of Config.tokenHashList) {
+    logger.info(await IERC20.getTokenName(tokenHash))
+    logger.info(await IERC20.getTokenSymbol(tokenHash))
+  }
 }
 
 main()

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff