diff --git a/margin/.gitignore b/margin/.gitignore new file mode 100644 index 00000000..73aa31e6 --- /dev/null +++ b/margin/.gitignore @@ -0,0 +1,2 @@ +target +.snfoundry_cache/ diff --git a/margin/README.md b/margin/README.md new file mode 100644 index 00000000..d53707ff --- /dev/null +++ b/margin/README.md @@ -0,0 +1,14 @@ +# Margin Leverage +This is the repository for our margin leverage smart contract's internals. + +### Project structure + +### Running tests +To run tests with a shortcut, give the `test.sh` appropriate permissions: +```sh +chmod +x ./test.sh +``` +Then, run all tests with backtrace(where it is supported by Foundry): +```sh +./test.sh +``` diff --git a/margin/Scarb.lock b/margin/Scarb.lock new file mode 100644 index 00000000..88a1dc04 --- /dev/null +++ b/margin/Scarb.lock @@ -0,0 +1,22 @@ +# Code generated by scarb DO NOT EDIT. +version = 1 + +[[package]] +name = "margin" +version = "0.1.0" +dependencies = [ + "snforge_std", +] + +[[package]] +name = "snforge_scarb_plugin" +version = "0.32.0" +source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.32.0#3817c903b640201c72e743b9bbe70a97149828a2" + +[[package]] +name = "snforge_std" +version = "0.32.0" +source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.32.0#3817c903b640201c72e743b9bbe70a97149828a2" +dependencies = [ + "snforge_scarb_plugin", +] diff --git a/margin/Scarb.toml b/margin/Scarb.toml new file mode 100644 index 00000000..fd7ea491 --- /dev/null +++ b/margin/Scarb.toml @@ -0,0 +1,39 @@ +[package] +name = "margin" +version = "0.1.0" +edition = "2023_11" + +# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html + +[dependencies] +starknet = "2.9.4" + +[dev-dependencies] +snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.32.0" } +assert_macros = "2.9.4" + +[[target.starknet-contract]] +sierra = true + +[scripts] +test = "snforge test" + +[profile.dev.cairo] +unstable-add-statements-code-locations-debug-info = true +unstable-add-statements-functions-debug-info = true +inlining-strategy = "avoid" + +[tool.snforge] +fuzzer_runs = 50 + +[[tool.snforge.fork]] +name = "MAINNET" +url = "http://51.195.57.196:6060/v0_7" +block_id.tag = "latest" + +[[tool.snforge.fork]] +name = "SEPOLIA" +url = "http://51.195.57.196:6062/v0_7" +block_id.tag = "latest" + +# Visit https://foundry-rs.github.io/starknet-foundry/appendix/scarb-toml.html for more information diff --git a/margin/snfoundry.toml b/margin/snfoundry.toml new file mode 100644 index 00000000..7d45ecda --- /dev/null +++ b/margin/snfoundry.toml @@ -0,0 +1,9 @@ +# Visit https://foundry-rs.github.io/starknet-foundry/appendix/snfoundry-toml.html for more information + +# [sncast.myprofile1] # Define a profile name +# url = "http://127.0.0.1:5050/" # Url of the RPC provider +# accounts_file = "../account-file" # Path to the file with the account data +# account = "mainuser" # Account from `accounts_file` or default account file that will be used for the transactions +# keystore = "~/keystore" # Path to the keystore file +# wait_params = { timeout = 500, retry_interval = 10 } # Wait for submitted transaction parameters +# block_explorer = "StarkScan" # Block explorer service used to display links to transaction details diff --git a/margin/src/interface.cairo b/margin/src/interface.cairo new file mode 100644 index 00000000..e3f3947a --- /dev/null +++ b/margin/src/interface.cairo @@ -0,0 +1,14 @@ +use starknet::ContractAddress; +use crate::types::{TokenAmount, PositionParameters}; + +#[starknet::interface] +pub trait IMargin { + fn deposit(ref self: TContractState, token: ContractAddress, amount: TokenAmount); + fn withdraw(ref self: TContractState, token: ContractAddress, amount: TokenAmount); + + // TODO: Add Ekubo data for swap + fn open_margin_position(ref self: TContractState, position_parameters: PositionParameters); + fn close_position(ref self: TContractState); + + fn liquidate(ref self: TContractState, user: ContractAddress); +} diff --git a/margin/src/lib.cairo b/margin/src/lib.cairo new file mode 100644 index 00000000..abaaf8e1 --- /dev/null +++ b/margin/src/lib.cairo @@ -0,0 +1,3 @@ +pub mod margin; +pub mod types; +pub mod interface; diff --git a/margin/src/margin.cairo b/margin/src/margin.cairo new file mode 100644 index 00000000..35b048c2 --- /dev/null +++ b/margin/src/margin.cairo @@ -0,0 +1,31 @@ +#[starknet::contract] +pub mod Margin { + use starknet::{ + event::EventEmitter, + storage::{StoragePointerReadAccess, StoragePointerWriteAccess, StoragePathEntry, Map}, + ContractAddress, get_contract_address, get_caller_address, ClassHash, + }; + use margin::{ + interface::IMargin, + types::{Position, TokenAmount, PositionParameters} + }; + + + #[storage] + struct Storage { + treasury_balances: Map<(ContractAddress, ContractAddress), TokenAmount>, + pools: Map, + positions: Map, + } + + #[abi(embed_v0)] + impl Margin of IMargin{ + fn deposit(ref self: ContractState, token: ContractAddress, amount: TokenAmount) {} + fn withdraw(ref self: ContractState, token: ContractAddress, amount: TokenAmount) {} + + // TODO: Add Ekubo data for swap + fn open_margin_position(ref self: ContractState, position_parameters: PositionParameters) {} + fn close_position(ref self: ContractState) {} + fn liquidate(ref self: ContractState, user: ContractAddress) {} + } +} diff --git a/margin/src/types.cairo b/margin/src/types.cairo new file mode 100644 index 00000000..46eb444a --- /dev/null +++ b/margin/src/types.cairo @@ -0,0 +1,21 @@ +use starknet::ContractAddress; + +pub type TokenAmount = u256; +pub type Timestamp = u128; + +#[derive(Serde, Drop)] +pub struct PositionParameters { + pub initial_token: ContractAddress, + pub debt_token: ContractAddress, + pub amount: TokenAmount, +} + +#[derive(Serde, starknet::Store)] +pub struct Position { + pub initial_token: ContractAddress, + pub traded_token: ContractAddress, + pub traded_amount: TokenAmount, + pub debt: TokenAmount, + pub is_open: bool, + pub open_time: Timestamp, +} diff --git a/margin/test.sh b/margin/test.sh new file mode 100755 index 00000000..26aa0e2a --- /dev/null +++ b/margin/test.sh @@ -0,0 +1 @@ +SNFORGE_BACKTRACE=1 snforge test \ No newline at end of file