Skip to content

Commit

Permalink
feat: update to 2.3.0 + support custom fee
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Oct 10, 2023
1 parent 4ca2091 commit 4da3f46
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion listings/ch02-applications/constant_product_amm/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ mod ConstantProductAmm {
reserve0: u256,
reserve1: u256,
total_supply: u256,
balance_of: LegacyMap::<ContractAddress, u256>
balance_of: LegacyMap::<ContractAddress, u256>,
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 });
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions listings/ch02-applications/constant_product_amm/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
mod constant_product_amm;

#[cfg(test)]
mod tests;
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(test)]
mod tests {
use core::traits::TryInto;
use openzeppelin::token::erc20::{
Expand All @@ -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;

Expand Down Expand Up @@ -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 }
}
Expand Down

0 comments on commit 4da3f46

Please sign in to comment.