parent
ff6060be7b
commit
cf8c97a338
@ -1,8 +1,44 @@ |
||||
import $ from 'jquery' |
||||
import humps from 'humps' |
||||
import numeral from 'numeral' |
||||
import { BigNumber } from 'bignumber.js' |
||||
import socket from '../socket' |
||||
|
||||
export function formatUsdValue (value) { |
||||
if (value === 0) return '$0.000000 USD' |
||||
if (value < 0.000001) return '< $0.000001 USD' |
||||
if (value < 1) return `$${numeral(value).format('0.000000')} USD` |
||||
if (value < 100000) return `$${numeral(value).format('0,0.00')} USD` |
||||
return `$${numeral(value).format('0,0')} USD` |
||||
} |
||||
|
||||
function weiToEther (wei) { |
||||
return new BigNumber(wei).dividedBy('1000000000000000000').toNumber() |
||||
} |
||||
|
||||
function etherToUSD (ether, usdExchangeRate) { |
||||
return new BigNumber(ether).multipliedBy(usdExchangeRate).toNumber() |
||||
} |
||||
|
||||
function formatAllUsdValues () { |
||||
$('[data-usd-value]').each((i, el) => { |
||||
el.innerHTML = formatUsdValue(el.dataset.usdValue) |
||||
}) |
||||
} |
||||
formatAllUsdValues() |
||||
|
||||
function tryUpdateCalculatedUsdValues (el, usdExchangeRate = el.dataset.usdExchangeRate) { |
||||
if (!el.dataset.hasOwnProperty('weiValue')) return |
||||
const ether = weiToEther(el.dataset.weiValue) |
||||
const usd = etherToUSD(ether, usdExchangeRate) |
||||
const formattedUsd = formatUsdValue(usd) |
||||
if (formattedUsd !== el.innerHTML) el.innerHTML = formattedUsd |
||||
} |
||||
function updateAllCalculatedUsdValues (usdExchangeRate) { |
||||
$('[data-usd-exchange-rate]').each((i, el) => tryUpdateCalculatedUsdValues(el, usdExchangeRate)) |
||||
} |
||||
updateAllCalculatedUsdValues() |
||||
|
||||
export const exchangeRateChannel = socket.channel(`exchange_rate:new_rate`) |
||||
exchangeRateChannel.join() |
||||
exchangeRateChannel.on('new_rate', (msg) => updateAllCalculatedUsdValues(humps.camelizeKeys(msg).exchangeRate.usdValue)) |
||||
|
@ -1,42 +0,0 @@ |
||||
defmodule BlockScoutWeb.ExchangeRates.USD do |
||||
@moduledoc """ |
||||
Struct and associated conversion functions for USD currency |
||||
""" |
||||
|
||||
@typedoc """ |
||||
Represents USD currency |
||||
|
||||
* `:value` - value in USD |
||||
""" |
||||
@type t :: %__MODULE__{ |
||||
value: Decimal.t() | nil |
||||
} |
||||
|
||||
defstruct ~w(value)a |
||||
|
||||
alias Explorer.Chain.Wei |
||||
alias Explorer.ExchangeRates.Token |
||||
|
||||
def from(nil), do: null() |
||||
|
||||
def from(%Decimal{} = usd_decimal) do |
||||
%__MODULE__{value: usd_decimal} |
||||
end |
||||
|
||||
def from(nil, _), do: null() |
||||
|
||||
def from(_, nil), do: null() |
||||
|
||||
def from(%Wei{value: nil}, _), do: null() |
||||
|
||||
def from(_, %Token{usd_value: nil}), do: null() |
||||
|
||||
def from(%Wei{} = wei, %Token{usd_value: exchange_rate}) do |
||||
ether = Wei.to(wei, :ether) |
||||
%__MODULE__{value: Decimal.mult(ether, exchange_rate)} |
||||
end |
||||
|
||||
def null do |
||||
%__MODULE__{value: nil} |
||||
end |
||||
end |
@ -1,48 +0,0 @@ |
||||
defmodule BlockScoutWeb.ExchangeRates.USDTest do |
||||
use ExUnit.Case, async: true |
||||
|
||||
alias BlockScoutWeb.ExchangeRates.USD |
||||
alias Explorer.ExchangeRates.Token |
||||
alias Explorer.Chain.Wei |
||||
|
||||
describe "from/2" do |
||||
test "with nil wei returns null object" do |
||||
token = %Token{usd_value: Decimal.new(0.5)} |
||||
|
||||
assert USD.null() == USD.from(nil, token) |
||||
end |
||||
|
||||
test "with nil token returns nil" do |
||||
wei = %Wei{value: Decimal.new(10_000_000_000_000)} |
||||
|
||||
assert USD.null() == USD.from(wei, nil) |
||||
end |
||||
|
||||
test "without a wei value returns nil" do |
||||
wei = %Wei{value: nil} |
||||
token = %Token{usd_value: Decimal.new(0.5)} |
||||
|
||||
assert USD.null() == USD.from(wei, token) |
||||
end |
||||
|
||||
test "without an exchange rate returns nil" do |
||||
wei = %Wei{value: Decimal.new(10_000_000_000_000)} |
||||
token = %Token{usd_value: nil} |
||||
|
||||
assert USD.null() == USD.from(wei, token) |
||||
end |
||||
|
||||
test "returns formatted usd value" do |
||||
wei = %Wei{value: Decimal.new(10_000_000_000_000)} |
||||
token = %Token{usd_value: Decimal.new(0.5)} |
||||
|
||||
assert %USD{value: Decimal.new(0.000005)} == USD.from(wei, token) |
||||
end |
||||
|
||||
test "returns USD struct from decimal usd value" do |
||||
value = Decimal.new(0.000005) |
||||
|
||||
assert %USD{value: ^value} = USD.from(value) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue