Skip to content

Commit

Permalink
Ana/add totalstake-fiatvalue to Overview (#384)
Browse files Browse the repository at this point in the history
* add totalstake fiat value to overview

* fix amount in totalstake fiatvalue

* make new code more readable

* Update lib/reducers/cosmosV0-reducers.js

* refactor unit conversion

Co-authored-by: Mario Pino <[email protected]>
  • Loading branch information
faboweb and mariopino authored Mar 4, 2020
1 parent 2d3714d commit e998d2e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
47 changes: 42 additions & 5 deletions lib/reducers/cosmosV0-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ function gasPriceReducer(gasPrice) {
}
}

function toMicroUnitBalance(balance) {
return {
amount: BigNumber(balance.amount).times(1000000),
denom: toMicroDenom(balance.denom)
}
}

function toMicroDenom(denom) {
return `u`.concat(denom.toLowerCase())
}

// delegations rewards in Tendermint are located in events as strings with this form:
// amount: {"15000umuon"}, or in multidenom networks they look like this:
// amount: {"15000ungm,100000uchf,110000ueur,2000000ujpy"}
Expand All @@ -320,9 +331,11 @@ function rewardCoinReducer(reward, stakingDenom) {
function balanceReducer(coin, gasPrices) {
return {
...coin,
gasPrice: gasPriceReducer(
gasPrices.find(gasPrice => denomLookup(gasPrice.denom) === coin.denom)
).price
gasPrice: gasPrices
? gasPriceReducer(
gasPrices.find(gasPrice => denomLookup(gasPrice.denom) === coin.denom)
).price
: null
}
}

Expand Down Expand Up @@ -356,12 +369,13 @@ function rewardReducer(reward, validator) {
}
}

function overviewReducer(
async function overviewReducer(
balances,
delegations,
undelegations,
rewards,
stakingDenom,
fiatValueAPI,
reducers
) {
stakingDenom = denomLookup(stakingDenom)
Expand Down Expand Up @@ -389,6 +403,7 @@ function overviewReducer(
(sum, { amount }) => BigNumber(sum).plus(amount),
0
)
const totalStake = liquidStake.plus(delegatedStake).plus(undelegatingStake)

return {
rewards:
Expand All @@ -400,10 +415,32 @@ function overviewReducer(
: null,
totalRewards: totalRewards,
liquidStake: liquidStake,
totalStake: liquidStake.plus(delegatedStake).plus(undelegatingStake)
totalStake,
totalStakeFiatValue: fiatValueAPI
? totalStakeFiatValueReducer(
fiatValueAPI,
totalStake,
stakingDenom,
reducers
)
: null
}
}

async function totalStakeFiatValueReducer(
fiatValueAPI,
totalStake,
stakingDenom
) {
return await fiatValueAPI.calculateFiatValue(
// we need to convert here to microunits since this is the format that calculateFiatValue is taking
toMicroUnitBalance({
amount: totalStake,
denom: stakingDenom
})
)
}

function getGroupByType(transactionType) {
const transactionGroup = {
[cosmosMessageType.SEND]: 'banking',
Expand Down
1 change: 1 addition & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ const typeDefs = gql`
networkId: String!
address: String!
totalStake: String!
totalStakeFiatValue: FiatValue
liquidStake: String!
totalRewards: String!
rewards: [Reward]
Expand Down
2 changes: 2 additions & 0 deletions lib/source/cosmosV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,14 @@ class CosmosV0API extends RESTDataSource {
validatorsDictionary,
delegations
)
const fiatValueAPI = this.calculateFiatValue ? this : null
return this.reducers.overviewReducer(
balances,
delegations,
undelegations,
rewards,
stakingDenom,
fiatValueAPI,
this.reducers
)
}
Expand Down
2 changes: 2 additions & 0 deletions lib/source/emoneyV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class EMoneyV0API extends TerraV3API {
return expectedReturns
}

// This function takes microunits as inputs

// Currently this function only works for e-Money and is very e-Money centered.
// But soon will also be enabled for other similar multiple-tokens networks like Terra.

Expand Down

0 comments on commit e998d2e

Please sign in to comment.