From 4da3f46e930a8d9b6c20d767c871f5637bb1ebff Mon Sep 17 00:00:00 2001 From: julio4 Date: Tue, 10 Oct 2023 18:02:13 +0900 Subject: [PATCH] feat: update to 2.3.0 + support custom fee --- .../ch02-applications/constant_product_amm/Scarb.toml | 2 +- .../src/constant_product_amm.cairo | 10 ++++++---- .../constant_product_amm/src/lib.cairo | 2 ++ .../constant_product_amm/src/tests.cairo | 6 +++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/listings/ch02-applications/constant_product_amm/Scarb.toml b/listings/ch02-applications/constant_product_amm/Scarb.toml index 840d36ec..94c4c902 100644 --- a/listings/ch02-applications/constant_product_amm/Scarb.toml +++ b/listings/ch02-applications/constant_product_amm/Scarb.toml @@ -3,7 +3,7 @@ name = "constant_product_amm" version = "0.1.0" [dependencies] -starknet = ">=2.2.0" +starknet = ">=2.3.0-rc0" openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.7.0" } [[target.starknet-contract]] diff --git a/listings/ch02-applications/constant_product_amm/src/constant_product_amm.cairo b/listings/ch02-applications/constant_product_amm/src/constant_product_amm.cairo index 0916b864..a90efbc8 100644 --- a/listings/ch02-applications/constant_product_amm/src/constant_product_amm.cairo +++ b/listings/ch02-applications/constant_product_amm/src/constant_product_amm.cairo @@ -21,11 +21,14 @@ mod ConstantProductAmm { reserve0: u256, reserve1: u256, total_supply: u256, - balance_of: LegacyMap:: + balance_of: LegacyMap::, + fee: u256, } #[constructor] - fn constructor(ref self: ContractState, token0: ContractAddress, token1: ContractAddress) { + fn constructor(ref self: ContractState, token0: ContractAddress, token1: ContractAddress, fee: u256) { + assert(fee <= 1000, 'fee > 1000') + self.fee.write(fee); self.token0.write(IERC20Dispatcher { contract_address: token0 }); self.token1.write(IERC20Dispatcher { contract_address: token1 }); } @@ -98,9 +101,8 @@ mod ConstantProductAmm { // y - xy / (x + dx) = dy // (yx + ydx - xy) / (x + dx) = dy // ydx / (x + dx) = dy - // 0.3% fee - let amount_in_with_fee = (amount_in * 997) / 1000; + let amount_in_with_fee = (amount_in * (1000 - fee)) / 1000; let amount_out = (reserve_out * amount_in_with_fee) / (reserve_in + amount_in_with_fee); token_out.transfer(caller, amount_out); diff --git a/listings/ch02-applications/constant_product_amm/src/lib.cairo b/listings/ch02-applications/constant_product_amm/src/lib.cairo index 78295f2f..d538d2c7 100644 --- a/listings/ch02-applications/constant_product_amm/src/lib.cairo +++ b/listings/ch02-applications/constant_product_amm/src/lib.cairo @@ -1,2 +1,4 @@ mod constant_product_amm; + +#[cfg(test)] mod tests; diff --git a/listings/ch02-applications/constant_product_amm/src/tests.cairo b/listings/ch02-applications/constant_product_amm/src/tests.cairo index 7286e7f4..e0ae300e 100644 --- a/listings/ch02-applications/constant_product_amm/src/tests.cairo +++ b/listings/ch02-applications/constant_product_amm/src/tests.cairo @@ -1,4 +1,3 @@ -#[cfg(test)] mod tests { use core::traits::TryInto; use openzeppelin::token::erc20::{ @@ -17,8 +16,6 @@ mod tests { use starknet::testing::set_contract_address; use starknet::class_hash::Felt252TryIntoClassHash; - use debug::PrintTrait; - const BANK: felt252 = 0x123; const INITIAL_SUPPLY: u256 = 10_000; @@ -51,6 +48,9 @@ mod tests { calldata.append_serde(token0_address); calldata.append_serde(token1_address); + // 0.3% fee + calldata.append_serde(3) + let contract_address = utils::deploy(ConstantProductAmm::TEST_CLASS_HASH, calldata); Deployment { contract: IConstantProductAmmDispatcher { contract_address }, token0, token1 } }