Skip to content

Commit

Permalink
EthTransaction: impl TopDecode/TopEncode
Browse files Browse the repository at this point in the history
TopEncode/TopDecode is needed to have optional args for EthTransaction
  • Loading branch information
CostinCarabas committed Feb 1, 2024
1 parent 2942f10 commit 2967979
Show file tree
Hide file tree
Showing 19 changed files with 215 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
members = [
"bridge-proxy",
"bridge-proxy/meta",
"bridge-proxy/meta",
"esdt-safe",
"esdt-safe/meta",
"multi-transfer-esdt",
Expand Down
2 changes: 0 additions & 2 deletions bridge-proxy/tests/bridge_proxy_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ fn deploy_deposit_test() {
);
}


#[test]
fn multiple_deposit_test() {
let mut test = BridgeProxyTestState::setup();
Expand Down Expand Up @@ -270,5 +269,4 @@ fn multiple_deposit_test() {
.call(test.adder_contract.sum())
.expect_value(SingleValue::from(BigUint::from(20u32))),
);

}
3 changes: 2 additions & 1 deletion common/fee-estimator-module/src/aggregator_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub struct AggregatorResult<M: ManagedTypeApi> {

impl<M: ManagedTypeApi> From<AggregatorResultAsMultiValue<M>> for AggregatorResult<M> {
fn from(multi_result: AggregatorResultAsMultiValue<M>) -> Self {
let (round_id, from_token_name, to_token_name, timestamp, price, decimals) = multi_result.into_tuple();
let (round_id, from_token_name, to_token_name, timestamp, price, decimals) =
multi_result.into_tuple();

AggregatorResult {
round_id,
Expand Down
4 changes: 2 additions & 2 deletions common/token-module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ pub trait TokenModule: fee_estimator_module::FeeEstimatorModule {
.get_esdt_balance(&self.blockchain().get_sc_address(), token_id, 0);
if &current_balance >= amount {
self.send().direct_esdt(&caller, token_id, 0, amount);
} else {
return EsdtTokenPayment::new(token_id.clone(), 0, BigUint::zero());
} else {
return EsdtTokenPayment::new(token_id.clone(), 0, BigUint::zero());
}
EsdtTokenPayment::new(token_id.clone(), 0, amount.clone())
}
Expand Down
157 changes: 156 additions & 1 deletion common/transaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use eth_address::EthAddress;
use multiversx_sc::codec::{EncodeErrorHandler, NestedDecodeInput, TopEncodeOutput};
// use multiversx_sc::codec::{DecodeErrorHandler, EncodeErrorHandler, NestedDecodeInput, TopDecodeInput, TopEncodeOutput};

pub mod transaction_status;

Expand All @@ -26,7 +28,7 @@ pub type TxAsMultiValue<M> = MultiValue6<
pub type PaymentsVec<M> = ManagedVec<M, EsdtTokenPayment<M>>;
pub type TxBatchSplitInFields<M> = MultiValue2<u64, MultiValueEncoded<M, TxAsMultiValue<M>>>;

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi, Clone, ManagedVecItem)]
#[derive(NestedEncode, NestedDecode, TypeAbi, Clone, ManagedVecItem)]
pub struct EthTransaction<M: ManagedTypeApi> {
pub from: EthAddress<M>,
pub to: ManagedAddress<M>,
Expand All @@ -38,6 +40,159 @@ pub struct EthTransaction<M: ManagedTypeApi> {
pub args: ManagedVec<M, ManagedBuffer<M>>,
}

impl<M: ManagedTypeApi> TopEncode for EthTransaction<M> {
fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
where
O: TopEncodeOutput,
H: EncodeErrorHandler,
{
let mut nested_buffer = output.start_nested_encode();
self.from.dep_encode_or_handle_err(&mut nested_buffer, h)?;
self.to.dep_encode_or_handle_err(&mut nested_buffer, h)?;
self.token_id
.dep_encode_or_handle_err(&mut nested_buffer, h)?;
self.amount
.dep_encode_or_handle_err(&mut nested_buffer, h)?;
self.tx_nonce
.dep_encode_or_handle_err(&mut nested_buffer, h)?;
self.data.dep_encode_or_handle_err(&mut nested_buffer, h)?;
self.gas_limit
.dep_encode_or_handle_err(&mut nested_buffer, h)?;
for arg in &self.args {
arg.dep_encode_or_handle_err(&mut nested_buffer, h)?;
}
output.finalize_nested_encode(nested_buffer);
Result::Ok(())
}
}

impl<M: ManagedTypeApi> TopDecode for EthTransaction<M> {
fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
where
I: codec::TopDecodeInput,
H: codec::DecodeErrorHandler,
{
let mut nested_buffer = input.into_nested_buffer();
let from = EthAddress::dep_decode_or_handle_err(&mut nested_buffer, h)?;
let to = ManagedAddress::dep_decode_or_handle_err(&mut nested_buffer, h)?;
let token_id = TokenIdentifier::dep_decode_or_handle_err(&mut nested_buffer, h)?;
let amount = BigUint::dep_decode_or_handle_err(&mut nested_buffer, h)?;
let tx_nonce = TxNonce::dep_decode_or_handle_err(&mut nested_buffer, h)?;
let data = ManagedBuffer::dep_decode_or_handle_err(&mut nested_buffer, h)?;
let gas_limit = u64::dep_decode_or_handle_err(&mut nested_buffer, h)?;
let mut args = ManagedVec::new();

while !nested_buffer.is_depleted() {
args.push(ManagedBuffer::dep_decode_or_handle_err(
&mut nested_buffer,
h,
)?);
}

Result::Ok(EthTransaction {
from,
to,
token_id,
amount,
tx_nonce,
data,
gas_limit,
args,
})
}
}

// impl<M: ManagedTypeApi> codec::TopEncode for EthTransaction<M> {
// fn top_encode_or_handle_err<O, H>(
// &self,
// output: O,
// h: H,
// ) -> core::result::Result<(), H::HandledErr>
// where
// O: codec::TopEncodeOutput,
// H: codec::EncodeErrorHandler,
// {
// self {

// }
// match self.args. {
// TokenMapperState::NotSet => codec::TopEncode::top_encode_or_handle_err(&"", output, h),
// TokenMapperState::Pending => {
// codec::TopEncode::top_encode_or_handle_err(&"pending", output, h)
// },
// TokenMapperState::Token(token) => {
// codec::TopEncode::top_encode_or_handle_err(&token, output, h)
// },
// }
// }
// }

// impl<M: ManagedTypeApi> TopDecode for EthTransaction<M> {
// fn top_decode_or_handle_err<I, H>(top_input: I, h: H) -> Result<Self, H::HandledErr>
// where
// I: codec::TopDecodeInput,
// H: codec::DecodeErrorHandler,
// {
// let mut nested_buffer = top_input.into_nested_buffer();
// let result = Self::dep_decode_or_handle_err(&mut nested_buffer, h)?;
// if !codec::NestedDecodeInput::is_depleted(&nested_buffer) {
// return Err(h.handle_error(codec::DecodeError::INPUT_TOO_LONG));
// }
// Ok(result)
// }
// }

// impl<M: ManagedTypeApi> codec::TopDecode for EthTransaction<M> {
// fn top_decode_or_handle_err<I, H>(input: I, h: H) -> core::result::Result<Self, H::HandledErr>
// where
// I: codec::TopDecodeInput,
// H: codec::DecodeErrorHandler,
// {
// let decoded_input = ManagedBuffer::top_decode_or_handle_err(input, h)?;
// if decoded_input.is_empty() {
// Ok(TokenMapperState::NotSet)
// } else if decoded_input == PENDING_ENCODING {
// Ok(TokenMapperState::Pending)
// } else {
// let token_id = TokenIdentifier::from_esdt_bytes(decoded_input);
// Ok(TokenMapperState::Token(token_id))
// }
// }
// }

// impl<T: NestedDecode, M: ManagedTypeApi> TopDecode for EthTransaction<M> {
// fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
// where
// I: codec::TopDecodeInput,
// H: codec::DecodeErrorHandler,
// {
// let mut result: EthTransaction<T, M> = EthTransaction::new();
// let mut nested_arg = input.into_nested_buffer();
// while !nested_arg.is_depleted() {
// if let Err(capacity_error) =
// result.try_push(T::dep_decode_or_handle_err(&mut nested_arg, h)?)
// {
// return Err(h.handle_error(DecodeError::from(capacity_error)));
// }
// }
// if !nested_arg.is_depleted() {
// if let Err(capacity_error) =
// result.try_push(T::dep_decode_or_handle_err(&mut nested_arg, h)?)
// {
// return Err(h.handle_error(DecodeError::from(capacity_error)));
// }
// }
// Ok(result)
// }

// fn top_decode<I>(input: I) -> Result<Self, DecodeError>
// where
// I: TopDecodeInput,
// {
// Self::top_decode_or_handle_err(input, codec::DefaultErrorHandler)
// }
// }

pub type EthTxAsMultiValue<M> = MultiValue8<
EthAddress<M>,
ManagedAddress<M>,
Expand Down
6 changes: 4 additions & 2 deletions esdt-safe/tests/esdt_safe_scenario_rs_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ fn world() -> ScenarioWorld {
blockchain.set_current_dir_from_workspace("esdt-safe/");

blockchain.register_contract("file:output/esdt-safe.wasm", esdt_safe::ContractBuilder);
blockchain.register_contract("file:../price-aggregator/multiversx-price-aggregator-sc.wasm", multiversx_price_aggregator_sc::ContractBuilder);
blockchain.register_contract(
"file:../price-aggregator/multiversx-price-aggregator-sc.wasm",
multiversx_price_aggregator_sc::ContractBuilder,
);

blockchain

}

#[test]
Expand Down
4 changes: 1 addition & 3 deletions esdt-safe/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
// Total number of exported functions: 40

#![no_std]

// Configuration that works with rustc < 1.73.0.
// TODO: Recommended rustc version: 1.73.0 or newer.
#![allow(internal_features)]
#![feature(lang_items)]

multiversx_sc_wasm_adapter::allocator!();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"function": "batchTransferEsdtToken",
"arguments": [
"1",
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:10000000|nested:0",
"0x0102030405060708091011121314151617181920|address:user2|nested:str:WRAPPED-123456|biguint:500|u64:2|nested:str:data|u64:10000000|nested:0"
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:10000000",
"0x0102030405060708091011121314151617181920|address:user2|nested:str:WRAPPED-123456|biguint:500|u64:2|nested:str:data|u64:10000000"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"function": "batchTransferEsdtToken",
"arguments": [
"1",
"0x0102030405060708091011121314151617181920|sc:multi_transfer_esdt|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:2000000|nested:0",
"0x0102030405060708091011121314151617181920|sc:multi_transfer_esdt|nested:str:WRAPPED-123456|biguint:100,500|u64:2|nested:str:data|u64:2000000|nested:0"
"0x0102030405060708091011121314151617181920|sc:multi_transfer_esdt|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:2000000",
"0x0102030405060708091011121314151617181920|sc:multi_transfer_esdt|nested:str:WRAPPED-123456|biguint:100,500|u64:2|nested:str:data|u64:2000000"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"function": "batchTransferEsdtToken",
"arguments": [
"1",
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:1000000|nested:0",
"0x0102030405060708091011121314151617181920|sc:multi_transfer_esdt|nested:str:WRAPPED-123456|biguint:500|u64:2|nested:str:data|u64:1000000|nested:0"
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:1000000",
"0x0102030405060708091011121314151617181920|sc:multi_transfer_esdt|nested:str:WRAPPED-123456|biguint:500|u64:2|nested:str:data|u64:1000000"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"function": "batchTransferEsdtToken",
"arguments": [
"1",
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:10000000|nested:0",
"0x0102030405060708091011121314151617181920|address:frozen_user|nested:str:BRIDGE-123456|biguint:500|u64:2|nested:str:data|u64:10000000|nested:0"
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:10000000",
"0x0102030405060708091011121314151617181920|address:frozen_user|nested:str:BRIDGE-123456|biguint:500|u64:2|nested:str:data|u64:10000000"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@
"function": "batchTransferEsdtToken",
"arguments": [
"1",
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:2000000|nested:0",
"0x0102030405060708091011121314151617181920|address:user2|nested:str:USDC-aaaaaa|biguint:500,000,000,000,000|u64:2|nested:str:data|u64:2000000|nested:0",
"0x0102030405060708091011121314151617181920|address:user1|nested:str:USDC-cccccc|biguint:1,000,000,000,000,000|u64:3|nested:str:data|u64:2000000|nested:0"
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:2000000",
"0x0102030405060708091011121314151617181920|address:user2|nested:str:USDC-aaaaaa|biguint:500,000,000,000,000|u64:2|nested:str:data|u64:2000000",
"0x0102030405060708091011121314151617181920|address:user1|nested:str:USDC-cccccc|biguint:1,000,000,000,000,000|u64:3|nested:str:data|u64:2000000"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"function": "batchTransferEsdtToken",
"arguments": [
"1",
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:10000000|nested:0"
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:10000000"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
Expand Down
2 changes: 1 addition & 1 deletion multi-transfer-esdt/scenarios/transfer_ok.scen.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"function": "batchTransferEsdtToken",
"arguments": [
"1",
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:10000000|nested:0"
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:10000000"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"function": "batchTransferEsdtToken",
"arguments": [
"1",
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:10000000|nested:0",
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:2|nested:str:data|u64:10000000|nested:0"
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:1|nested:str:data|u64:10000000",
"0x0102030405060708091011121314151617181920|address:user1|nested:str:BRIDGE-123456|biguint:100,200|u64:2|nested:str:data|u64:10000000"
],
"gasLimit": "50,000,000",
"gasPrice": "0"
Expand Down
6 changes: 3 additions & 3 deletions multi-transfer-esdt/tests/multi_transfer_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use multiversx_sc::{
storage::mappers::SingleValue,
types::{
Address, BigUint, CodeMetadata, ManagedAddress, ManagedBuffer, ManagedByteArray,
MultiValueEncoded, TokenIdentifier, ManagedVec,
ManagedVec, MultiValueEncoded, TokenIdentifier,
},
};
use multiversx_sc_modules::pause::ProxyTrait;
Expand Down Expand Up @@ -279,7 +279,7 @@ fn basic_setup_test() {
tx_nonce: 1u64,
data: ManagedBuffer::from("data"),
gas_limit: GAS_LIMIT,
args: ManagedVec::new()
args: ManagedVec::new(),
};

test.world.check_state_step(
Expand Down Expand Up @@ -316,7 +316,7 @@ fn basic_transfer_test() {
tx_nonce: 1u64,
data: ManagedBuffer::from("data"),
gas_limit: GAS_LIMIT,
args: ManagedVec::new()
args: ManagedVec::new(),
};

test.world.check_state_step(
Expand Down
20 changes: 16 additions & 4 deletions multi-transfer-esdt/tests/multi_transfer_esdt_scenario_rs_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ fn world() -> ScenarioWorld {
let mut blockchain = ScenarioWorld::new();
blockchain.set_current_dir_from_workspace("multi-transfer-esdt/");

blockchain.register_contract("file:output/multi-transfer-esdt.wasm", multi_transfer_esdt::ContractBuilder);
blockchain.register_contract("file:../esdt-safe/output/esdt-safe.wasm", esdt_safe::ContractBuilder);
blockchain.register_contract("file:../bridge-proxy/output/bridge-proxy.wasm", bridge_proxy::ContractBuilder);
blockchain.register_contract("file:../bridged-tokens-wrapper/output/bridged-tokens-wrapper.wasm", bridged_tokens_wrapper::ContractBuilder);
blockchain.register_contract(
"file:output/multi-transfer-esdt.wasm",
multi_transfer_esdt::ContractBuilder,
);
blockchain.register_contract(
"file:../esdt-safe/output/esdt-safe.wasm",
esdt_safe::ContractBuilder,
);
blockchain.register_contract(
"file:../bridge-proxy/output/bridge-proxy.wasm",
bridge_proxy::ContractBuilder,
);
blockchain.register_contract(
"file:../bridged-tokens-wrapper/output/bridged-tokens-wrapper.wasm",
bridged_tokens_wrapper::ContractBuilder,
);

blockchain
}
Expand Down
Loading

0 comments on commit 2967979

Please sign in to comment.