|
| 1 | +#![cfg(test)] |
| 2 | +extern crate std; |
| 3 | + |
| 4 | +use soroban_sdk::testutils::Address as _; |
| 5 | +use soroban_sdk::{ |
| 6 | + contract, contractimpl, symbol_short, vec, Address, Env, IntoVal, |
| 7 | + Symbol, Val, Vec, |
| 8 | +}; |
| 9 | +use stellar_axelar_std::events::{fmt_last_emitted_event, Event}; |
| 10 | +use stellar_axelar_std::interfaces::OwnableInterface; |
| 11 | +use stellar_axelar_std::{ |
| 12 | + assert_contract_err, interfaces, IntoEvent, |
| 13 | +}; |
| 14 | + |
| 15 | +use crate::error::ContractError; |
| 16 | +use crate::types::FunctionCall; |
| 17 | +use crate::{Multicall, MulticallClient}; |
| 18 | + |
| 19 | +#[contract] |
| 20 | +pub struct TestTarget; |
| 21 | + |
| 22 | +#[derive(Debug, PartialEq, Eq, IntoEvent)] |
| 23 | +pub struct ExecutedEvent { |
| 24 | + pub value: u32, |
| 25 | +} |
| 26 | + |
| 27 | +#[contractimpl] |
| 28 | +impl OwnableInterface for TestTarget { |
| 29 | + fn owner(env: &Env) -> Address { |
| 30 | + interfaces::owner(env) |
| 31 | + } |
| 32 | + |
| 33 | + fn transfer_ownership(env: &Env, new_owner: Address) { |
| 34 | + interfaces::transfer_ownership::<Self>(env, new_owner); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[contractimpl] |
| 39 | +impl TestTarget { |
| 40 | + pub fn __constructor(env: Env, owner: Address) { |
| 41 | + interfaces::set_owner(&env, &owner); |
| 42 | + } |
| 43 | + |
| 44 | + pub fn method(env: &Env, value: u32) { |
| 45 | + ExecutedEvent { value }.emit(env); |
| 46 | + } |
| 47 | + |
| 48 | + pub fn failing(_env: &Env) { |
| 49 | + panic!("This method should fail"); |
| 50 | + } |
| 51 | + |
| 52 | + pub fn failing_with_error(_env: &Env) -> Result<Val, ContractError> { |
| 53 | + Err(ContractError::FunctionCallFailed) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +fn setup<'a>() -> (Env, MulticallClient<'a>, Address, Address) { |
| 58 | + let env = Env::default(); |
| 59 | + |
| 60 | + let owner = Address::generate(&env); |
| 61 | + let contract_id = env.register(Multicall, ()); |
| 62 | + let client = MulticallClient::new(&env, &contract_id); |
| 63 | + |
| 64 | + let target_id = env.register(TestTarget, (owner.clone(),)); |
| 65 | + |
| 66 | + (env, client, target_id, owner) |
| 67 | +} |
| 68 | + |
| 69 | +#[test] |
| 70 | +fn multicall_succeeds() { |
| 71 | + let (env, client, target, _) = setup(); |
| 72 | + |
| 73 | + let params = vec![ |
| 74 | + &env, |
| 75 | + FunctionCall { |
| 76 | + contract: target.clone(), |
| 77 | + function: symbol_short!("method"), |
| 78 | + args: vec![&env, IntoVal::<_, Val>::into_val(&42u32, &env)], |
| 79 | + }, |
| 80 | + ]; |
| 81 | + |
| 82 | + client.multicall(¶ms); |
| 83 | + goldie::assert!(fmt_last_emitted_event::<ExecutedEvent>(&env)); |
| 84 | +} |
| 85 | + |
| 86 | +#[test] |
| 87 | +#[should_panic(expected = "HostError: Error(WasmVm, InvalidAction)")] |
| 88 | +fn multicall_fails_when_target_panics() { |
| 89 | + let (env, client, target, _) = setup(); |
| 90 | + |
| 91 | + let params = vec![ |
| 92 | + &env, |
| 93 | + FunctionCall { |
| 94 | + contract: target, |
| 95 | + function: symbol_short!("failing"), |
| 96 | + args: Vec::<Val>::new(&env), |
| 97 | + }, |
| 98 | + ]; |
| 99 | + |
| 100 | + client.multicall(¶ms); |
| 101 | +} |
| 102 | + |
| 103 | +#[test] |
| 104 | +fn multicall_fails_when_target_returns_error() { |
| 105 | + let (env, client, target, _) = setup(); |
| 106 | + |
| 107 | + let params = vec![ |
| 108 | + &env, |
| 109 | + FunctionCall { |
| 110 | + contract: target, |
| 111 | + function: Symbol::new(&env, "failing_with_error"), |
| 112 | + args: Vec::<Val>::new(&env), |
| 113 | + }, |
| 114 | + ]; |
| 115 | + |
| 116 | + assert_contract_err!( |
| 117 | + client.try_multicall(¶ms), |
| 118 | + ContractError::FunctionCallFailed |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | +#[test] |
| 123 | +fn multicall_fails_when_some_calls_returns_error() { |
| 124 | + let (env, client, target, _) = setup(); |
| 125 | + |
| 126 | + let params = vec![ |
| 127 | + &env, |
| 128 | + FunctionCall { |
| 129 | + contract: target.clone(), |
| 130 | + function: symbol_short!("method"), |
| 131 | + args: vec![&env, IntoVal::<_, Val>::into_val(&42u32, &env)], |
| 132 | + }, |
| 133 | + FunctionCall { |
| 134 | + contract: target.clone(), |
| 135 | + function: Symbol::new(&env, "failing_with_error"), |
| 136 | + args: Vec::<Val>::new(&env), |
| 137 | + }, |
| 138 | + FunctionCall { |
| 139 | + contract: target.clone(), |
| 140 | + function: symbol_short!("method"), |
| 141 | + args: vec![&env, IntoVal::<_, Val>::into_val(&0u32, &env)], |
| 142 | + }, |
| 143 | + ]; |
| 144 | + assert_contract_err!( |
| 145 | + client.try_multicall(¶ms), |
| 146 | + ContractError::FunctionCallFailed |
| 147 | + ); |
| 148 | +} |
0 commit comments