Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra deposit and withdraw methods #150

Merged
merged 21 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/spotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ The method has next parameters:
* `debt_token`: ContractAddress - Address of the token used as borrowing.
* `pool_key`: PoolKey - Ekubo type for obtaining info about the pool and swapping tokens.
* `ekubo_limits`: EkuboSlippageLimits - Object of internal type which represents upper and lower sqrt_ratio values on Ekubo. Used to control slippage while swapping.
* `repay_const`: u8 - Sets how much to borrow from free amount. Parameter is used for dealing with price error on zklend or for pairs where debt interest rate accumulates faster than supply interest rate.
* `supply_price`: TokenPrice - Price of `supply` token in terms of `debt` token(so for ex. 2400000000 USDC for ETH).
* `debt_price`: TokenPrice - Price of `debt` token in terms of `supply` token(for ex. 410000000000000 ETH for USDC).

Expand Down
10 changes: 7 additions & 3 deletions src/deposit.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ mod Deposit {
total_debt: TokenAmount,
collateral_factor: felt252,
borrow_factor: felt252,
repay_const: u8,
supply_token_price: TokenPrice,
debt_token_price: TokenPrice,
supply_decimals: DecimalScale,
Expand All @@ -91,8 +92,7 @@ mod Deposit {
* borrow_factor.into()
/ ZK_SCALE_DECIMALS)
- total_debt.into();
let withdraw_amount = free_amount * debt_token_price.into() / debt_decimals.into();
withdraw_amount
free_amount * debt_token_price.into() / debt_decimals.into() * repay_const.into() / 100
faurdent marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(starknet::Event, Drop)]
Expand Down Expand Up @@ -264,6 +264,7 @@ mod Deposit {
/// tokens.
/// * `ekubo_limits`: EkuboSlippageLimits - Represents upper and lower sqrt_ratio values on
/// Ekubo. Used to control slippage while swapping.
/// * `repay_const`: u8 - Sets how much to borrow from free amount.
/// * `supply_price`: TokenPrice - Price of `supply` token in terms of `debt` token.
/// * `debt_price`: TokenPrice - Price of `debt` token in terms of `supply` token.
fn close_position(
Expand All @@ -272,6 +273,7 @@ mod Deposit {
debt_token: ContractAddress,
pool_key: PoolKey,
ekubo_limits: EkuboSlippageLimits,
repay_const: u8,
supply_price: TokenPrice,
debt_price: TokenPrice
) {
Expand Down Expand Up @@ -316,6 +318,7 @@ mod Deposit {
debt,
collateral_factor,
borrow_factor,
repay_const,
supply_price,
debt_price,
supply_decimals,
Expand Down Expand Up @@ -407,7 +410,7 @@ mod Deposit {
let strk = ERC20ABIDispatcher { contract_address: STRK_ADDRESS.try_into().unwrap() };
let zk_market = self.zk_market.read();
let part_for_treasury = claim_data.amount
- claim_data.amount / 5; // u128 integer division, rounds down
- claim_data.amount / 2; // u128 integer division, rounds down

let treasury_addr = self.treasury.read();
let remainder = if (treasury_addr
Expand Down Expand Up @@ -443,6 +446,7 @@ mod Deposit {
);
token_dispatcher.transferFrom(get_caller_address(), get_contract_address(), amount);
token_dispatcher.approve(zk_market.contract_address, amount);
zk_market.enable_collateral(token);
zk_market.deposit(token, amount.try_into().unwrap());
self.reentrancy_guard.end();
}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub trait IDeposit<TContractState> {
debt_token: ContractAddress,
pool_key: PoolKey,
ekubo_limits: EkuboSlippageLimits,
repay_const: u8,
supply_price: TokenPrice,
debt_price: TokenPrice
);
Expand Down
2 changes: 1 addition & 1 deletion src/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct SwapData {
pub struct DepositData {
pub token: ContractAddress,
pub amount: TokenAmount,
pub multiplier: u32,
pub multiplier: u8,
pub borrow_const: u8
}

Expand Down
5 changes: 2 additions & 3 deletions tests/test_defispring.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ fn test_claim_as_keeper() {
};
// eligible for 0x2a52c411698a729 = 190607217296713513 fri (fri is lowest denominator of strk
// token)
// treasury should get 152485773837370811 which is 80 %

// treasury should get 95303608648356757 which is 50 %
deposit_contract.claim_reward(claim, proof.span(), defispring_claim_contract);

let fri_in_treasury = strk.balance_of(hypothetical_treasury_address.try_into().unwrap());
assert(fri_in_treasury == 152485773837370811, 'incorrect amount in treasury');
assert(fri_in_treasury == 95303608648356757, 'incorrect amount in treasury');
let strk_left_in_contract = strk
.balance_of(address_eligible_for_zklend_rewards.try_into().unwrap());
assert!(
Expand Down
80 changes: 79 additions & 1 deletion tests/test_loop.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ fn test_close_position_usdc_valid_time_passed() {
eth_addr,
pool_key,
get_slippage_limits(pool_key),
100,
pool_price,
quote_token_price
);
Expand Down Expand Up @@ -493,21 +494,96 @@ fn test_close_position_amounts_cleared() {
get_slippage_limits(pool_key),
pool_price
);
deposit_disp
.close_position(
usdc_addr,
eth_addr,
pool_key,
get_slippage_limits(pool_key),
100,
pool_price,
quote_token_price
);
stop_cheat_account_contract_address(deposit_disp.contract_address);
let zk_market = IMarketTestingDispatcher {
contract_address: contracts::ZKLEND_MARKET.try_into().unwrap()
};

assert(
zk_market.get_user_debt_for_token(deposit_disp.contract_address, eth_addr) == 0,
'Debt remains after repay'
);
assert(
ERC20ABIDispatcher {
contract_address: zk_market.get_reserve_data(usdc_addr).z_token_address
}
.balanceOf(deposit_disp.contract_address) == 0,
'Not all withdrawn'
);
}

#[test]
#[fork("MAINNET")]
fn test_close_position_partial_debt_utilization() {
let usdc_addr: ContractAddress =
0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8
.try_into()
.unwrap();
let eth_addr: ContractAddress =
0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7
.try_into()
.unwrap();
let user: ContractAddress = 0x0038925b0bcf4dce081042ca26a96300d9e181b910328db54a6c89e5451503f5
.try_into()
.unwrap();

let pool_key = PoolKey {
token0: eth_addr,
token1: usdc_addr,
fee: 170141183460469235273462165868118016,
tick_spacing: 1000,
extension: 0.try_into().unwrap()
};
let pool_price = get_asset_price_pragma('ETH/USD').into();

let token_disp = ERC20ABIDispatcher { contract_address: eth_addr };
let decimals_sum_power: u128 = fast_power(
10,
(ERC20ABIDispatcher { contract_address: usdc_addr }.decimals() + token_disp.decimals())
.into()
);
let quote_token_price = 1 * decimals_sum_power.into() / pool_price;

let deposit_disp = get_deposit_dispatcher(user);

start_cheat_caller_address(eth_addr.try_into().unwrap(), user);
token_disp.approve(deposit_disp.contract_address, 1000000000000000);
stop_cheat_caller_address(eth_addr);

start_cheat_account_contract_address(deposit_disp.contract_address, user);
deposit_disp
.loop_liquidity(
DepositData {
token: eth_addr, amount: 1000000000000000, multiplier: 4, borrow_const: 98
},
pool_key,
get_slippage_limits(pool_key),
pool_price
);
deposit_disp
.close_position(
usdc_addr,
eth_addr,
usdc_addr,
pool_key,
get_slippage_limits(pool_key),
85,
pool_price,
quote_token_price
);
stop_cheat_account_contract_address(deposit_disp.contract_address);
let zk_market = IMarketTestingDispatcher {
contract_address: contracts::ZKLEND_MARKET.try_into().unwrap()
};

assert(
zk_market.get_user_debt_for_token(deposit_disp.contract_address, eth_addr) == 0,
Expand Down Expand Up @@ -662,6 +738,7 @@ fn test_extra_deposit_supply_token_close_position_fuzz(extra_amount: u32) {
eth_addr,
pool_key,
get_slippage_limits(pool_key),
100,
pool_price,
quote_token_price
);
Expand Down Expand Up @@ -741,6 +818,7 @@ fn test_withdraw_valid_fuzz(amount: u32) {
eth_addr,
pool_key,
get_slippage_limits(pool_key),
100,
pool_price,
quote_token_price
);
Expand Down
Loading