Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1660 from bandprotocol/remove-graphqlparser-milli…
Browse files Browse the repository at this point in the history
…on-divide

scan: remove divided by 1e6 in graphql parser and added unbonding amount in accountindex
  • Loading branch information
Kanisorn Thongprapaisaeng authored May 18, 2020
2 parents b1673c3 + 152cb9f commit 59688a9
Show file tree
Hide file tree
Showing 11 changed files with 5,122 additions and 574 deletions.
5,523 changes: 5,006 additions & 517 deletions scan/graphql_schema.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions scan/src/components/PieChart.re
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ let renderSegment = (offset, angle, color) =>
</>;

[@react.component]
let make = (~size, ~availableBalance, ~balanceAtStake, ~reward) => {
let totalBalance = availableBalance +. balanceAtStake;
let make = (~size, ~availableBalance, ~balanceAtStake, ~reward, ~unbonding) => {
let totalBalance = availableBalance +. balanceAtStake +. unbonding +. reward;
let balanceAtStakeAngle = totalBalance == 0. ? 0. : 360. *. balanceAtStake /. totalBalance;
let unbondingAngle = totalBalance == 0. ? 0. : 360. *. unbonding /. totalBalance;
let rewardAngle = totalBalance == 0. ? 0. : 360. *. reward /. totalBalance;

<div className={Styles.pie(size, Colors.bandBlue)}>
{renderSegment(0., balanceAtStakeAngle, Colors.chartBalanceAtStake)}
{renderSegment(balanceAtStakeAngle, rewardAngle, Colors.chartReward)}
{renderSegment(balanceAtStakeAngle +. rewardAngle, unbondingAngle, Colors.blue4)}
</div>;
};
10 changes: 8 additions & 2 deletions scan/src/components/account/AccountIndexDelegations.re
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,18 @@ let make = (~address) =>
</Col>
<Col size=0.6>
<div className=Styles.alignRight>
<Text value={delegation.amount |> Format.fPretty} code=true />
<Text
value={delegation.amount |> Coin.getBandAmountFromCoin |> Format.fPretty}
code=true
/>
</div>
</Col>
<Col size=0.6>
<div className=Styles.alignRight>
<Text value={delegation.reward |> Format.fPretty} code=true />
<Text
value={delegation.reward |> Coin.getBandAmountFromCoin |> Format.fPretty}
code=true
/>
</div>
</Col>
<Col> <HSpacing size=Spacing.lg /> </Col>
Expand Down
2 changes: 1 addition & 1 deletion scan/src/components/validator/DelegatorsTable.re
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ let make = (~address) =>
<div className=Styles.fillLeft />
<Text
block=true
value={amount |> Format.fPretty}
value={amount |> Coin.getBandAmountFromCoin |> Format.fPretty}
size=Text.Md
weight=Text.Regular
color=Colors.gray7
Expand Down
49 changes: 25 additions & 24 deletions scan/src/pages/AccountIndexPage.re
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Styles = {
let separatorLine =
style([
width(`px(1)),
height(`px(200)),
height(`px(275)),
backgroundColor(Colors.gray7),
marginLeft(`px(20)),
opacity(0.3),
Expand All @@ -34,14 +34,14 @@ module Styles = {
flexDirection(`column),
justifyContent(`spaceBetween),
alignItems(`flexEnd),
height(`px(190)),
height(`px(200)),
padding2(~v=`px(12), ~h=`zero),
]);

let totalBalance = style([display(`flex), flexDirection(`column), alignItems(`flexEnd)]);
};

let balanceDetail = (title, amount, amountUsd, color) => {
let balanceDetail = (title, amount, usdPrice, color) => {
<Row alignItems=Css.flexStart>
<Col size=0.25> <div className={Styles.ovalIcon(color)} /> </Col>
<Col size=1.2>
Expand All @@ -51,7 +51,7 @@ let balanceDetail = (title, amount, amountUsd, color) => {
<div className=Styles.cFlex>
<div className=Styles.rFlex>
<Text
value=amount
value={amount |> Format.fPretty}
size=Text.Lg
weight=Text.Semibold
spacing={Text.Em(0.02)}
Expand All @@ -71,7 +71,7 @@ let balanceDetail = (title, amount, amountUsd, color) => {
<VSpacing size=Spacing.xs />
<div className={Css.merge([Styles.rFlex, Styles.balance])}>
<Text
value=amountUsd
value={amount *. usdPrice |> Format.fPretty}
size=Text.Sm
spacing={Text.Em(0.02)}
weight=Text.Thin
Expand Down Expand Up @@ -118,14 +118,21 @@ let make = (~address, ~hashtag: Route.account_tab_t) =>
let accountSub = AccountSub.get(address);
let infoSub = React.useContext(GlobalContext.context);
let balanceAtStakeSub = DelegationSub.getTotalStakeByDelegator(address);
let unbondingSub = UnbondingSub.getUnbondingBalance(address);

let%Sub info = infoSub;
let%Sub account = accountSub;
let%Sub balanceAtStake = balanceAtStakeSub;
let%Sub unbonding = unbondingSub;

let availableBalance = account.balance->Coin.getBandAmountFromCoins;
let usdPrice = info.financial.usdPrice;
let totalBalance = availableBalance +. balanceAtStake.amount +. balanceAtStake.reward;

let balanceAtStakeAmount = balanceAtStake.amount->Coin.getBandAmountFromCoin;
let rewardAmount = balanceAtStake.reward->Coin.getBandAmountFromCoin;
let unbondingAmount = unbonding->Coin.getBandAmountFromCoin;

let totalBalance = availableBalance +. balanceAtStakeAmount +. rewardAmount +. unbondingAmount;

<>
<Row justify=Row.Between>
Expand Down Expand Up @@ -156,34 +163,28 @@ let make = (~address, ~hashtag: Route.account_tab_t) =>
<PieChart
size=187
availableBalance
balanceAtStake={balanceAtStake.amount}
reward={balanceAtStake.reward}
balanceAtStake=balanceAtStakeAmount
reward=rewardAmount
unbonding=unbondingAmount
/>
</Col>
<Col size=1.>
<VSpacing size=Spacing.md />
{balanceDetail(
"AVAILABLE BALANCE",
availableBalance |> Format.fPretty,
availableBalance *. usdPrice |> Format.fPretty,
Colors.bandBlue,
)}
<VSpacing size=Spacing.xl />
{balanceDetail("AVAILABLE BALANCE", availableBalance, usdPrice, Colors.bandBlue)}
<VSpacing size=Spacing.lg />
<VSpacing size=Spacing.md />
{balanceDetail(
"BALANCE AT STAKE",
balanceAtStake.amount |> Format.fPretty,
balanceAtStake.amount *. usdPrice |> Format.fPretty,
balanceAtStakeAmount,
usdPrice,
Colors.chartBalanceAtStake,
)}
<VSpacing size=Spacing.xl />
<VSpacing size=Spacing.lg />
<VSpacing size=Spacing.md />
{balanceDetail(
"REWARD",
balanceAtStake.reward |> Format.fPretty,
balanceAtStake.reward *. usdPrice |> Format.fPretty,
Colors.chartReward,
)}
{balanceDetail("UNBONDING AMOUNT", unbondingAmount, usdPrice, Colors.blue4)}
<VSpacing size=Spacing.lg />
<VSpacing size=Spacing.md />
{balanceDetail("REWARD", rewardAmount, usdPrice, Colors.chartReward)}
</Col>
<div className=Styles.separatorLine />
<Col size=1. alignSelf=Col.Start>
Expand Down
10 changes: 7 additions & 3 deletions scan/src/pages/ValidatorHomePage.re
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ let renderBody =
| Data({tokens, votingPower}) =>
<div>
<Text
value={tokens |> Format.fPretty}
value={tokens |> Coin.getBandAmountFromCoin |> Format.fPretty}
color=Colors.gray7
code=true
weight=Text.Regular
Expand Down Expand Up @@ -353,7 +353,7 @@ let make = () => {
<InfoHL
info={
InfoHL.Fraction(
bondedTokenCount |> int_of_float,
bondedTokenCount |> Coin.getBandAmountFromCoin |> int_of_float,
metadata.totalSupply->Coin.getBandAmountFromCoins |> int_of_float,
true,
)
Expand Down Expand Up @@ -437,7 +437,11 @@ let make = () => {
{validators->Belt_Array.size > 0
? validators
->Belt_Array.mapWithIndex((i, e) =>
renderBody(i + 1 + (page - 1) * pageSize, Sub.resolve(e), bondedTokenCount)
renderBody(
i + 1 + (page - 1) * pageSize,
Sub.resolve(e),
bondedTokenCount.amount,
)
)
->React.array
: <div className=Styles.emptyContainer>
Expand Down
4 changes: 3 additions & 1 deletion scan/src/pages/ValidatorIndexPage.re
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ let make = (~address, ~hashtag: Route.validator_tab_t) =>
let validatorSub = ValidatorSub.get(address);
let bondedTokenCountSub = ValidatorSub.getTotalBondedAmount();
let%Sub validator = validatorSub;
let%Sub bondedTokenCount = bondedTokenCountSub;
let%Sub rawBondedTokenCount = bondedTokenCountSub;

let bondedTokenCount = rawBondedTokenCount.amount;

<>
<Row justify=Row.Between>
Expand Down
20 changes: 10 additions & 10 deletions scan/src/subscriptions/DelegationSub.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ type t = {
};

type stake_t = {
amount: float,
reward: float,
amount: Coin.t,
reward: Coin.t,
sharePercentage: float,
delegatorAddress: Address.t,
validatorAddress: Address.t,
};

type stake_aggregate_t = {
amount: float,
reward: float,
amount: Coin.t,
reward: Coin.t,
};

module StakeConfig = [%graphql
{|
subscription Stake($limit: Int!, $offset: Int!, $delegator_address: String!) {
delegations_view(offset: $offset, limit: $limit, order_by: {amount: desc}, where: {delegator_address: {_eq: $delegator_address}}) @bsRecord {
amount @bsDecoder(fn: "GraphQLParser.numberExn")
reward @bsDecoder(fn: "GraphQLParser.numberExn")
amount @bsDecoder(fn: "GraphQLParser.coinExn")
reward @bsDecoder(fn: "GraphQLParser.coinExn")
sharePercentage: share_percentage @bsDecoder(fn: "GraphQLParser.floatWithDefault")
delegatorAddress: delegator_address @bsDecoder(fn: "GraphQLParser.addressExn")
validatorAddress: validator_address @bsDecoder(fn: "GraphQLParser.addressExn")
Expand All @@ -37,8 +37,8 @@ module TotalStakeByDelegatorConfig = [%graphql
delegations_view_aggregate(where: {delegator_address: {_eq: $delegator_address}}){
aggregate{
sum{
amount @bsDecoder(fn: "GraphQLParser.numberWithDefault")
reward @bsDecoder(fn: "GraphQLParser.numberWithDefault")
amount @bsDecoder(fn: "GraphQLParser.coinWithDefault")
reward @bsDecoder(fn: "GraphQLParser.coinWithDefault")
}
}
}
Expand All @@ -62,8 +62,8 @@ module DelegatorsByValidatorConfig = [%graphql
{|
subscription Stake($limit: Int!, $offset: Int!, $validator_address: String!) {
delegations_view(offset: $offset, limit: $limit, order_by: {amount: desc}, where: {validator_address: {_eq: $validator_address}}) @bsRecord {
amount @bsDecoder(fn: "GraphQLParser.numberExn")
reward @bsDecoder(fn: "GraphQLParser.numberExn")
amount @bsDecoder(fn: "GraphQLParser.coinExn")
reward @bsDecoder(fn: "GraphQLParser.coinExn")
sharePercentage: share_percentage @bsDecoder(fn: "GraphQLParser.floatWithDefault")
delegatorAddress: delegator_address @bsDecoder(fn: "GraphQLParser.addressExn")
validatorAddress: validator_address @bsDecoder(fn: "GraphQLParser.addressExn")
Expand Down
26 changes: 18 additions & 8 deletions scan/src/subscriptions/GraphQLParser.re
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ let hash = json =>
json |> Js.Json.decodeString |> Belt.Option.getExn |> Js.String.substr(~from=2) |> Hash.fromHex;

let coinRegEx = "([0-9]+)([a-z][a-z0-9/]{2,31})" |> Js.Re.fromString;
let coin = json => {
json |> Js.Json.decodeNumber |> Belt_Option.getExn |> Coin.newUBANDFromAmount;
};
let coinExn = jsonOpt => {
jsonOpt
|> Belt_Option.flatMap(_, Js.Json.decodeNumber)
|> Belt.Option.getExn
|> Coin.newUBANDFromAmount;
};
let coinWithDefault = jsonOpt => {
jsonOpt
|> Belt_Option.flatMap(_, Js.Json.decodeNumber)
|> Belt.Option.getWithDefault(_, 0.0)
|> Coin.newUBANDFromAmount;
};
let coins = str =>
str
|> Js.String.split(",")
Expand All @@ -35,17 +50,12 @@ let coins = str =>

let addressExn = jsonOpt => jsonOpt |> Belt_Option.getExn |> Address.fromBech32;

// TODO: remove 1e6.
let numberExn = jsonOpt =>
(jsonOpt |> Belt_Option.flatMap(_, Js.Json.decodeNumber) |> Belt.Option.getExn) /. 1_000_000.;

let numberWithDefault = jsonOpt =>
(jsonOpt |> Belt_Option.flatMap(_, Js.Json.decodeNumber) |> Belt.Option.getWithDefault(_, 0.0))
/. 1_000_000.;
jsonOpt |> Belt_Option.flatMap(_, Js.Json.decodeNumber) |> Belt.Option.getWithDefault(_, 0.0);

let floatWithDefault = jsonOpt =>
jsonOpt |> Belt_Option.flatMap(_, Js.Json.decodeNumber) |> Belt.Option.getWithDefault(_, 0.);

let floatWithMillionDivision = json => {
(json |> Js.Json.decodeNumber |> Belt.Option.getExn) /. 1_000_000.;
let floatExn = json => {
json |> Js.Json.decodeNumber |> Belt.Option.getExn;
};
34 changes: 34 additions & 0 deletions scan/src/subscriptions/UnbondingSub.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
type t = {balance: Coin.t};

module SingleConfig = [%graphql
{|
subscription Unbonding($delegator_address: String!) {
unbonding_delegations_aggregate(where: {delegator_address: {_eq: $delegator_address}}) {
aggregate {
sum {
balance @bsDecoder(fn: "GraphQLParser.coinWithDefault")
}
}
}
}
|}
];

let getUnbondingBalance = delegatorAddress => {
let (result, _) =
ApolloHooks.useSubscription(
SingleConfig.definition,
~variables=
SingleConfig.makeVariables(~delegator_address=delegatorAddress |> Address.toBech32, ()),
);

let unbondingInfoSub =
result
|> Sub.map(_, a =>
(a##unbonding_delegations_aggregate##aggregate |> Belt_Option.getExn)##sum
|> Belt_Option.getExn
);

let%Sub unbondingInfo = unbondingInfoSub;
unbondingInfo##balance |> Sub.resolve;
};
12 changes: 6 additions & 6 deletions scan/src/subscriptions/ValidatorSub.re
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type internal_t = {
moniker: string,
identity: string,
website: string,
tokens: float,
tokens: Coin.t,
commissionRate: float,
consensusPubKey: PubKey.t,
bondedHeight: int,
Expand All @@ -31,7 +31,7 @@ type t = {
identity: string,
website: string,
details: string,
tokens: float,
tokens: Coin.t,
commission: float,
bondedHeight: int,
completedRequestCount: int,
Expand Down Expand Up @@ -59,7 +59,7 @@ let toExternal =
operatorAddress,
consensusAddress,
consensusPubKey,
votingPower: tokens,
votingPower: tokens.amount,
moniker,
identity,
website,
Expand Down Expand Up @@ -89,7 +89,7 @@ module SingleConfig = [%graphql
moniker
identity
website
tokens @bsDecoder(fn: "GraphQLParser.floatWithMillionDivision")
tokens @bsDecoder(fn: "GraphQLParser.coin")
commissionRate: commission_rate @bsDecoder(fn: "float_of_string")
consensusPubKey: consensus_pubkey @bsDecoder(fn: "PubKey.fromBech32")
bondedHeight: bonded_height @bsDecoder(fn: "GraphQLParser.int64")
Expand All @@ -109,7 +109,7 @@ module MultiConfig = [%graphql
moniker
identity
website
tokens @bsDecoder(fn: "GraphQLParser.floatWithMillionDivision")
tokens @bsDecoder(fn: "GraphQLParser.coin")
commissionRate: commission_rate @bsDecoder(fn: "float_of_string")
consensusPubKey: consensus_pubkey @bsDecoder(fn: "PubKey.fromBech32")
bondedHeight: bonded_height @bsDecoder(fn: "GraphQLParser.int64")
Expand All @@ -126,7 +126,7 @@ module TotalBondedAmountConfig = [%graphql
validators_aggregate{
aggregate{
sum{
tokens @bsDecoder(fn: "GraphQLParser.numberWithDefault")
tokens @bsDecoder(fn: "GraphQLParser.coinWithDefault")
}
}
}
Expand Down

0 comments on commit 59688a9

Please sign in to comment.