Commit Graph

94 Commits

Author SHA1 Message Date
Jon Cinque d336b8b714
stake-pool: Use checked_ceil_div for withdraw calc (#1482)
* stake-pool: Use checked_ceil_div for withdraw calc

When a stake account is totally removed from a stake pool by the
manager, there's a chance that the operation would not take enough of
the manager's pool tokens by 1 due to truncation.

Do a ceiling division instead, and refactor ceiling division into the
math library.

* Use new function name on CLI

* Cargo fmt
2021-03-23 00:33:51 +01:00
Michael Vines 662f38f939 Bump Solana version to 1.6.1 2021-03-18 04:07:16 +00:00
Michael Vines 626376653d Clippy 2021-03-16 18:33:37 -07:00
Michael Vines 686444088b Bump Solana version to 1.5.15 2021-03-16 18:33:37 -07:00
Jon Cinque 9c12b50703
Update solana-program to 1.5.11 (#1362)
* Update solana-program to 1.5.11

* Update all programs
2021-03-02 19:19:50 +01:00
dependabot[bot] 5b4c62ce49
build(deps): bump solana-program from 1.5.6 to 1.5.8 (#1261)
Bumps [solana-program](https://github.com/solana-labs/solana) from 1.5.6 to 1.5.8.
- [Release notes](https://github.com/solana-labs/solana/releases)
- [Changelog](https://github.com/solana-labs/solana/blob/master/RELEASE.md)
- [Commits](https://github.com/solana-labs/solana/compare/v1.5.6...v1.5.8)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-02-17 23:56:18 +00:00
Trent Nelson 2316b6092e chore: token - bump version to 3.1.0 2021-02-11 16:30:03 -07:00
Trent Nelson 188c03e9a1 chore: bump solana crates to 1.5.6 2021-02-11 16:30:03 -07:00
Jon Cinque beb4aa7e7f
spl-math: Add separate package to test instruction usage (#1205)
* Refactor to use spl-math

* Run cargo fmt

* Fixup fuzz

* Add spl math program

* Add u64-based approximation

* Cleanup

* Downgrade solana sdk for CI

* Move U256 impl to stable curve

* Remove generic newtonian, use traits

* Cargo fmt

* Move math/program -> libraries/math

* Revert Cargo.lock changes

* Add u128 instruction count tests

* cargo fmt
2021-02-10 17:40:54 +01:00
Jon Cinque b26491552f
token-swap: Add version to state (#985)
* Add version to token swap state

* Fix fuzz

* Cargo fmt

* Revert solana localnet version

* Update enum dispatch
2021-02-04 15:22:33 +01:00
Jon Cinque d16da5b913
Update rust stable to 1.49 (and add token swap program id) (#1158)
* Update token swap program id

* Update rust stable to 1.49
2021-02-04 14:20:15 +01:00
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
Jon Cinque fd24042815
Pin honggfuzz to 0.5.52 (#1072) 2021-01-14 23:07:07 +01:00
Jon Cinque 7f17a140b1
swap: Avoid fuzz 1 token withdrawal errors by using fee account (#1058)
* Fix fuzz 1 token withdrawal errors with fee account

* Reintroduce special logic

* Cargo fmt
2021-01-12 13:57:58 +01:00
Jon Cinque 608b75a115
swap: Move transfers into user account to the end of instructions (#1047)
* Move transfers into user account to the end of instructions

* Run cargo fmt
2021-01-11 19:20:34 +01:00
Jon Cinque a86abe49cd
token-swap: Add user transfer authority (#1033)
* 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
2021-01-06 20:55:31 +01:00
dependabot[bot] 7b81c0d3c0
build(deps): bump solana-program from 1.5.0 to 1.5.1 (#1025)
Bumps [solana-program](https://github.com/solana-labs/solana) from 1.5.0 to 1.5.1.
- [Release notes](https://github.com/solana-labs/solana/releases)
- [Changelog](https://github.com/solana-labs/solana/blob/master/RELEASE.md)
- [Commits](https://github.com/solana-labs/solana/compare/v1.5.0...v1.5.1)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-01-04 13:51:29 +00:00
Jon Cinque 9e7e63f405
token-swap: Add new instructions to fuzzing (#961) 2020-12-18 16:35:17 +01:00
Jon Cinque 942bf77c90
token-swap: Add proptest for deposit draining (#959)
* Add deposit drain proptest

* Add special logic for constant price curve normalized value when close to u128

* Cargo fmt

* Fix bpf build
2020-12-18 13:32:34 +01:00
Michael Vines 8ae34d2137 Update to Solana 1.5.0 2020-12-17 21:24:30 -08:00
Jon Cinque 2158214c84
token-swap: Add fuzz check for pool token value (#958)
* 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
2020-12-17 21:13:48 +01:00
Jon Cinque cc7e8e0d98
token-swap: Add proptests for decreasing curve value (#952)
* Add proptest-regressions to .gitignore, created from failed proptests

* Add proptests to ensure swapping maintains value

* Address review feedback

* Add doc to public test function
2020-12-17 20:09:58 +01:00
Jon Cinque d46f010195
token-swap: Add proptests for single token conversion (#950)
* Add proptests for single token conversion

* Update token-swap/program/src/curve/calculator.rs

Co-authored-by: Tyera Eulberg <teulberg@gmail.com>

* Update token-swap/program/src/curve/calculator.rs

Co-authored-by: Tyera Eulberg <teulberg@gmail.com>

* Revert change from fuzzing

* Reduce the constant price b->a test range

* Use constant for basis points guarantee

* Address review feedback

Co-authored-by: Tyera Eulberg <teulberg@gmail.com>
2020-12-16 18:30:28 +01:00
Jon Cinque ec2cd04c82
Add proptest for stable swap intead of all numbers (#944) 2020-12-16 10:19:08 +01:00
Jon Cinque 7190672a0f
token-swap: Add instructions to deposit / withdraw one token (#937)
* 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
2020-12-16 10:02:27 +01:00
Jon Cinque f8ba091a36
Fix bpf build, need to fully reference u128::MAX (#946) 2020-12-14 18:06:38 +01:00
Jon Cinque 8233d35fda
token-swap: Refactor math into sqrt function and add proptest (#943)
* Refactor into sqrt function and add proptest

* Run cargo fmt + clippy

* Address review feedback
2020-12-14 17:48:41 +01:00
Jon Cinque 55deb6c1af
token-swap: Constant price curve (#936)
* 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
2020-12-11 10:27:28 +01:00
Jon Cinque 087ae2e242
token-swap: Add offset curve and math (#935)
* 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
2020-12-10 18:31:21 +01:00
Jon Cinque 3dcb1c5665
token-swap: Add pool token conversion interface (#934)
* Add pool token conversion interface for all curves

This reverts commit 8400bc7bfe4fcc18580d8f81cbb19a4ef5a437ff.

* Improve tests

* Run cargo fmt
2020-12-09 11:30:22 +01:00
Jon Cinque c0f5ff182c
token-swap: Refactor for new curves (#930)
* 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
2020-12-08 18:22:32 +01:00
Jon Cinque 04a3c833f0
token-swap: Cleanup check (#903)
* 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>
2020-12-03 12:17:32 +01:00
Michael Vines 158fa1dea6 Upgrade to Solana v1.4.14 2020-12-03 05:39:55 +00:00
dependabot[bot] 6aa1d8cce1
Bump solana-program from 1.4.9 to 1.4.13 (#916)
Bumps [solana-program](https://github.com/solana-labs/solana) from 1.4.9 to 1.4.13.
- [Release notes](https://github.com/solana-labs/solana/releases)
- [Changelog](https://github.com/solana-labs/solana/blob/master/RELEASE.md)
- [Commits](https://github.com/solana-labs/solana/compare/v1.4.9...v1.4.13)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-12-02 12:54:42 +00:00
Jon Cinque b40e0dd3fd
token-swap: Add fuzzer for swap / withdraw / deposit (#875)
* 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
2020-11-30 13:01:31 +01:00
Yuriy Savchenko a0d0ae63ca
token-swap stable curve: 256-bit math added with tests (#886)
* Stable curve math rewritten to 256-bit, reference simulation added with test integration

* Formatting error fixed

* Clippy warnings fixed

* Fixed dependencies to remove crates not supportinf bpf

* Fixed indentations

* Fixed lint errors

* Fixed format warnings

* Fixing clippy lint errors

* U256 math and simulation library refactoring, checked math used everywhere, other small fixes

* Fixed conversion to optional u128
2020-11-25 21:19:25 +01:00
Michael Vines c6a4ac9a82 Bump solana version to v1.4.9 2020-11-17 20:02:17 -08:00
Yuriy Savchenko d62ddd2b94
Added stable curve invariant to the token swap smart contract (#838)
* 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
2020-11-17 14:13:18 +01:00
Michael Vines f4af9054a2 Update to Solana 1.4.8 2020-11-14 02:35:20 +00:00
Michael Vines 5030a87021 Upgrade to Solana 1.4.7 2020-11-12 11:05:20 -08:00
dependabot[bot] b71456a8f1
Bump solana-program from 1.4.5 to 1.4.6 (#826)
Bumps [solana-program](https://github.com/solana-labs/solana) from 1.4.5 to 1.4.6.
- [Release notes](https://github.com/solana-labs/solana/releases)
- [Changelog](https://github.com/solana-labs/solana/blob/master/RELEASE.md)
- [Commits](https://github.com/solana-labs/solana/compare/v1.4.5...v1.4.6)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-11-11 12:39:52 +00:00
Jon Cinque 75acddc100
Fix zero fee calculation (#813)
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.
2020-11-09 19:01:21 +01:00
Michael Vines 238eabc1d0 Bump solana version to v1.4.5 2020-11-06 21:29:04 -08:00
Michael Vines 89de77aad8 Bump spl-token to v3.0.0 2020-11-04 18:58:37 +00:00
Michael Vines 341149d0e9 Back to no-entrypoint feature name 2020-11-03 10:34:43 -08:00
Michael Huang 5bf4b76df4
Refactor curve.rs (#754)
* Refactor shared out of curve.rs

* Refactor out flat curve

* Refactor out constant product curve

* Cargo fmt

* More moving

* Fixup

* use::super -> user::create::curve

* cp -> constant_product

* Fix circular dependency

* Docs

* Trigger Build
2020-11-02 14:29:00 +01:00
Michael Vines 2b5e9d1286 Drop lifetimes 2020-11-01 20:48:15 -08:00
Michael Vines 9f4e6d8beb Update to solana v1.4.4 2020-11-01 20:48:15 -08:00
Tyera Eulberg c312a0576a
Clippy suggestions (#765) 2020-10-31 19:10:33 -06:00
Jon Cinque 9cd1cb0900
Token-swap: Add "production mode" hard-coded fee constraints (#731)
* Add statically configured program constraints

* Allow missing host fee account

* Run cargo fmt + clippy

* Update JS for new host fee account param

* Fix merge issues

* Add host fee to curve, constrain on curves

* Cleanup static variable

* Add production test to CI

Integrate review feedback

* Run cargo fmt, clippy, prettier

* Re-run cargo fmt

* Fix test for eslint / prettier clash
2020-10-29 12:52:17 +01:00