Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EthTransaction: impl TopDecode/TopEncode #163

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
65 changes: 64 additions & 1 deletion common/transaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ multiversx_sc::imports!();
multiversx_sc::derive_imports!();

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

pub mod transaction_status;

Expand All @@ -26,7 +27,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 +39,68 @@ 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,
})
}
}

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
5 changes: 3 additions & 2 deletions multisig/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ pub trait UtilModule: crate::storage::StorageModule {
) -> ManagedVec<EthTransaction<Self::Api>> {
let mut transfers_as_eth_tx = ManagedVec::new();
for transfer in transfers {
let (from, to, token_id, amount, tx_nonce, data, gas_limit, args) = transfer.into_tuple();
let (from, to, token_id, amount, tx_nonce, data, gas_limit, args) =
transfer.into_tuple();

transfers_as_eth_tx.push(EthTransaction {
from,
Expand All @@ -59,7 +60,7 @@ pub trait UtilModule: crate::storage::StorageModule {
tx_nonce,
data,
gas_limit,
args
args,
});
}

Expand Down
15 changes: 12 additions & 3 deletions multisig/tests/multisig_scenario_rs_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ fn world() -> ScenarioWorld {
blockchain.set_current_dir_from_workspace("multisig/");

blockchain.register_contract("file:output/multisig.wasm", multisig::ContractBuilder);
blockchain.register_contract("file:../multi-transfer-esdt/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:../price-aggregator/multiversx-price-aggregator-sc.wasm", multiversx_price_aggregator_sc::ContractBuilder);
blockchain.register_contract(
"file:../multi-transfer-esdt/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:../price-aggregator/multiversx-price-aggregator-sc.wasm",
multiversx_price_aggregator_sc::ContractBuilder,
);

blockchain
}
Expand Down
Loading