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

BRC20 Swap Module Indexer #58

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

jiedo
Copy link

@jiedo jiedo commented Dec 11, 2024

Preliminary implementation of the brc20 swap module.

Copy link

@alpergundogdu alpergundogdu left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the PR, this makes it much easier to test and review.

I'll look more into module_commit_*** files, as I didn't have time to go through all of them, but here's some initial feedback.

}
})

app.get('/v1/brc20/get_current_balance_of_wallet_2', async (request, response) => {

Choose a reason for hiding this comment

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

How is this different from get_current_balance_of_wallet? It might be better to add some comments explaining the difference

_2 was a bit confusing for me, is there a /v1/brc20/get_current_balance_of_wallet endpoint too?


// Decimal represents a fixed-point decimal number with 18 decimal places
type Decimal struct {
Precition uint

Choose a reason for hiding this comment

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

Precision* throughout this file

Choose a reason for hiding this comment

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

Is there a library we can use for these? I'm wary of adding all this logic in the module itself for maintenance purposes. Also, some methods in this file don't have tests and there's some bugs it looks like, added some comments in individual functions.

This also needs to be better documented and tested, as it's highly crucial for the rest of the module.

network = bitcoin.networks.regtest;
}

const pkscript_to_address = {};

Choose a reason for hiding this comment

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

Do we need to memorise all these? Would this cause memory bloat over a long period of time? Same below

@@ -0,0 +1,340 @@

CREATE TABLE public.brc20_ticker_info (

Choose a reason for hiding this comment

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

Makes sense to prefix these with brc20_swap to make it clear these are consumed/written by this module, so we don't have conflicts with different modules

Comment on lines +226 to +227
return &Decimal{Precition: d.Precition, Val: value}
}

Choose a reason for hiding this comment

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

This seems wrong, as precision can't be derived just from the first one.

Seems like this needs more tests, similar to addition and subtraction.

Copy link
Author

Choose a reason for hiding this comment

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

For convenience, the decimal places of the first argument are inherited directly when performing multiplication and division. This way, the arguments and results do not need to be re-adjusted to the same decimal places. However, addition and subtraction require strict consistency in the decimal places. We may be able to improve this and make the behavior of the decimal library more independent.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's the same with addition. A future contributor may not know how the custom decimal library works so it should do what its signature says in all cases. Using a well-documented and tested decimal library solves this issue.

}
if feeRateSwapAmt.Sign() > 0 {
// lp = (poolLp * (rootK - rootKLast)) / (rootK * 5 + rootKLast)
rootK := pool.TickBalance[token0Idx].Mul(pool.TickBalance[token1Idx]).Sqrt()

Choose a reason for hiding this comment

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

Not sure if I'm missing anything but rootK here seems like it will be the same as lastRootK, because the tick balances didn't change in-between, making (rootK - rootKLast) always 0.

Copy link
Author

Choose a reason for hiding this comment

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

Please check the logic of commit_swap.go. Because the actual amount is adjusted according to the fee when the user swaps, rootK has a slight change compared to the case when no one swaps.

value := new(big.Int).Set(other.Val)
return &Decimal{Precition: other.Precition, Val: value}
}
if d.Precition != other.Precition {

Choose a reason for hiding this comment

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

This function should be able to handle this case by using the highest precision

Copy link
Author

Choose a reason for hiding this comment

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

The Decimal library is closely related to the brc20 logic. Currently, there seems to be no scenario for adding the amounts of different tickers.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is an open-source project, a future contributor may not know this and may expect the decimal library to just work. So, every function should do what its signature says. In this case, the signature is to add 2 decimal values so it should work on all cases.

It would be much better to use a standard and easy-to-use library instead of a custom decimal library.

return nil
}
// value0 := new(big.Int).Mul(d.Value, precisionFactor[d.Precition])
value := new(big.Int).Sqrt(d.Val)

Choose a reason for hiding this comment

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

This needs better testing too, this works only for integers, not decimals.

Did a little testing here and NewDecimalFromString("100", 18).Sqrt() returns 0.00000001

Copy link
Author

Choose a reason for hiding this comment

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

Yes, currently sqrt is only performed after multiplying two numbers. The swap module needs a function that performs sqrt after multiplication instead of a separate sqrt.

if d == nil {
return -other.Val.Sign()
}
if d.Precition != other.Precition {

Choose a reason for hiding this comment

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

This also should be able to handle this case by converting one to the other

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