Skip to content

Commit

Permalink
erc20 components wip
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed Dec 6, 2023
1 parent 4b75394 commit 1051b41
Show file tree
Hide file tree
Showing 13 changed files with 735 additions and 158 deletions.
13 changes: 13 additions & 0 deletions token/src/components/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
mod mocks {
mod initializable_mock;
mod erc20_allowance_mock;
mod erc20_balance_mock;
mod erc20_metadata_mock;
}

mod security {
#[cfg(test)]
mod test_initializable;
}

mod token {
#[cfg(test)]
mod test_erc20_allowance;
#[cfg(test)]
mod test_erc20_balance;
#[cfg(test)]
mod test_erc20_metadata;
}
31 changes: 31 additions & 0 deletions token/src/components/tests/mocks/erc20_allowance_mock.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[dojo::contract]
mod ERC20AllowanceMock {
use token::components::token::erc20_allowance::ERC20AllowanceComponent;

component!(path: ERC20AllowanceComponent, storage: erc20_allowance, event: ERC20AllowanceEvent);

#[abi(embed_v0)]
impl ERC20AllowanceImpl =
ERC20AllowanceComponent::ERC20AllowanceImpl<ContractState>;
#[abi(embed_v0)]
impl ERC20SafeAllowanceImpl =
ERC20AllowanceComponent::ERC20SafeAllowanceImpl<ContractState>;
#[abi(embed_v0)]
impl ERC20SafeAllowanceCamelImpl =
ERC20AllowanceComponent::ERC20SafeAllowanceCamelImpl<ContractState>;

impl ERC20AllowanceInternalImpl = ERC20AllowanceComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
erc20_allowance: ERC20AllowanceComponent::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
ERC20AllowanceEvent: ERC20AllowanceComponent::Event
}
}
24 changes: 24 additions & 0 deletions token/src/components/tests/mocks/erc20_balance_mock.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#[dojo::contract]
mod ERC20BalanceMock {
use token::components::token::erc20_balance::ERC20BalanceComponent;

component!(path: ERC20BalanceComponent, storage: erc20_balance, event: ERC20BalanceEvent);

#[abi(embed_v0)]
impl ERC20BalanceImpl = ERC20BalanceComponent::ERC20BalanceImpl<ContractState>;

impl ERC20BalanceInternalImpl = ERC20BalanceComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
erc20_balance: ERC20BalanceComponent::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
ERC20BalanceEvent: ERC20BalanceComponent::Event
}
}
25 changes: 25 additions & 0 deletions token/src/components/tests/mocks/erc20_metadata_mock.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[dojo::contract]
mod ERC20MetadataMock {
use token::components::token::erc20_metadata::ERC20MetadataComponent;

component!(path: ERC20MetadataComponent, storage: erc20_metadata, event: ERC20MetadataEvent);

#[abi(embed_v0)]
impl ERC20MetadataImpl =
ERC20MetadataComponent::ERC20MetadataImpl<ContractState>;

impl ERC20MetadataInternalImpl = ERC20MetadataComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
erc20_metadata: ERC20MetadataComponent::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
ERC20MetadataEvent: ERC20MetadataComponent::Event
}
}
28 changes: 28 additions & 0 deletions token/src/components/tests/token/test_erc20_allowance.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use integer::BoundedInt;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use dojo::test_utils::spawn_test_world;
use token::tests::constants::{ADMIN, OWNER, SPENDER};

use token::components::token::erc20_allowance::{erc_20_allowance_model, ERC20AllowanceModel,};
use token::components::token::erc20_allowance::ERC20AllowanceComponent::{
ERC20AllowanceImpl, InternalImpl
};
use token::components::tests::mocks::erc20_allowance_mock::ERC20AllowanceMock;
use token::components::tests::mocks::erc20_allowance_mock::ERC20AllowanceMock::world_dispatcherContractMemberStateTrait;


fn STATE() -> (IWorldDispatcher, ERC20AllowanceMock::ContractState) {
let world = spawn_test_world(array![erc_20_allowance_model::TEST_CLASS_HASH,]);

let mut state = ERC20AllowanceMock::contract_state_for_testing();
state.world_dispatcher.write(world);

(world, state)
}

#[test]
#[available_gas(100000000)]
fn test_erc20_allowance_initialize() {
let (world, mut state) = STATE();
// state.erc20_allowance
}
101 changes: 101 additions & 0 deletions token/src/components/tests/token/test_erc20_balance.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use integer::BoundedInt;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use dojo::test_utils::spawn_test_world;
use token::tests::constants::{ADMIN, ZERO, OWNER, OTHER};

use token::components::token::erc20_balance::{erc_20_balance_model, ERC20BalanceModel,};
use token::components::token::erc20_balance::ERC20BalanceComponent::{
ERC20BalanceImpl, InternalImpl
};
use token::components::tests::mocks::erc20_balance_mock::ERC20BalanceMock;
use token::components::tests::mocks::erc20_balance_mock::ERC20BalanceMock::world_dispatcherContractMemberStateTrait;


fn STATE() -> (IWorldDispatcher, ERC20BalanceMock::ContractState) {
let world = spawn_test_world(array![erc_20_balance_model::TEST_CLASS_HASH,]);

let mut state = ERC20BalanceMock::contract_state_for_testing();
state.world_dispatcher.write(world);

(world, state)
}

#[test]
#[available_gas(100000000)]
fn test_erc20_balance_initialize() {
let (world, mut state) = STATE();

assert(state.erc20_balance.balance_of(ADMIN()) == 0, 'Should be 0');
assert(state.erc20_balance.balance_of(OWNER()) == 0, 'Should be 0');
assert(state.erc20_balance.balance_of(OTHER()) == 0, 'Should be 0');
}

#[test]
#[available_gas(100000000)]
fn test_erc20_balance__update_balance() {
let (world, mut state) = STATE();

state.erc20_balance._update_balance(ZERO(), 0, 420);
assert(state.erc20_balance.balance_of(ZERO()) == 420, 'Should be 420');

state.erc20_balance._update_balance(ZERO(), 0, 1000);
assert(state.erc20_balance.balance_of(ZERO()) == 1420, 'Should be 1420');

state.erc20_balance._update_balance(ZERO(), 420, 0);
assert(state.erc20_balance.balance_of(ZERO()) == 1000, 'Should be 1000');
}

#[test]
#[available_gas(10000000)]
#[should_panic(expected: ('u256_sub Overflow',))]
fn test_erc20_balance__update_balance_sub_overflow() {
let (world, mut state) = STATE();

state.erc20_balance._update_balance(ZERO(), 1, 0);
}

#[test]
#[available_gas(10000000)]
#[should_panic(expected: ('u256_add Overflow',))]
fn test_erc20_balance__update_balance_add_overflow() {
let (world, mut state) = STATE();

state.erc20_balance._update_balance(ZERO(), 0, BoundedInt::max());
state.erc20_balance._update_balance(ZERO(), 0, 1);
}


#[test]
#[available_gas(100000000)]
fn test_erc20_balance__transfer() {
let (world, mut state) = STATE();

state.erc20_balance._update_balance(ADMIN(), 0, 420);
state.erc20_balance._update_balance(OTHER(), 0, 1000);

state.erc20_balance._transfer(ADMIN(), OTHER(), 100);
assert(state.erc20_balance.balance_of(ADMIN()) == 320, 'Should be 320');
assert(state.erc20_balance.balance_of(OTHER()) == 1100, 'Should be 1100');

state.erc20_balance._transfer(OTHER(), ADMIN(), 1000);
assert(state.erc20_balance.balance_of(ADMIN()) == 1320, 'Should be 1320');
assert(state.erc20_balance.balance_of(OTHER()) == 100, 'Should be 100');
}

#[test]
#[available_gas(100000000)]
#[should_panic(expected: ('ERC20: transfer from 0',))]
fn test_erc20_balance__transfer_from_zero() {
let (world, mut state) = STATE();

state.erc20_balance._transfer(ZERO(), ADMIN(), 420);
}

#[test]
#[available_gas(100000000)]
#[should_panic(expected: ('ERC20: transfer to 0',))]
fn test_erc20_balance__transfer_to_zero() {
let (world, mut state) = STATE();

state.erc20_balance._transfer(ADMIN(), ZERO(), 420);
}
70 changes: 70 additions & 0 deletions token/src/components/tests/token/test_erc20_metadata.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use integer::BoundedInt;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use dojo::test_utils::spawn_test_world;
use token::tests::constants::{NAME, SYMBOL, DECIMALS};

use token::components::token::erc20_metadata::{erc_20_metadata_model, ERC20MetadataModel,};
use token::components::token::erc20_metadata::ERC20MetadataComponent::{
ERC20MetadataImpl, InternalImpl
};
use token::components::tests::mocks::erc20_metadata_mock::ERC20MetadataMock;
use token::components::tests::mocks::erc20_metadata_mock::ERC20MetadataMock::world_dispatcherContractMemberStateTrait;


fn STATE() -> (IWorldDispatcher, ERC20MetadataMock::ContractState) {
let world = spawn_test_world(array![erc_20_metadata_model::TEST_CLASS_HASH,]);

let mut state = ERC20MetadataMock::contract_state_for_testing();
state.world_dispatcher.write(world);

(world, state)
}

#[test]
#[available_gas(100000000)]
fn test_erc20_metadata__initialize() {
let (world, mut state) = STATE();

state.erc20_metadata._initialize(NAME, SYMBOL, DECIMALS);

assert(state.erc20_metadata.name() == NAME, 'Should be NAME');
assert(state.erc20_metadata.symbol() == SYMBOL, 'Should be SYMBOL');
assert(state.erc20_metadata.decimals() == DECIMALS, 'Should be 18');
assert(state.erc20_metadata.total_supply() == 0, 'Should be 0');
}

#[test]
#[available_gas(100000000)]
fn test_erc20_metadata__update_total_supply() {
let (world, mut state) = STATE();

state.erc20_metadata._update_total_supply(0, 420);
assert(state.erc20_metadata.total_supply() == 420, 'Should be 420');

state.erc20_metadata._update_total_supply(0, 1000);
assert(state.erc20_metadata.total_supply() == 1420, 'Should be 1420');

state.erc20_metadata._update_total_supply(420, 0);
assert(state.erc20_metadata.total_supply() == 1000, 'Should be 1000');
}


#[test]
#[available_gas(10000000)]
#[should_panic(expected: ('u256_sub Overflow',))]
fn test_erc20_metadata__update_total_supply_sub_overflow() {
let (world, mut state) = STATE();

state.erc20_metadata._update_total_supply(1, 0);
}


#[test]
#[available_gas(10000000)]
#[should_panic(expected: ('u256_add Overflow',))]
fn test_erc20_metadata__update_total_supply_add_overflow() {
let (world, mut state) = STATE();

state.erc20_metadata._update_total_supply(0, BoundedInt::max());
state.erc20_metadata._update_total_supply(0, 1);
}
Loading

0 comments on commit 1051b41

Please sign in to comment.