Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(raiden): channel balance by currency #1066

Merged
1 commit merged into from
Jul 9, 2019
Merged

feat(raiden): channel balance by currency #1066

1 commit merged into from
Jul 9, 2019

Conversation

sangaman
Copy link
Collaborator

Closes #1051.

Note: This is based off of #1064.

This modifies the logic around querying raiden for channel balances. Rather than summing up all balances across all tokens, it allows for specifying a currency and retrieving only the balance for that currency. Calling ChannelBalance without specifying a currency will return each currency separately.

It also scales the balance returned by raiden to satoshis (10^-8). This is because the default units of 10^-18 for currencies such as WETH can exceed the maximum value of a uint64 used by the gRPC layer.

@sangaman sangaman added grpc gRPC API raiden labels Jun 25, 2019
@sangaman sangaman requested a review from a user June 25, 2019 12:12
@sangaman sangaman self-assigned this Jun 25, 2019
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good at first glance. I'll wait for #1064 to be merged and this rebased against master before doing another round.

@kilrau
Copy link
Contributor

kilrau commented Jun 28, 2019

Needs rebase

@sangaman sangaman requested a review from a user July 1, 2019 19:47
@sangaman
Copy link
Collaborator Author

sangaman commented Jul 1, 2019

Rebased and tested locally, the new logic works for me.

@kilrau kilrau requested a review from michael1011 July 2, 2019 16:12
@kilrau
Copy link
Contributor

kilrau commented Jul 2, 2019

Restarted the failed tests - passed

@@ -41,6 +41,12 @@ class RaidenClient extends SwapClient {
private host: string;
private disable: boolean;

// TODO: Populate the mapping from the database (Currency.decimalPlaces).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe open an issue for this TODO so that you don't forget about it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think #1054 is tracking this.

const balance = channels.filter(channel => channel.state === 'opened')
.map(channel => channel.balance)
.reduce((acc, sum) => sum + acc, 0);
.reduce((acc, sum) => sum + (acc / RaidenClient.UNITS_PER_CURRENCY[currency]), 0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we want to switch places with sum and acc here (and rename them to sum and channelBalance.
arr.reduce(callback(accumulator, currentValue[, index[, array]]), [, initialValue])

Maybe do the conversion after reduce so that it's only going to be calculated once?

I think this will cause confusion on the CLI level if we show satoshis per coin as it's meant to be our internal unit only. What do you think about converting Raiden channel balance values to satsToCoinsStr and then doing additional conversion to smallest unit of the currency on CLI level?

Copy link
Collaborator Author

@sangaman sangaman Jul 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused by what you mean in the first paragraph, could you clarify? I went ahead and made the conversion happen after reduce though.

We can have the cli print whole units for the "formatted" output, that'd be consistent with how we're displaying amounts most other places. That should be a separate PR though I think. It would tricky to convert back down to the smallest units, since I figure that would require a DB lookup or an additional field in the rpc response, and I don't see it as being particularly useful or readable to display values smaller than 10^-8.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I was trying to say was that the first argument for the callback of reduce is sum and the second acc.

With the previous logic:

.reduce((acc, sum) => {
  console.log('sum is', sum, 'and converted', (acc / RaidenClient.UNITS_PER_CURRENCY[currency]));
  return sum + (acc / RaidenClient.UNITS_PER_CURRENCY[currency]);
}, 0);

Printed:

[1] sum is 75000000000000000 and converted 0

As a result converting back to satoshis per unit didn't have any effect:

➜  xud git:(channel-mgmt-plus-raiden-balance) ✗ ./bin/xucli channelbalance
{
  "balancesMap": [
    ...
    [
      "WETH",
      {
        "balance": 75000000000000000,
        "pendingOpenBalance": 0
      }
    ]
  ]
}

With the new logic this isn't an issue, but I'd still rename the arguments.

As for converting back to smallest units on the CLI level - let's open an issue for that to discuss. I think it will cause confusion for the end user:

"WETH",
{
  "balance": 7500000,
  "pendingOpenBalance": 0
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha.

lib/raidenclient/RaidenClient.ts Show resolved Hide resolved
lib/service/Service.ts Show resolved Hide resolved
@sangaman sangaman requested a review from a user July 6, 2019 16:23
This modifies the logic around querying raiden for channel balances.
Rather than summing up all balances across all tokens, it allows for
specifying a currency and retrieving only the balance for that
currency. Calling `ChannelBalance` without specifying a currency will
return each currency separately.

It also scales the balance returned by raiden to satoshis (10^-8). This
is because the default units of 10^-18 for currencies such as WETH can
exceed the maximum value of a `uint64` used by the gRPC layer.

Closes #1051.
const balance = channels.filter(channel => channel.state === 'opened')
.map(channel => channel.balance)
.reduce((acc, sum) => sum + acc, 0);
.reduce((sum, acc) => sum + acc, 0)
/ (RaidenClient.UNITS_PER_CURRENCY[currency] || 1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last nit - when commenting out this line the tests are still passing. We might want to consider covering the conversion in channelBalance calculates the total balance of open channels for a currency test as well. Up to you :).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I'll add this test case and merge.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I remember now that I tried to do this originally but in its current state I'd have to mock a static value and I'm not really sure how to do that. Maybe revisit this when we get the units per currency from the database?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.

@ghost ghost merged commit cc8e89f into master Jul 9, 2019
@ghost ghost deleted the raiden/balance branch July 9, 2019 08:01
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
grpc gRPC API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

channel balance (raiden)
3 participants