* Update all dependencies to 1.7.3, fix issues
* Remove esm from mocha
* Fix missed token test
* Also update rust version
* token-swap: update tolerance on sim test
* Run `cargo clippy --fix` for needless_borrow errors
* Rerun cargo fmt
* Add spl-token program id check helper function. Add program id to instruction bindings.
* Run cargo fmt
* Fixup tests
* Skip ATA tests when custom token program-id
Co-authored-by: Tyera Eulberg <tyera@solana.com>
* token-swap: Fix withdrawal all tokens slippage
When withdrawing, the slippage check is done before `min`ing the token a
and b amounts, which makes it possible to ignore the desired slippage,
and lose out on a lot more than expected.
This has an additional knock-on effect. When burning all of the pool
tokens, it becomes impossible to ever use it again.
* Check for slippage after getting the actual amount that would be
traded
* Re-initialize the pool token amount on the next deposit if all pool
tokens were burned
Fixes#1629
* Fmt + clippy
* Deposit one side on 0 pool tokens just gives new supply back
* Add rounding specification for deposit / withdraw
The fuzzing test was improved to make sure that the value of pool tokens
(minus fees) never changed, which revealed a calc bug with deposits and
withdrawals. If someone withdraws, they can take additional small
value out of the pool in the form of truncated value.
Before, deposit and withdrawal used the same calculation, rounding up the token
A and B amounts. Remember that deposit / withdrawal specify the pool
token amount, and calculate the amount of A / B required to get to that
many pool tokens. On withdrawal, we were giving back a little bit too
much. The concept is that everything would even out since a deposit is
required for a withdrawal, which makes everything a wash.
Fuzzing found this issue very quickly! The change is to introduce a
rounding parameter to avoid ever giving away too much value, either
during deposit or withdrawal.
* Cleanup
* Add withdraw test
* Cleanup
* Cargo fmt
* Remove curve tests from fuzzing cfg
* Update JS test
* Cleanup test
* Update honggfuzz again
* Fix fee on JS test
* Force correct version of honggfuzz for CI (remove later)
* Improve value tests using PreciseNumber everywhere
* Fix ceiling
* Add comment for 0 tokens
* Fix ceiling div, add in constant price calc
* Revert ceiling division as template and use trait, too much boilerplate
* Run cargo fmt
* Add user transfer authority to swap instruction
* Add user transfer authority to deposit all
* Add authority to withdraw all
* Add authority to deposit one
* Add authority to withdraw one
* Update fuzzer
* Add user transfer authority to JS
* Add extra check on instruction fuzz
* Refactor division
* Fix tests from fixed deposit math
* Fixup comments and cleanup fuzz
* Run fmt
* Address review feedback
* Fix JS test
* Add instructions to deposit / withdraw one token
* Run cargo fmt
* Fix clippy issues
* Add JS interface
* Add tests for new instructions
* Run prettier
* Rename deposit and withdraw
* Rename withdraw -> withdraw all in program
* Rename single token instructions
* Update constant price curve conversion to/from pool tokens
* Add test for pool token conversion
* Fix tests
* run cargo fmt
* Assess fee on half source amount for conversion
* Add offset curve
* Fix for math
* Add PreciseNumber
* Use Balancer formulation for trading token -> pool token conversion
* Add round-trip conversion testing
* Add offset curve to JS
* Run cargo fmt
* Update JS test numbers for new calcs
* Integrate review feedback
* Allow for withdrawals when one side is 0
* Run cargo fmt
* Disallow deposits for offset curve
* Run cargo fmt
* Allow for withdrawals through 0
* Add price parameter for flat / constant price curve
This encompasses a lot of changes to properly support the constant price
curve, and fixes some old pain points to make the whole thing easier to
use, including:
* refactor fees out of the curve
* move all fee calculations into new Fees type
* modify constraints to only care about curve types and fee
* add price parameter into constant price curve (flat curve)
* Run cargo fmt and clippy
* Revert interface changes and math update
* Run cargo fmt
* Address review feedback
* Fix fraction validation for zeros
* Add extra check
* Add extra check on init
* Run cargo fmt
* Clippy
* Add token program id check on unpack
* Run cargo fmt
* Add checks for program id ownership on swap account
* Add truncation check during swapping
* Run cargo fmt
* Update truncation to ceiling the value
* Run cargo fmt
* Fix JS test
* Refund back not needed source tokens
* Run cargo fmt
* Add swap_without_fees method to trait
* Fix merge problem
Co-authored-by: Justin Starry <justin@solana.com>
* token-swap: Add fuzzer for swap / withdraw / deposit
* Run cargo fmt
* Make end-to-end testing work
* Fix test failures
* Cleanup for review
* Remove Clone trait for Initialize
* Fix building fuzz targets
* Fix fuzz withdraw logic to avoid ZeroTradingTokens error
* Cargo fmt
* Clippy / fmt again
* Integrate fuzzer in main workspace to share clippy / fmt
* Fix clippy
* Switch to stable honggfuzz that could work with BPF
* Update to most recent honggfuzz
* Add build / run requirements
* Add fuzz CI and cleanup for honggfuzz
* Revert to using fuzz feature to integrate with workspace
* Make fuzz script executable
* Refactor and cleanup
* Add nightly run
* Fix workflow typo
* Add runtime for pull request fuzz workflow
* Fix unrelated new clippy errors
* Added stable curve invariant to the token swap smart contract
* Fixed formatting
* Added missing stable curve constraints
* Symbol renames to make math clearer
* Small refactoring according to PR comments, fixes for JS tests
If a swap has a trading fee and a zero owner fee, when calculating the owner
fee to pool token conversion, the simulated trading fee will be non-zero
because we floor the trading fee to 1 token. If the input to trading
fee calculation is 0, then we will try to do `0 - 1` on an unsigned int,
which causes an underflow error. The fix is to simply return a 0 fee
for a 0 input.
* Use u128 for all the math but store in u64
* tests
* missing changes
* bulds
* specific conversion failure
* fix tests
* use large numbers
* Rebase and fix merge issue from new tests
Co-authored-by: Jon Cinque <jon.cinque@gmail.com>
* Add mints to swap info
* Add mints to JS
* Add fee account in SwapInfo / init
* Add test for 0 fee, init test fully
* Add withdraw command interface
* Add fee accounts to swap instruction
* Add calculations for swap and withdraw fees
* Run cargo fmt
* Add new fees to JS and test
* Review feedback: fixup instruction doc and clone
* Update order of accounts in instructions
* Run cargo fmt
* Fix owner fee pool token calculation to include trading fee
* Add owner fees to flat curve, per request
* Fix instruction comment numbering
* Add more errors types for clearer calculation errors
Add a check for withdrawing from fee account
* Cargo fmt
* Pass TokenSwap state into the constructor to simplify function calls
* WIP: Update program to return token_program_id
* Add tokenProgramId to js TokenSwapInfo object
* Address pr comments
* Remove TokenSwapInfo and use loadTokenSwap as the primary way to fetch TokenSwap metadata
* Fix module.d.ts
* Address pr comments
* Clarification on comments
Co-authored-by: Yutaro Mori <yutaro@umaproject.org>
* token-swap: Add token supply in invariant calculation
* Refactor state classes into curve components for future use
* Align pool initialization with Uniswap using geometric mean of token
amounts
* Fix deposit and withdraw instruction to work as a proportion of pool
tokens
* Add math utilities to calculate the geometric mean with u64
* Improve variable names
* Use a fixed starting pool size
* Run cargo fmt
* Update js tests with new pool numbers
* Run linting
* Remove math
* Fix BN type issues found by flow
* Pack instructions and state more efficiently
* SwapInfo and SwapInstruction are more memory-efficient, following the
example from SPL Token
* Remove unnecessary helper structs to make instructions clearer
* Error -> SwapError
* Update JS tests to reflect the change
* Integrate review feedback
* Fix local token-swap tests
* Change generation of program address to use a nonce
* Accept nonce properly in initialization
* Include nonce in TokenSwap structure
* Fixup serialization with new parameter (padding used for now)
* Update dependencies
Update toml / lock files
Fix token swap initialization end-to-end
Cleanup unit test to use `find_program_address`
Add / refactor tests
Most importantly, added a special test to make sure that
token_program_id is provided at the end of CPI instructions, since
unit testing did not pick up that problem, and it was tough to debug
during end-to-end testing
* Revert some testing changes for PR
* Update token-swap/program/src/processor.rs
Co-authored-by: Tyera Eulberg <teulberg@gmail.com>
* Integrate review comments
* Fmt and clippy
* Refactor for clippy
* Fmt again
* Fix npm lint error
* Clarify signers line as requested
Co-authored-by: Tyera Eulberg <teulberg@gmail.com>