-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fabo/query past rewards script (#564)
* initial working state * working, this time is true ;-) * fix validator, comments * fix, cleanup * fix validator * update comment * Use coinReducer * cleanup * cleanup unused vars * cleanup * Flatten era rewards and then aggregate per valid * refactored * typo * uüdated polkadot * load rewards * steps towards storing the rewards * fix era format * fix validator in row needs to be address * fix gql query * format bug * some refactoring * fix missing await * correctly get api * push validator getter inside sources * added address to lunie reward internaly * add address correctly * fix variable naming * filter empty rewards * add height from rewards * added query past rewards script * calling main * missing imports * upsert to prevent conflicts * flatten rewards * fix too many labels issue * typo * write for each validator * await finishing * add validators correctly to the store * remove timing for rewards * correctly write validator and filter 0 * add check and filter out undefined validators * fix amount decimals to 9. allow validator unknown * added address better * refactor * fix tests * only add needed columns to rows * missing refactor * fix getoldpolkadot rewards eras script * possible fix to lastreward of undefined * delete forgotten only * typo Co-authored-by: mariopino <[email protected]> Co-authored-by: Bitcoinera <[email protected]>
- Loading branch information
1 parent
45512bf
commit f9c9fc2
Showing
3 changed files
with
68 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const database = require('../lib/database') | ||
const config = require('../config') | ||
const db = database(config)('polkadot_testnet') | ||
const { ApiPromise, WsProvider } = require('@polkadot/api') | ||
const _ = require('lodash') | ||
|
||
async function initPolkadotRPC(network, store) { | ||
console.time('init polkadot') | ||
const api = new ApiPromise({ | ||
provider: new WsProvider(network.rpc_url) | ||
}) | ||
store.polkadotRPC = api | ||
await api.isReady | ||
console.timeEnd('init polkadot') | ||
} | ||
|
||
function storeRewards(rewards, chainId) { | ||
return db.upsert('rewards', rewards, undefined, chainId) // height is in the rewards rows already | ||
} | ||
|
||
async function main() { | ||
const networks = require('../data/networks') | ||
const network = networks.find(({ id }) => id === 'polkadot-testnet') | ||
const PolkadotApiClass = require('../lib/' + network.source_class_name) | ||
const store = {} | ||
await initPolkadotRPC(network, store) | ||
const polkadotAPI = new PolkadotApiClass(network, store) | ||
|
||
const validators = await polkadotAPI.getAllValidators() | ||
store.validators = _.keyBy(validators, 'operatorAddress') | ||
const delegators = await polkadotAPI.getAllDelegators() | ||
|
||
await Promise.all( | ||
delegators.map(async delegator => { | ||
const rewards = await polkadotAPI.getRewards(delegator) | ||
const storableRewards = rewards | ||
? rewards.filter(({ amount }) => amount > 0) | ||
: [] | ||
if (storableRewards.length > 0) { | ||
await storeRewards( | ||
rewards.map(reward => ({ | ||
amount: reward.amount, | ||
height: reward.height, | ||
denom: reward.denom, | ||
address: reward.address, | ||
validator: reward.validatorAddress | ||
})), | ||
validators[0].chainId | ||
) | ||
} | ||
}) | ||
) | ||
} | ||
|
||
main() |