|
|
@@ -1,9 +1,15 @@
|
|
|
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
|
|
|
@@ -13,8 +19,30 @@ module.exports = class Token {
|
|
|
this.ierc20.decimals = ierc20Decimals
|
|
|
}
|
|
|
|
|
|
+ async save() {
|
|
|
+ const localData = {
|
|
|
+ orderPrice: this.orderPrice,
|
|
|
+ orderAmount: this.orderAmount,
|
|
|
+ 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.nextHandleTime = localData.nextHandleTime
|
|
|
+ } catch (e) {}
|
|
|
+ }
|
|
|
+
|
|
|
static async init(context, tokenHash, priceTickFilterMap, lotSizeFilterMap) {
|
|
|
const config = context.config
|
|
|
+ const logger = context.logger
|
|
|
|
|
|
// token初始化
|
|
|
const ierc20Decimals = parseInt(await IERC20.getDecimals(tokenHash))
|
|
|
@@ -23,7 +51,14 @@ module.exports = class Token {
|
|
|
const exchangePriceTick = priceTickFilterMap[exchangePair]
|
|
|
const exchangeLotSize = lotSizeFilterMap[exchangePair]
|
|
|
|
|
|
- context.tokenMap[tokenHash] = new Token(exchangeSymbol, exchangePriceTick, exchangeLotSize, exchangePair, ierc20Decimals)
|
|
|
+ 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}.`)
|
|
|
}
|
|
|
|
|
|
static async batchInit(context, ierc20TokenAddressList, priceTickFilterMap, lotSizeFilterMap) {
|