From e6313dfb68aba6289a8d31c808e5d9c515595f3b Mon Sep 17 00:00:00 2001 From: mootz12 Date: Fri, 26 Apr 2024 11:24:00 -0400 Subject: [PATCH 1/3] fix: ensure swap amounts in and out are not 0 --- contracts/src/c_pool/call_logic/pool.rs | 6 ++--- contracts/src/tests/c_pool_swap.rs | 30 +++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/contracts/src/c_pool/call_logic/pool.rs b/contracts/src/c_pool/call_logic/pool.rs index 2bd742e..2915fa4 100644 --- a/contracts/src/c_pool/call_logic/pool.rs +++ b/contracts/src/c_pool/call_logic/pool.rs @@ -119,7 +119,7 @@ pub fn execute_swap_exact_amount_in( user: Address, ) -> (i128, i128) { assert_with_error!(&e, !read_freeze(&e), Error::ErrFreezeOnlyWithdrawals); - assert_with_error!(&e, token_amount_in >= 0, Error::ErrNegative); + assert_with_error!(&e, token_amount_in > 0, Error::ErrNegativeOrZero); assert_with_error!(&e, min_amount_out >= 0, Error::ErrNegative); assert_with_error!(&e, max_price >= 0, Error::ErrNegative); @@ -217,8 +217,8 @@ pub fn execute_swap_exact_amount_out( user: Address, ) -> (i128, i128) { assert_with_error!(&e, !read_freeze(&e), Error::ErrFreezeOnlyWithdrawals); - assert_with_error!(&e, token_amount_out >= 0, Error::ErrNegative); - assert_with_error!(&e, max_amount_in >= 0, Error::ErrNegative); + assert_with_error!(&e, token_amount_out > 0, Error::ErrNegativeOrZero); + assert_with_error!(&e, max_amount_in > 0, Error::ErrNegativeOrZero); assert_with_error!(&e, max_price >= 0, Error::ErrNegative); let swap_fee = read_swap_fee(&e); diff --git a/contracts/src/tests/c_pool_swap.rs b/contracts/src/tests/c_pool_swap.rs index cf15151..fdafccb 100644 --- a/contracts/src/tests/c_pool_swap.rs +++ b/contracts/src/tests/c_pool_swap.rs @@ -69,7 +69,16 @@ fn test_swap_out_given_in() { assert_eq!( result.err(), Some(Ok(Error::from_contract_error( - CometError::ErrNegative as u32 + CometError::ErrNegativeOrZero as u32 + ))) + ); + + // verify zero input + let result = comet.try_swap_exact_amount_in(&token_1, &0, &token_2, &0, &i128::MAX, &user); + assert_eq!( + result.err(), + Some(Ok(Error::from_contract_error( + CometError::ErrNegativeOrZero as u32 ))) ); @@ -228,7 +237,24 @@ fn test_swap_in_given_out() { assert_eq!( result.err(), Some(Ok(Error::from_contract_error( - CometError::ErrNegative as u32 + CometError::ErrNegativeOrZero as u32 + ))) + ); + + // verify zero input + let result = + comet.try_swap_exact_amount_out(&token_2, &i128::MAX, &token_1, &0, &i128::MAX, &user); + assert_eq!( + result.err(), + Some(Ok(Error::from_contract_error( + CometError::ErrNegativeOrZero as u32 + ))) + ); + let result = comet.try_swap_exact_amount_out(&token_2, &0, &token_1, &1, &i128::MAX, &user); + assert_eq!( + result.err(), + Some(Ok(Error::from_contract_error( + CometError::ErrNegativeOrZero as u32 ))) ); From 86481d1031ed14d5031fc6744b7175eeadd2b6db Mon Sep 17 00:00:00 2001 From: mootz12 Date: Fri, 26 Apr 2024 11:31:28 -0400 Subject: [PATCH 2/3] fix: use floor for max_in / max_out ratios and update bad error type --- contracts/src/c_pool/call_logic/pool.rs | 14 +++++++------- contracts/src/tests/c_pool_single_sided.rs | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/contracts/src/c_pool/call_logic/pool.rs b/contracts/src/c_pool/call_logic/pool.rs index 2915fa4..494771b 100644 --- a/contracts/src/c_pool/call_logic/pool.rs +++ b/contracts/src/c_pool/call_logic/pool.rs @@ -136,7 +136,7 @@ pub fn execute_swap_exact_amount_in( token_amount_in <= in_record .balance - .fixed_mul_ceil(MAX_IN_RATIO, STROOP) + .fixed_mul_floor(MAX_IN_RATIO, STROOP) .unwrap_optimized(), Error::ErrMaxInRatio ); @@ -234,7 +234,7 @@ pub fn execute_swap_exact_amount_out( token_amount_out <= out_record .balance - .fixed_mul_ceil(MAX_OUT_RATIO, STROOP) + .fixed_mul_floor(MAX_OUT_RATIO, STROOP) .unwrap_optimized(), Error::ErrMaxOutRatio ); @@ -321,7 +321,7 @@ pub fn execute_dep_tokn_amt_in_get_lp_tokns_out( token_amount_in <= in_record .balance - .fixed_mul_ceil(MAX_IN_RATIO, STROOP) + .fixed_mul_floor(MAX_IN_RATIO, STROOP) .unwrap_optimized(), Error::ErrMaxInRatio ); @@ -392,7 +392,7 @@ pub fn execute_dep_lp_tokn_amt_out_get_tokn_in( token_amount_in <= in_record .balance - .fixed_mul_ceil(MAX_IN_RATIO, STROOP) + .fixed_mul_floor(MAX_IN_RATIO, STROOP) .unwrap_optimized(), Error::ErrMaxInRatio ); @@ -447,9 +447,9 @@ pub fn execute_wdr_tokn_amt_in_get_lp_tokns_out( token_amount_out <= out_record .balance - .fixed_mul_ceil(MAX_OUT_RATIO, STROOP) + .fixed_mul_floor(MAX_OUT_RATIO, STROOP) .unwrap_optimized(), - Error::ErrMaxInRatio + Error::ErrMaxOutRatio ); assert_with_error!( &e, @@ -495,7 +495,7 @@ pub fn execute_wdr_tokn_amt_out_get_lp_tokns_in( token_amount_out <= out_record .balance - .fixed_mul_ceil(MAX_OUT_RATIO, STROOP) + .fixed_mul_floor(MAX_OUT_RATIO, STROOP) .unwrap_optimized(), Error::ErrMaxOutRatio ); diff --git a/contracts/src/tests/c_pool_single_sided.rs b/contracts/src/tests/c_pool_single_sided.rs index 6e626a3..29cb01b 100644 --- a/contracts/src/tests/c_pool_single_sided.rs +++ b/contracts/src/tests/c_pool_single_sided.rs @@ -288,12 +288,12 @@ fn test_single_sided_wdr() { let bal_token_out_fixed = bal_token_out.to_i128(&7); let under_out = bal_token_out_fixed - 1000; - // verify MAX_IN_RATIO + // verify MAX_OUT_RATIO let result = comet.try_wdr_tokn_amt_in_get_lp_tokns_out(&token_1, &99_9999999, &0, &admin); assert_eq!( result.err(), Some(Ok(Error::from_contract_error( - CometError::ErrMaxInRatio as u32 + CometError::ErrMaxOutRatio as u32 ))) ); From 05b4b7ac2d97382d1fa66d43590963af6fbab406 Mon Sep 17 00:00:00 2001 From: mootz12 Date: Tue, 30 Apr 2024 09:19:49 -0400 Subject: [PATCH 3/3] chore: add Stellar Expert WASM release workflow --- .github/workflows/release.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..76222c8 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,25 @@ +name: Stellar Expert WASM Release +on: + push: + tags: + - 'v*' # triggered whenever a new tag (previxed with "v") is pushed to the repository +jobs: + release-contract-governor: + uses: stellar-expert/soroban-build-workflow/.github/workflows/release.yml@main + with: + release_name: ${{ github.ref_name }} + release_description: 'Comet Factory Release' + relative_path: '["factory"]' + package: 'factory' + secrets: + release_token: ${{ secrets.GITHUB_TOKEN }} + + release-contract-votes: + uses: stellar-expert/soroban-build-workflow/.github/workflows/release.yml@main + with: + release_name: ${{ github.ref_name }} + release_description: 'Comet Pool Release' + relative_path: '["contracts"]' + package: 'contracts' + secrets: + release_token: ${{ secrets.GITHUB_TOKEN }}