Skip to content

Commit

Permalink
Merge pull request #1625 from multiversx/type-abi-from-multivaluen
Browse files Browse the repository at this point in the history
From conversion for MultiValueN
  • Loading branch information
andrei-marinica committed May 14, 2024
2 parents 526d319 + de13520 commit 863cc34
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 28 deletions.
11 changes: 11 additions & 0 deletions contracts/feature-tests/scenario-tester/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ pub trait ScenarioTester {
fn add(&self, value: BigUint) {
self.sum().update(|sum| *sum += value);
}

/// Tests "from" conversion for MultiValueN parameters
#[endpoint]
fn multi_param(&self, _value: MultiValue2<BigUint, BigUint>) {}

/// Tests "from" conversion for MultiValueN return function
#[endpoint]
fn multi_return(&self, value: BigUint) -> MultiValue2<BigUint, BigUint> {
let value_plus_one = &value + 1u32;
(value, value_plus_one).into()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,32 @@ where
.argument(&value)
.original_result()
}

/// Tests "from" conversion for MultiValueN parameters
pub fn multi_param<
Arg0: ProxyArg<MultiValue2<BigUint<Env::Api>, BigUint<Env::Api>>>,
>(
self,
_value: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("multi_param")
.argument(&_value)
.original_result()
}

/// Tests "from" conversion for MultiValueN return function
pub fn multi_return<
Arg0: ProxyArg<BigUint<Env::Api>>,
>(
self,
value: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, MultiValue2<BigUint<Env::Api>, BigUint<Env::Api>>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("multi_return")
.argument(&value)
.original_result()
}
}
30 changes: 30 additions & 0 deletions contracts/feature-tests/scenario-tester/tests/st_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@ fn st_blackbox() {
.add(1u32)
.run();

world
.tx()
.from(OTHER_ADDRESS)
.to(ST_ADDRESS)
.typed(scenario_tester_proxy::ScenarioTesterProxy)
.multi_param(MultiValue2((1u32, 1u16)))
.run();

world
.tx()
.from(OTHER_ADDRESS)
.to(ST_ADDRESS)
.typed(scenario_tester_proxy::ScenarioTesterProxy)
.multi_return(1u32)
.returns(ExpectValue(MultiValue2((1u32, 2u32))))
.run();

let value = world
.tx()
.from(OTHER_ADDRESS)
.to(ST_ADDRESS)
.typed(scenario_tester_proxy::ScenarioTesterProxy)
.multi_return(1u32)
.returns(ReturnsResultUnmanaged)
.run();
assert_eq!(
value,
MultiValue2((RustBigUint::from(1u32), RustBigUint::from(2u32)))
);

world.write_scenario_trace("trace1.scen.json");
}

Expand Down
6 changes: 4 additions & 2 deletions contracts/feature-tests/scenario-tester/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 2
// Endpoints: 4
// Async Callback (empty): 1
// Total number of exported functions: 5
// Total number of exported functions: 7

#![no_std]

Expand All @@ -22,6 +22,8 @@ multiversx_sc_wasm_adapter::endpoints! {
upgrade => upgrade
getSum => sum
add => add
multi_param => multi_param
multi_return => multi_return
)
}

Expand Down
52 changes: 26 additions & 26 deletions framework/base/src/abi/type_abi_impl_codec_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,18 @@ impl<T: TypeAbi> TypeAbi for OptionalValue<T> {
}

macro_rules! multi_arg_impls {
($(($mval_struct:ident $($n:tt $name:ident)+) )+) => {
($(($mval_struct:ident $($n:tt $t:ident $u:ident)+) )+) => {
$(
impl<$($name),+ > TypeAbiFrom<Self> for crate::codec::multi_types::$mval_struct<$($name,)+>
impl<$($t, $u),+> TypeAbiFrom<crate::codec::multi_types::$mval_struct<$($u,)+>> for crate::codec::multi_types::$mval_struct<$($t,)+>
where
$($name: TypeAbi,)+
$($t: TypeAbiFrom<$u>,)+
{}

impl<$($name),+ > TypeAbi for crate::codec::multi_types::$mval_struct<$($name,)+>
impl<$($t),+> TypeAbi for crate::codec::multi_types::$mval_struct<$($t,)+>
where
$($name: TypeAbi,)+
$($t: TypeAbi,)+
{
type Unmanaged = Self;
type Unmanaged = crate::codec::multi_types::$mval_struct<$($t::Unmanaged,)+>;

fn type_name() -> TypeName {
let mut repr = TypeName::from("multi");
Expand All @@ -95,7 +95,7 @@ macro_rules! multi_arg_impls {
if $n > 0 {
repr.push(',');
}
repr.push_str($name::type_name().as_str());
repr.push_str($t::type_name().as_str());
)+
repr.push('>');
repr
Expand All @@ -108,15 +108,15 @@ macro_rules! multi_arg_impls {
if $n > 0 {
repr.push_str(", ");
}
repr.push_str($name::type_name_rust().as_str());
repr.push_str($t::type_name_rust().as_str());
)+
repr.push('>');
repr
}

fn provide_type_descriptions<TDC: TypeDescriptionContainer>(accumulator: &mut TDC) {
$(
$name::provide_type_descriptions(accumulator);
$t::provide_type_descriptions(accumulator);
)+
}

Expand All @@ -128,10 +128,10 @@ macro_rules! multi_arg_impls {
let mut result = OutputAbis::new();
$(
if output_names.len() > $n {
result.append(&mut $name::output_abis(&[output_names[$n]]));
result.append(&mut $t::output_abis(&[output_names[$n]]));

} else {
result.append(&mut $name::output_abis(&[]));
result.append(&mut $t::output_abis(&[]));
}

)+
Expand All @@ -143,19 +143,19 @@ macro_rules! multi_arg_impls {
}

multi_arg_impls! {
(MultiValue2 0 T0 1 T1)
(MultiValue3 0 T0 1 T1 2 T2)
(MultiValue4 0 T0 1 T1 2 T2 3 T3)
(MultiValue5 0 T0 1 T1 2 T2 3 T3 4 T4)
(MultiValue6 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
(MultiValue7 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
(MultiValue8 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
(MultiValue9 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
(MultiValue10 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
(MultiValue11 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
(MultiValue12 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
(MultiValue13 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
(MultiValue14 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
(MultiValue15 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
(MultiValue16 0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
(MultiValue2 0 T0 U0 1 T1 U1)
(MultiValue3 0 T0 U0 1 T1 U1 2 T2 U2)
(MultiValue4 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3)
(MultiValue5 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4)
(MultiValue6 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4 5 T5 U5)
(MultiValue7 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4 5 T5 U5 6 T6 U6)
(MultiValue8 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4 5 T5 U5 6 T6 U6 7 T7 U7)
(MultiValue9 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4 5 T5 U5 6 T6 U6 7 T7 U7 8 T8 U8)
(MultiValue10 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4 5 T5 U5 6 T6 U6 7 T7 U7 8 T8 U8 9 T9 U9)
(MultiValue11 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4 5 T5 U5 6 T6 U6 7 T7 U7 8 T8 U8 9 T9 U9 10 T10 U10)
(MultiValue12 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4 5 T5 U5 6 T6 U6 7 T7 U7 8 T8 U8 9 T9 U9 10 T10 U10 11 T11 U11)
(MultiValue13 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4 5 T5 U5 6 T6 U6 7 T7 U7 8 T8 U8 9 T9 U9 10 T10 U10 11 T11 U11 12 T12 U12)
(MultiValue14 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4 5 T5 U5 6 T6 U6 7 T7 U7 8 T8 U8 9 T9 U9 10 T10 U10 11 T11 U11 12 T12 U12 13 T13 U13)
(MultiValue15 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4 5 T5 U5 6 T6 U6 7 T7 U7 8 T8 U8 9 T9 U9 10 T10 U10 11 T11 U11 12 T12 U12 13 T13 U13 14 T14 U14)
(MultiValue16 0 T0 U0 1 T1 U1 2 T2 U2 3 T3 U3 4 T4 U4 5 T5 U5 6 T6 U6 7 T7 U7 8 T8 U8 9 T9 U9 10 T10 U10 11 T11 U11 12 T12 U12 13 T13 U13 14 T14 U14 15 T15 U15)
}

0 comments on commit 863cc34

Please sign in to comment.