-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from eoscostarica/fix-claim-rewards-libre
fix(app): Update code for libre claim rewards
- Loading branch information
Showing
4 changed files
with
107 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |