Skip to content

Commit

Permalink
Merge pull request #4 from eoscostarica/fix-claim-rewards-libre
Browse files Browse the repository at this point in the history
fix(app): Update code for libre claim rewards
  • Loading branch information
xavier506 authored Apr 28, 2023
2 parents e63054b + 69d94aa commit cf0fc9e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 56 deletions.
1 change: 1 addition & 0 deletions .env.example → .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ CLAIM_API_CHAIN_ID = "aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642
CLAIM_BP_ACCOUNT = "costaricaeos"
CLAIM_PERMISION = "claim"
CLAIM_PRIV_KEY = "5..."
CLAIM_NETWORK = "libre-testnet"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <YOUR_PROJECT_NAME>`
2. Move to the appropriate directory: `cd <YOUR_PROJECT_NAME>`.
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

Expand Down
3 changes: 2 additions & 1 deletion src/config/eos.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
158 changes: 103 additions & 55 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit cf0fc9e

Please sign in to comment.