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

How to calculate channel fees using the RPC ? #4289

Closed

Conversation

hosiawak
Copy link
Contributor

Rationale: I'm working on a tool for C-Lightning and need to calculate on chain channel opening/closing/other fees. I've added getrawtransaction to bcli.c that lets me do that. It should come in handy in other situations too.

Please review my code if you have a minute and let me know if you see any issues.


tokens = json_parse_simple(bcli->output,
bcli->output, bcli->output_bytes);
if (!tokens) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We usually elide { } for single-statement if/while/for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mimicked the style from *process_getblockchaininfo and *process_getutxout etc.

@@ -959,6 +1002,14 @@ static const struct plugin_command commands[] = {
"",
getutxout
},
{
"getrawtransaction",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason why it needs to go through lightningd instead of directly to bitcoind? This would increase effort for #3858. On the other hand, if #3858 pushes through, then it is reasonable for a plugin to prefer this interface rather than directly calling into bitcoind, as the backend might not be bitcoind anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tool I'm working on is web based node dashboard (I haven't found anything decent for C-Lightning). The reason why I'd like to avoid setting up connection details to both lightningd and bitcoind is better user experience when setting them up. Currently I'm running C-Lightning node on a remote machine whilst the tool is running locally so I'm using socat on the remote + socat on localhost to get a local socket that will work with lightning-client for node. It's not ideal if I have to do this for bitcoind as well and expect the users to do the same.

If #3858 goes through couldn't we disable getrawtransaction if it's not available in the backend or cannot be implemented ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

What transactions specifically are you extracting? My understanding is that if the bitcoind backend itself is not txindex=1, it would not be able to provide arbitrary transactions. We do not inform the backend of any pubkeys to watch as well (we just download every block as it comes in and iterate over each tx and check for those we are interested in), so without txindex=1 I am uncertain what a bitcoind would be able to provide.

If the transactions you are extracting are those that the LN node wants to track as well, maybe it would be better to expose those in our RPC (which is probably a lot more effort over here in C-Lightning, but feels like the "right thing").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm interested in channel opening and closing transactions only and specifically their on chain fees. After your comment I realised that this PR wouldn't work without a txindex=1 and that I can probably use getrawblockbyheight to get the data I want.

I still don't know how to calculate the value of the total inputs having just their txids and block data though. Does fee calculation still require txindex=1 in core and there's no way around it currently ?

I noticed that getblock will return tx fees when run in verbose mode soon (when it gets merged into core) so maybe I could use that instead and modify getrawblockbyheight to accept an optional tx position (index) parameter which will make it return only that transaction data (with calculated fee included because verbosity will be set to 2).

Is it a good idea that has a chance of being accepted ?

Copy link
Member

Choose a reason for hiding this comment

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

If we are the funders of the channel we'll have both the funding tx as well as the outputs corresponding to the funding's inputs, so for these we could compute them ourselves quite easily. The downside of this is that the data is pretty spotty (only covers our own transactions), but that may match your exact use-case if all you want to do is show users the fees they paid themselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cdecker Yes, I'm interested in showing how much we paid for channel txs. Do you know if it's possible if it's possible to get the outputs corresponding to funding's inputs via the JSON RPC somehow and if it's not possible where I can look in the C-Lightning code and learn how to expose this via the RPC ?

Copy link
Member

Choose a reason for hiding this comment

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

We currently don't have a way to list spent outputs which is what you'd require to compute fees. We can easily add a spent=True flag to listfunds to also include spent outputs.

From there on you'd just

  • call lightning-cli listtransactions, stash the result
  • call lightning-cli listfunds spent=true

And then merge the two results by:

  • Iterate through transactions
  • For each transaction:
    • Iterate over inputs
    • Look up matching output
    • Compute input amounts, and subtract output amounts.

Adding the flag is a good first issue, and if you'd like to go that way I'll open an issue tracking progress :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @cdecker I'll do that. I guess this PR can be closed then ?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me 👍

I'll open the tracking issue and close this PR then. Would you like to work on the spent=true param yourself (in which case I'll earmark the issue with your name)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cdecker Yes, I'll pick it up. Thanks.

@hosiawak hosiawak changed the title Added getrawtransaction to bcli.c How to calculate channel fees using the RPC ? Dec 21, 2020
@cdecker cdecker closed this Dec 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants