Skip to content

Commit

Permalink
Merge pull request #582 from djeck1432/feat/margin-contract-setup
Browse files Browse the repository at this point in the history
Base margin project structure
  • Loading branch information
faurdent authored Feb 20, 2025
2 parents a216e5f + 96b8ed4 commit 96468a5
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 0 deletions.
2 changes: 2 additions & 0 deletions margin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
.snfoundry_cache/
14 changes: 14 additions & 0 deletions margin/README.md
Original file line number Diff line number Diff line change
@@ -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
```
22 changes: 22 additions & 0 deletions margin/Scarb.lock
Original file line number Diff line number Diff line change
@@ -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",
]
39 changes: 39 additions & 0 deletions margin/Scarb.toml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions margin/snfoundry.toml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions margin/src/interface.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use starknet::ContractAddress;
use crate::types::{TokenAmount, PositionParameters};

#[starknet::interface]
pub trait IMargin<TContractState> {
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);
}
3 changes: 3 additions & 0 deletions margin/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod margin;
pub mod types;
pub mod interface;
31 changes: 31 additions & 0 deletions margin/src/margin.cairo
Original file line number Diff line number Diff line change
@@ -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<ContractAddress, TokenAmount>,
positions: Map<ContractAddress, Position>,
}

#[abi(embed_v0)]
impl Margin of IMargin<ContractState>{
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) {}
}
}
21 changes: 21 additions & 0 deletions margin/src/types.cairo
Original file line number Diff line number Diff line change
@@ -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,
}
1 change: 1 addition & 0 deletions margin/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SNFORGE_BACKTRACE=1 snforge test

0 comments on commit 96468a5

Please # to comment.