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

Conversion utilities #14

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

0xsimulacra
Copy link
Contributor

@0xsimulacra 0xsimulacra commented Jun 4, 2024

Hello,

This pull request includes the following implementations and changes:

  • Upgraded uint256 to its latest version (v1.2.4)
  • FromDecimal() to parse a int256 in the form of "-1123" or "1235"
  • Dec() will transform to a string to its decimal refresentation "1234" or "-234"
  • String() now internaly use Dec() method
  • FromHex() o parse a int256 in the form of "0x12fe" or "-0x23fd"
  • ToHex() will transform to a string in the form of "0x.." or "-0x.."
  • MarshalTextand UnmarshalText methods: implements encoding.TextMarshaler & encoding.TextUnmarshaler interfaces.
  • MarshalJSON and UnmarshalJSON methods: to implements json.Marshaler & json.Unmarshaler interfaces, so we can encode a int256 to its json representation and we can decode it from a json.
  • Test have been included for almost all the newly added methods (including a fuzz test for the FromDecimal methods that helped catch an implemntation error: when a string start with two or more signs ("--", "-+"..) it should produce an error same as in big.Int)
  • MostSignificantBit() return the most significant bit of the abs part of z, so ignoring the bit or the sign
  • Added Three usage examples in the read me, One of them being an almost finshed version of UniswapV3 GetTickAtSqrtRatio and another one is a Json Marshaling/UnMarshaling example.

Regarding the following fucniton that I added, maybe you don't want to have it and have only a BitLen() instead ?

// MostSignificantBit return the most significant bit of z, ignoring the bit or the sign
func (z *Int) MostSignificantBit() uint8 {
	return uint8(z.abs.BitLen() - 1)
}

Almost all what has been added (tests included) were taken/inspired by the uint256 package.

0xsimulacra added 6 commits June 4, 2024 22:54
- FromDecimal() will accept a string in the form of "-1123" or "1235"
- Dec() will transform to a string to its decimal refresentation "1234" or "-234"
- String() now internaly use Dec() method
- FromHex() will accept a string in the form of "0x12fe" or "-0x23fd"
- ToHex() will transform to a string in the form of "0x.." or "-0x.."
- MarshalText: implements encoding.TextMarshaler
- UnmarshalText: implements encoding.TextUnmarshaler
- MarshalJSON: implements json.Marshaler.
- UnmarshalJSON: implements json.Unmarshaler
- Forgot to mention that in these new commits I upgraded uint256 to its
  latest version
  int256
- Updated readMe with some usage examples
@0xsimulacra 0xsimulacra mentioned this pull request Jun 5, 2024
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.

1 participant