From 93d3836e1889c7994f61d43750e7bdd77a8b648f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francis=20G=C3=B3mez?= Date: Thu, 27 Apr 2023 17:18:08 -0500 Subject: [PATCH 1/3] fix(app): Update code for libre claim rewards --- .env.example => .env.example | 1 + src/config/eos.config.js | 3 +- src/index.js | 158 +++++++++++++++++++++------------ 3 files changed, 106 insertions(+), 56 deletions(-) rename .env.example => .env.example (87%) 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/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() From d49722a3dfbd0440a2400d8f25b125ac3d230e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francis=20G=C3=B3mez?= Date: Thu, 27 Apr 2023 17:32:40 -0500 Subject: [PATCH 2/3] fix(app): Update readme with support networks --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index cf7c695..66888b5 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 From 69d94aa0da4b760d0b6397f64009eeb985f1e85a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francis=20G=C3=B3mez?= Date: Thu, 27 Apr 2023 17:35:08 -0500 Subject: [PATCH 3/3] fix(app): Update readme with support networks wax --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 66888b5..97221d0 100644 --- a/README.md +++ b/README.md @@ -30,7 +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` +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