From e93ab677ab6a8b662dbd36eca972ec8b02000c8e Mon Sep 17 00:00:00 2001 From: Ana G <40721795+Bitcoinera@users.noreply.github.com> Date: Wed, 19 Feb 2020 22:00:07 +0100 Subject: [PATCH] Ana/add only tokens gas prices 2nd attempt (#344) * add terra and emoney gas price. terra reducer * delete fiatvalue from get balances in cosmos * add emoney reducer * hyper important emoney fixes * fix for emoney denoms * change gas price to micro units * add emoney denoms to denomlookup * transform to microunit also for terra tokens * return null for emoney gas prices * add harcoded gas prices * correct emoney hardcoded values * hardcoded terra gas prices. not working * update hardcoded values to working ones * delete unrelated changes to gas prices * apply suggestions except coinreducer * they call me mr coinreducer * change amount for price. add gas price reducer * change naming in gas price reducer * add error message Co-authored-by: Fabian --- lib/reducers/cosmosV0-reducers.js | 10 +++--- lib/reducers/terraV3-reducers.js | 29 ++++++++++++++++- lib/schema.js | 1 + lib/source/cosmosV0-source.js | 21 ++----------- lib/source/emoneyV0-source.js | 24 ++++++++++++++ lib/source/terraV3-source.js | 52 +++++++++++++++++++++++++++++++ 6 files changed, 111 insertions(+), 26 deletions(-) diff --git a/lib/reducers/cosmosV0-reducers.js b/lib/reducers/cosmosV0-reducers.js index 302f16a85c..d3f070aac7 100644 --- a/lib/reducers/cosmosV0-reducers.js +++ b/lib/reducers/cosmosV0-reducers.js @@ -283,11 +283,8 @@ function coinReducer(coin) { } } -function balanceReducer(coin, fiatValue) { - return { - ...coin, - fiatValue - } +function balanceReducer(coin) { + return coin } function delegationReducer(delegation, validator) { @@ -656,5 +653,6 @@ module.exports = { getTotalVotePercentage, getValidatorStatus, expectedRewardsPerToken, - getGroupByType + getGroupByType, + denomLookup } diff --git a/lib/reducers/terraV3-reducers.js b/lib/reducers/terraV3-reducers.js index cb5ff6cca6..bb89c4316b 100644 --- a/lib/reducers/terraV3-reducers.js +++ b/lib/reducers/terraV3-reducers.js @@ -1,5 +1,6 @@ const cosmosV2Reducers = require('./cosmosV2-reducers') -const { atoms } = cosmosV2Reducers +const BigNumber = require('bignumber.js') +const { atoms, denomLookup } = cosmosV2Reducers // Terra has a slightly different structure and needs its own undelegationEndTimeReducer function undelegationEndTimeReducer(transaction) { @@ -24,6 +25,31 @@ function undelegationEndTimeReducer(transaction) { } } +function gasPriceReducer(gasPrice) { + if (!gasPrice) { + throw new Error( + 'The token you are trying to request data for is not supported by Lunie.' + ) + } + + // we want to show only atoms as this is what users know + const denom = denomLookup(gasPrice.denom) + return { + denom: denom, + price: BigNumber(gasPrice.price).div(1000000) // Danger: this might not be the case for all future tokens + } +} + +function balanceReducer(coin, fiatValue, gasPrices) { + return { + ...coin, + fiatValue, + gasPrice: gasPriceReducer( + gasPrices.find(gasprice => denomLookup(gasprice.denom) === coin.denom) + ).price + } +} + function rewardReducer(rewards, validatorsDictionary) { const formattedRewards = rewards.map( reward => @@ -47,6 +73,7 @@ function rewardReducer(rewards, validatorsDictionary) { module.exports = { ...cosmosV2Reducers, + balanceReducer, rewardReducer, undelegationEndTimeReducer } diff --git a/lib/schema.js b/lib/schema.js index da2b92e4b8..35e52040e9 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -29,6 +29,7 @@ const typeDefs = gql` denom: String! amount: String! fiatValue: String + gasPrice: String } type Proposal { diff --git a/lib/source/cosmosV0-source.js b/lib/source/cosmosV0-source.js index a013d5d407..39b67026cb 100644 --- a/lib/source/cosmosV0-source.js +++ b/lib/source/cosmosV0-source.js @@ -316,30 +316,13 @@ class CosmosV0API extends RESTDataSource { return this.reducers.blockReducer(this.networkId, block, transactions) } - async calculateFiatValues(balances, fiatCurrency) { - return Promise.all( - balances.map(balance => ({ - denom: this.reducers.coinReducer(balance).denom, - fiatValue: this.calculateFiatValue - ? this.calculateFiatValue(balance, fiatCurrency) - : null - })) - ) - } - - async getBalancesFromAddress(address, fiatCurrency = `EUR`) { + async getBalancesFromAddress(address) { this.checkAddress(address) const response = await this.query(`bank/balances/${address}`) let balances = response || [] const coins = balances.map(this.reducers.coinReducer) - // We calculate the fiatValue field for networks with multiple tokens - // For now, it is just e-Money - const fiatBalances = await this.calculateFiatValues(balances, fiatCurrency) return coins.map(coin => { - return this.reducers.balanceReducer( - coin, - fiatBalances.find(({ denom }) => denom === coin.denom).fiatValue - ) + return this.reducers.balanceReducer(coin) }) } diff --git a/lib/source/emoneyV0-source.js b/lib/source/emoneyV0-source.js index ac42dc9580..7f7a88fdd4 100644 --- a/lib/source/emoneyV0-source.js +++ b/lib/source/emoneyV0-source.js @@ -3,9 +3,33 @@ const BigNumber = require('bignumber.js') const fetch = require('node-fetch') const apiURL = `https://api.exchangeratesapi.io/latest?` +const gasPrices = [ + { + denom: 'echf', + price: '0.400000' + }, + { + denom: 'eeur', + price: '0.400000' + }, + { + denom: 'ejpy', + price: '48.800000' + }, + { + denom: 'eusd', + price: '0.440000' + }, + { + denom: 'ungm', + price: '0.800000' + } +] + class EMoneyV0API extends TerraV3API { setReducers() { this.reducers = require('../reducers/emoneyV0-reducers') + this.gasPrices = gasPrices } // Here we query for the current inflation rates for all the backed tokens diff --git a/lib/source/terraV3-source.js b/lib/source/terraV3-source.js index 924838a439..645f405d15 100644 --- a/lib/source/terraV3-source.js +++ b/lib/source/terraV3-source.js @@ -6,9 +6,33 @@ const { pubkeyToAddress } = require('../tools') // Terra is provisioning this amount manually https://medium.com/terra-money/project-santa-community-initiative-b8ab6c4d79be const annualProvision = '21700000000000' // 21.7 million in uluna +const gasPrices = [ + { + denom: 'ukrw', + price: '0.091000' + }, + { + denom: 'uluna', + price: '0.001000' + }, + { + denom: 'umnt', + price: '0.091000' + }, + { + denom: 'usdr', + price: '0.091000' + }, + { + denom: 'uusd', + price: '0.091000' + } +] + class TerraV3API extends CosmosV2API { setReducers() { this.reducers = require('../reducers/terraV3-reducers') + this.gasPrices = gasPrices } async getAllValidators(height) { @@ -78,6 +102,34 @@ class TerraV3API extends CosmosV2API { ) return this.reducers.rewardReducer(rewards, validatorsDictionary) } + + async calculateFiatValues(balances, fiatCurrency) { + return Promise.all( + balances.map(balance => ({ + denom: this.reducers.coinReducer(balance).denom, + fiatValue: this.calculateFiatValue + ? this.calculateFiatValue(balance, fiatCurrency) + : null + })) + ) + } + + async getBalancesFromAddress(address, fiatCurrency = `EUR`) { + this.checkAddress(address) + const response = await this.query(`bank/balances/${address}`) + let balances = response || [] + const coins = balances.map(this.reducers.coinReducer) + // We calculate the fiatValue field for networks with multiple tokens + // For now, it is just e-Money (probably also Terra) + const fiatBalances = await this.calculateFiatValues(balances, fiatCurrency) + return coins.map(coin => { + return this.reducers.balanceReducer( + coin, + fiatBalances.find(({ denom }) => denom === coin.denom).fiatValue, + this.gasPrices + ) + }) + } } module.exports = TerraV3API