@@ -7,8 +7,16 @@ pub mod Margin {
77 storage :: {StoragePointerReadAccess , StoragePointerWriteAccess , StoragePathEntry , Map },
88 ContractAddress , get_contract_address, get_caller_address,
99 };
10- use margin :: {interface :: IMargin , types :: {Position , TokenAmount , PositionParameters }};
10+ use margin :: {
11+ interface :: IMargin ,
12+ types :: {Position , TokenAmount , PositionParameters , SwapData , EkuboSlippageLimits },
13+ };
1114 use openzeppelin_token :: erc20 :: interface :: {IERC20Dispatcher };
15+ use ekubo :: {
16+ interfaces :: core :: {ICoreDispatcher , ILocker , ICoreDispatcherTrait },
17+ types :: {keys :: PoolKey , delta :: Delta },
18+ components :: shared_locker :: {consume_callback_data, handle_delta, call_core_with_callback},
19+ };
1220
1321 #[derive(starknet:: Event , Drop )]
1422 struct Deposit {
@@ -34,11 +42,26 @@ pub mod Margin {
3442
3543 #[storage]
3644 struct Storage {
45+ ekubo_core : ICoreDispatcher ,
3746 treasury_balances : Map <(ContractAddress , ContractAddress ), TokenAmount >,
3847 pools : Map <ContractAddress , TokenAmount >,
3948 positions : Map <ContractAddress , Position >,
4049 }
4150
51+ #[constructor]
52+ fn constructor (ref self : ContractState , ekubo_core : ICoreDispatcher ) {
53+ self . ekubo_core. write (ekubo_core );
54+ }
55+
56+
57+ #[generate_trait]
58+ pub impl InternalImpl of InternalTrait {
59+ fn swap (ref self : ContractState , swap_data : SwapData ) -> Delta {
60+ call_core_with_callback (self . ekubo_core. read (), @ swap_data )
61+ }
62+ }
63+
64+
4265 #[abi(embed_v0)]
4366 impl Margin of IMargin <ContractState > {
4467 /// Deposits specified amount of ERC20 tokens into the contract's treasury
@@ -79,9 +102,37 @@ pub mod Margin {
79102 self . emit (Withdraw { withdrawer , token , amount });
80103 }
81104
82- // TODO: Add Ekubo data for swap
83- fn open_margin_position (ref self : ContractState , position_parameters : PositionParameters ) {}
84- fn close_position (ref self : ContractState ) {}
85- fn liquidate (ref self : ContractState , user : ContractAddress ) {}
105+ fn open_margin_position (
106+ ref self : ContractState ,
107+ position_parameters : PositionParameters ,
108+ pool_key : PoolKey ,
109+ ekubo_limits : EkuboSlippageLimits ,
110+ ) {}
111+ fn close_position (
112+ ref self : ContractState , pool_key : PoolKey , ekubo_limits : EkuboSlippageLimits ,
113+ ) {}
114+ fn liquidate (
115+ ref self : ContractState ,
116+ user : ContractAddress ,
117+ pool_key : PoolKey ,
118+ ekubo_limits : EkuboSlippageLimits ,
119+ ) {}
120+ }
121+
122+
123+ #[abi(embed_v0)]
124+ impl Locker of ILocker <ContractState > {
125+ fn locked (ref self : ContractState , id : u32 , data : Span <felt252 >) -> Span <felt252 > {
126+ let core = self . ekubo_core. read ();
127+ let SwapData { pool_key , params , caller } = consume_callback_data (core , data );
128+ let delta = core . swap (pool_key , params );
129+
130+ handle_delta (core , pool_key . token0, delta . amount0, caller );
131+ handle_delta (core , pool_key . token1, delta . amount1, caller );
132+
133+ let mut arr : Array <felt252 > = ArrayTrait :: new ();
134+ Serde :: serialize (@ delta , ref arr );
135+ arr . span ()
136+ }
86137 }
87138}
0 commit comments