-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from khaeljy/khaeljy/issue7
feat: Implement router contract for JediSwap
- Loading branch information
Showing
9 changed files
with
353 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,11 @@ mod account { | |
mod error; | ||
} | ||
|
||
mod router { | ||
mod router; | ||
mod error; | ||
mod jedi_swap_router; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod RouterError { | ||
const ALREADY_INITIALIZED: felt252 = 'ALREADY_INITIALIZED'; | ||
const ONLY_OWNER: felt252 = 'ONLY_OWNER'; | ||
const ROUTER_ADDRESS_UNDEFINED: felt252 = 'ROUTER_ADDRESS_UNDEFINED'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#[starknet::contract] | ||
mod JediSwapRouter { | ||
// ************************************************************************* | ||
// IMPORTS | ||
// ************************************************************************* | ||
|
||
// Core lib imports. | ||
use core::zeroable::Zeroable; | ||
use starknet::{get_caller_address, ContractAddress}; | ||
|
||
// Local imports. | ||
use swappy::router::router::IRouter; | ||
use swappy::router::error::RouterError; | ||
|
||
// ************************************************************************* | ||
// STORAGE | ||
// ************************************************************************* | ||
#[storage] | ||
struct Storage { | ||
owner: ContractAddress, | ||
router_address: ContractAddress | ||
} | ||
|
||
// ************************************************************************* | ||
// CONSTRUCTOR | ||
// ************************************************************************* | ||
|
||
/// Constructor of the contract. | ||
/// # Arguments | ||
/// * `owner` - The owner address. | ||
#[constructor] | ||
fn constructor(ref self: ContractState, owner: ContractAddress) { | ||
self.initialize(owner); | ||
} | ||
|
||
// ************************************************************************* | ||
// EXTERNAL FUNCTIONS | ||
// ************************************************************************* | ||
#[external(v0)] | ||
impl JediSwapRouterImpl of IRouter<ContractState> { | ||
fn initialize(ref self: ContractState, owner: ContractAddress) { | ||
// Make sure the contract is not already initialized. | ||
assert(self.owner.read().is_zero(), RouterError::ALREADY_INITIALIZED); | ||
|
||
self.owner.write(owner); | ||
} | ||
|
||
fn get_owner(ref self: ContractState) -> ContractAddress { | ||
self.owner.read() | ||
} | ||
|
||
fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { | ||
self.only_owner(); | ||
self.owner.write(new_owner); | ||
} | ||
|
||
fn get_router_address(ref self: ContractState) -> ContractAddress { | ||
self.router_address.read() | ||
} | ||
|
||
fn set_router_address(ref self: ContractState, router_address: ContractAddress) { | ||
self.only_owner(); | ||
self.router_address.write(router_address) | ||
} | ||
|
||
fn swap_exact_tokens_for_tokens( | ||
ref self: ContractState, | ||
amount_in: u256, | ||
amount_out_min: u256, | ||
path: Span<ContractAddress>, | ||
to: ContractAddress, | ||
deadline: u32 | ||
) { | ||
self | ||
.swap_exact_tokens_for_tokens_internal( | ||
amount_in, amount_out_min, path, to, deadline | ||
); | ||
} | ||
} | ||
|
||
#[generate_trait] | ||
impl JediSwapRouterInternalImpl of JediSwapRouterInternal { | ||
fn only_owner(self: @ContractState) { | ||
let sender = get_caller_address(); | ||
let owner = self.owner.read(); | ||
|
||
assert(sender == owner, RouterError::ONLY_OWNER); | ||
} | ||
|
||
fn swap_exact_tokens_for_tokens_internal( | ||
ref self: ContractState, | ||
amount_in: u256, | ||
amount_out_min: u256, | ||
path: Span<ContractAddress>, | ||
to: ContractAddress, | ||
deadline: u32 | ||
) { | ||
let router_address = self.router_address.read(); | ||
assert(router_address.is_non_zero(), RouterError::ROUTER_ADDRESS_UNDEFINED); | ||
|
||
// TODO : Call router address swap_exact_tokens_for_tokens | ||
// Until JediSwap is updated to Cairo 1, panic with 'NOT_IMPLEMENTED' | ||
panic_with_felt252('NOT_IMPLEMENTED'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// ************************************************************************* | ||
// IMPORTS | ||
// ************************************************************************* | ||
use starknet::ContractAddress; | ||
|
||
// ************************************************************************* | ||
// STRUCTS / CONST | ||
// ************************************************************************* | ||
|
||
// ************************************************************************* | ||
// Interfaces of the `Router` contract. | ||
// ************************************************************************* | ||
#[starknet::interface] | ||
trait IRouter<TContractState> { | ||
/// Initialize the contract | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `owner` - The contract owner address | ||
fn initialize(ref self: TContractState, owner: ContractAddress); | ||
|
||
/// Get the owner address | ||
fn get_owner(ref self: TContractState) -> ContractAddress; | ||
|
||
/// Transfer the ownership | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `new_owner` - The new owner address | ||
fn transfer_ownership(ref self: TContractState, new_owner: ContractAddress); | ||
|
||
/// Get the router address | ||
fn get_router_address(ref self: TContractState) -> ContractAddress; | ||
|
||
/// Set the router address | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `router_address` - The router address | ||
fn set_router_address(ref self: TContractState, router_address: ContractAddress); | ||
|
||
/// Swap exact tokens for tokens | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `amount_in` - Amount of token to swap | ||
/// * `amount_out_min` - Min out token amount | ||
/// * `path` - Route path | ||
/// * `to` - The recipient of swap | ||
/// * `deadline` - The deadline of the swap transaction | ||
fn swap_exact_tokens_for_tokens( | ||
ref self: TContractState, | ||
amount_in: u256, | ||
amount_out_min: u256, | ||
path: Span<ContractAddress>, | ||
to: ContractAddress, | ||
deadline: u32 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
#[cfg(test)] | ||
mod test_account; | ||
|
||
#[cfg(test)] | ||
mod router { | ||
#[cfg(test)] | ||
mod test_jedi_swap_router; | ||
} |
Oops, something went wrong.