Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix contract math #298

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/contracts/invariant/invariant.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0x6535e78f66cf026b69bf940c0439337244baa2e34a6e9059a4730e00f9783811",
"hash": "0x8dc9df28bd121964ee2a8bb81ea6945b0d5ac58dfa67869ea48dcabdba92dc07",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down
Binary file modified sdk/contracts/invariant/invariant.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion sdk/contracts/psp22/psp22.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"source": {
"hash": "0xb4082b7d4aedb8479925aa96a383ea722dc1a7e9b29ad5a4d5f95ad707db4e09",
"hash": "0xc286df19a483e4b4fcb2b2342b14e33cdb6dade49ebe0dea80f4fab27ae200ba",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down
Binary file modified sdk/contracts/psp22/psp22.wasm
Binary file not shown.
15 changes: 9 additions & 6 deletions sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@invariant-labs/a0-sdk",
"version": "0.2.24",
"version": "0.2.25",
"collaborators": [
"Invariant Labs"
],
Expand Down Expand Up @@ -60,7 +60,7 @@
"test:update-position-seconds-per-liquidity": "mocha ./tests/update-position-seconds-per-liquidity.test.ts -g update-position-seconds-per-liquidity"
},
"dependencies": {
"@invariant-labs/a0-sdk-wasm": "0.1.26",
"@invariant-labs/a0-sdk-wasm": "0.1.27",
"@polkadot/api": "^10.12.4",
"@polkadot/api-contract": "^10.12.4",
"@scio-labs/use-inkathon": "^0.6.3",
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/abis/invariant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const abi = `
{
"source": {
"hash": "0x6535e78f66cf026b69bf940c0439337244baa2e34a6e9059a4730e00f9783811",
"hash": "0x8dc9df28bd121964ee2a8bb81ea6945b0d5ac58dfa67869ea48dcabdba92dc07",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/abis/psp22.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const abi = `
{
"source": {
"hash": "0xb4082b7d4aedb8479925aa96a383ea722dc1a7e9b29ad5a4d5f95ad707db4e09",
"hash": "0xc286df19a483e4b4fcb2b2342b14e33cdb6dade49ebe0dea80f4fab27ae200ba",
"language": "ink! 5.0.0",
"compiler": "rustc 1.77.0",
"build_info": {
Expand Down
4 changes: 3 additions & 1 deletion sdk/src/wasm/clamm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ pub fn get_delta_x(
sqrt_price_a
.cast::<U256>()
.checked_mul(sqrt_price_b.here())
.ok_or_else(|| err!(TrackableError::MUL))?,
.ok_or_else(|| err!(TrackableError::MUL))?
.checked_add(SqrtPrice::almost_one().here())
.ok_or_else(|| err!(TrackableError::ADD))?,
),
})
}
Expand Down
8 changes: 4 additions & 4 deletions sdk/src/wasm/types/sqrt_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ impl SqrtPrice {
let nominator = U448::uint_cast(nominator);
let denominator = U448::uint_cast(denominator);

let intermediate_u320 = nominator
let intermediate_u448 = nominator
.checked_mul(Self::one().cast::<U448>())
.ok_or_else(|| err!(TrackableError::MUL))?
.checked_mul(Self::one().cast::<U448>())
.ok_or_else(|| err!(TrackableError::MUL))?
.checked_div(denominator)
.ok_or_else(|| err!(TrackableError::DIV))?;

let result = U256::uint_checked_cast(intermediate_u320)
let result = U256::uint_checked_cast(intermediate_u448)
.map_err(|e| err!(&e))?
.checked_div(U256::from(Self::one().get()))
.ok_or_else(|| err!(TrackableError::DIV))?
Expand All @@ -54,7 +54,7 @@ impl SqrtPrice {
let nominator = U448::uint_cast(nominator);
let denominator = U448::uint_cast(denominator);

let intermediate_u320 = nominator
let intermediate_u448 = nominator
.checked_mul(Self::one().cast::<U448>())
.ok_or_else(|| err!(TrackableError::MUL))?
.checked_mul(Self::one().cast::<U448>())
Expand All @@ -64,7 +64,7 @@ impl SqrtPrice {
.checked_div(denominator)
.ok_or_else(|| err!(TrackableError::DIV))?;

let result = U256::uint_checked_cast(intermediate_u320)
let result = U256::uint_checked_cast(intermediate_u448)
.map_err(|e| err!(&e))?
.checked_add(U256::from(Self::almost_one().get()))
.ok_or_else(|| err!(TrackableError::ADD))?
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/storage/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ mod tests {
.update_liquidity(liquidity_delta, liquidity_sign, upper_tick, lower_tick)
.unwrap();

assert_eq!(x, TokenAmount(4999999999999999999999995));
assert_eq!(x, TokenAmount(2500187490625273443357761));
assert_eq!(y, TokenAmount(5));
assert_eq!(pool.liquidity, Liquidity::from_integer(5))
}
Expand Down
23 changes: 14 additions & 9 deletions src/math/clamm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ pub fn get_delta_x(
sqrt_price_a
.cast::<U256>()
.checked_mul(sqrt_price_b.here())
.ok_or_else(|| err!(TrackableError::MUL))?,
.ok_or_else(|| err!(TrackableError::MUL))?
.checked_add(SqrtPrice::almost_one().here())
.ok_or_else(|| err!(TrackableError::ADD))?,
),
})
}
Expand Down Expand Up @@ -1030,7 +1032,7 @@ mod tests {
SwapResult {
next_sqrt_price: SqrtPrice::new(2000000000000000000000000),
amount_in: TokenAmount(340282366920938463463374607431769),
amount_out: TokenAmount(170141183460469231731687303715884),
amount_out: TokenAmount(170141183460469231731687218645292),
fee_amount: TokenAmount(0)
}
)
Expand Down Expand Up @@ -1064,7 +1066,7 @@ mod tests {
let expected_result = SwapResult {
next_sqrt_price: SqrtPrice::new(1000018999999999999999999),
amount_in: TokenAmount(6465364971497830805463835175),
amount_out: TokenAmount(6465242131897324756293472063),
amount_out: TokenAmount(6465242131897324756293465598),
fee_amount: TokenAmount(340282366914473098491876776626304376280),
};

Expand Down Expand Up @@ -1127,7 +1129,7 @@ mod tests {
SwapResult {
next_sqrt_price: SqrtPrice::new(MAX_SQRT_PRICE),
amount_in: TokenAmount(340282366891153598040639304410374383019),
amount_out: TokenAmount(340282366920938463463374607431551802886),
amount_out: TokenAmount(340282366920938463463374267149184852163),
fee_amount: TokenAmount(0)
}
)
Expand Down Expand Up @@ -1203,7 +1205,7 @@ mod tests {
SwapResult {
next_sqrt_price: SqrtPrice::new(MAX_SQRT_PRICE),
amount_in: TokenAmount(79230632432451957492634392915),
amount_out: TokenAmount(79230632439387003072335686522),
amount_out: TokenAmount(79230632439387003072335607291),
fee_amount: TokenAmount(0)
}
);
Expand Down Expand Up @@ -1548,7 +1550,7 @@ mod tests {
let result = get_delta_x(
SqrtPrice::from_integer(1u8),
SqrtPrice::from_integer(2u8),
Liquidity::from_integer(2u8),
Liquidity::from_integer(2u8) + Liquidity::new(1),
false,
)
.unwrap();
Expand Down Expand Up @@ -1576,7 +1578,10 @@ mod tests {
let result_down = get_delta_x(sqrt_price_a, sqrt_price_b, liquidity, false).unwrap();
let result_up = get_delta_x(sqrt_price_a, sqrt_price_b, liquidity, true).unwrap();

assert_eq!(result_down, TokenAmount::from_decimal(liquidity));
assert_eq!(
result_down,
TokenAmount::from_decimal(liquidity) - TokenAmount::new(1)
);
assert_eq!(result_up, TokenAmount::from_decimal(liquidity));
}
// no more overflow after extending the type to U320 in intermediate operations
Expand Down Expand Up @@ -1627,7 +1632,7 @@ mod tests {
let result =
get_delta_x(max_sqrt_price, min_sqrt_price, max_liquidity, false).unwrap();

assert_eq!(result, TokenAmount(340282366920938463463374468856710103650))
assert_eq!(result, TokenAmount(340282366920938463463374128574343152927))
}
}
{
Expand Down Expand Up @@ -1661,7 +1666,7 @@ mod tests {
false,
)
.unwrap();
assert_eq!(TokenAmount(340282366920938463463374607431768146213), result);
assert_eq!(TokenAmount(4294940505789835294958676759947697), result);
}
}
// minimize denominator on minimize liquidity which fits into TokenAmount
Expand Down
Loading