Skip to content

Commit

Permalink
test conversions MultiValueEncodedCounted <-> MultiValueManagedVecCou…
Browse files Browse the repository at this point in the history
…nted
  • Loading branch information
andrei-marinica committed Nov 15, 2024
1 parent 317f8df commit 570a0cf
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,76 @@
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "convert1",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "convert_varags_vec_with_counted_pairs_1",
"arguments": [
"0x68d79a75b4aa11395dd08994855bd1d90b6b7583d7296dca31c2f8f59e0e7a68",
"0x0f",
"2",
"5",
"5",
"4",
"4"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"0x68d79a75b4aa11395dd08994855bd1d90b6b7583d7296dca31c2f8f59e0e7a68",
"0x0f",
"2",
"5",
"5",
"4",
"4"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "convert2",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "convert_varags_vec_with_counted_pairs_2",
"arguments": [
"0x68d79a75b4aa11395dd08994855bd1d90b6b7583d7296dca31c2f8f59e0e7a68",
"0x0f",
"2",
"5",
"5",
"4",
"4"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"0x68d79a75b4aa11395dd08994855bd1d90b6b7583d7296dca31c2f8f59e0e7a68",
"0x0f",
"2",
"5",
"5",
"4",
"4"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,4 @@
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,4 @@
}
}
]
}
}
47 changes: 47 additions & 0 deletions contracts/feature-tests/basic-features/src/echo_managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,51 @@ pub trait EchoManagedTypes {
> {
m
}

#[endpoint]
fn convert_varags_vec_with_counted_pairs_1(
&self,
address_number_pairs: MultiValueEncoded<
MultiValue3<ManagedAddress, usize, MultiValueEncodedCounted<MultiValue2<usize, usize>>>,
>,
) -> MultiValueManagedVec<
MultiValue3<ManagedAddress, usize, MultiValueManagedVecCounted<MultiValue2<usize, usize>>>,
> {
let mut result = MultiValueManagedVec::new();
for triple in address_number_pairs {
let (address, num, counted_lazy) = triple.into_tuple();
let mut counted_list = MultiValueManagedVecCounted::new();
for pair in counted_lazy {
counted_list.push(pair);
}
result.push((address, num, counted_list).into());
}
result
}

#[endpoint]
fn convert_varags_vec_with_counted_pairs_2(
&self,
address_number_pairs: MultiValueManagedVec<
MultiValue3<
ManagedAddress,
usize,
MultiValueManagedVecCounted<MultiValue2<usize, usize>>,
>,
>,
) -> MultiValueEncoded<
MultiValue3<ManagedAddress, usize, MultiValueEncodedCounted<MultiValue2<usize, usize>>>,
> {
let mut result = MultiValueEncoded::new();
for triple in address_number_pairs.into_iter() {
let (address, x, counted_list) = triple.into_tuple();
let mut counted_lazy = MultiValueEncodedCounted::new();
let v = counted_list.into_vec();
for pair in &v {
counted_lazy.push(pair);
}
result.push((address, x, counted_lazy).into());
}
result
}
}
6 changes: 4 additions & 2 deletions contracts/feature-tests/basic-features/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 411
// Endpoints: 413
// Async Callback: 1
// Total number of exported functions: 413
// Total number of exported functions: 415

#![no_std]

Expand Down Expand Up @@ -181,6 +181,8 @@ multiversx_sc_wasm_adapter::endpoints! {
echo_varags_managed_sum => echo_varags_managed_sum
echo_varags_vec_with_counted => echo_varags_vec_with_counted
echo_varags_vec_with_counted_pairs => echo_varags_vec_with_counted_pairs
convert_varags_vec_with_counted_pairs_1 => convert_varags_vec_with_counted_pairs_1
convert_varags_vec_with_counted_pairs_2 => convert_varags_vec_with_counted_pairs_2
compute_get_values => compute_get_values
compute_create_ec => compute_create_ec
compute_get_ec_length => compute_get_ec_length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ where
}
}

impl<M, T> ManagedVecItem for MultiValueManagedVecCounted<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem,
{
type PAYLOAD = <ManagedVec<M, T> as ManagedVecItem>::PAYLOAD;
const SKIPS_RESERIALIZATION: bool = false;
type Ref<'a> = Self;

fn from_byte_reader<Reader: FnMut(&mut [u8])>(reader: Reader) -> Self {
Self::from(ManagedVec::<M, T>::from_byte_reader(reader))
}

unsafe fn from_byte_reader_as_borrow<'a, Reader: FnMut(&mut [u8])>(
reader: Reader,
) -> Self::Ref<'a> {
Self::from_byte_reader(reader)
}

fn into_byte_writer<R, Writer: FnMut(&[u8]) -> R>(self, writer: Writer) -> R {
self.contents.into_byte_writer(writer)
}
}

impl<M, T> TopEncodeMulti for MultiValueManagedVecCounted<M, T>
where
M: ManagedTypeApi,
Expand Down

0 comments on commit 570a0cf

Please sign in to comment.