Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bridge proxy unit tests #223

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bridge-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ path = "../esdt-safe"
[dependencies.bridged-tokens-wrapper]
path = "../bridged-tokens-wrapper"

[dependencies.mock-bridged-tokens-wrapper]
path = "../common/mock-contracts/mock-bridged-tokens-wrapper"

[dependencies.multiversx-sc]
version = "=0.53.2"

Expand Down
240 changes: 175 additions & 65 deletions bridge-proxy/tests/bridge_proxy_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use multiversx_sc_scenario::{
use multiversx_sc_scenario::{ExpectValue, ScenarioTxRun};

use eth_address::*;
use sc_proxies::{bridge_proxy_contract_proxy, bridged_tokens_wrapper_proxy};
use sc_proxies::{bridge_proxy_contract_proxy, bridged_tokens_wrapper_proxy, esdt_safe_proxy};
use transaction::{CallData, EthTransaction};

const BRIDGE_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("BRIDGE-123456");
Expand All @@ -52,14 +52,15 @@ const CROWDFUNDING_ADDRESS: TestSCAddress = TestSCAddress::new("crowfunding");
const MULTI_TRANSFER_ADDRESS: TestSCAddress = TestSCAddress::new("multi-transfer");
const ESDT_SAFE_ADDRESS: TestSCAddress = TestSCAddress::new("esdt-safe");
const BRIDGED_TOKENS_WRAPPER_ADDRESS: TestSCAddress = TestSCAddress::new("bridged-tokens-wrapper");
const NO_INIT_SC_ADDRESS: TestSCAddress = TestSCAddress::new("no-init-sc");

const BRIDGE_PROXY_PATH_EXPR: MxscPath = MxscPath::new("output/bridge-proxy.mxsc.json");
const CROWDFUNDING_PATH_EXPR: MxscPath =
MxscPath::new("tests/test-contract/crowdfunding-esdt.mxsc.json");
const MULTI_TRANSFER_PATH_EXPR: &str =
"mxsc:../multi-transfer-esdt/output/multi-transfer-esdt.mxsc.json";
const ESDT_SAFE_PATH_EXPR: &str = "mxsc:../esdt-safe/output/esdt-safe.mxsc.json";
const BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR: MxscPath =
const MOCK_MULTI_TRANSFER_PATH_EXPR: MxscPath = MxscPath::new("mxsc:../common/mock-contracts/mock-multi-transfer-esdt/output/mock-multi-transfer-esdt.mxsc.json");
const MOCK_ESDT_SAFE_PATH_EXPR: MxscPath =
MxscPath::new("mxsc:../common/mock-contrats/mock-esdt-safe/output/mock-esdt-safe.mxsc.json");
const MOCK_BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR: MxscPath =
MxscPath::new("../common/mock-contracts/mock-bridged-tokens-wrapper/output/mock-bridged-tokens-wrapper.mxsc.json");

fn world() -> ScenarioWorld {
Expand All @@ -68,17 +69,16 @@ fn world() -> ScenarioWorld {
blockchain.register_contract(BRIDGE_PROXY_PATH_EXPR, bridge_proxy::ContractBuilder);
blockchain.register_contract(CROWDFUNDING_PATH_EXPR, crowdfunding_esdt::ContractBuilder);
blockchain.register_contract(
BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR,
MOCK_BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR,
mock_bridged_tokens_wrapper::ContractBuilder,
);
blockchain.register_contract(ESDT_SAFE_PATH_EXPR, esdt_safe::ContractBuilder);
// blockchain.register_contract(MOCK_ESDT_SAFE_PATH_EXPR, mock_esdt_safe::ContractBuilder);

blockchain
}

type BridgeProxyContract = ContractInfo<bridge_proxy::Proxy<StaticApi>>;
type CrowdfundingContract = ContractInfo<crowdfunding_esdt::Proxy<StaticApi>>;
type BridgedTokensWrapperContract = ContractInfo<bridged_tokens_wrapper::Proxy<StaticApi>>;

struct BridgeProxyTestState {
world: ScenarioWorld,
Expand All @@ -87,8 +87,10 @@ struct BridgeProxyTestState {
impl BridgeProxyTestState {
fn new() -> Self {
let mut world = world();
let multi_transfer_code = world.code_expression(MULTI_TRANSFER_PATH_EXPR);
let esdt_safe_code = world.code_expression(ESDT_SAFE_PATH_EXPR);
let multi_transfer_code =
world.code_expression(MOCK_MULTI_TRANSFER_PATH_EXPR.eval_to_expr().as_str());
let esdt_safe_code =
world.code_expression(MOCK_ESDT_SAFE_PATH_EXPR.eval_to_expr().as_str());

world
.account(OWNER_ADDRESS)
Expand All @@ -109,15 +111,13 @@ impl BridgeProxyTestState {
.account(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.esdt_roles(WBRIDGE_TOKEN_ID, roles.clone())
.esdt_roles(BRIDGE_TOKEN_ID, roles)
.esdt_balance(TokenIdentifier::from(WBRIDGE_TOKEN_ID), 10_000u64)
.esdt_balance(TokenIdentifier::from(BRIDGE_TOKEN_ID), 10_000u64)
.code(BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR)
.code(MOCK_BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR)
.owner(OWNER_ADDRESS);

Self { world }
}

fn bridge_proxy_deploy(&mut self) -> &mut Self {
fn deploy_bridge_proxy(&mut self) -> &mut Self {
self.world
.tx()
.from(OWNER_ADDRESS)
Expand All @@ -130,13 +130,30 @@ impl BridgeProxyTestState {
self
}

fn bridged_tokens_wrapper_deploy(&mut self) -> &mut Self {
fn deploy_esdt_safe(&mut self) -> &mut Self {
self.world
.tx()
.from(OWNER_ADDRESS)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.init(
ManagedAddress::zero(),
ManagedAddress::zero(),
BigUint::zero(),
)
.code(MOCK_ESDT_SAFE_PATH_EXPR)
.new_address(BRIDGE_PROXY_ADDRESS)
.run();

self
}

fn deploy_bridged_tokens_wrapper(&mut self) -> &mut Self {
self.world
.tx()
.from(OWNER_ADDRESS)
.typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
.init()
.code(BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR)
.code(MOCK_BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR)
.new_address(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.run();

Expand Down Expand Up @@ -168,13 +185,13 @@ impl BridgeProxyTestState {
.unpause_endpoint()
.run();

self.world
.tx()
.from(OWNER_ADDRESS)
.to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
.unpause_endpoint()
.run();
// self.world
// .tx()
// .from(OWNER_ADDRESS)
// .to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
// .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
// .unpause_endpoint()
// .run();

self.world
.tx()
Expand All @@ -186,42 +203,68 @@ impl BridgeProxyTestState {
))
.run();

self.world
.tx()
.from(OWNER_ADDRESS)
.to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
.whitelist_token(BRIDGE_TOKEN_ID, 18u32, WBRIDGE_TOKEN_ID)
.run();

self.world
.tx()
.from(OWNER_ADDRESS)
.to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
.add_wrapped_token(WBRIDGE_TOKEN_ID, 18u32)
.run();

self.world
.tx()
.from(OWNER_ADDRESS)
.to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
.deposit_liquidity()
.single_esdt(
&TokenIdentifier::from(BRIDGE_TOKEN_ID),
0u64,
&BigUint::from(5_000u64),
)
.run();

self.world
.tx()
.from(OWNER_ADDRESS)
.to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
.set_esdt_safe_contract_address(OptionalValue::Some(ESDT_SAFE_ADDRESS))
.run();
// self.world
// .tx()
// .from(OWNER_ADDRESS)
// .to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
// .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
// .whitelist_token(BRIDGE_TOKEN_ID, 18u32, WBRIDGE_TOKEN_ID)
// .run();

// let default_price_nonce: OptionalValue<BigUint<StaticApi>> = OptionalValue::None;
// self.world
// .tx()
// .from(OWNER_ADDRESS)
// .to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
// .typed(esdt_safe_proxy::EsdtSafeProxy)
// .add_token_to_whitelist(
// BRIDGE_TOKEN_ID,
// "BRIDGE",
// true,
// false,
// BigUint::zero(),
// BigUint::zero(),
// BigUint::zero(),
// default_price_nonce,
// )
// .run();

// self.world
// .tx()
// .from(OWNER_ADDRESS)
// .to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
// .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
// .set_bridge_proxy_contract_address(OptionalValue::Some(BRIDGE_PROXY_ADDRESS))
// .run();

// self.world
// .tx()
// .from(OWNER_ADDRESS)
// .to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
// .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
// .add_wrapped_token(WBRIDGE_TOKEN_ID, 18u32)
// .run();

// self.world
// .tx()
// .from(OWNER_ADDRESS)
// .to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
// .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
// .deposit_liquidity()
// .single_esdt(
// &TokenIdentifier::from(BRIDGE_TOKEN_ID),
// 0u64,
// &BigUint::from(5_000u64),
// )
// .run();

// self.world
// .tx()
// .from(OWNER_ADDRESS)
// .to(BRIDGED_TOKENS_WRAPPER_ADDRESS)
// .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy)
// .set_esdt_safe_contract_address(OptionalValue::Some(ESDT_SAFE_ADDRESS))
// .run();
self
}
}
Expand All @@ -230,7 +273,7 @@ impl BridgeProxyTestState {
fn deploy_test() {
let mut test = BridgeProxyTestState::new();

test.bridge_proxy_deploy();
test.deploy_bridge_proxy();
test.deploy_crowdfunding();
test.config_bridge();
}
Expand All @@ -241,7 +284,7 @@ fn bridge_proxy_execute_crowdfunding_test() {

test.world.start_trace();

test.bridge_proxy_deploy();
test.deploy_bridge_proxy();
test.deploy_crowdfunding();
test.config_bridge();

Expand Down Expand Up @@ -304,15 +347,15 @@ fn bridge_proxy_execute_crowdfunding_test() {
.returns(ExpectValue(500u64))
.run();

test.world
.write_scenario_trace("scenarios/bridge_proxy_execute_crowdfunding.scen.json");
// test.world
// .write_scenario_trace("scenarios/bridge_proxy_execute_crowdfunding.scen.json");
}

#[test]
fn multiple_deposit_test() {
let mut test = BridgeProxyTestState::new();

test.bridge_proxy_deploy();
test.deploy_bridge_proxy();
test.deploy_crowdfunding();
test.config_bridge();

Expand Down Expand Up @@ -426,7 +469,7 @@ fn multiple_deposit_test() {
fn test_lowest_tx_id() {
let mut test = BridgeProxyTestState::new();

test.bridge_proxy_deploy();
test.deploy_bridge_proxy();
test.deploy_crowdfunding();
test.config_bridge();

Expand Down Expand Up @@ -520,3 +563,70 @@ fn test_lowest_tx_id() {
.returns(ExpectValue(51usize))
.run();
}

#[test]
fn bridge_proxy_wrong_formatting_sc_call_test() {
let mut test = BridgeProxyTestState::new();

test.world.start_trace();

test.deploy_bridge_proxy();
test.deploy_crowdfunding();
test.config_bridge();

let mut args = ManagedVec::new();

let call_data: CallData<StaticApi> = CallData {
endpoint: ManagedBuffer::from("fund"),
gas_limit: GAS_LIMIT,
args: ManagedOption::some(args),
};

let call_data: ManagedBuffer<StaticApi> =
ManagedSerializer::new().top_encode_to_managed_buffer(&call_data);

let eth_tx = EthTransaction {
from: EthAddress {
raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"),
},
to: ManagedAddress::from(NO_INIT_SC_ADDRESS.eval_to_array()),
token_id: BRIDGE_TOKEN_ID.into(),
amount: BigUint::from(500u64),
tx_nonce: 1u64,
call_data: ManagedOption::none(),
};

test.world
.tx()
.from(MULTI_TRANSFER_ADDRESS)
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.deposit(&eth_tx)
.egld_or_single_esdt(
&EgldOrEsdtTokenIdentifier::esdt(BRIDGE_TOKEN_ID),
0,
&BigUint::from(500u64),
)
.run();

test.world
.query()
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.get_pending_transaction_by_id(1u32)
.returns(ExpectValue(eth_tx))
.run();

test.world
.tx()
.from(OWNER_ADDRESS)
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.execute(1u32)
.run();

// Refund: Funds are transfered to BridgedTokensWrapper
test.world
.check_account(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.esdt_balance(BRIDGE_TOKEN_ID, BigUint::from(500u64));
}
Loading