Skip to content

Commit

Permalink
fix: place/update transfer tests under starknet_ibc_presets
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani committed Aug 30, 2024
1 parent ecd44f1 commit f8f96b5
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 149 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ corelib/
target/
**/Cargo.lock

# snforge
.snfoundry_cache/
.snfoundry_trace/
.snfoundry_versioned_programs/

.env

# vscode
Expand Down
2 changes: 1 addition & 1 deletion cairo-contracts/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ dependencies = [
"starknet_ibc_app_transfer",
"starknet_ibc_client_cometbft",
"starknet_ibc_core_client",
"starknet_ibc_testing",
"starknet_ibc_utils",
]

Expand All @@ -137,7 +138,6 @@ dependencies = [
"snforge_std",
"starknet_ibc_app_transfer",
"starknet_ibc_core_client",
"starknet_ibc_presets",
"starknet_ibc_utils",
]

Expand Down
3 changes: 2 additions & 1 deletion cairo-contracts/apps/ics20-transfer/src/component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,8 @@ pub mod ICS20TransferComponent {
address: ContractAddress,
initial_supply: u256,
) {
self.emit(CreateTokenEvent { name, symbol, address, initial_supply });
let event = CreateTokenEvent { name, symbol, address, initial_supply };
self.emit(event);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions cairo-contracts/presets/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ starknet_ibc_core_client = { workspace = true }
starknet_ibc_client_cometbft = { workspace = true }
starknet_ibc_app_transfer = { workspace = true }

[dev-dependencies]
starknet_ibc_testing = { workspace = true }

[lib]

[tool]
Expand Down
2 changes: 1 addition & 1 deletion cairo-contracts/presets/src/apps/transfer.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[starknet::contract]
pub(crate) mod TransferApp {
pub mod TransferApp {
use starknet::ClassHash;
use starknet_ibc_app_transfer::ICS20TransferComponent;
use starknet_ibc_app_transfer::transferrable::TransferrableComponent;
Expand Down
2 changes: 1 addition & 1 deletion cairo-contracts/presets/src/clients/cometbft.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[starknet::contract]
pub(crate) mod CometClient {
pub mod CometClient {
use starknet_ibc_client_cometbft::CometClientComponent;
use starknet_ibc_utils::governance::IBCGovernanceComponent;

Expand Down
2 changes: 1 addition & 1 deletion cairo-contracts/presets/src/core/ibc.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[starknet::contract]
pub(crate) mod IBC {
pub mod IBC {
use starknet_ibc_core_client::ClientHandlerComponent;

component!(path: ClientHandlerComponent, storage: client, event: ClientHandlerEvent);
Expand Down
2 changes: 1 addition & 1 deletion cairo-contracts/presets/src/erc20.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[starknet::contract]
pub(crate) mod ERC20Mintable {
pub mod ERC20Mintable {
use openzeppelin_access::ownable::OwnableComponent;
use openzeppelin_token::erc20::{ERC20Component, ERC20HooksEmptyImpl};
use starknet::{ContractAddress, ClassHash};
Expand Down
1 change: 1 addition & 0 deletions cairo-contracts/presets/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod apps;
mod clients;
mod core;
mod erc20;
mod tests;

pub use apps::transfer::TransferApp;
pub use clients::cometbft::CometClient;
Expand Down
4 changes: 4 additions & 0 deletions cairo-contracts/presets/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod mocks;

#[cfg(test)]
mod test_transfer;
1 change: 1 addition & 0 deletions cairo-contracts/presets/src/tests/mocks.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod transfer_mock;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// granting external access to all internal validation and execution functions
// for testing purposes.
#[starknet::contract]
pub(crate) mod TransferMock {
pub(crate) mod MockTransferApp {
use starknet::ClassHash;
use starknet::ContractAddress;
use starknet_ibc_app_transfer::ICS20TransferComponent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
use openzeppelin_testing::EventSpyExt;
use core::traits::TryInto;
use starknet::ContractAddress;
use openzeppelin_testing::declare_class;
use starknet_ibc_app_transfer::ERC20Contract;
use starknet_ibc_testing::config::TestConfigTrait;
use starknet_ibc_testing::constants::{SUPPLY, OWNER, COSMOS, STARKNET};
use starknet_ibc_testing::setup::{ERC20ContractTrait, ICS20TransferContractTrait};
use starknet_ibc_testing::constants::{NAME, SYMBOL, SUPPLY, OWNER, COSMOS, STARKNET};
use starknet_ibc_testing::setup::{ERC20ContractTrait, TransferAppHandlerTrait};
use starknet_ibc_utils::ComputeKeyTrait;
use snforge_std::{start_cheat_caller_address, EventSpyTrait, spy_events};

#[test]
fn test_escrow_unescrow_roundtrip() {
// -----------------------------------------------------------
// Setup Contracts
// -----------------------------------------------------------

// Declare the ERC20 contract class.
let erc20_contract_class = declare_class("ERC20Mintable");

// Deploy an ERC20 contract.
let erc20 = ERC20ContractTrait::setup();
let mut erc20 = ERC20ContractTrait::setup(erc20_contract_class);

// Deploy an ICS20 Token Transfer contract.
let ics20 = ICS20TransferContractTrait::setup();
let mut ics20 = TransferAppHandlerTrait::setup(erc20_contract_class);

let mut cfg = TestConfigTrait::default();
cfg.set_native_denom(erc20.contract_address);

cfg.set_native_denom(erc20.address);

// -----------------------------------------------------------
// Escrow
Expand All @@ -33,10 +41,10 @@ fn test_escrow_unescrow_roundtrip() {
ics20.send_execute(msg_transfer);

// Assert the `SendEvent` emitted.
let event = ics20.assert_send_event();
ics20.assert_send_event(STARKNET(), COSMOS(), cfg.native_denom.clone(), cfg.amount);

// Check the balance of the sender.
erc20.assert_balance(event.sender.try_into().unwrap(), SUPPLY - cfg.amount);
erc20.assert_balance(OWNER(), SUPPLY - cfg.amount);

// Check the balance of the `Transfer` contract.
erc20.assert_balance(ics20.contract_address, cfg.amount);
Expand All @@ -45,20 +53,22 @@ fn test_escrow_unescrow_roundtrip() {
// Unescrow
// -----------------------------------------------------------

cfg.prefix_native_denom();
ics20.drop_all_events();

let prefixed_denom = cfg.prefix_native_denom();

let recv_packet = cfg.dummy_recv_packet(cfg.native_denom, COSMOS(), STARKNET());
let recv_packet = cfg.dummy_recv_packet(prefixed_denom.clone(), COSMOS(), STARKNET());

// Submit a `RecvPacket` to the `Transfer` contract.
ics20.recv_execute(recv_packet);

// Assert the `RecvEvent` emitted.
let event = ics20.assert_recv_event();
ics20.assert_recv_event(COSMOS(), STARKNET(), prefixed_denom, cfg.amount, true);

erc20.assert_balance(ics20.contract_address, 0);

// Check the balance of the recipient.
erc20.assert_balance(event.receiver.try_into().unwrap(), SUPPLY);
erc20.assert_balance(OWNER(), SUPPLY);
}

#[test]
Expand All @@ -67,8 +77,11 @@ fn test_mint_burn_roundtrip() {
// Setup Contracts
// -----------------------------------------------------------

// Declare the ERC20 contract class.
let erc20_contract_class = declare_class("ERC20Mintable");

// Deploy an ICS20 Token Transfer contract.
let ics20 = ICS20TransferContractTrait::setup();
let mut ics20 = TransferAppHandlerTrait::setup(erc20_contract_class);

let mut cfg = TestConfigTrait::default();

Expand All @@ -81,26 +94,29 @@ fn test_mint_burn_roundtrip() {
// Submit a `RecvPacket`, which will create a new ERC20 contract.
ics20.recv_execute(recv_packet.clone());

let prefixed_denom = cfg.prefix_hosted_denom();

// Fetch the token address.
let token_address = ics20.ibc_token_address(prefixed_denom.compute_key()).unwrap();

// Assert the `CreateTokenEvent` emitted.
ics20.assert_create_token_event();
ics20.assert_create_token_event(NAME(), SYMBOL(), token_address, cfg.amount);

// Assert the `RecvEvent` emitted.
ics20.assert_recv_event();
ics20.assert_recv_event(COSMOS(), STARKNET(), prefixed_denom.clone(), cfg.amount, true);

ics20.drop_all_events();

// Submit another `RecvPacket`, which will mint the amount of tokens.
ics20.recv_execute(recv_packet);

// Assert the `RecvEvent` emitted.
let event = ics20.assert_recv_event();
ics20.assert_recv_event(COSMOS(), STARKNET(), prefixed_denom.clone(), cfg.amount, true);

cfg.prefix_hosted_denom();
let erc20: ERC20Contract = token_address.into();

// Check the balance of the receiver.
let token_address = ics20.ibc_token_address(cfg.hosted_denom.compute_key()).unwrap();

let erc20 = ERC20ContractTrait::setup_with_addr(token_address);

erc20.assert_balance(event.receiver.try_into().unwrap(), cfg.amount * 2);
erc20.assert_balance(OWNER(), cfg.amount * 2);

// Check the total supply of the ERC20 contract.
erc20.assert_total_supply(cfg.amount * 2);
Expand All @@ -109,18 +125,20 @@ fn test_mint_burn_roundtrip() {
// Burn
// -----------------------------------------------------------

let msg_transfer = cfg.dummy_msg_transder(cfg.hosted_denom, STARKNET(), COSMOS());
ics20.drop_all_events();

// Owner approves the amount of allowance for the `Transfer` contract.
let msg_transfer = cfg.dummy_msg_transder(prefixed_denom.clone(), STARKNET(), COSMOS());

// Owner approves the amount of allowance for the `TransferApp` contract.
ics20.send_execute(msg_transfer);

// Assert the `SendEvent` emitted.
let event = ics20.assert_send_event();
ics20.assert_send_event(STARKNET(), COSMOS(), prefixed_denom, cfg.amount);

// Check the balance of the sender.
erc20.assert_balance(event.sender.try_into().unwrap(), cfg.amount);
erc20.assert_balance(OWNER(), cfg.amount);

// Check the balance of the `Transfer` contract.
// Check the balance of the `TransferApp` contract.
erc20.assert_balance(ics20.contract_address, 0);

// Chekck the total supply of the ERC20 contract.
Expand Down
3 changes: 1 addition & 2 deletions cairo-contracts/testing/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ openzeppelin_utils = { workspace = true }
starknet_ibc_utils = { workspace = true }
starknet_ibc_core_client = { workspace = true }
starknet_ibc_app_transfer = { workspace = true }
starknet_ibc_presets = { workspace = true }

[dev-dependencies]
cairo_test = { workspace = true }
snforge_std = { workspace = true }
cairo_test = { workspace = true }

[lib]

Expand Down
36 changes: 15 additions & 21 deletions cairo-contracts/testing/src/config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,8 @@ pub struct TestConfig {
pub amount: u256,
}

pub trait TestConfigTrait {
fn default() -> TestConfig;
fn set_native_denom(ref self: TestConfig, native_token_address: ContractAddress);
fn prefix_native_denom(ref self: TestConfig);
fn prefix_hosted_denom(ref self: TestConfig);
fn dummy_msg_transder(
self: @TestConfig, denom: PrefixedDenom, sender: Participant, receiver: Participant
) -> MsgTransfer;
fn dummy_recv_packet(
self: @TestConfig, denom: PrefixedDenom, sender: Participant, receiver: Participant
) -> Packet;
fn dummy_packet_data(
self: @TestConfig, denom: PrefixedDenom, sender: Participant, receiver: Participant
) -> PacketData;
}

impl TestConfigImpl of TestConfigTrait {
#[generate_trait]
pub impl TestConfigImpl of TestConfigTrait {
fn default() -> TestConfig {
let native_denom = PrefixedDenom {
trace_path: array![], base: Denom::Native(PUBKEY().into())
Expand All @@ -61,20 +46,29 @@ impl TestConfigImpl of TestConfigTrait {
}


fn prefix_native_denom(ref self: TestConfig) {
fn prefix_native_denom(self: @TestConfig) -> PrefixedDenom {
let trace_prefix = TracePrefixTrait::new(
PortId { port_id: TRANSFER_PORT_ID() },
ChannelId { channel_id: self.chan_id_on_a.clone() }
);
self.native_denom.add_prefix(trace_prefix);
let mut native_denom = self.native_denom.clone();

native_denom.add_prefix(trace_prefix);

native_denom
}

fn prefix_hosted_denom(ref self: TestConfig) {
fn prefix_hosted_denom(self: @TestConfig) -> PrefixedDenom {
let trace_prefix = TracePrefixTrait::new(
PortId { port_id: TRANSFER_PORT_ID() },
ChannelId { channel_id: self.chan_id_on_b.clone() }
);
self.hosted_denom.add_prefix(trace_prefix);

let mut hosted_denom = self.hosted_denom.clone();

hosted_denom.add_prefix(trace_prefix);

hosted_denom
}

fn dummy_msg_transder(
Expand Down
20 changes: 10 additions & 10 deletions cairo-contracts/testing/src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ use starknet::ContractAddress;
use starknet::contract_address_const;
use starknet_ibc_app_transfer::types::Participant;

pub(crate) const SUPPLY: u256 = 2000;
pub(crate) const DECIMALS: u8 = 18_u8;
pub(crate) const AMOUNT: u256 = 100;
pub(crate) const SALT: felt252 = 'SALT';
pub const SUPPLY: u256 = 2000;
pub const DECIMALS: u8 = 18_u8;
pub const AMOUNT: u256 = 100;
pub const SALT: felt252 = 'SALT';

pub(crate) fn NAME() -> ByteArray {
pub fn NAME() -> ByteArray {
"UATOM"
}

pub(crate) fn SYMBOL() -> ByteArray {
pub fn SYMBOL() -> ByteArray {
"IBC/UATOM"
}

pub(crate) fn PUBKEY() -> ContractAddress {
pub fn PUBKEY() -> ContractAddress {
contract_address_const::<0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7>()
}

pub(crate) fn OWNER() -> ContractAddress {
pub fn OWNER() -> ContractAddress {
contract_address_const::<'OWNER'>()
}

pub(crate) fn STARKNET() -> Participant {
pub fn STARKNET() -> Participant {
OWNER().into()
}

pub(crate) fn COSMOS() -> Participant {
pub fn COSMOS() -> Participant {
let bech32_address: ByteArray = "cosmos1wxeyh7zgn4tctjzs0vtqpc6p5cxq5t2muzl7ng";
let mut serialized_address: Array<felt252> = ArrayTrait::new();
Serde::serialize(@bech32_address, ref serialized_address);
Expand Down
10 changes: 3 additions & 7 deletions cairo-contracts/testing/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
mod config;
mod constants;
mod mocks;
#[cfg(test)]
mod setup;
#[cfg(test)]
mod transfer;
pub mod config;
pub mod constants;
pub mod setup;
1 change: 0 additions & 1 deletion cairo-contracts/testing/src/mocks.cairo

This file was deleted.

Loading

0 comments on commit f8f96b5

Please sign in to comment.