Skip to content

Commit

Permalink
add skip participants with too low balance. closes #42
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Dec 5, 2024
1 parent 3167b3e commit 0d3c53a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
40 changes: 29 additions & 11 deletions bin/release-rewards.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,46 @@ import * as SparkImpactEvaluator from '@filecoin-station/spark-impact-evaluator'
import readline from 'node:readline/promises'
import pRetry from 'p-retry'
import beeper from 'beeper'
import pMap from 'p-map'

process.title = 'release-rewards'
const { RPC_URL = 'https://api.node.glif.io/rpc/v1', WALLET_SEED } = process.env

const provider = new ethers.JsonRpcProvider(RPC_URL)
const ie = new ethers.Contract(SparkImpactEvaluator.ADDRESS, SparkImpactEvaluator.ABI, provider)

const rawRewardsRes = await fetch('https://spark-rewards.fly.dev/scheduled-rewards')
const rawRewards = await rawRewardsRes.json()
const rewards = Object.entries(rawRewards)
const unfilteredRewards = Object.entries(rawRewards)
.map(([address, amount]) => ({
address,
amount: BigInt(amount),
amountFIL: Number(amount) / 1e18
amount: BigInt(amount)
}))
.filter(({ amount }) => amount > 0n)
unfilteredRewards.sort((a, b) => Number(b.amount - a.amount))

console.log(`Found ${unfilteredRewards.length} participants with spark-rewards scheduled rewards`)
console.log('Filtering out participants with total scheduled rewards (spark-rewards + smart contract) below 0.1 FIL...')
const rewards = []
await pMap(
unfilteredRewards,
async ({ address, amount }, index) => {
if (index > 0 && index % 100 === 0) {
console.log(`${index}/${unfilteredRewards.length}`)
}
if (amount === 0) return
const totalScheduledRewards =
(await ie.rewardsScheduledFor(address)) + amount
if (totalScheduledRewards >= 0.1e18) {
rewards.push({ address, amount })
}
},
{ concurrency: 100 }
)

if (rewards.length === 0) {
console.log('No rewards to release')
process.exit(0)
}
rewards.sort((a, b) => Number(b.amount - a.amount))

const total = rewards.reduce((acc, { amount }) => acc + amount, 0n)
console.log(
`About to send ~${Math.ceil(Number(total) / 1e18)} FIL (+~10FIL gas) ${WALLET_SEED ? '' : 'from your hardware wallet (Eth account)'} to the IE`
Expand All @@ -36,13 +57,10 @@ if (!/^y(es)?$/.test(answer)) {
process.exit(1)
}

const provider = new ethers.JsonRpcProvider(RPC_URL)
const signer = WALLET_SEED
? ethers.Wallet.fromPhrase(WALLET_SEED, provider)
: new LedgerSigner(HIDTransport, provider)
const ie = new ethers
.Contract(SparkImpactEvaluator.ADDRESS, SparkImpactEvaluator.ABI, provider)
.connect(signer)
const ieWithSigner = ie.connect(signer)

const addresses = rewards.map(({ address }) => address)
const amounts = rewards.map(({ amount }) => amount)
Expand All @@ -64,7 +82,7 @@ for (let i = 0; i < batchCount; i++) {
await beeper()
console.log('Please approve on ledger...')
}
const tx = await ie.addBalances(
const tx = await ieWithSigner.addBalances(
batchAddresses,
batchAmounts,
{ value: batchAmounts.reduce((acc, amount) => acc + amount, 0n) }
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"ethers": "^6.13.4",
"http-assert": "^1.5.0",
"http-responders": "^2.2.0",
"p-map": "^7.0.2",
"p-retry": "^6.2.1",
"pg": "^8.13.1",
"pg-cursor": "^2.12.1",
Expand Down

0 comments on commit 0d3c53a

Please sign in to comment.