Skip to content

Commit

Permalink
Feat: merge public 20230519
Browse files Browse the repository at this point in the history
* Update Cargo.toml

Co-authored-by: Timofey <[email protected]>

* chore

* revert some changes

* repair github release action (astroport-fi#353)

* Fix: use saturating_sub in compute_swap (astroport-fi#351)

* fix: Use saturating_sub in compute_swap

* test: Add test case for compute_swap fix

---------

Co-authored-by: Timofey <[email protected]>
Co-authored-by: Sturdy <[email protected]>
  • Loading branch information
3 people authored May 22, 2023
1 parent ff7473a commit c22a2d1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code_coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
with:
version: '0.22.0'

- name: Upload coverage reports to Codecov
- name: Upload to codecov.io
if: github.ref == 'refs/heads/main'
uses: codecov/codecov-action@v3
with:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release_artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
jobs:
release-artifacts:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
- name: Build Artifacts
Expand All @@ -17,6 +19,4 @@ jobs:
uses: softprops/action-gh-release@v1
with:
files: cosmwasm-artifacts.tar.gz
token: ${{ secrets.GH_TOKEN }}
env:
GITHUB_REPOSITORY: astroport-fi/astroport-core
token: ${{ secrets.GITHUB_TOKEN }}
22 changes: 21 additions & 1 deletion contracts/pair/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ pub fn compute_swap(

// Calculate spread & commission
let spread_amount: Uint256 =
(offer_amount * Decimal256::from_ratio(ask_pool, offer_pool)) - return_amount;
(offer_amount * Decimal256::from_ratio(ask_pool, offer_pool)).saturating_sub(return_amount);
let commission_amount: Uint256 = return_amount * commission_rate;

// The commision (minus the part that goes to the Maker contract) will be absorbed by the pool
Expand Down Expand Up @@ -1286,3 +1286,23 @@ pub fn pool_info(querier: QuerierWrapper, config: &Config) -> StdResult<(Vec<Ass

Ok((pools, total_share))
}

#[cfg(test)]
mod tests {
use crate::contract::compute_swap;
use cosmwasm_std::{Decimal, Uint128};

#[test]
fn compute_swap_does_not_panic_on_spread_calc() {
let offer_pool = Uint128::from(u128::MAX / 2);
let ask_pool = Uint128::from(u128::MAX / 1000000000);
let offer_amount = Uint128::from(1000000000u128);
let commission_rate = Decimal::permille(3);

let (return_amount, spread_amount, commission_amount) =
compute_swap(offer_pool, ask_pool, offer_amount, commission_rate).unwrap();
assert_eq!(return_amount, Uint128::from(2u128));
assert_eq!(spread_amount, Uint128::zero());
assert_eq!(commission_amount, Uint128::zero());
}
}
2 changes: 1 addition & 1 deletion contracts/pair_concentrated/src/math/math_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use astroport::cosmwasm_ext::AbsDiff;
use crate::consts::{HALFPOW_TOL, MAX_ITER, N, N_POW2, TOL};
use crate::math::signed_decimal::SignedDecimal256;

/// Internal constant to increase calculation accuracy. (100.0)
/// Internal constant to increase calculation accuracy. (1000.0)
const PADDING: Decimal256 = Decimal256::raw(1000000000000000000000);

pub fn geometric_mean(x: &[Decimal256]) -> Decimal256 {
Expand Down

0 comments on commit c22a2d1

Please sign in to comment.