-
Notifications
You must be signed in to change notification settings - Fork 168
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
Add core math packages (using wasm) & high-level typescript sdk #201
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
few nits but looks good!
15185d6
to
acc72f8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is looking good my only thought is to use checked_div
instead of /
so we can avoid any unintended divide-by-zero errors
@yugure-orca I think this now contains all the core math and quoting logic for whirlpools 🥳. I am however not 100% confident yet that all the logic is implemented correctly 😬. Especially some of the round ups and downs when doing when doing muldiv and bitshifts As stated this same logic is exported through wasm (still with std lib but we can fix that later) so rust sdk and ts sdk will share the exact same logic 🎊 (If in the future we ever want to make sure that the core logic is shared between the SDKs and the program that should theoretically be possible with the way I set up the core package) |
20a1a9d
to
a11aeff
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, final versions of the macros look sound and well tested
447528b
to
92ed426
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for hard work!
WASM integration is really interesting.
I have not reviewed the following 2 files yet, but push current review comments.
- core/src/math/price.rs
- core/src/quote/liquidity.rs
We have added comments, but my overall impression is as follows:
- u128 is often used as type for token amount. is it impossible to u64 with wasm ?
- I believe we should use same logic to the latest contract because it have been battle-tested and audited and reviewed by us.
- we need to be careful for negative tick_index, especially with % (mod) operation.
- I think panic! is the end of the world and should not be used for invalid input.
- .unwrap() can be used if we can assure that it will not lead to panic. (logically correct but compiler cannot determine)
- .saturating_x function is so dangerous because it will drop overflow and underflow errors.
- If contract uses checked_add or checked_sub, it is safe to use them in client-side.
- Is it impossible to use Result<T, E> and Option with wasm ? I noticed that many Result and Option type is removed and I feel that those changes are unsafe.
- Math for TransferFee calc, Slippage calc and Trade fee calc are similar, but I think "integrated" function is not intuitive as one reader.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed the following 2 files 🫡
- core/src/math/price.rs
- core/src/quote/liquidity.rs
need to be careful:
- we need to use sqrt_price (tick_index to sqrt_price conversion is safe, so if devs have tick_index only, they can safely convert it to sqrt_price) because tick_current_index is not exact pool price.
- in this area, many overflow/underflow risk exist, so Result type and checked_x function will help us (no saturating function)
- we can use real value of SOL/USDC for PriceMath logic testing.
proposal:
- nit: slippage_tolerance: u16 --> slippage_tolerance_bps: u16
- token amount based slippage is sensitive and easy to fail transactions (so we may add improved instruction in this area, so reserving "general" name may be nice)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pushed some comments
rust-sdk/core/src/math/token.rs
Outdated
/// # Returns | ||
/// - `u128`: The amount after transfer fee | ||
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = adjust_amount, skip_jsdoc))] | ||
pub fn adjust_amount(amount: U128, adjust_type: AdjustmentType, adjust_up: bool) -> U128 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wjthieme
At least, Whirlpool Program uses only 2 logic, so if we truely 4 logic, it is critical issue.
In ExactIn:
[ for input token ]
- user specify "Pre fee amount (transfer fee included amount)"
- the program calculate "Post fee amount (transfer fee exluced amount)"
- swap core logic use "Post fee amount" (swap core logic don't need to handle transfer fee)
[ for output token ]
- swap core logic generate output amount and it is "Pre fee amount"
- the program calculate "Post fee amount (transfer fee excluded amount)" and compare it with other_amount_threshold
User spacify amount transferred from their wallet as amount, and specify amount received as other_amount_threshold.
In ExactOut:
[ for output token ]
- user specify "Post fee amount (transfer fee excluded amount)"
- the program calculate "Pre fee amount"
- swap core logic use "Pre fee amount" (swap core logic don't need to handle transfer fee)
[ for input token ]
- swap core logic generate required input amount and it is "Post fee amount"
- the program calculate "Pre fee amount (transfer fee included amount)" and compare it with other_amount_threshold
Since the keyword adjust is somewhat ambiguous to me, I would like to see three separate functions (TransferFee, Slippage, SwapFee) for different purposes.
The fact that they call a common code internally would be a way to maintain readability.
However, if the common function branches for each AdjustmentType, it would be safer to separate the functions originally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pushed comments for previous review comments
rust-sdk/core/src/quote/swap.rs
Outdated
tick_sequence.next_initialized_tick(current_tick_index) | ||
} else { | ||
tick_sequence.prev_initialized_tick(current_tick_index) | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wjthieme
If sqrt_price is on between tick_index = 0 and tick_index = 1 in USDC/USDT pool, tick_current_index is 0.
I think prev_initialized_tick(0)
will be -1
and skip 0
.
So pool liquidity calculation will be de-sync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pushed comments for review comments on #262
889ebfa
to
ff9dbf5
Compare
ff9dbf5
to
f36ef1f
Compare
commit 5f67e17 Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 11:04:53 2024 -0400 Tweaks commit 30e8806 Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 11:00:38 2024 -0400 Test commit 363167a Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 10:59:06 2024 -0400 test commit 694373a Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 10:58:01 2024 -0400 Test commit a0bee58 Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 10:46:54 2024 -0400 Fixes commit 310a3c9 Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 10:40:23 2024 -0400 Tweak commit 80942de Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 10:38:46 2024 -0400 Tweak commit af029c5 Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 10:36:35 2024 -0400 Fix commit 0e74d16 Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 10:35:30 2024 -0400 Tweaks commit 87adc7d Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 10:29:19 2024 -0400 Tweak commit 805bfd7 Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 10:24:37 2024 -0400 Fix commit 78c137d Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 10:23:29 2024 -0400 Tweaks commit 907ff55 Author: Wilhelm Thieme <[email protected]> Date: Sun Oct 20 10:20:39 2024 -0400 Use local anchor setup instead of docker image commit 5b8c691 Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 19 21:48:55 2024 -0400 test? commit a02b9e1 Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 19 21:07:52 2024 -0400 Revert test commit 559caa4 Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 19 20:45:48 2024 -0400 test commit f18648d Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 19 20:19:51 2024 -0400 Run ci in container commit c51bd9a Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 19 15:30:42 2024 -0400 Tweaks commit e789058 Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 19 15:27:24 2024 -0400 Tweak commit 174c99d Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 19 11:03:15 2024 -0400 Tweak commit 928580a Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 19 09:58:47 2024 -0400 Tweak CI commit 675889b Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 19 09:46:06 2024 -0400 Node 22 commit edb7b91 Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 19 09:34:40 2024 -0400 Tweak commit a3c067f Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 19 09:26:30 2024 -0400 Build fix commit f36ef1f Author: Wilhelm Thieme <[email protected]> Date: Fri Oct 18 23:38:27 2024 -0400 Fix failing tests commit 0073f8d Author: Wilhelm Thieme <[email protected]> Date: Fri Oct 18 22:38:35 2024 -0400 vitest commit 564c701 Author: Wilhelm Thieme <[email protected]> Date: Fri Oct 18 17:39:36 2024 -0400 Edge case fix commit 7ac67d6 Merge: 057136c 1985fac Author: Wilhelm Thieme <[email protected]> Date: Fri Oct 18 14:31:07 2024 -0400 Merge branch 'main' into wjthieme/utils-package # Conflicts: # docs/ts/package.json # yarn.lock commit 057136c Author: Wilhelm Thieme <[email protected]> Date: Fri Oct 18 14:30:24 2024 -0400 Review comments commit 5127075 Author: calintje <[email protected]> Date: Thu Oct 17 14:40:03 2024 +0200 Fix description of slippage tolerance commit de26337 Author: Calin <[email protected]> Date: Thu Oct 17 14:28:40 2024 +0200 Typedocs for TS SDK (#381) * Base typedocs * Update docs * Finish typedocs for PR. Add export keyword for types. Update examples. Update typedocs for types. * Resolve comments --------- Co-authored-by: calintje <[email protected]> commit 8ecfdb0 Author: Wilhelm Thieme <[email protected]> Date: Tue Oct 15 17:06:02 2024 -0400 Swap tests and logic commit 6a088eb Author: Wilhelm Thieme <[email protected]> Date: Tue Oct 15 13:39:42 2024 -0400 Tick array sequence function signature commit 81da815 Merge: 5e262b0 cd4fa33 Author: Wilhelm Thieme <[email protected]> Date: Tue Oct 15 11:32:52 2024 -0400 Merge branch 'main' into wjthieme/utils-package # Conflicts: # docs/ts/package.json # yarn.lock commit 5e262b0 Author: Wilhelm Thieme <[email protected]> Date: Tue Oct 15 11:17:58 2024 -0400 Better swap quote function signature commit 78de101 Author: Wilhelm Thieme <[email protected]> Date: Tue Oct 15 10:15:09 2024 -0400 Deposit ratio commit 48819ae Author: Wilhelm Thieme <[email protected]> Date: Mon Oct 14 17:12:43 2024 -0400 Fixes commit 11e4a3d Author: Wilhelm Thieme <[email protected]> Date: Mon Oct 14 09:26:13 2024 -0400 High level TS function naming commit 706313f Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 12 21:52:36 2024 -0400 Fix some tests commit 4aa3fd2 Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 12 21:42:41 2024 -0400 Refactor macros commit 3e65908 Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 12 15:17:22 2024 -0400 Fix some tests commit d95f035 Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 12 14:37:02 2024 -0400 Format commit 5f5b111 Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 12 12:58:05 2024 -0400 Merge commit 33cf14c Author: Wilhelm Thieme <[email protected]> Date: Sat Oct 12 10:44:07 2024 -0400 Squashed commit of the following: commit 6f50883 Author: Wilhelm Thieme <[email protected]> Date: Thu Oct 10 11:13:31 2024 -0400 Create pool naming commit f54d406 Author: Wilhelm Thieme <[email protected]> Date: Wed Oct 9 11:45:09 2024 -0400 Fix build commit 04e0dab Author: Wilhelm Thieme <[email protected]> Date: Wed Oct 9 09:13:34 2024 -0400 Squashed commit of the following: commit 05cad83 Author: Wilhelm Thieme <[email protected]> Date: Tue Oct 8 21:37:12 2024 -0400 Tweaks commit db00d93 Author: Wilhelm Thieme <[email protected]> Date: Tue Oct 8 11:20:12 2024 -0400 New open/close instructions commit 1fd4563 Merge: df7466d 447528b Author: Wilhelm Thieme <[email protected]> Date: Tue Oct 8 11:01:03 2024 -0400 Merge branch 'wjthieme/utils-package' into wjthieme/ts-sdk # Conflicts: # yarn.lock commit 447528b Merge: aa32a8f 16661e2 Author: Wilhelm Thieme <[email protected]> Date: Tue Oct 8 11:00:17 2024 -0400 Merge branch 'main' into wjthieme/utils-package # Conflicts: # docs/ts/package.json # yarn.lock commit df7466d Author: Wilhelm Thieme <[email protected]> Date: Tue Oct 8 10:59:02 2024 -0400 Fetch positions and Transfer fees commit a4a967b Author: Wilhelm Thieme <[email protected]> Date: Mon Oct 7 08:34:20 2024 -0400 Add TODOs commit 8454019 Merge: f66c061 aa32a8f Author: Wilhelm Thieme <[email protected]> Date: Wed Oct 2 17:46:49 2024 -0400 Merge branch 'wjthieme/utils-package' into wjthieme/ts-sdk # Conflicts: # ts-sdk/client/package.json # yarn.lock commit aa32a8f Author: Wilhelm Thieme <[email protected]> Date: Wed Oct 2 17:45:24 2024 -0400 Cargo lock commit 6ac1a3f Merge: 3c32ace 2c35e54 Author: Wilhelm Thieme <[email protected]> Date: Wed Oct 2 17:45:03 2024 -0400 Merge branch 'main' into wjthieme/utils-package # Conflicts: # rust-sdk/client/Cargo.toml # yarn.lock commit f66c061 Author: Wilhelm Thieme <[email protected]> Date: Wed Oct 2 17:42:23 2024 -0400 Build fix commit 4604375 Author: Wilhelm Thieme <[email protected]> Date: Tue Sep 17 11:04:09 2024 +0800 Failing test commit 373ad97 Merge: 919f0dd 3c32ace Author: Wilhelm Thieme <[email protected]> Date: Tue Sep 17 09:54:29 2024 +0800 Merge branch 'wjthieme/utils-package' into wjthieme/ts-sdk # Conflicts: # ts-sdk/whirlpool/package.json # yarn.lock commit 3c32ace Merge: 7424e40 18d5bcc Author: Wilhelm Thieme <[email protected]> Date: Tue Sep 17 09:52:38 2024 +0800 Merge branch 'main' into wjthieme/utils-package # Conflicts: # docs/ts/package.json # yarn.lock commit 919f0dd Author: Wilhelm Thieme <[email protected]> Date: Mon Sep 9 18:06:26 2024 -0400 Fix tests commit 7424e40 Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 22:31:55 2024 -0500 Format commit f61b31f Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 22:31:32 2024 -0500 Format commit acaee2a Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 22:26:51 2024 -0500 Swap commit 4cd718a Merge: 31956ef 4d627c4 Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 22:13:05 2024 -0500 Merge branch 'wjthieme/utils-package' into wjthieme/ts-sdk commit 4d627c4 Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 22:12:57 2024 -0500 Fix test commit 31956ef Merge: 25a2b87 0dbd9dd Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 22:10:54 2024 -0500 Merge branch 'wjthieme/utils-package' into wjthieme/ts-sdk commit 0dbd9dd Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 22:10:45 2024 -0500 More tick arrays commit a0834c1 Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 11:36:11 2024 -0500 Manifest and readme files commit 25a2b87 Author: Wilhelm Thieme <[email protected]> Date: Thu Aug 29 11:27:07 2024 -0500 Token 22 test case commit c5afd33 Author: Wilhelm Thieme <[email protected]> Date: Thu Aug 29 11:21:04 2024 -0500 Typedoc commit 030ff19 Author: Wilhelm Thieme <[email protected]> Date: Thu Aug 29 11:08:22 2024 -0500 Instruction assertions commit bba3da0 Author: Wilhelm Thieme <[email protected]> Date: Thu Aug 29 09:22:11 2024 -0500 Tests for token account creation commit dde599b Author: Wilhelm Thieme <[email protected]> Date: Wed Aug 28 17:00:31 2024 -0500 Amend commit fc75f93 Author: Wilhelm Thieme <[email protected]> Date: Wed Aug 28 16:59:59 2024 -0500 Sol wrapping and unwrapping commit f8c00d4 Author: Wilhelm Thieme <[email protected]> Date: Wed Aug 28 11:16:54 2024 -0400 Mock rpc for testing ts whirlpool lib commit 82415e4 Merge: addf594 c2cfe57 Author: Wilhelm Thieme <[email protected]> Date: Wed Aug 28 07:28:27 2024 -0400 Merge branch 'wjthieme/utils-package' into wjthieme/ts-sdk # Conflicts: # rust-sdk/core/src/quote/swap.rs commit c2cfe57 Author: Wilhelm Thieme <[email protected]> Date: Wed Aug 28 07:27:20 2024 -0400 Tweaks commit addf594 Author: Wilhelm Thieme <[email protected]> Date: Tue Aug 27 11:30:13 2024 -0400 Types tests commit 5be25bd Author: Wilhelm Thieme <[email protected]> Date: Tue Aug 27 09:59:00 2024 -0400 Enum type commit f113c26 Author: Wilhelm Thieme <[email protected]> Date: Mon Aug 26 17:09:01 2024 -0400 Ts core smoke test commit 14158b4 Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 23 14:49:50 2024 -0400 Decrease liq and harvest position commit d0a75e8 Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 23 13:06:50 2024 -0400 Format commit edf241d Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 23 12:39:15 2024 -0400 Format commit 621424d Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 23 12:37:10 2024 -0400 open position and increase liquidity commit fbf54d1 Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 23 08:48:19 2024 -0400 Format commit ef97db2 Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 23 08:39:58 2024 -0400 PDA tests commit 78bea38 Author: Sam Johnson <[email protected]> Date: Fri Aug 23 00:19:45 2024 -0400 add support for arrays commit f4dabc0 Author: Wilhelm Thieme <[email protected]> Date: Thu Aug 22 17:47:33 2024 -0400 Create Pool commit 57b7613 Author: Wilhelm Thieme <[email protected]> Date: Thu Aug 22 15:49:29 2024 -0400 Scaffolding commit 4f637c6 Author: Wilhelm Thieme <[email protected]> Date: Thu Aug 22 13:00:51 2024 -0400 Fix failing tests commit 4ab7c35 Merge: a11aeff 261905d Author: Wilhelm Thieme <[email protected]> Date: Thu Aug 22 12:03:55 2024 -0400 Merge branch 'main' into wjthieme/utils-package # Conflicts: # yarn.lock commit a11aeff Author: Wilhelm Thieme <[email protected]> Date: Wed Aug 21 21:09:16 2024 -0400 Squash commit 92ed426 Author: Wilhelm Thieme <[email protected]> Date: Wed Oct 9 09:04:28 2024 -0400 Squashed commit of the following: commit 447528b Merge: aa32a8f 16661e2 Author: Wilhelm Thieme <[email protected]> Date: Tue Oct 8 11:00:17 2024 -0400 Merge branch 'main' into wjthieme/utils-package # Conflicts: # docs/ts/package.json # yarn.lock commit aa32a8f Author: Wilhelm Thieme <[email protected]> Date: Wed Oct 2 17:45:24 2024 -0400 Cargo lock commit 6ac1a3f Merge: 3c32ace 2c35e54 Author: Wilhelm Thieme <[email protected]> Date: Wed Oct 2 17:45:03 2024 -0400 Merge branch 'main' into wjthieme/utils-package # Conflicts: # rust-sdk/client/Cargo.toml # yarn.lock commit 3c32ace Merge: 7424e40 18d5bcc Author: Wilhelm Thieme <[email protected]> Date: Tue Sep 17 09:52:38 2024 +0800 Merge branch 'main' into wjthieme/utils-package # Conflicts: # docs/ts/package.json # yarn.lock commit 7424e40 Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 22:31:55 2024 -0500 Format commit 4d627c4 Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 22:12:57 2024 -0500 Fix test commit 0dbd9dd Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 22:10:45 2024 -0500 More tick arrays commit a0834c1 Author: Wilhelm Thieme <[email protected]> Date: Fri Aug 30 11:36:11 2024 -0500 Manifest and readme files commit c2cfe57 Author: Wilhelm Thieme <[email protected]> Date: Wed Aug 28 07:27:20 2024 -0400 Tweaks commit 4f637c6 Author: Wilhelm Thieme <[email protected]> Date: Thu Aug 22 13:00:51 2024 -0400 Fix failing tests commit 4ab7c35 Merge: a11aeff 261905d Author: Wilhelm Thieme <[email protected]> Date: Thu Aug 22 12:03:55 2024 -0400 Merge branch 'main' into wjthieme/utils-package # Conflicts: # yarn.lock commit a11aeff Author: Wilhelm Thieme <[email protected]> Date: Wed Aug 21 21:09:16 2024 -0400 Squash
5f67e17
to
1152cec
Compare
2f3b0b4
to
3eb0295
Compare
# Conflicts: # package.json # yarn.lock
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your hard work.
Yep, many code looks much cleaner to me.
I made some comments, but I would like to see the whole thing for the last time now. Therefore, I think it would be better to merge the PR once only the commented parts have been resolved and work on another small PR thereafter.
We can write the core logic in Rust, so I think it's safer to use the contract code as is as much as possible, although there is no way around things like U128/u128 conversions.
If the logic is the same, I don't have to review it as so carefully, and we can continue to have the peace of mind that it has been battle-tested.
# Conflicts: # package.json # yarn.lock
rust-sdk/core/src/math/token.rs
Outdated
) -> Result<u64, ErrorCode> { | ||
try_reverse_adjust_amount( | ||
amount, | ||
transfer_fee.fee_bps.into(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add test case with the following condition ?
[case1]
-
transfer_fee_rate: 10_000u16 (100%)
-
transfer_fee_max: 500u64
-
token_amount: 100_000u64
-
expected: 100_500u64
[case2]
-
transfer_fee_rate: 10_000u16 (100%)
-
transfer_fee_max: 500u64
-
token_amount: u64::MAX
-
expected: overflow
The reason why I commented that I want to use proven code (as-is), such as SPL-token code and whirlpool contract code, is to prevent such regressions. It is very sad as a reviewer to create another bug that has been solved before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to look the remaining 2 files:
- rust-sdk/core/src/math/tick_array.rs
- rust-sdk/core/src/quote/swap.rs
pushed some comments for previously commented part. 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your grate work to integrate Rust/TS core logic! 🚀
let raw_pre_fee_amount = numerator.div_ceil(denominator); | ||
let fee_amount = raw_pre_fee_amount | ||
.checked_sub(amount.into()) | ||
.ok_or(AMOUNT_EXCEEDS_MAX_U64)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: AMOUNT_EXCEEDS_MAX_U64
-> ARITHMETIC_OVERFLOW
because no into()
ops here.
core lib
Add two new packages to the monorepo
This approach is great because this allows us to have a single source of truth for math functions that are used throughout other libs (and potentially even the program). Writing it once means that the implementations will always be identical regardless of which platform you are on. Also means we only have to write tests for them once
The wasm lib currently only builds with std enabled because of something in
tsify
. This inflates the wasm bundle by quite a bit (65Kb instead of 25Kb). I opened a ticket: madonoharu/tsify#56. This only affects thets-sdk/core
and is a bundle size thing so not the end of the world. All code behaves identically as if it was built with/without stdThis PR only contains basic test cases and does not test for any of the more tricky / edge cases for swap quoting. My brain is fried from looking at tick array traversal and bit math the past couple of days so I'd like to continue with other parts of the SDK and revisit this later.
High Level TS sdk
Goals:
Still missing (added TODOs for these but not strictly necessary for this PR):
vitest
I had to move away from
ts-mocha
(hasn't been updated in more than 2 years 😮) because it would not work correctly with the esm/monorepo setup. I chosevitest
which is built esm-first and we already use it in other repos. Added benefit: the test suite seems to run a little bit faster nownote: Since this is not added to
publish
workflow yet this will not be deployed yet on tag