Skip to content

Commit

Permalink
Merge pull request #188 from multiversx/use-multivalueencoded-instead…
Browse files Browse the repository at this point in the history
…-of-managedvec

Use MultiValueEncoded instead of ManagedVec
  • Loading branch information
CostinCarabas authored Aug 14, 2024
2 parents c7bd9d1 + 5ad062d commit e03195c
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 34 deletions.
9 changes: 9 additions & 0 deletions common/transaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ pub struct EthTransaction<M: ManagedTypeApi> {
pub call_data: ManagedOption<M, ManagedBuffer<M>>,
}

pub type EthTxAsMultiValue<M> = MultiValue6<
EthAddress<M>,
ManagedAddress<M>,
TokenIdentifier<M>,
BigUint<M>,
TxNonce,
ManagedOption<M, ManagedBuffer<M>>,
>;

#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)]
pub struct Transaction<M: ManagedTypeApi> {
Expand Down
4 changes: 2 additions & 2 deletions multi-transfer-esdt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub trait MultiTransferEsdt:
fn batch_transfer_esdt_token(
&self,
batch_id: u64,
transfers: ManagedVec<EthTransaction<Self::Api>>,
transfers: MultiValueEncoded<EthTransaction<Self::Api>>,
) {
let mut valid_payments_list = ManagedVec::new();
let mut valid_tx_list = ManagedVec::new();
Expand All @@ -55,7 +55,7 @@ pub trait MultiTransferEsdt:

let safe_address = self.esdt_safe_contract_address().get();

for eth_tx in transfers.iter() {
for eth_tx in transfers {
let is_success: bool = self
.tx()
.to(safe_address.clone())
Expand Down
2 changes: 1 addition & 1 deletion multi-transfer-esdt/src/multi_transfer_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
{
pub fn batch_transfer_esdt_token<
Arg0: ProxyArg<u64>,
Arg1: ProxyArg<ManagedVec<Env::Api, transaction::EthTransaction<Env::Api>>>,
Arg1: ProxyArg<MultiValueEncoded<Env::Api, transaction::EthTransaction<Env::Api>>>,
>(
self,
batch_id: Arg0,
Expand Down
14 changes: 9 additions & 5 deletions multi-transfer-esdt/tests/multi_transfer_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use multiversx_sc::{
api::{HandleConstraints, ManagedTypeApi},
codec::{
multi_types::{MultiValueVec, OptionalValue},
Empty,
Empty, TopEncode,
},
contract_base::ManagedSerializer,
storage::mappers::SingleValue,
Expand Down Expand Up @@ -297,7 +297,8 @@ fn basic_transfer_test() {
call_data: ManagedOption::some(call_data),
};

let mut transfers: ManagedVec<StaticApi, EthTransaction<StaticApi>> = ManagedVec::new();
let mut transfers: MultiValueEncoded<StaticApi, EthTransaction<StaticApi>> =
MultiValueEncoded::new();
transfers.push(eth_tx);

state
Expand Down Expand Up @@ -360,7 +361,8 @@ fn batch_transfer_both_executed_test() {
call_data: ManagedOption::some(call_data),
};

let mut transfers: ManagedVec<StaticApi, EthTransaction<StaticApi>> = ManagedVec::new();
let mut transfers: MultiValueEncoded<StaticApi, EthTransaction<StaticApi>> =
MultiValueEncoded::new();
transfers.push(eth_tx1);
transfers.push(eth_tx2);

Expand Down Expand Up @@ -429,7 +431,8 @@ fn batch_two_transfers_same_token_test() {
call_data: ManagedOption::some(call_data),
};

let mut transfers: ManagedVec<StaticApi, EthTransaction<StaticApi>> = ManagedVec::new();
let mut transfers: MultiValueEncoded<StaticApi, EthTransaction<StaticApi>> =
MultiValueEncoded::new();
transfers.push(eth_tx1);
transfers.push(eth_tx2);

Expand Down Expand Up @@ -498,7 +501,8 @@ fn batch_transfer_both_failed_test() {
call_data: ManagedOption::some(call_data),
};

let mut transfers: ManagedVec<StaticApi, EthTransaction<StaticApi>> = ManagedVec::new();
let mut transfers: MultiValueEncoded<StaticApi, EthTransaction<StaticApi>> =
MultiValueEncoded::new();
transfers.push(eth_tx1);
transfers.push(eth_tx2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@
"function": "proposeMultiTransferEsdtBatch",
"arguments": [
"0x01",
"0x356439353965393865613733633335373738666665726431647977376179736e306e776d75616876786e68326530706d306b676a0000000e455448555344432d616661363839000000021388000000000000000100356439353965393865613733633335373738666665726431647977376179736e306e776d75616876786e68326530706d306b676a0000000e455448555344432d616661363839000000021388000000000000000200356439353965393865613733633335373738666665726431647977376179736e306e776d75616876786e68326530706d306b676a0000000e455448555344432d616661363839000000021388000000000000000301000000110000000466756e640000000005f5e10000356439353965393865613733633335373738666665726431647977376179736e306e776d75616876786e68326530706d306b676a0000000e455448555344432d6166613638390000000213880000000000000004010000001a0000000466756e640000000005f5e10001000000010000000135"
"0x356439353965393865613733633335373738666665726431647977376179736e306e776d75616876786e68326530706d306b676a0000000e455448555344432d616661363839000000021388000000000000000100",
"0x356439353965393865613733633335373738666665726431647977376179736e306e776d75616876786e68326530706d306b676a0000000e455448555344432d616661363839000000021388000000000000000200",
"0x356439353965393865613733633335373738666665726431647977376179736e306e776d75616876786e68326530706d306b676a0000000e455448555344432d616661363839000000021388000000000000000301000000110000000466756e640000000005f5e10000",
"0x356439353965393865613733633335373738666665726431647977376179736e306e776d75616876786e68326530706d306b676a0000000e455448555344432d6166613638390000000213880000000000000004010000001a0000000466756e640000000005f5e10001000000010000000135"
],
"gasLimit": "5,000,000"
},
Expand Down
5 changes: 2 additions & 3 deletions multisig/scenarios/unstake.scen.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@
"value": "0",
"function": "proposeMultiTransferEsdtBatch",
"arguments": [
"1",
"0x"
"1"
],
"gasLimit": "40,000,000",
"gasPrice": "0"
Expand All @@ -194,4 +193,4 @@
}
}
]
}
}
13 changes: 8 additions & 5 deletions multisig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,18 @@ pub trait Multisig:
fn propose_multi_transfer_esdt_batch(
&self,
eth_batch_id: u64,
transfers: ManagedVec<EthTransaction<Self::Api>>,
transfers: MultiValueEncoded<EthTxAsMultiValue<Self::Api>>,
) -> usize {
let next_eth_batch_id = self.last_executed_eth_batch_id().get() + 1;
require!(
eth_batch_id == next_eth_batch_id,
"Can only propose for next batch ID"
);

self.require_valid_eth_tx_ids(&transfers);
let transfers_as_eth_tx = self.transfers_multi_value_to_eth_tx_vec(transfers);
self.require_valid_eth_tx_ids(&transfers_as_eth_tx);

let batch_hash = self.hash_eth_tx_batch(&transfers);
let batch_hash = self.hash_eth_tx_batch(&transfers_as_eth_tx);
require!(
self.batch_id_to_action_id_mapping(eth_batch_id)
.get(&batch_hash)
Expand All @@ -287,7 +288,7 @@ pub trait Multisig:

let action_id = self.propose_action(Action::BatchTransferEsdtToken {
eth_batch_id,
transfers,
transfers: transfers_as_eth_tx,
});

self.batch_id_to_action_id_mapping(eth_batch_id)
Expand Down Expand Up @@ -409,11 +410,13 @@ pub trait Multisig:
self.last_executed_eth_tx_id().set(last_tx.tx_nonce);

let multi_transfer_esdt_addr = self.multi_transfer_esdt_address().get();
let transfers_multi: MultiValueEncoded<Self::Api, EthTransaction<Self::Api>> =
transfers.into();

self.tx()
.to(multi_transfer_esdt_addr)
.typed(multi_transfer_esdt_proxy::MultiTransferEsdtProxy)
.batch_transfer_esdt_token(eth_batch_id, transfers)
.batch_transfer_esdt_token(eth_batch_id, transfers_multi)
.sync_call();
}
}
Expand Down
2 changes: 1 addition & 1 deletion multisig/src/multi_transfer_esdt_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
{
pub fn batch_transfer_esdt_token<
Arg0: ProxyArg<u64>,
Arg1: ProxyArg<ManagedVec<Env::Api, transaction::EthTransaction<Env::Api>>>,
Arg1: ProxyArg<MultiValueEncoded<Env::Api, transaction::EthTransaction<Env::Api>>>,
>(
self,
batch_id: Arg0,
Expand Down
6 changes: 3 additions & 3 deletions multisig/src/multisig_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ where
/// Sender Address, Destination Address, Token ID, Amount, Tx Nonce
pub fn propose_multi_transfer_esdt_batch<
Arg0: ProxyArg<u64>,
Arg1: ProxyArg<ManagedVec<Env::Api, transaction::EthTransaction<Env::Api>>>,
Arg1: ProxyArg<MultiValueEncoded<Env::Api, transaction::EthTransaction<Env::Api>>>,
>(
self,
eth_batch_id: Arg0,
Expand Down Expand Up @@ -843,7 +843,7 @@ where
/// To check if it was executed as well, use the wasActionExecuted view
pub fn was_transfer_action_proposed<
Arg0: ProxyArg<u64>,
Arg1: ProxyArg<ManagedVec<Env::Api, transaction::EthTransaction<Env::Api>>>,
Arg1: ProxyArg<MultiValueEncoded<Env::Api, transaction::EthTransaction<Env::Api>>>,
>(
self,
eth_batch_id: Arg0,
Expand All @@ -862,7 +862,7 @@ where
/// Will return 0 if the transfers were not proposed
pub fn get_action_id_for_transfer_batch<
Arg0: ProxyArg<u64>,
Arg1: ProxyArg<ManagedVec<Env::Api, transaction::EthTransaction<Env::Api>>>,
Arg1: ProxyArg<MultiValueEncoded<Env::Api, transaction::EthTransaction<Env::Api>>>,
>(
self,
eth_batch_id: Arg0,
Expand Down
9 changes: 5 additions & 4 deletions multisig/src/queries.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use multiversx_sc::imports::*;

use crate::{action::Action, esdt_safe_proxy, multi_transfer_esdt_proxy, user_role::UserRole};
use transaction::{transaction_status::TransactionStatus, EthTransaction, TxBatchSplitInFields};
use transaction::{transaction_status::TransactionStatus, EthTxAsMultiValue, TxBatchSplitInFields};

/// Note: Additional queries can be found in the Storage module
#[multiversx_sc::module]
Expand Down Expand Up @@ -73,7 +73,7 @@ pub trait QueriesModule: crate::storage::StorageModule + crate::util::UtilModule
fn was_transfer_action_proposed(
&self,
eth_batch_id: u64,
transfers: ManagedVec<EthTransaction<Self::Api>>,
transfers: MultiValueEncoded<EthTxAsMultiValue<Self::Api>>,
) -> bool {
let action_id = self.get_action_id_for_transfer_batch(eth_batch_id, transfers);

Expand All @@ -87,9 +87,10 @@ pub trait QueriesModule: crate::storage::StorageModule + crate::util::UtilModule
fn get_action_id_for_transfer_batch(
&self,
eth_batch_id: u64,
transfers: ManagedVec<EthTransaction<Self::Api>>,
transfers: MultiValueEncoded<EthTxAsMultiValue<Self::Api>>,
) -> usize {
let batch_hash = self.hash_eth_tx_batch(&transfers);
let transfers_as_struct = self.transfers_multi_value_to_eth_tx_vec(transfers);
let batch_hash = self.hash_eth_tx_batch(&transfers_as_struct);

self.batch_id_to_action_id_mapping(eth_batch_id)
.get(&batch_hash)
Expand Down
18 changes: 15 additions & 3 deletions multisig/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use multiversx_sc::imports::*;

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

use crate::storage::EthBatchHash;
use crate::user_role::UserRole;
Expand Down Expand Up @@ -45,11 +45,23 @@ pub trait UtilModule: crate::storage::StorageModule {

fn transfers_multi_value_to_eth_tx_vec(
&self,
transfers: MultiValueEncoded<EthTransaction<Self::Api>>,
transfers: MultiValueEncoded<EthTxAsMultiValue<Self::Api>>,
) -> ManagedVec<EthTransaction<Self::Api>> {
let mut transfers_as_eth_tx = ManagedVec::new();
// for transfer in transfers {
// transfers_as_eth_tx.push(transfer);
// }
for transfer in transfers {
transfers_as_eth_tx.push(transfer);
let (from, to, token_id, amount, tx_nonce, call_data) = transfer.into_tuple();

transfers_as_eth_tx.push(EthTransaction {
from,
to,
token_id,
amount,
tx_nonce,
call_data,
});
}

transfers_as_eth_tx
Expand Down
18 changes: 12 additions & 6 deletions multisig/tests/multisig_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ fn ethereum_to_multiversx_call_data_empty_test() {
call_data: ManagedOption::none(),
};

let mut transfers: ManagedVec<StaticApi, EthTransaction<StaticApi>> = ManagedVec::new();
let mut transfers: MultiValueEncoded<StaticApi, EthTransaction<StaticApi>> =
MultiValueEncoded::new();
transfers.push(eth_tx);

state
Expand Down Expand Up @@ -493,7 +494,8 @@ fn ethereum_to_multiversx_relayer_call_data_several_tx_test() {
tx_nonce: 4u64,
call_data: ManagedOption::some(call_data2),
};
let mut transfers: ManagedVec<StaticApi, EthTransaction<StaticApi>> = ManagedVec::new();
let mut transfers: MultiValueEncoded<StaticApi, EthTransaction<StaticApi>> =
MultiValueEncoded::new();
transfers.push(eth_tx);
transfers.push(eth_tx2);
transfers.push(eth_tx3);
Expand Down Expand Up @@ -556,7 +558,8 @@ fn ethereum_to_multiversx_relayer_query_test() {
call_data: ManagedOption::none(),
};

let mut transfers: ManagedVec<StaticApi, EthTransaction<StaticApi>> = ManagedVec::new();
let mut transfers: MultiValueEncoded<StaticApi, EthTransaction<StaticApi>> =
MultiValueEncoded::new();
transfers.push(eth_tx);

state
Expand Down Expand Up @@ -647,7 +650,8 @@ fn ethereum_to_multiversx_relayer_query2_test() {
call_data: ManagedOption::none(),
};

let mut transfers: ManagedVec<StaticApi, EthTransaction<StaticApi>> = ManagedVec::new();
let mut transfers: MultiValueEncoded<StaticApi, EthTransaction<StaticApi>> =
MultiValueEncoded::new();
transfers.push(eth_tx);

state
Expand Down Expand Up @@ -751,7 +755,8 @@ fn ethereum_to_multiversx_tx_batch_ok_test() {
call_data: ManagedOption::some(call_data),
};

let mut transfers: ManagedVec<StaticApi, EthTransaction<StaticApi>> = ManagedVec::new();
let mut transfers: MultiValueEncoded<StaticApi, EthTransaction<StaticApi>> =
MultiValueEncoded::new();
transfers.push(eth_tx1);
transfers.push(eth_tx2);

Expand Down Expand Up @@ -835,7 +840,8 @@ fn ethereum_to_multiversx_tx_batch_rejected_test() {
call_data: ManagedOption::some(call_data),
};

let mut transfers: ManagedVec<StaticApi, EthTransaction<StaticApi>> = ManagedVec::new();
let mut transfers: MultiValueEncoded<StaticApi, EthTransaction<StaticApi>> =
MultiValueEncoded::new();
transfers.push(eth_tx1);
transfers.push(eth_tx2);

Expand Down

0 comments on commit e03195c

Please sign in to comment.