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

Lightning fiat #2574

Merged
merged 2 commits into from
Feb 27, 2024
Merged

Lightning fiat #2574

merged 2 commits into from
Feb 27, 2024

Conversation

Beerosagos
Copy link
Collaborator

@Beerosagos Beerosagos commented Feb 26, 2024

This is a first PR to start adding Fiat conversion support for lightning.
Fiat conversions for LN send/receive will be addressed in a separate PR.

@Beerosagos Beerosagos marked this pull request as ready for review February 26, 2024 10:10
@Beerosagos Beerosagos requested a review from benma February 26, 2024 10:10
@Beerosagos
Copy link
Collaborator Author

Requested reviews from both @benma and @thisconnect for backend/frontend 😇 🙏 thanks a lot

Copy link
Collaborator

@thisconnect thisconnect left a comment

Choose a reason for hiding this comment

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

tested a bit, looks very nice. added some comments.

frontends/web/src/api/lightning.ts Show resolved Hide resolved
@@ -217,7 +216,7 @@ export function AccountsSummary({ accounts, devices }: TProps) {
totalBalancePerCoin={ balancePerCoin ? balancePerCoin[keystore.rootFingerprint] : undefined}
totalBalance={ accountsTotalBalance ? accountsTotalBalance[keystore.rootFingerprint] : undefined}
balances={balances}
lightningNodeState={(lightningConfig.accounts.length && lightningConfig.accounts[0].rootFingerprint === keystore.rootFingerprint) ? nodeState : undefined}
lightningBalance={(lightningConfig.accounts.length && lightningConfig.accounts[0].rootFingerprint === keystore.rootFingerprint) ? lightningBalance : undefined}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This component doesn't really need lightningConfig and lightningBalance as those are only used in SummaryBalance component, wdyt about moving as much s possible into SummaryBalance?

you'll probably have to pass keystore.rootFingerprint as well somehow for the condition.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah, I moved the LN logic inside the component and refactored a bit to only pass it the keystore struct, instead of various fields belonging to it.

frontends/web/src/api/lightning.ts Outdated Show resolved Hide resolved
func (lightning *Lightning) GetBalance(_ *http.Request) interface{} {
balance, err := lightning.Balance()
if err != nil {
return responseDto{Success: false, ErrorMessage: err.Error()}
Copy link
Contributor

Choose a reason for hiding this comment

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

what does Dto mean? man i really dislike abbreviations lol

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's Data Transfer Object. It's quite used as an abbreviation, afaik.

@@ -154,13 +167,51 @@ func (lightning *Lightning) Deactivate() error {
return nil
}

// AssertActive returns an error if the lightning service not has been activated.
func (lightning *Lightning) AssertActive() error {
Copy link
Contributor

Choose a reason for hiding this comment

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

assert is the wrong word, as assert in all contexts I know (in Python, C, ...), it crashes/panics/exists.

CheckActive or ValidateActive would be better.

}

// Balance returns the balance of the lightning account, together with its conversions to fiat.
func (lightning *Lightning) Balance() (interface{}, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

don't return interface{} but a concrete type

Copy link
Contributor

Choose a reason for hiding this comment

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

this function should really return *accounts.Balance, like btc/account.go and eth/account.go, and the handler should convert to AccountBalance.

Reason: AccountBalance is frontend focused, and it's the handler's job to convert input/output from/to the frontend.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agree 👍 done.

Copy link
Contributor

Choose a reason for hiding this comment

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

Only now I see that the handlers are methods on the same struct :O but the split was still good, as we will hopefully refactor this later and make a clean split 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, we should definitely refactor the lightning handlers to belong to the handlers package, as done in the other places, but didn't want to do it now 😬 So that we can also use handlers.formatAmountAsJSON()

// Balance returns the balance of the lightning account, together with its conversions to fiat.
func (lightning *Lightning) Balance() (interface{}, error) {
if err := lightning.AssertActive(); err != nil {
return coin.FormattedAmount{}, err
Copy link
Contributor

Choose a reason for hiding this comment

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

return nil, err


nodeInfo, err := lightning.sdkService.NodeInfo()
if err != nil {
return coin.FormattedAmount{}, err
Copy link
Contributor

Choose a reason for hiding this comment

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

return nil, err

return coin.FormattedAmount{}, err
}

amount := coin.NewAmount(big.NewInt(int64(nodeInfo.ChannelsBalanceMsat / 1000)))
Copy link
Contributor

Choose a reason for hiding this comment

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

there is coin.NewAmountFromInt64()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nice! thanks

@@ -32,6 +32,14 @@ type AddressList struct {
Addresses []Address
}

// AccountBalance includes the total and incoming balance of an account.
type AccountBalance struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

there is already a Balance struct (in balance.go). making this ultra confusing.

Maybe move this to balance.go too and name it FormattedAccountBalance.

FormattedAmount is needed inside the lightning package. Moving it
into the `coin` package allow us to import it there too. Same for
AccountBalance, which is explicitly declared inside `accounts`.
Adds a new backend endpoint that allows to fetch the node balance and
the relative fiat conversions.
@Beerosagos
Copy link
Collaborator Author

Rebased and addressed comments. @benma @thisconnect PTAL 🙏

Copy link
Contributor

@benma benma left a comment

Choose a reason for hiding this comment

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

Thanks, utACK

Copy link
Collaborator

@thisconnect thisconnect left a comment

Choose a reason for hiding this comment

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

tested LGTM 👍

@Beerosagos Beerosagos merged commit 83836fe into BitBoxSwiss:staging-ln Feb 27, 2024
5 checks passed
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.

3 participants