| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- const IERC20 = require("./web3/ierc20-token");
- const { Level } = require("level");
- const db = new Level('app.db', { valueEncoding: 'json' })
- module.exports = class Token {
- exchange = {}
- ierc20 = {}
- orderPrice = 0
- orderAmount = 0
- nextHandleTime = 0
- constructor(exchangeSymbol, exchangePriceTick, exchangeLotSize, exchangePair, ierc20Decimals) {
- this.exchange.symbol = exchangeSymbol
- this.exchange.priceTick = exchangePriceTick
- this.exchange.lotSize = exchangeLotSize
- this.exchange.pair = exchangePair
- this.ierc20.decimals = ierc20Decimals
- }
- async save() {
- const localData = {
- orderPrice: this.orderPrice,
- orderAmount: this.orderAmount,
- orderTime: this.orderTime,
- nextHandleTime: this.nextHandleTime
- }
- await db.put(this.exchange.pair, JSON.stringify(localData))
- }
- async load() {
- try {
- const localDataJsonStr = await db.get(this.exchange.pair)
- const localData = JSON.parse(localDataJsonStr)
- this.orderAmount = localData.orderAmount
- this.orderPrice = localData.orderPrice
- this.orderTime = localData.orderTime ? localData.orderTime : 0
- this.nextHandleTime = localData.nextHandleTime
- } catch (e) {}
- }
- static async init(context, logger, tokenHash, priceTickFilterMap, lotSizeFilterMap) {
- const config = context.config
- // token初始化
- const ierc20Decimals = parseInt(await IERC20.getDecimals(tokenHash))
- const exchangeSymbol = config.tokenMapping[tokenHash]
- const exchangePair = `${exchangeSymbol}${config.baseIerc20Token.symbol}`
- const exchangePriceTick = priceTickFilterMap[exchangePair]
- const exchangeLotSize = lotSizeFilterMap[exchangePair]
- const token = new Token(exchangeSymbol, exchangePriceTick, exchangeLotSize, exchangePair, ierc20Decimals)
- // 载入时持久化
- await token.load()
- context.tokenMap[tokenHash] = token
- logger.info(`${token.exchange.pair}初始化完毕,orderPrice:${token.orderPrice}, orderAmount:${token.orderAmount}, orderTime:${token.orderTime}.`)
- }
- static async batchInit(context, logger, ierc20TokenAddressList, priceTickFilterMap, lotSizeFilterMap) {
- context.tokenMap = {}
- for (const tokenHash of ierc20TokenAddressList) {
- // 初始化token
- await Token.init(context, logger, tokenHash, priceTickFilterMap, lotSizeFilterMap)
- }
- }
- }
|