Skip to content

Commit

Permalink
Merge branch 'main' of git.cplus.link:cetus/cetus-amm into main
Browse files Browse the repository at this point in the history
# Conflicts:
#	sources/amm_math.move
  • Loading branch information
Mikaelson committed Aug 29, 2022
2 parents 49c1e69 + 1f5211c commit 675f160
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions sources/amm_math.move
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
module cetus_amm::amm_math {
use cetus_amm::u256::{Self, U256};
use std::error;

const ERROR_ROUTER_PARAMETER_INVALID: u64 = 1001;
//
// Errors
//
const EMATH_DIVIDE_BY_ZERO: u64 = 2001;
const ERROR_ROUTER_PARAMETER_INVALID: u64 = 2001;

public fun safe_compare_mul_u128(a1: u128, b1: u128, a2: u128, b2: u128): u8 {
let left = u256::mul(u256::from_u128(a1), u256::from_u128(b1));
let right = u256::mul(u256::from_u128(a2), u256::from_u128(b2));
u256::compare(left, right)
}

public fun safe_mul_div_u128(x: u128, y: u128, z: u128): u128 {
u256::as_u128(mul_div_u128(x, y, z))
}

public fun mul_div_u128(x: u128, y: u128, z: u128): U256 {
if (z == 0) {
abort error::invalid_argument(EMATH_DIVIDE_BY_ZERO)
};

let x_u256 = u256::from_u128(x);
let y_u256 = u256::from_u128(y);
let z_u256 = u256::from_u128(z);
u256::div(u256::mul(x_u256, y_u256), z_u256)
}

public fun quote(amount_a: u128, reserve_a: u128, reserve_b: u128): u128 {
assert!(amount_a > 0, error::invalid_argument(ERROR_ROUTER_PARAMETER_INVALID));
assert!(reserve_a > 0 && reserve_b> 0, error::invalid_argument(ERROR_ROUTER_PARAMETER_INVALID));
let amount_y = amount_a * reserve_b / reserve_a;
let amount_y = safe_mul_div_u128(amount_a,reserve_b,reserve_a);
amount_y
}

Expand All @@ -24,6 +50,6 @@ module cetus_amm::amm_math {
}

public fun min(x: u128, y: u128): u128 {
if (x < y) x else y
if (x < y) x else y
}
}

0 comments on commit 675f160

Please sign in to comment.