Skip to content

Commit

Permalink
Merge pull request #150 from multiversx/bridge-proxy-tests
Browse files Browse the repository at this point in the history
Bridge proxy tests
  • Loading branch information
CostinCarabas authored Oct 17, 2023
2 parents 7231845 + 4c8596a commit 196565e
Show file tree
Hide file tree
Showing 64 changed files with 431 additions and 202 deletions.
3 changes: 3 additions & 0 deletions bridge-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ path = "src/bridge-proxy.rs"
[dependencies.transaction]
path = "../common/transaction"

[dependencies.eth-address]
path = "../common/eth-address"

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

Expand Down
45 changes: 0 additions & 45 deletions bridge-proxy/scenarios/bridge-proxy.scen.json

This file was deleted.

8 changes: 4 additions & 4 deletions bridge-proxy/src/bridge-proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

mod config;
pub mod config;

use transaction::{EthTransaction, EthTransactionPayment};

#[multiversx_sc::contract]
pub trait BridgeProxyContract: config::ConfigModule {
#[init]
fn init(&self, multi_transfer_address: ManagedAddress) {
self.multi_transfer_address()
.set_if_empty(&multi_transfer_address);
fn init(&self, opt_multi_transfer_address: OptionalValue<ManagedAddress>) {
self.set_multi_transfer_contract_address(opt_multi_transfer_address);
}

#[payable("*")]
#[endpoint]
fn deposit(&self, eth_tx: EthTransaction<Self::Api>) {
let (token_id, nonce, amount) = self.call_value().single_esdt().into_tuple();
Expand Down
27 changes: 20 additions & 7 deletions bridge-proxy/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use transaction::EthTransactionPayment;
use transaction::{EthTransactionPayment, EthTransaction};

#[multiversx_sc::module]
pub trait ConfigModule {
#[only_owner]
#[endpoint(setupMultiTransfer)]
fn setup_multi_transfer(&self, multi_transfer_address: ManagedAddress) {
require!(
self.blockchain().is_smart_contract(&multi_transfer_address),
"Invalid multi-transfer address"
);
fn set_multi_transfer_contract_address(&self, opt_multi_transfer_address: OptionalValue<ManagedAddress>) {
match opt_multi_transfer_address {
OptionalValue::Some(sc_addr) => {
require!(
self.blockchain().is_smart_contract(&sc_addr),
"Invalid multi-transfer address"
);
self.multi_transfer_address().set(&sc_addr);
}
OptionalValue::None => self.multi_transfer_address().clear(),
}
}

self.multi_transfer_address().set(&multi_transfer_address);
#[view(getEthTransactionById)]
fn get_eth_transaction_by_id(&self, id: u32) -> EthTransaction<Self::Api> {
let eth_tx_list = self.eth_transaction_list();
match eth_tx_list.get_node_by_id(id) {
Some(tx) => tx.get_value_cloned().eth_tx,
None => sc_panic!("No transaction with this id!")
}
}

#[view(getMultiTransferAddress)]
Expand Down
125 changes: 125 additions & 0 deletions bridge-proxy/tests/bridge_proxy_blackbox_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#![allow(unused)]

use std::collections::LinkedList;

use bridge_proxy::config::ProxyTrait as _;
use bridge_proxy::ProxyTrait;

use multiversx_sc::{
api::ManagedTypeApi,
codec::multi_types::{MultiValueVec, OptionalValue},
storage::mappers::SingleValue,
types::{
Address, BigUint, CodeMetadata, ManagedAddress, ManagedBuffer, ManagedByteArray,
TokenIdentifier,
},
};
use multiversx_sc_scenario::{
api::StaticApi,
scenario_format::interpret_trait::{InterpretableFrom, InterpreterContext},
scenario_model::*,
ContractInfo, ScenarioWorld,
};

use eth_address::*;
use transaction::{EthTransaction, EthTransactionPayment};

const BRIDGE_TOKEN_ID: &[u8] = b"BRIDGE-123456";
const GAS_LIMIT: u64 = 1_000_000;
const BRIDGE_PROXY_PATH_EXPR: &str = "file:output/bridge-proxy.wasm";

fn world() -> ScenarioWorld {
let mut blockchain = ScenarioWorld::new();

blockchain.register_contract(BRIDGE_PROXY_PATH_EXPR, bridge_proxy::ContractBuilder);
blockchain
}

#[test]
fn deploy_deposit_test() {
let mut test = BridgeProxyTestState::setup();
let bridge_token_id_expr = "str:BRIDGE-123456"; // when specifying the token transfer

test.bridge_proxy_deploy();

let eth_tx = EthTransaction {
from: test.eth_user,
to: ManagedAddress::from_address(&test.user.value),
token_id: TokenIdentifier::from_esdt_bytes(BRIDGE_TOKEN_ID),
amount: BigUint::from(500u64),
tx_nonce: 1u64,
data: ManagedBuffer::from("data"),
gas_limit: GAS_LIMIT,
};

test.world.set_state_step(SetStateStep::new().put_account(
&test.owner,
Account::new().esdt_balance(bridge_token_id_expr, 1_000u64),
));

test.world.sc_call(
ScCallStep::new()
.from(&test.owner)
.to(&test.bridge_proxy)
.call(test.bridge_proxy.deposit(&eth_tx))
.esdt_transfer(bridge_token_id_expr, 0u64, 500u64),
);

test.world.sc_query(
ScQueryStep::new()
.to(&test.bridge_proxy)
.call(test.bridge_proxy.get_eth_transaction_by_id(1u32))
.expect_value(eth_tx),
);
}

type BridgeProxyContract = ContractInfo<bridge_proxy::Proxy<StaticApi>>;

struct BridgeProxyTestState<M: ManagedTypeApi> {
world: ScenarioWorld,
owner: AddressValue,
user: AddressValue,
eth_user: EthAddress<M>,
bridge_proxy: BridgeProxyContract,
}

impl<M: ManagedTypeApi> BridgeProxyTestState<M> {
fn setup() -> Self {
let world = world();
let ic = &world.interpreter_context();

let mut state = BridgeProxyTestState {
world,
owner: "address:owner".into(),
user: "address:user".into(),
eth_user: EthAddress {
raw_addr: ManagedByteArray::default(),
},
bridge_proxy: BridgeProxyContract::new("sc:bridge_proxy"),
};

state
.world
.set_state_step(SetStateStep::new().put_account(&state.owner, Account::new().nonce(1)));

state
}

fn bridge_proxy_deploy(&mut self) -> &mut Self {
self.world.set_state_step(
SetStateStep::new()
.put_account(&self.owner, Account::new().nonce(1))
.new_address(&self.owner, 1, &self.bridge_proxy),
);

let ic = &self.world.interpreter_context();
self.world.sc_deploy(
ScDeployStep::new()
.from(self.owner.clone())
.code(self.world.code_expression(BRIDGE_PROXY_PATH_EXPR))
.call(self.bridge_proxy.init(ManagedAddress::zero())),
);

self
}
}
64 changes: 0 additions & 64 deletions bridge-proxy/tests/bridge_proxy_rust_test.rs

This file was deleted.

10 changes: 0 additions & 10 deletions bridge-proxy/tests/bridge_proxy_scenario_go_test.rs

This file was deleted.

16 changes: 0 additions & 16 deletions bridge-proxy/tests/bridge_proxy_scenario_rs_test.rs

This file was deleted.

1 change: 1 addition & 0 deletions bridge-proxy/wasm/Cargo.lock

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

7 changes: 4 additions & 3 deletions bridge-proxy/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 7
// Endpoints: 8
// Async Callback: 1
// Total number of exported functions: 9
// Total number of exported functions: 10

#![no_std]
#![feature(lang_items)]
Expand All @@ -22,7 +22,8 @@ multiversx_sc_wasm_adapter::endpoints! {
deposit => deposit
executeWithAsnyc => execute_with_async
refundTransactions => refund_transactions
setupMultiTransfer => setup_multi_transfer
setupMultiTransfer => set_multi_transfer_contract_address
getEthTransactionById => get_eth_transaction_by_id
getMultiTransferAddress => multi_transfer_address
getEthTransactionList => eth_transaction_list
getEthFailedTransactionList => eth_failed_transaction_list
Expand Down
Loading

0 comments on commit 196565e

Please sign in to comment.