Skip to content

Commit

Permalink
Ana/add fiatvalue in rewards (#396)
Browse files Browse the repository at this point in the history
* add fiatvalue to rewards

* add fiatcurrency to rewards query

* handle unsupported fiat currencies

* apply suggestions
  • Loading branch information
faboweb authored Mar 5, 2020
1 parent bc45c6c commit 32e0fd5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/reducers/terraV3-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function rewardReducer(rewards, validatorsDictionary) {
multiDenomRewardsArray.push({
denom: correctTerraDenomsReducer(denomLookup(denomReward.denom)),
amount: atoms(denomReward.amount),
fiatValue: denomReward.fiatValue,
validator: validator
})
})
Expand Down
5 changes: 3 additions & 2 deletions lib/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,14 @@ const resolvers = {
remoteFetch(dataSources, networkId).getAnnualProvision(),
rewards: async (
_,
{ networkId, delegatorAddress, operatorAddress },
{ networkId, delegatorAddress, operatorAddress, fiatCurrency },
{ dataSources }
) => {
const validatorsDictionary = localStore(dataSources, networkId).validators
let rewards = await remoteFetch(dataSources, networkId).getRewards(
delegatorAddress,
validatorsDictionary
validatorsDictionary,
fiatCurrency
)

// filter to a specific validator
Expand Down
2 changes: 2 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const typeDefs = gql`
validator: Validator
denom: String
amount: String
fiatValue: FiatValue
}
type FiatValue {
Expand Down Expand Up @@ -341,6 +342,7 @@ const typeDefs = gql`
networkId: String!
delegatorAddress: String!
operatorAddress: String
fiatCurrency: String
): [Reward]
transactions(
networkId: String!
Expand Down
8 changes: 8 additions & 0 deletions lib/source/emoneyV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const gasPrices = [
price: '0.800'
}
]
const supportedFiatCurrencies = new Set(['EUR', 'USD', 'GBP', 'CHF', 'JPY'])

class EMoneyV0API extends TerraV3API {
setReducers() {
Expand Down Expand Up @@ -85,6 +86,13 @@ class EMoneyV0API extends TerraV3API {
// We just default to EUR if there is no fiat currency included in the query
// This is because the standard price for NGM tokens given by e-Money team is 0.50€
async calculateFiatValue(balance, selectedFiatCurrency = `EUR`) {
// firt check if the selectedFiatCurrency is a supported currency
if (!supportedFiatCurrencies.has(selectedFiatCurrency)) {
throw new Error(
'We currently only support "EUR", "USD", "GBP", "CHF" and "JPY" as fiat currencies. Remember they should be written in uppercases'
)
}

// When e-Money goes live they will count with a trading platform where the value
// for the different backed tokens will be changing slightly.
// They will provide with an API for us to query these values.
Expand Down
34 changes: 32 additions & 2 deletions lib/source/terraV3-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,46 @@ class TerraV3API extends CosmosV2API {
return Number(validator.expectedReturns).toFixed(4)
}

async insertFiatValueinRewards(rewards, fiatCurrency) {
return await Promise.all(
rewards.map(
async reward =>
(reward = {
...reward,
reward: await Promise.all(
reward.reward.map(
async denomReward =>
(denomReward = {
...denomReward,
fiatValue: await this.calculateFiatValue(
denomReward,
fiatCurrency
)
})
)
)
})
)
)
}

// Terra will be the root source for functions specific to Tendermint multidenom networks
async getRewards(delegatorAddress, validatorsDictionary) {
async getRewards(delegatorAddress, validatorsDictionary, fiatCurrency) {
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)
const rewardsWithFiatValue = await this.insertFiatValueinRewards(
rewards,
fiatCurrency
)
return this.reducers.rewardReducer(
rewardsWithFiatValue,
validatorsDictionary
)
}

async calculateFiatValues(balances, fiatCurrency) {
Expand Down

0 comments on commit 32e0fd5

Please sign in to comment.