solana-program-library/token-swap
Jon Cinque 40b7690a58
swap: Add rounding correction for deposit / withdraw (#1061)
* 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
2021-01-15 18:17:27 +01:00
..
js swap: Add rounding correction for deposit / withdraw (#1061) 2021-01-15 18:17:27 +01:00
program swap: Add rounding correction for deposit / withdraw (#1061) 2021-01-15 18:17:27 +01:00
.gitignore token-swap stable curve: 256-bit math added with tests (#886) 2020-11-25 21:19:25 +01:00
README.md Fix broken stake-pool docs link and make readme's consistent (#805) 2020-11-06 09:15:27 +08:00

README.md

Token-swap program

A Uniswap-like exchange for the Token program on the Solana blockchain.

Full documentation is available at https://spl.solana.com/token-swap

JavaScript binding are available in the ./js directory.