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

Issue 36 #40

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .snfoundry_cache/.prev_tests_failed
Original file line number Diff line number Diff line change
@@ -1 +1 @@
auto_swappr_tests::test_swap::test_swap
auto_swappr_tests::test_fibrous_swap::test_fibrous_swap
49 changes: 48 additions & 1 deletion src/autoswappr.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// @dev Implements upgradeable pattern and ownership control
pub mod AutoSwappr {
use crate::interfaces::iautoswappr::{IAutoSwappr, ContractInfo};
use crate::base::types::{Route, Assets};
use crate::base::types::{Route, Assets, RouteParams, SwapParams};
use openzeppelin_upgrades::UpgradeableComponent;
use openzeppelin_upgrades::interface::IUpgradeable;
use core::starknet::storage::{
Expand All @@ -19,6 +19,7 @@ pub mod AutoSwappr {

use openzeppelin::access::ownable::OwnableComponent;
use crate::interfaces::iavnu_exchange::{IExchangeDispatcher, IExchangeDispatcherTrait};
use crate::interfaces::fibrous_exchange::{IFibrousExchangeDispatcher, IFibrousExchangeDispatcherTrait};
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};

use core::integer::{u256, u128};
Expand Down Expand Up @@ -47,6 +48,7 @@ pub mod AutoSwappr {
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
avnu_exchange_address: ContractAddress,
fibrous_exchange_address: ContractAddress,
}

// @notice Events emitted by the contract
Expand Down Expand Up @@ -101,6 +103,7 @@ pub mod AutoSwappr {
ref self: ContractState,
fees_collector: ContractAddress,
avnu_exchange_address: ContractAddress,
fibrous_exchange_address: ContractAddress,
_strk_token: ContractAddress,
_eth_token: ContractAddress,
owner: ContractAddress,
Expand All @@ -109,6 +112,7 @@ pub mod AutoSwappr {
self.strk_token.write(_strk_token);
self.eth_token.write(_eth_token);
self.avnu_exchange_address.write(avnu_exchange_address);
self.fibrous_exchange_address.write(fibrous_exchange_address);
self.ownable.initializer(owner);
self.supported_assets.write(_strk_token, true);
self.supported_assets.write(_eth_token, true);
Expand Down Expand Up @@ -201,6 +205,35 @@ pub mod AutoSwappr {
);
}

fn fibrous_swap(
ref self: ContractState,
routeParams: RouteParams,
swapParams: Array<SwapParams>,
){
let caller_address = get_caller_address();

assert(
self.supported_assets.entry(routeParams.token_in).read(), Errors::UNSUPPORTED_TOKEN,
);
assert(!routeParams.amount_in.is_zero(), Errors::ZERO_AMOUNT);

let token = IERC20Dispatcher { contract_address: routeParams.token_in };

assert(
token.balance_of(caller_address) >= routeParams.amount_in, Errors::INSUFFICIENT_BALANCE,
);
// assert(
// token.allowance(caller_address, this_contract) >= routeParams.amount_in,
// Errors::INSUFFICIENT_ALLOWANCE,
// );

self
._fibrous_swap(
routeParams,
swapParams,
);
}


// @notice Returns the contract's current parameters
// @return ContractInfo struct containing current contract parameters
Expand Down Expand Up @@ -248,6 +281,20 @@ pub mod AutoSwappr {
)
}

fn _fibrous_swap(
ref self: ContractState,
routeParams: RouteParams,
swapParams: Array<SwapParams>,
) {
let fibrous = IFibrousExchangeDispatcher { contract_address: self.fibrous_exchange_address.read() };

fibrous
.swap(
routeParams,
swapParams
);
}

fn collect_fees(ref self: ContractState) {}

// @notice Returns the zero address constant
Expand Down
20 changes: 20 additions & 0 deletions src/base/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,23 @@ pub struct Assets {
pub strk: bool,
pub eth: bool,
}

// Fibrous exchange
#[derive(Drop, Serde, Clone)]
pub struct RouteParams {
pub token_in: ContractAddress,
pub token_out: ContractAddress,
pub amount_in: u256,
pub min_received: u256,
pub destination: ContractAddress,
}

#[derive(Drop, Serde, Clone)]
pub struct SwapParams {
pub token_in: ContractAddress,
pub token_out: ContractAddress,
pub rate: u32,
pub protocol_id: u32,
pub pool_address: ContractAddress,
pub extra_data: Array<felt252>,
}
8 changes: 8 additions & 0 deletions src/interfaces/fibrous_exchange.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use starknet::ContractAddress;
use crate::base::types::{RouteParams, SwapParams};

#[starknet::interface]
pub trait IFibrousExchange<TContractState> {
#[external(v0)]
fn swap(ref self: TContractState, route: RouteParams, swap_parameters: Array<SwapParams>);
}
6 changes: 6 additions & 0 deletions src/interfaces/iautoswappr.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::starknet::ContractAddress;
use crate::base::types::Route;
use crate::base::types::{RouteParams, SwapParams};

// @title Contract Information Structure
// @notice Holds the essential addresses and parameters for the AutoSwappr contract
Expand Down Expand Up @@ -30,6 +31,11 @@ pub trait IAutoSwappr<TContractState> {
integrator_fee_recipient: ContractAddress,
routes: Array<Route>,
);
fn fibrous_swap(
ref self: TContractState,
routeParams: RouteParams,
swapParams: Array<SwapParams>,
);

// @notice Retrieves the current contract parameters
// @return ContractInfo struct containing the contract's current configuration
Expand Down
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod base {
pub mod interfaces {
pub mod iautoswappr;
pub mod iavnu_exchange;
pub mod fibrous_exchange;
}

pub mod autoswappr;
3 changes: 2 additions & 1 deletion tests/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod test_autoswapper;
// mod test_autoswapper;
mod test_fibrous_swap;
// mod test_swap;


4 changes: 4 additions & 0 deletions tests/test_autoswapper.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub fn FEE_COLLECTOR_ADDR() -> ContractAddress {
pub fn AVNU_ADDR() -> ContractAddress {
contract_address_const::<'AVNU_ADDR'>()
}
pub fn FIBROUS_ADDR() -> ContractAddress {
contract_address_const::<'FIBROUS_ADDR'>()
}
pub fn OWNER() -> ContractAddress {
contract_address_const::<'OWNER'>()
}
Expand Down Expand Up @@ -73,6 +76,7 @@ fn __setup__() -> (ContractAddress, IERC20Dispatcher, IERC20Dispatcher) {
let mut autoSwappr_constructor_calldata: Array<felt252> = array![];
FEE_COLLECTOR_ADDR().serialize(ref autoSwappr_constructor_calldata);
AVNU_ADDR().serialize(ref autoSwappr_constructor_calldata);
FIBROUS_ADDR().serialize(ref autoSwappr_constructor_calldata);
strk_contract_address.serialize(ref autoSwappr_constructor_calldata);
eth_contract_address.serialize(ref autoSwappr_constructor_calldata);
OWNER().serialize(ref autoSwappr_constructor_calldata);
Expand Down
Loading