diff --git a/.env.example b/ .env.example similarity index 87% rename from .env.example rename to .env.example index e5d9a24..96614fb 100644 --- a/.env.example +++ b/ .env.example @@ -3,3 +3,4 @@ CLAIM_API_CHAIN_ID = "aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642 CLAIM_BP_ACCOUNT = "costaricaeos" CLAIM_PERMISION = "claim" CLAIM_PRIV_KEY = "5..." +CLAIM_NETWORK = "libre-testnet" diff --git a/README.md b/README.md index cf7c695..97221d0 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Some things you need before getting started: 1. Clone this repo using `git clone --depth=1 https://github.com/eoscostarica/claim-bp-rewards.git ` 2. Move to the appropriate directory: `cd `. 3. Copy and rename the `.env.example` to `.env` then update the environment variables according to your needs +4. For the CLAIM_NETWORK, the supports networks are: `libre`,`libre-testnet`, `antelope`, `jungle`, `telos`, `telos-testnet`, `proton`, `proton-testnet`, `wax`, `wax-testnet` ### Quick start diff --git a/src/config/eos.config.js b/src/config/eos.config.js index 4403a0d..240da34 100644 --- a/src/config/eos.config.js +++ b/src/config/eos.config.js @@ -3,5 +3,6 @@ module.exports = { chainId: process.env.CLAIM_API_CHAIN_ID, bpAccount: process.env.CLAIM_BP_ACCOUNT, claimPerms: process.env.CLAIM_PERMISION, - claimKey: process.env.CLAIM_PRIV_KEY + claimKey: process.env.CLAIM_PRIV_KEY, + network: process.env.CLAIM_NETWORK } diff --git a/src/index.js b/src/index.js index f37b6d6..226a3e3 100644 --- a/src/index.js +++ b/src/index.js @@ -1,75 +1,123 @@ const eosUtil = require('./utils/eos.util') const { eosConfig } = require('./config') -const bpAccounts = [eosConfig.bpAccount] +const bpAccount = eosConfig.bpAccount -async function init() { +const getProducer = async ({ + producer, + table = 'producers', + limit = 1 +} = {}) => { + return await eosUtil.getTableRows({ + code: 'eosio', + scope: 'eosio', + table, + limit, + lower_bound: producer, + upper_bound: producer + }) +} + +const claimLibreRewards = async() => { try { - for (const account of bpAccounts) { - console.log("Checking if it's time to claim rewards:", account) - - const getProducers = async ({ - next_key: nextKey = null, - limit = 100 - } = {}) => { - return await eosUtil.getTableRows({ - code: 'eosio', - scope: 'eosio', - table: 'producers', - limit, - lower_bound: nextKey - }) - } + console.log("Checking if it's time to claim rewards:", bpAccount) - const getProducer = async () => { - let nextKey = null + const data = await getProducer({ producer: bpAccount, table: 'payments' }) + + if(!data.rows.length) { + console.log("Bp does not exist") + return + } - while (true) { - const producers = await getProducers({ next_key: nextKey }) + const bpPayment = data.rows[0] - for (const producer of producers.rows) { - if (!(producer.owner === account)) continue + console.log("BP Payment: ", bpPayment) - return producer - } + if(!bpPayment.amount) { + console.log("No founds to claim") + return + } - if (!producers.more) break + console.log("Time to Claim Rewards!") - nextKey = producers.next_key + const transact = await eosUtil + .transact([ + { + account: 'eosio', + name: 'claimrewards', + authorization: [ + { actor: bpAccount, permission: eosConfig.claimPerms } + ], + data: { + owner: bpAccount } } + ]) + .catch(er => console.log("Fail to claim the rewards: ", er.toString())) - const bp = await getProducer() - const lastClaim = new Date(bp.last_claim_time) - const now = new Date() - const msBetweenDates = Math.abs(lastClaim.getTime() - now.getTime()) - const hoursBetweenDates = msBetweenDates / (60 * 60 * 1000) - - if (hoursBetweenDates > 24) { - console.log('Time to Claim Rewards!') - - const transact = await eosUtil - .transact([ - { - account: 'eosio', - name: 'claimrewards', - authorization: [ - { actor: eosConfig.bpAccount, permission: eosConfig.claimPerms } - ], - data: { - owner: account - } - } - ]) - .catch(er => console.log(er.toString())) - - if (transact) console.log(transact) - } else { - console.log('Last claim is within 24 hours') - } + if (transact) console.log("Transaction trace: ", transact) + + } catch (error) { + console.error(error) + } +} + +const claimRewards = async() => { + try { + console.log("Checking if it's time to claim rewards:", bpAccount) + + const data = await getProducer({ producer: bpAccount }) + + if(!data.rows.length) { + console.log("Bp does not exist") + return + } + + const bp = data.rows[0] + + console.log("BP: ", bp) + + const lastClaim = new Date(bp.last_claim_time) + const now = new Date() + const msBetweenDates = Math.abs(lastClaim.getTime() - now.getTime()) + const hoursBetweenDates = msBetweenDates / (60 * 60 * 1000) + + if (hoursBetweenDates > 24) { + console.log('Time to Claim Rewards!') + + const transact = await eosUtil + .transact([ + { + account: 'eosio', + name: 'claimrewards', + authorization: [ + { actor: bpAccount, permission: eosConfig.claimPerms } + ], + data: { + owner: bpAccount + } + } + ]) + .catch(er => console.log("Fail to claim the rewards: ", er.toString())) + + if (transact) console.log("Transaction trace: ", transact) + }else { + console.log(`The next claim is in ${hoursBetweenDates} hours`) } } catch (error) { console.error(error) } } + +async function init() { + switch (eosConfig.network) { + case "libre": + case "libre-testnet": + claimLibreRewards() + break + default: + claimRewards() + } +} + init()