-
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/fix gas (#250) * fix gas estimate * linted * fixed test * do not keep data sources (#251) * track failing transactions in Sentry (#249) * correctly set the tx schema for a failing tx (#248) * Fabo/remove per block caching as not working (#247) * remove per block caching as not working * fix memoized results Co-authored-by: Ana G. <[email protected]> * delete perblockcachedatasource (#253) * Ana/fix balances in actionmodal (#255) * fix action modal available balance * include regen * use dictionary for denomlookup * use correct events for received txs (#257) * enable account creation for some networks (#252) * network update time metric added (#256) * network update time metric added * added missing dep Co-authored-by: Fabian <[email protected]> * Fix proposal deposit (#261) * Remove denom handling from getDeposit() * Revert undesired change * delete package-lock.json * localtestnet config change (#265) * Ana/handle "address not from this network" error (#263) * add check address function for all queries * apply suggestions * Ana/add fiatvalue to balances query (e-Money) (#262) * preparation * more preparation * add fiatvalue field to balances query * fix get account info * apply suggestions * apply one last suggestion * suggestions+ Co-authored-by: Fabian <[email protected]> * Ana/emoney fix expected returns with inflation and totalbacked (#243) * fix expected returns with inflation and supply * minor fixes. dictionary * query exchange rates from emoney api * fix infinite expected returns * convert api url to const * add eur value to totalbackedvalue. totalngm gains * add important comment * finish calculation * lint * catch errors with sentry Co-authored-by: Fabian <[email protected]> * readd coin conversion (#268) * delete amount field (#274) * Fabo/increase gas again (#271) * icrease gas again * fixed test * Fabo/load all txs (even if more then first page in response) (#270) * load all txs (even if more then first page in response) * improved handling of txs * missing renaming * fixed paginated load * add pagination fix also to cosmosV0-source Co-authored-by: iambeone <[email protected]> Co-authored-by: Ana G. <[email protected]> * fixing issue with multiple senders in one event (#273) * fixing issue with multiple senders in one event * Update lib/source/cosmosV2-source.js Co-authored-by: Fabian <[email protected]> Co-authored-by: Ana G. <[email protected]> Co-authored-by: iambeone <[email protected]> Co-authored-by: Mario Pino <[email protected]>
- Loading branch information
1 parent
4321a85
commit be29a21
Showing
15 changed files
with
715 additions
and
87 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
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 |
---|---|---|
@@ -1,29 +1,126 @@ | ||
const terraV3Reducers = require('./terraV3-reducers') | ||
const BigNumber = require('bignumber.js') | ||
const fetch = require('node-fetch') | ||
const _ = require('lodash') | ||
const EMoneyAPIUrl = `https://api.e-money.com/v1/` | ||
const exchangeAPIURL = `https://api.exchangeratesapi.io/latest?` | ||
const Sentry = require('@sentry/node') | ||
|
||
function expectedRewardsPerToken( | ||
async function totalBackedValueReducer(totalBackedValue) { | ||
const exchangeRates = await fetchTokenExchangeRates() | ||
const token = `e`.concat(totalBackedValue.denom.substring(2)) | ||
const ticker = totalBackedValue.denom.substring(2).toUpperCase() | ||
// First we calculate the fiat net value of the token's total supply in it counterpart | ||
// fiat currency | ||
// TODO Not all the tokens have yet their rate value in the e-Money API. So the calculation | ||
// is not yet 100% accurate | ||
const fiatValue = exchangeRates[token] | ||
? BigNumber(totalBackedValue.amount) | ||
.div(100000000) | ||
.times(exchangeRates[token][ticker]) | ||
.toNumber() | ||
: null | ||
// Now that we have its net fiat value, we transform this value into euro | ||
const { rates } = await fetchFiatExchangeRates(`EUR`, ticker) | ||
const eurRate = ticker === `EUR` ? 1 : rates[ticker] | ||
return (totalBackedValue = { | ||
...totalBackedValue, | ||
amount: exchangeRates[token] | ||
? BigNumber(totalBackedValue.amount).times( | ||
Object.values(exchangeRates[token])[0] | ||
) | ||
: BigNumber(totalBackedValue.amount), | ||
// The total net EUR value of the token's total supply equals its net value in its | ||
// counterpart fiat currency times this fiat currency's EUR rate | ||
eurValue: fiatValue * eurRate | ||
}) | ||
} | ||
|
||
async function fetchTokenExchangeRates() { | ||
return await fetch(`${EMoneyAPIUrl}rates.json`) | ||
.then(r => r.json()) | ||
.catch(err => { | ||
Sentry.withScope(function(scope) { | ||
scope.setExtra('fetch', `${EMoneyAPIUrl}rates.json`) | ||
Sentry.captureException(err) | ||
}) | ||
}) | ||
} | ||
|
||
async function fetchFiatExchangeRates(selectedFiatCurrency, ticker) { | ||
return await fetch( | ||
`${exchangeAPIURL}base=${selectedFiatCurrency}&symbols=${ticker}` | ||
) | ||
.then(r => r.json()) | ||
.catch(err => { | ||
Sentry.withScope(function(scope) { | ||
scope.setExtra( | ||
'fetch', | ||
`${exchangeAPIURL}base=${selectedFiatCurrency}&symbols=${ticker}` | ||
) | ||
Sentry.captureException(err) | ||
}) | ||
}) | ||
} | ||
|
||
async function expectedRewardsPerToken( | ||
validator, | ||
commission, | ||
inflation, | ||
totalBackedValue | ||
inflations, | ||
totalBackedValues | ||
) { | ||
const percentTotalStakedNGM = BigNumber(validator.votingPower).times(1000) | ||
const totalNGMStakedToValidator = validator.tokens | ||
const division = BigNumber(percentTotalStakedNGM) | ||
.times(BigNumber(1).minus(commission)) | ||
.times(BigNumber(1).minus(BigNumber(commission))) | ||
.div(BigNumber(totalNGMStakedToValidator)) | ||
// Now we need to multiply each total supply of backed tokens with its corresponding | ||
// inflation | ||
let accumulator = BigNumber(0) | ||
const totalBackedValueDictionary = _.keyBy(totalBackedValues, 'denom') | ||
|
||
inflations.forEach(inflation => { | ||
accumulator = BigNumber(accumulator) | ||
.plus(accumulator) | ||
.plus( | ||
BigNumber(inflation.inflation) | ||
.times(totalBackedValueDictionary[inflation.denom].amount) | ||
.div(1000000) | ||
) | ||
}) | ||
const totalBackedValuesTimesInflations = accumulator | ||
// First we calculate the total value of rewards we get for staking one single | ||
// NGM in this particular validator | ||
const delegatorSharePerToken = BigNumber(inflation * totalBackedValue).div( | ||
division | ||
) | ||
return delegatorSharePerToken.div(1000000) | ||
// TODO | ||
// Now we convert delegatorRewardsPerToken to the amount of NGM we can buy now | ||
// with this reward | ||
let delegatorSharePerToken = totalBackedValuesTimesInflations.div(division) | ||
// In testnet some validators have commission at 100% and therefore division is | ||
// equal to 0. Dividing by 0 we get infinity, and we want to make sure that | ||
// we don't display such value | ||
const infinity = new BigNumber(Infinity) | ||
// This is just how BigNumber works. Comparing to 0 and if true, bignumber equals the given parameter. | ||
// Documentation on this method can be found here: https://mikemcl.github.io/bignumber.js/#cmp | ||
delegatorSharePerToken = | ||
delegatorSharePerToken.comparedTo(infinity) === 0 | ||
? BigNumber(0) | ||
: delegatorSharePerToken.div(1000000) | ||
// Now we get the total net value in EUR of all token's total supplies | ||
let eurAccumulator = 0 | ||
totalBackedValues.forEach(totalBackedValue => { | ||
eurAccumulator += totalBackedValue.eurValue | ||
}) | ||
const totalEURGains = eurAccumulator * delegatorSharePerToken.toNumber() | ||
// How many NGM tokens can we buy with the total gain in EUR we make in a year's time? | ||
// 0.50€ is the price the NGM tokens will be first sold. Therefore, this is the official value | ||
// until they reach an exchange | ||
const pricePerNGM = 0.5 | ||
const ngmGains = totalEURGains / pricePerNGM | ||
// We divide by 1 because we assume 1 NGM > gain per 1 NGM | ||
const expectedReturns = parseFloat(ngmGains / 1).toFixed(2) | ||
// TODO the FE is the one converting to percentage now, so we need to divide by 100 | ||
return expectedReturns / 100 | ||
} | ||
|
||
module.exports = { | ||
...terraV3Reducers, | ||
expectedRewardsPerToken | ||
expectedRewardsPerToken, | ||
totalBackedValueReducer | ||
} |
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
Oops, something went wrong.