Skip to content

Commit

Permalink
added test for transfer manager erc1155
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Jan 11, 2024
1 parent 9d84269 commit 06a05e8
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [push, pull_request]

env:
SCARB_VERSION: 2.4.0
SNFORGE_VERSION: 0.13.0
SNFORGE_VERSION: 0.13.1

jobs:
check:
Expand Down
7 changes: 6 additions & 1 deletion flex_marketplace/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use core::fmt::{Display, Error, Formatter, Debug};
use starknet::contract_address_to_felt252;

impl DisplayContractAddress of Display<starknet::ContractAddress> {
fn fmt(self: @starknet::ContractAddress, ref f: Formatter) -> Result<(), Error> {
write!(f, "{}", *self)
write!(f, "{}", contract_address_to_felt252(*self))
}
}

Expand Down Expand Up @@ -49,3 +50,7 @@ mod marketplace {
mod transfer_manager_ERC1155;
mod transfer_selector_NFT;
}

mod mocks {
mod erc1155;
}
26 changes: 10 additions & 16 deletions flex_marketplace/src/marketplace/transfer_manager_ERC1155.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,14 @@ trait IERC1155TransferManager<TState> {
fn upgrade(ref self: TState, impl_hash: ClassHash);
}

#[starknet::interface]
trait IERC1155<TContractState> {
fn safe_transfer_from(
ref self: TContractState,
from: ContractAddress,
to: ContractAddress,
id: u256,
amount: u128,
data: Span<felt252>
);
}

#[starknet::contract]
mod ERC1155TransferManager {
use starknet::{ContractAddress, contract_address_const};
use starknet::{ContractAddress, contract_address_const, get_caller_address};

use super::ClassHash;
use super::{IERC1155Dispatcher, IERC1155DispatcherTrait};
use starknet::get_caller_address;
use flex::{DebugContractAddress, DisplayContractAddress};
use flex::mocks::erc1155::{IERC1155Dispatcher, IERC1155DispatcherTrait};

use openzeppelin::access::ownable::OwnableComponent;
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
#[abi(embed_v0)]
Expand Down Expand Up @@ -84,7 +74,11 @@ mod ERC1155TransferManager {
) {
let caller: ContractAddress = get_caller_address();
let marketplace: ContractAddress = self.get_marketplace();
assert(caller == marketplace, 'caller is not marketplace');
assert!(
caller == marketplace,
"ERC1155TransferManager: caller {} is not marketplace",
caller
);
IERC1155Dispatcher { contract_address: collection }
.safe_transfer_from(from, to, token_id, amount, data);
}
Expand Down
30 changes: 30 additions & 0 deletions flex_marketplace/src/mocks/erc1155.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#[starknet::interface]
trait IERC1155<TContractState> {
fn safe_transfer_from(
ref self: TContractState,
from: starknet::ContractAddress,
to: starknet::ContractAddress,
id: u256,
amount: u128,
data: Span<felt252>
);
}

#[starknet::contract]
mod ERC1155 {
#[storage]
struct Storage {}

#[external(v0)]
impl ERC1155Impl of super::IERC1155<ContractState> {
fn safe_transfer_from(
ref self: ContractState,
from: starknet::ContractAddress,
to: starknet::ContractAddress,
id: u256,
amount: u128,
data: Span<felt252>
) {}
}
}

52 changes: 39 additions & 13 deletions flex_marketplace/tests/transfer_manager_erc1155_test.cairo
Original file line number Diff line number Diff line change
@@ -1,32 +1,58 @@
use tests::utils::{setup, initialize_test};
use snforge_std::{start_prank, stop_prank, PrintTrait, CheatTarget};

use flex::marketplace::transfer_manager_ERC1155::{
IERC1155TransferManagerDispatcher, IERC1155TransferManagerDispatcherTrait
};
use tests::utils::{
setup, initialize_test, ACCOUNT1, ACCOUNT2, OWNER, ZERO_ADDRESS, deploy_mock_1155
};

const TOKEN_ID: u256 = 1;

#[test]
fn test_transfer_non_fungible_token_success() {
let dsp = setup();
initialize_test(dsp);
// TODO
let collection = deploy_mock_1155();

start_prank(
CheatTarget::One(dsp.transfer_manager_erc1155.contract_address),
dsp.marketplace.contract_address
);
dsp
.transfer_manager_erc1155
.transfer_non_fungible_token(
collection, ACCOUNT1(), ACCOUNT2(), TOKEN_ID, 1, array![].span()
);
}

#[test]
#[should_panic()]
#[should_panic(expected: ("ERC1155TransferManager: caller 0 is not marketplace",))]
fn test_transfer_non_fungible_token_fails_caller_not_marketplace() {
let dsp = setup();
initialize_test(dsp);
assert(false, '');
// TODO
let collection = deploy_mock_1155();

start_prank(CheatTarget::One(dsp.transfer_manager_erc1155.contract_address), ZERO_ADDRESS());
dsp
.transfer_manager_erc1155
.transfer_non_fungible_token(
collection, ACCOUNT1(), ACCOUNT2(), TOKEN_ID, 1, array![].span()
);
}

#[test]
fn test_update_marketplace_success() {
let dsp = setup();
initialize_test(dsp);
// TODO
}
let collection = deploy_mock_1155();
let new_marketplace = starknet::contract_address_const::<'new_marketplace'>();

// TESTS VIEWS
#[test]
fn test_get_marketplace() {
let dsp = setup();
initialize_test(dsp);
// TODO
start_prank(CheatTarget::One(dsp.transfer_manager_erc1155.contract_address), OWNER());
dsp.transfer_manager_erc1155.update_marketplace(new_marketplace);

let actual_marketplace = dsp.transfer_manager_erc1155.get_marketplace();

assert(actual_marketplace == new_marketplace, 'update marketplace failed');
}

10 changes: 10 additions & 0 deletions flex_marketplace/tests/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ use flex::marketplace::{
},
};

use flex::mocks::erc1155::ERC1155;

const HASH_DOMAIN: felt252 = 'HASH_DOMAIN';
const FEE_LIMIT: u128 = 1_000;

Expand Down Expand Up @@ -71,6 +73,9 @@ fn ACCOUNT4() -> ContractAddress {
fn PROXY_ADMIN() -> ContractAddress {
contract_address_const::<'PROXY_ADMIN'>()
}
fn ZERO_ADDRESS() -> ContractAddress {
contract_address_const::<0>()
}

fn setup() -> Dispatchers {
let contract = declare('MarketPlace');
Expand Down Expand Up @@ -155,6 +160,11 @@ fn initialize_test(dsp: Dispatchers) {
dsp.transfer_manager_erc1155.initializer(dsp.marketplace.contract_address, OWNER());
}

fn deploy_mock_1155() -> ContractAddress {
let contract = declare('ERC1155');
contract.deploy(@array![]).expect('failed marketplace')
}

#[test]
fn deploy_test() {
let dsp = setup();
Expand Down

0 comments on commit 06a05e8

Please sign in to comment.