Skip to content

Commit

Permalink
feat: Constant Product Amm
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Oct 5, 2023
1 parent 9da1312 commit f896d74
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ mod ConstantProductAmm {
}
}
}
// ANCHOR_END: StoreArrayContract
// ANCHOR_END: ConstantProductAmmContract

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -285,7 +285,6 @@ mod tests {
#[derive(Drop, Copy)]
struct Deployment {
contract: IConstantProductAmmDispatcher,
contract_address: ContractAddress,
token0: IERC20Dispatcher,
token1: IERC20Dispatcher
}
Expand Down Expand Up @@ -315,7 +314,6 @@ mod tests {
let contract_address = utils::deploy(ConstantProductAmm::TEST_CLASS_HASH, calldata);
Deployment {
contract: IConstantProductAmmDispatcher { contract_address },
contract_address,
token0,
token1
}
Expand All @@ -327,8 +325,8 @@ mod tests {
let provider: ContractAddress = BANK.try_into().unwrap();
set_contract_address(provider);

deploy.token0.approve(deploy.contract_address, amount);
deploy.token1.approve(deploy.contract_address, amount);
deploy.token0.approve(deploy.contract.contract_address, amount);
deploy.token1.approve(deploy.contract.contract_address, amount);

deploy.contract.add_liquidity(amount, amount)
}
Expand Down Expand Up @@ -367,4 +365,33 @@ mod tests {
assert(deploy.token0.balance_of(provider) == INITIAL_SUPPLY, 'Wrong balance token0');
assert(deploy.token1.balance_of(provider) == INITIAL_SUPPLY, 'Wrong balance token1');
}

#[test]
#[available_gas(20000000)]
fn should_swap() {
let deploy = setup();
let shares = add_liquidity(deploy, INITIAL_SUPPLY / 2);

let provider: ContractAddress = BANK.try_into().unwrap();
let user = contract_address_const::<0x1>();

// Provider send some token0 to user
set_contract_address(provider);
let amount = deploy.token0.balance_of(provider) / 2;
deploy.token0.transfer(user, amount);

// user swap for token1 using AMM liquidity
set_contract_address(user);
deploy.token0.approve(deploy.contract.contract_address, amount);
deploy.contract.swap(deploy.token0.contract_address, amount);
let amount_token1_received = deploy.token1.balance_of(user);
assert(amount_token1_received > 0, 'Swap: wrong balance token1');

// User can swap back token1 to token0
// As each swap has a 0.3% fee, user will receive less token0
deploy.token1.approve(deploy.contract.contract_address, amount_token1_received);
deploy.contract.swap(deploy.token1.contract_address, amount_token1_received);
let amount_token0_received = deploy.token0.balance_of(user);
assert(amount_token0_received < amount, 'Swap: wrong balance token0');
}
}
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Summary

- [Upgradeable Contract](./ch02-01-upgradeable_contract.md)
- [Defi Vault](./ch02-02-simple_vault.md)
- [Constant Product AMM](./ch02-03-constant-product-amm.md)

- [Optimisations](./ch03-00-optimisations.md)

Expand Down
9 changes: 9 additions & 0 deletions src/ch02-03-constant-product-amm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Constant Product AMM

This is the Cairo adaptation of the [Solidity by example Constant Product AMM](https://solidity-by-example.org/defi/constant-product-amm/).

```rust
{{#include ../listings/ch02-applications/constant_product_amm/src/constant_product_amm.cairo:ConstantProductAmmContract}}
```

Play with this contract in [Remix](https://remix.ethereum.org/?#activate=Starknet&url=https://github.com/NethermindEth/StarknetByExample/blob/main/listings/ch02-applications/constant_product_amm/src/constant_product_amm.cairo).

0 comments on commit f896d74

Please sign in to comment.