From 408186a3ef743ca51d513eb91c937f49960e7492 Mon Sep 17 00:00:00 2001 From: Ana G <40721795+Bitcoinera@users.noreply.github.com> Date: Fri, 14 Feb 2020 14:19:51 +0100 Subject: [PATCH] WIP: Ana/More Terra fixes (#323) * fixing my mess * move all rewards logic to terra * clearly signal single denom rewards --- lib/reducers/cosmosV0-reducers.js | 11 +++++------ lib/reducers/emoneyV0-reducers.js | 26 ++------------------------ lib/reducers/terraV3-reducers.js | 23 +++++++++++++++++++++++ lib/source/emoneyV0-source.js | 11 ----------- lib/source/terraV3-source.js | 12 ++++++++++++ 5 files changed, 42 insertions(+), 41 deletions(-) diff --git a/lib/reducers/cosmosV0-reducers.js b/lib/reducers/cosmosV0-reducers.js index 6e59b46ef9..4ccec60275 100644 --- a/lib/reducers/cosmosV0-reducers.js +++ b/lib/reducers/cosmosV0-reducers.js @@ -303,7 +303,6 @@ function undelegationReducer(undelegation, validator) { function rewardReducer(reward, validator) { return { amount: atoms(reward.amount), - denom: reward.denom, validator } } @@ -392,12 +391,12 @@ function formatTransactionsReducer(txs, reducers) { const sortedTxs = sortBy(duplicateFreeTxs, ['timestamp']) const reversedTxs = reverse(sortedTxs) // here we filter out all transactions related to validators - reversedTxs.forEach(tx => { - tx.tx.value.msg = tx.tx.value.msg.filter(msg => - cosmosWhitelistedMessageTypes.has(msg.type.split('/')[1]) + const filteredTxs = reversedTxs.filter(transaction => + cosmosWhitelistedMessageTypes.has( + transaction.tx.value.msg[0].type.split('/')[1] ) - }) - return reversedTxs.map(tx => transactionReducer(tx, reducers)) + ) + return filteredTxs.map(tx => transactionReducer(tx, reducers)) } // to be able to catch all validators from a multi-claim reward tx, we need to capture diff --git a/lib/reducers/emoneyV0-reducers.js b/lib/reducers/emoneyV0-reducers.js index 96345e825b..92a479a62e 100644 --- a/lib/reducers/emoneyV0-reducers.js +++ b/lib/reducers/emoneyV0-reducers.js @@ -5,7 +5,6 @@ const _ = require('lodash') const EMoneyAPIUrl = `https://api.e-money.com/v1/` const exchangeAPIURL = `https://api.exchangeratesapi.io/latest?` const Sentry = require('@sentry/node') -const { atoms } = terraV3Reducers async function totalBackedValueReducer(totalBackedValue) { const exchangeRates = await fetchTokenExchangeRates() @@ -120,30 +119,9 @@ async function expectedRewardsPerToken( return expectedReturns / 100 } -function rewardReducer(rewards, validatorsDictionary) { - const formattedRewards = rewards.map( - reward => - (reward = { - reward: reward.reward, - validator: validatorsDictionary[reward.validator_address] - }) - ) - let multiDenomRewardsArray = [] - formattedRewards.map(({ reward, validator }) => - reward.forEach(denomReward => { - multiDenomRewardsArray.push({ - denom: denomReward.denom, - amount: atoms(denomReward.amount), - validator: validator - }) - }) - ) - return multiDenomRewardsArray -} - module.exports = { ...terraV3Reducers, expectedRewardsPerToken, - totalBackedValueReducer, - rewardReducer + totalBackedValueReducer + // rewardReducer } diff --git a/lib/reducers/terraV3-reducers.js b/lib/reducers/terraV3-reducers.js index 9095485948..2e0bed0ebb 100644 --- a/lib/reducers/terraV3-reducers.js +++ b/lib/reducers/terraV3-reducers.js @@ -1,4 +1,5 @@ const cosmosV2Reducers = require('./cosmosV2-reducers') +const { atoms } = cosmosV2Reducers // Terra has a slightly different structure and needs its own undelegationEndTimeReducer function undelegationEndTimeReducer(transaction) { @@ -23,7 +24,29 @@ function undelegationEndTimeReducer(transaction) { } } +function rewardReducer(rewards, validatorsDictionary) { + const formattedRewards = rewards.map( + reward => + (reward = { + reward: reward.reward, + validator: validatorsDictionary[reward.validator_address] + }) + ) + let multiDenomRewardsArray = [] + formattedRewards.map(({ reward, validator }) => + reward.forEach(denomReward => { + multiDenomRewardsArray.push({ + denom: denomReward.denom, + amount: atoms(denomReward.amount), + validator: validator + }) + }) + ) + return multiDenomRewardsArray +} + module.exports = { ...cosmosV2Reducers, + rewardReducer, undelegationEndTimeReducer } diff --git a/lib/source/emoneyV0-source.js b/lib/source/emoneyV0-source.js index 1115ddb6eb..b31e7e2981 100644 --- a/lib/source/emoneyV0-source.js +++ b/lib/source/emoneyV0-source.js @@ -135,17 +135,6 @@ class EMoneyV0API extends TerraV3API { return `?` } } - - async getRewards(delegatorAddress, validatorsDictionary) { - this.checkAddress(delegatorAddress) - const result = await this.query( - `distribution/delegators/${delegatorAddress}/rewards` - ) - const rewards = (result.rewards || []).filter( - ({ reward }) => reward && reward.length > 0 - ) - return this.reducers.rewardReducer(rewards, validatorsDictionary) - } } module.exports = EMoneyV0API diff --git a/lib/source/terraV3-source.js b/lib/source/terraV3-source.js index 66f849db9b..924838a439 100644 --- a/lib/source/terraV3-source.js +++ b/lib/source/terraV3-source.js @@ -66,6 +66,18 @@ class TerraV3API extends CosmosV2API { ) return expectedReturns } + + // Terra will be the root source for functions specific to Tendermint multidenom networks + async getRewards(delegatorAddress, validatorsDictionary) { + this.checkAddress(delegatorAddress) + const result = await this.query( + `distribution/delegators/${delegatorAddress}/rewards` + ) + const rewards = (result.rewards || []).filter( + ({ reward }) => reward && reward.length > 0 + ) + return this.reducers.rewardReducer(rewards, validatorsDictionary) + } } module.exports = TerraV3API