Skip to content

Commit

Permalink
feat: added qwChild contract
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNilesh committed Jun 23, 2024
1 parent 28636b5 commit b78a4cb
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 7 deletions.
106 changes: 106 additions & 0 deletions src/child/qwChild.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use starknet::ContractAddress;

#[starknet::interface]
trait IERC20<T> {
fn get_name(self: @T) -> felt252;
fn get_symbol(self: @T) -> felt252;
fn get_decimals(self: @T) -> u8;
fn get_total_supply(self: @T) -> u256;
fn balance_of(self: @T, account: ContractAddress) -> u256;
fn allowance(self: @T, owner: ContractAddress, spender: ContractAddress) -> u256;
fn transfer(ref self: T, recipient: ContractAddress, amount: u256);
fn transfer_from(
ref self: T, sender: ContractAddress, recipient: ContractAddress, amount: u256
);
fn approve(ref self: T, spender: ContractAddress, amount: u256);
fn increase_allowance(ref self: T, spender: ContractAddress, added_value: u256);
fn decrease_allowance(
ref self: T, spender: ContractAddress, subtracted_value: u256
);
}

#[starknet::interface]
trait IManager<T> {
// Returns the registry address
fn getRegistry(self: @T) -> ContractAddress;
// Executes the investment
fn execute(ref self: T, targetQwChilds_: Array<ContractAddress>, tokenAddress_: Array<ContractAddress>, amount_: Array<u128>);
// Closes the investment
fn close(ref self: T, targetQwChilds_: Array<ContractAddress>, tokenAddress_: Array<ContractAddress>, amount_: Array<u128>);
}

#[starknet::interface]
trait IChild<T> {
// Executes the investment
fn create(ref self: T, tokenAddress_: ContractAddress, amount_: u256);
// Closes the investment
fn close(ref self: T, tokenAddress_: ContractAddress, amount_: u256);
// Returns the manager address
fn getManager(self: @T) -> ContractAddress;
}

#[starknet::contract]
mod QwChild {
use starknet::{get_caller_address, ContractAddress, get_contract_address};
use super::{IChildDispatcher, IChildDispatcherTrait};
use super::{IManagerDispatcher, IManagerDispatcherTrait};
use super::{IERC20Dispatcher, IERC20DispatcherTrait};

// *************************************************************************
// STORAGE
// *************************************************************************
#[storage]
struct Storage {
manager: IManagerDispatcher,
}

// *************************************************************************
// ERRORS
// *************************************************************************
mod Errors {
const Invalid_Input_Length: felt252 = 'Invalid Input Length';
const Contract_Not_Whitelisted: felt252 = 'Contract Not whitelisted';
}

// *************************************************************************
// CONSTRUCTOR
// *************************************************************************
#[constructor]
fn constructor(ref self: ContractState, manager_: ContractAddress) {
let managerContract = IManagerDispatcher {
contract_address: manager_
};
self.manager.write(managerContract);
}

// *************************************************************************
// EXTERNAL FUNCTIONS
// *************************************************************************
#[abi(embed_v0)]
impl QwChild of super::IChild<ContractState> {
fn getManager(self: @ContractState) -> ContractAddress {
self.manager.read().contract_address
}
fn create(ref self: ContractState, tokenAddress_: ContractAddress, amount_: u256) {
let erc20Token = IERC20Dispatcher {
contract_address: tokenAddress_
};

let manager_address = self.manager.read().contract_address; // Get the manager's address
let current_address = get_contract_address(); // Get the current contract's address

erc20Token.transfer_from(manager_address, current_address, amount_);
}

fn close(ref self: ContractState, tokenAddress_: ContractAddress, amount_: u256) {
let erc20Token = IERC20Dispatcher {
contract_address: tokenAddress_
};

let manager_address = self.manager.read().contract_address; // Get the manager's address
let current_address = get_contract_address(); // Get the current contract's address

erc20Token.transfer_from(manager_address, current_address, amount_);
}
}
}
15 changes: 11 additions & 4 deletions src/qwManager.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ trait IERC20<T> {

#[starknet::interface]
trait IManager<T> {
// Returns the registry address
fn getRegistry(self: @T) -> ContractAddress;
// Executes the investment
fn execute(ref self: T, targetQwChilds_: Array<ContractAddress>, tokenAddress_: Array<ContractAddress>, amount_: Array<u128>);
// Closes the investment
fn close(ref self: T, targetQwChilds_: Array<ContractAddress>, tokenAddress_: Array<ContractAddress>, amount_: Array<u128>);
}

Expand All @@ -35,9 +38,11 @@ trait IRegistry<T> {
#[starknet::interface]
trait IChild<T> {
// Executes the investment
fn create(self: @T, tokenAddress_: ContractAddress, amount_: u256) -> ContractAddress;
fn create(ref self: T, tokenAddress_: ContractAddress, amount_: u256);
// Closes the investment
fn close(self: @T, tokenAddress_: ContractAddress, amount_: u256) -> ContractAddress;
fn close(ref self: T, tokenAddress_: ContractAddress, amount_: u256);
// Returns the manager address
fn getManager(self: @T) -> ContractAddress;
}

#[starknet::contract]
Expand All @@ -52,8 +57,7 @@ mod QwManager {
// *************************************************************************
#[storage]
struct Storage {
registry: IRegistryDispatcher,
whitelist: LegacyMap<ContractAddress, bool>
registry: IRegistryDispatcher
}

// *************************************************************************
Expand All @@ -80,6 +84,9 @@ mod QwManager {
// *************************************************************************
#[abi(embed_v0)]
impl QwManager of super::IManager<ContractState> {
fn getRegistry(self: @ContractState) -> ContractAddress {
self.registry.read().contract_address
}
fn execute(ref self: ContractState, targetQwChilds_: Array<ContractAddress>, tokenAddress_: Array<ContractAddress>, amount_: Array<u128>) {
assert(targetQwChilds_.len() == tokenAddress_.len(), Errors::Invalid_Input_Length);
assert(targetQwChilds_.len() == amount_.len(), Errors::Invalid_Input_Length);
Expand Down
9 changes: 6 additions & 3 deletions src/qwRegistry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ trait IRegistry<T> {
fn getIsChildWhitelisted(self: @T, child_: ContractAddress) -> bool;
// Registers the child contract
fn registerChild(ref self: T, child_: ContractAddress);
// Updates the manager address
fn updateManager(ref self: T, manager_: ContractAddress);
}

#[starknet::interface]
Expand Down Expand Up @@ -56,15 +58,16 @@ mod QwRegistry {
// CONSTRUCTOR
// *************************************************************************
#[constructor]
fn constructor(ref self: ContractState, manager_: ContractAddress) {
self.manager.write(manager_);
}
fn constructor(ref self: ContractState) { }

// *************************************************************************
// EXTERNAL FUNCTIONS
// *************************************************************************
#[abi(embed_v0)]
impl QwRegistry of super::IRegistry<ContractState> {
fn updateManager(ref self: ContractState, manager_: ContractAddress) {
self.manager.write(manager_);
}
fn getManager(self: @ContractState) -> ContractAddress {
self.manager.read()
}
Expand Down

0 comments on commit b78a4cb

Please sign in to comment.