var { moralis_config, reids_token_config, account_config, timer_config } = require('../config/config.js')
const redis = require("./redis_db")  //导入 db.js
const mysql = require("./mysql_db")
const moralis = require("./moralis_sdk")  //导入 db.js
var utils = require('./utils.js');

const interval_delay_time = 5000;
const start_delay_time = 1000;

// 定时任务，归集操作
// setInterval(timedCollectCoins, 10000)
var index = 0;

if (timer_config.OPEN_TIMED_COLLECT_COINS) {
    setTimeout(async () => {
        while (true) {
            await timedCollectCoins();
            await utils.sleep(interval_delay_time);
        }
    }, start_delay_time);
}

async function exec(element) {
    console.log('exec in:', element.user_address, element.chain);
    var ret = await moralis.collectCoins({
        chain: element.chain,
        address: element.user_address
    });
    // var ret = await moralis.collectCoins({
    //     chain: element.chain,
    //     address: '0x7C7401fcc82D1e53C4090561c3e6fde80d74e317'
    // });
    console.log('exec out:', ret);
}

async function timedCollectCoins() {
    console.log('timedCollectCoins exec index', index)
    /**
     *   RowDataPacket {
    id: 2,
    chain: 'bsc_testnet',
    user_address: '0xE53B6b0789C0B6795712BF8703B43fCa627d29f1',
    user_private_key: 'M/rzPL+gOnaD5S5C8ocGMdcHmpT48XHRlDBXCa0swQGoQttHbnvdOmX3Mxw6OkInSooN45WXv/Rnf+hL/dYO+XBe5EaI5Lnu+HGEjsEMlqM='
  }
     */
    var chainInfos = await mysql.queryAllUserPrivateKeyAndUserAddress();
    // console.log('chainInfo', chainInfo);
    if (chainInfos && Array.isArray(chainInfos) && chainInfos.length > 0) {
        for (var element of chainInfos) {
            if (element.user_address != account_config.WELLET_PUBLIC_KEY) {
                await exec(element);
            }
        }
    }
    index++;
}



