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

feat: user pool in aux contract #11

Open
wants to merge 2 commits into
base: aux_contract
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions src/amm_core/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const TOKEN_WBTC_ADDRESS: felt252 =
0x03fe2b97c1fd336e750087d68b9b867997fd64a2661ff3ca5a7c771641e8e7ac; // mainnet
// const TOKEN_WBTC_ADDRESS: felt252 =
// 0x012d537dc323c439dc65c976fad242d5610d27cfb5f31689a0a319b8be7f3d56; // testnet

const TOKEN_STRK_ADDRESS: felt252 =
0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d; // mainnet

// Tests --------------------------------------------------------------------------------------------------------------

#[cfg(test)]
Expand Down
104 changes: 93 additions & 11 deletions src/aux_contract.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use starknet::ContractAddress;
use carmine_protocol::types::option_::OptionWithPremia;
use carmine_protocol::types::pool::UserPoolInfo;

#[starknet::interface]
trait IAuxContract<TContractState> {
fn get_all_non_expired_options_with_premia(
self: @TContractState, lpt_addr: ContractAddress
) -> Array<OptionWithPremia>;
fn get_user_pool_info(
self: @TContractState, user: ContractAddress, lpt_addr: ContractAddress
) -> UserPoolInfo;
}

#[starknet::contract]
Expand All @@ -16,33 +20,104 @@ mod AuxContract {

use carmine_protocol::types::option_::OptionWithPremia;
use carmine_protocol::types::option_::Option_Trait;
use carmine_protocol::types::pool::UserPoolInfo;
use carmine_protocol::types::pool::{PoolInfo, Pool};
use carmine_protocol::amm_interface::IAMM;
use carmine_protocol::amm_interface::{IAMMDispatcher, IAMMDispatcherTrait};
use carmine_protocol::erc20_interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use cubit::f128::types::fixed::{Fixed, FixedTrait};

use carmine_protocol::amm_core::constants::{
TOKEN_USDC_ADDRESS, TOKEN_ETH_ADDRESS, TOKEN_WBTC_ADDRESS
TOKEN_USDC_ADDRESS, TOKEN_ETH_ADDRESS, TOKEN_WBTC_ADDRESS, TOKEN_STRK_ADDRESS
};

#[storage]
struct Storage {}
#[generate_trait]
impl PoolImpl of PoolTrait {
fn from_lpt_address(lpt_addr: ContractAddress, amm: IAMMDispatcher) -> Pool {
amm.get_pool_definition_from_lptoken_address(lpt_addr)
}

fn lpt_addr(self: Pool, amm: IAMMDispatcher) -> ContractAddress {
amm
.get_lptoken_address_for_given_option(
self.quote_token_address, self.base_token_address, self.option_type
)
}

fn lpool_balance(self: Pool, amm: IAMMDispatcher) -> u256 {
amm.get_lpool_balance(self.lpt_addr(amm))
}

fn unlocked_capital(self: Pool, amm: IAMMDispatcher) -> u256 {
amm.get_unlocked_capital(self.lpt_addr(amm))
}

fn value_of_position(self: Pool, amm: IAMMDispatcher) -> Fixed {
amm.get_value_of_pool_position(self.lpt_addr(amm))
}

fn to_PoolInfo(self: Pool, amm: IAMMDispatcher) -> PoolInfo {
PoolInfo {
pool: self,
lptoken_address: self.lpt_addr(amm),
staked_capital: self.lpool_balance(amm),
unlocked_capital: self.unlocked_capital(amm),
value_of_pool_position: self.value_of_position(amm)
}
}
}
#[generate_trait]
impl PoolInfoImpl of PoolInfoTrait {
fn to_UserPoolInfo(
self: PoolInfo, user_address: ContractAddress, amm: IAMMDispatcher
) -> UserPoolInfo {
let lptoken_balance = IERC20Dispatcher { contract_address: self.lptoken_address }
.balanceOf(user_address);

let stake_value = amm
.get_underlying_for_lptokens(self.lptoken_address, lptoken_balance);

UserPoolInfo {
value_of_user_stake: stake_value,
size_of_users_tokens: lptoken_balance,
pool_info: self
}
}
}

const AMM_ADDR: felt252 = 0x01007d87af0a2b9b6199f5f09ab9c230f415470eeceb5a8b01590c51229da562;

#[storage]
struct Storage {
amm_address: ContractAddress
}

#[constructor]
fn constructor(ref self: ContractState, amm_address: ContractAddress) {
self.amm_address.write(amm_address);
}

#[external(v0)]
impl AuxContract of super::IAuxContract<ContractState> {
fn get_all_non_expired_options_with_premia(
self: @ContractState, lpt_addr: ContractAddress
) -> Array<OptionWithPremia> {
let amm = IAMMDispatcher { contract_address: AMM_ADDR.try_into().unwrap() };
let amm_address = self.amm_address.read();
let amm = IAMMDispatcher { contract_address: amm_address };
let pool = amm.get_pool_definition_from_lptoken_address(lpt_addr);
let base: felt252 = pool.base_token_address.into();
let quote: felt252 = pool.quote_token_address.into();

let opt_size = if base == TOKEN_ETH_ADDRESS {
1000000000000000000 // 10**18 * 1
} else {
10000000 // 10**8 * 0.1
};
let mut opt_size = 1000000000000000000; // 1 ETH -> 10**18 * 1

if quote == TOKEN_STRK_ADDRESS {
// for ETH/STRK options
opt_size = 1000000000000000; // 0.001 ETH -> 10**18 * 0.001
}

if base == TOKEN_WBTC_ADDRESS {
// for BTC/USDC options
opt_size = 10000000; // 0.1 BTC -> 10**8 * 0.1
}

let mut i: u32 = 0;
let current_block_time = get_block_timestamp();
Expand All @@ -68,6 +143,13 @@ mod AuxContract {

arr
}
fn get_user_pool_info(
self: @ContractState, user: ContractAddress, lpt_addr: ContractAddress
) -> UserPoolInfo {
let amm_address = self.amm_address.read();
let amm = IAMMDispatcher { contract_address: amm_address };
let pool_info = PoolTrait::from_lpt_address(lpt_addr, amm).to_PoolInfo(amm);
pool_info.to_UserPoolInfo(user, amm)
}
}
}

Loading