Skip to content

Commit

Permalink
Merge branch 'add-swaplace-contract' of https://github.com/dpinones/s…
Browse files Browse the repository at this point in the history
…waplace-cairo into add-swaplace-contract
  • Loading branch information
dubzn committed Jan 16, 2024
2 parents 024bf0e + 872794c commit ce837e2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ version = "0.1.0"
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.14.0" }
starknet = "2.4.0"
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.14.0" }
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.8.0" }

[[target.starknet-contract]]
casm = true
casm = true
5 changes: 5 additions & 0 deletions src/mocks/MockERC20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ trait IMockERC20<TContractState> {
fn transfer_from(
ref self: TContractState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
fn balance_of(self: @TContractState, account: ContractAddress) -> u256;
}

#[starknet::contract]
Expand Down Expand Up @@ -63,5 +64,9 @@ mod MockERC20 {
) -> bool {
self.erc20.transfer_from(sender, recipient, amount)
}

fn balance_of(self: @ContractState, account: ContractAddress) -> u256 {
self.erc20.balance_of(account)
}
}
}
5 changes: 5 additions & 0 deletions src/mocks/MockERC721.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ trait IMockERC721<TContractState> {
fn transfer_from(
ref self: TContractState, from: ContractAddress, to: ContractAddress, token_id: u256
);
fn owner_of(self: @TContractState, token_id: u256) -> ContractAddress;
}

#[starknet::contract]
Expand Down Expand Up @@ -78,5 +79,9 @@ mod MockERC721 {
);
self.erc721._transfer(from, to, token_id);
}

fn owner_of(self: @ContractState, token_id: u256) -> ContractAddress {
self.erc721.owner_of(token_id)
}
}
}
61 changes: 44 additions & 17 deletions src/tests/test_swaplace.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,25 @@ mod SwaplaceTests {
fn test_should_be_able_to_accept_swap_as_1_1_swap() {
let (swap, biding, asking, swaplace, mock_erc20, mock_erc721) = before_each();

let mut erc20_balance = mock_erc20.balance_of(ACCEPTEE());
let mut token_1_owner = mock_erc721.owner_of(1);

assert(erc20_balance == 1000, 'err wrong accepte balance');
assert(token_1_owner == OWNER(), 'err wrong owner');

start_prank(CheatTarget::One(swaplace.contract_address), OWNER());
let swap_id = swaplace.create_swap(swap, biding, asking);
stop_prank(CheatTarget::One(swaplace.contract_address));

start_prank(CheatTarget::One(swaplace.contract_address), ACCEPTEE());
swaplace.accept_swap(swap_id);
stop_prank(CheatTarget::One(swaplace.contract_address));
let swap_result = swaplace.get_swap(swap_id);
assert(swap_result.expiry == 0, 'err expiry');

erc20_balance = mock_erc20.balance_of(OWNER());
token_1_owner = mock_erc721.owner_of(1);

assert(erc20_balance == 1000, 'err wrong owner balance');
assert(token_1_owner == ACCEPTEE(), 'err wrong owner');
}

#[test]
Expand Down Expand Up @@ -498,6 +508,16 @@ mod SwaplaceTests {
swap.biding_count = 2;
swap.asking_count = 2;

let mut erc20_balance_owner = mock_erc20.balance_of(OWNER());
let mut erc20_balance_acceptee = mock_erc20.balance_of(ACCEPTEE());
let mut owner_of_nft_1 = mock_erc721.owner_of(1);
let mut owner_of_nft_5_ = mock_erc721.owner_of(5);

assert(erc20_balance_owner == 500, 'err wrong owner balance');
assert(erc20_balance_acceptee == 1000, 'err wrong accepte balance');
assert(owner_of_nft_1 == OWNER(), 'err wrong nft 1 owner');
assert(owner_of_nft_5_ == ACCEPTEE(), 'err wrong nft 5 owner');

start_prank(CheatTarget::One(swaplace.contract_address), OWNER());
let swap_id = swaplace.create_swap(swap, biding.span(), asking.span());
stop_prank(CheatTarget::One(swaplace.contract_address));
Expand All @@ -511,30 +531,32 @@ mod SwaplaceTests {
swaplace.accept_swap(swap_id);
stop_prank(CheatTarget::One(swaplace.contract_address));

let swap_result = swaplace.get_swap(swap_id);
assert(swap_result.expiry == 0, 'err expiry');
erc20_balance_owner = mock_erc20.balance_of(OWNER());
erc20_balance_acceptee = mock_erc20.balance_of(ACCEPTEE());
owner_of_nft_1 = mock_erc721.owner_of(1);
owner_of_nft_5_ = mock_erc721.owner_of(5);

assert(erc20_balance_owner == 1000, 'err wrong owner balance');
assert(erc20_balance_acceptee == 500, 'err wrong accepte balance');
assert(owner_of_nft_1 == ACCEPTEE(), 'err wrong nft 1 owner');
assert(owner_of_nft_5_ == OWNER(), 'err wrong nft 5 owner');
}

#[test]
fn test_should_be_able_to_accept_swap_as_P2P_swap() {
let (_, _, _, swaplace, mock_erc20, mock_erc721) = before_each();

mock_erc20.mint_to(OWNER(), 1000);
mock_erc721.mint_to(ACCEPTEE(), 10);

start_prank(CheatTarget::One(mock_erc20.contract_address), OWNER());
mock_erc20.approve(swaplace.contract_address, 1000);
stop_prank(CheatTarget::One(mock_erc20.contract_address));

start_prank(CheatTarget::One(mock_erc721.contract_address), ACCEPTEE());
mock_erc721.approve(swaplace.contract_address, 10);
stop_prank(CheatTarget::One(mock_erc721.contract_address));

let (mut swap, biding, asking) = mock_swap(
mock_erc20.contract_address, mock_erc721.contract_address
);
swap.allowed = ACCEPTEE();

let erc20_balance = mock_erc20.balance_of(ACCEPTEE());
let mut token_1_owner = mock_erc721.owner_of(1);

assert(erc20_balance == 1000, 'err wrong accepte balance');
assert(token_1_owner == OWNER(), 'err wrong owner');

start_prank(CheatTarget::One(swaplace.contract_address), OWNER());
let swap_id = swaplace.create_swap(swap, biding, asking);
stop_prank(CheatTarget::One(swaplace.contract_address));
Expand All @@ -548,8 +570,13 @@ mod SwaplaceTests {
swaplace.accept_swap(swap_id);
stop_prank(CheatTarget::One(swaplace.contract_address));

let swap_result = swaplace.get_swap(swap_id);
assert(swap_result.expiry == 0, 'err expiry');
let erc20_balance_owner = mock_erc20.balance_of(OWNER());
let erc20_balance_acceptee = mock_erc20.balance_of(ACCEPTEE());
token_1_owner = mock_erc721.owner_of(1);

assert(erc20_balance_owner == 50, 'err wrong owner balance');
assert(erc20_balance_acceptee == 950, 'err wrong acceptee balance');
assert(token_1_owner == ACCEPTEE(), 'err wrong owner');
}
}

Expand Down

0 comments on commit ce837e2

Please sign in to comment.