From 3f0f774d764849fa569336357987bfde51a7b976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Wed, 4 Oct 2023 08:14:55 +0300 Subject: [PATCH 1/2] Proxy Actions SC --- Cargo.toml | 2 + contracts/proxy-actions/.gitignore | 7 + contracts/proxy-actions/Cargo.toml | 21 ++ contracts/proxy-actions/meta/Cargo.toml | 14 ++ contracts/proxy-actions/meta/src/main.rs | 3 + contracts/proxy-actions/multiversx.json | 3 + .../proxy-actions/scenarios/empty.scen.json | 39 ++++ contracts/proxy-actions/src/proxy_actions.rs | 12 + contracts/proxy-actions/src/structs.rs | 144 ++++++++++++ .../tests/empty_scenario_go_test.rs | 10 + .../tests/empty_scenario_rs_test.rs | 17 ++ contracts/proxy-actions/wasm/Cargo.lock | 209 ++++++++++++++++++ contracts/proxy-actions/wasm/Cargo.toml | 27 +++ contracts/proxy-actions/wasm/src/lib.rs | 26 +++ 14 files changed, 534 insertions(+) create mode 100644 contracts/proxy-actions/.gitignore create mode 100644 contracts/proxy-actions/Cargo.toml create mode 100644 contracts/proxy-actions/meta/Cargo.toml create mode 100644 contracts/proxy-actions/meta/src/main.rs create mode 100644 contracts/proxy-actions/multiversx.json create mode 100644 contracts/proxy-actions/scenarios/empty.scen.json create mode 100644 contracts/proxy-actions/src/proxy_actions.rs create mode 100644 contracts/proxy-actions/src/structs.rs create mode 100644 contracts/proxy-actions/tests/empty_scenario_go_test.rs create mode 100644 contracts/proxy-actions/tests/empty_scenario_rs_test.rs create mode 100644 contracts/proxy-actions/wasm/Cargo.lock create mode 100644 contracts/proxy-actions/wasm/Cargo.toml create mode 100644 contracts/proxy-actions/wasm/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 4950b7e5..c3ec5860 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,8 @@ members = [ "contracts/paymaster/meta", "contracts/ping-pong-egld", "contracts/ping-pong-egld/meta", + "contracts/proxy-actions", + "contracts/proxy-actions/meta", "contracts/proxy-pause", "contracts/proxy-pause/meta", "contracts/rewards-distribution", diff --git a/contracts/proxy-actions/.gitignore b/contracts/proxy-actions/.gitignore new file mode 100644 index 00000000..2c76bc98 --- /dev/null +++ b/contracts/proxy-actions/.gitignore @@ -0,0 +1,7 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ +*/target/ + +# The mxpy output +/output*/ diff --git a/contracts/proxy-actions/Cargo.toml b/contracts/proxy-actions/Cargo.toml new file mode 100644 index 00000000..73929c0c --- /dev/null +++ b/contracts/proxy-actions/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "proxy-actions" +version = "0.0.0" +authors = [ "MultiversX " ] +edition = "2021" +publish = false + +[lib] +path = "src/proxy_actions.rs" + +[dev-dependencies] +num-bigint = "0.4.2" + +[dependencies.multiversx-sc] +version = "0.43.4" + +[dev-dependencies.multiversx-sc-scenario] +version = "0.43.4" + +[dev-dependencies.multiversx-wegld-swap-sc] +path = "../wegld-swap" diff --git a/contracts/proxy-actions/meta/Cargo.toml b/contracts/proxy-actions/meta/Cargo.toml new file mode 100644 index 00000000..7ffa7ef6 --- /dev/null +++ b/contracts/proxy-actions/meta/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "proxy-actions-meta" +version = "0.0.0" +edition = "2018" +publish = false +authors = [ "you",] + +[dev-dependencies] + +[dependencies.proxy-actions] +path = ".." + +[dependencies.multiversx-sc-meta] +version = "0.43.4" diff --git a/contracts/proxy-actions/meta/src/main.rs b/contracts/proxy-actions/meta/src/main.rs new file mode 100644 index 00000000..b266f345 --- /dev/null +++ b/contracts/proxy-actions/meta/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + multiversx_sc_meta::cli_main::(); +} diff --git a/contracts/proxy-actions/multiversx.json b/contracts/proxy-actions/multiversx.json new file mode 100644 index 00000000..73655396 --- /dev/null +++ b/contracts/proxy-actions/multiversx.json @@ -0,0 +1,3 @@ +{ + "language": "rust" +} \ No newline at end of file diff --git a/contracts/proxy-actions/scenarios/empty.scen.json b/contracts/proxy-actions/scenarios/empty.scen.json new file mode 100644 index 00000000..1d569c21 --- /dev/null +++ b/contracts/proxy-actions/scenarios/empty.scen.json @@ -0,0 +1,39 @@ +{ + "name": "proxy-actions", + "steps": [ + { + "step": "setState", + "accounts": { + "address:owner": { + "nonce": "1", + "balance": "0" + } + }, + "newAddresses": [ + { + "creatorAddress": "address:owner", + "creatorNonce": "1", + "newAddress": "sc:empty" + } + ] + }, + { + "step": "scDeploy", + "id": "deploy", + "tx": { + "from": "address:owner", + "contractCode": "file:../output/proxy-actions.wasm", + "arguments": [], + "gasLimit": "5,000,000", + "gasPrice": "0" + }, + "expect": { + "out": [], + "status": "", + "logs": [], + "gas": "*", + "refund": "*" + } + } + ] +} \ No newline at end of file diff --git a/contracts/proxy-actions/src/proxy_actions.rs b/contracts/proxy-actions/src/proxy_actions.rs new file mode 100644 index 00000000..055be105 --- /dev/null +++ b/contracts/proxy-actions/src/proxy_actions.rs @@ -0,0 +1,12 @@ +#![no_std] + +multiversx_sc::imports!(); + +mod structs; + +/// An empty contract. To be used as a template when starting a new contract from scratch. +#[multiversx_sc::contract] +pub trait ProxyActionsContract: structs::ActionCall { + #[init] + fn init(&self) {} +} diff --git a/contracts/proxy-actions/src/structs.rs b/contracts/proxy-actions/src/structs.rs new file mode 100644 index 00000000..957349ae --- /dev/null +++ b/contracts/proxy-actions/src/structs.rs @@ -0,0 +1,144 @@ +multiversx_sc::imports!(); +multiversx_sc::derive_imports!(); + +#[derive(TypeAbi, TopEncode, TopDecode, PartialEq, ManagedVecItem)] +pub enum TaskType { + None, + WrapEGLD, + UnwrapEgld, + Swap, + SendEsdt, + ExitLP, +} + +#[multiversx_sc::module] +pub trait TaskCall { + #[payable("*")] + #[endpoint(composeTasks1)] + fn compose_tasks1( + &self, + tasks: MultiValueEncoded< + MultiValue5< + TaskType, + ManagedAddress, + ManagedBuffer, + MultiValueEncoded, + u64, + >, + >, + ) //-> Result<(), EsdtTokenPayment> + { + let payment = self.call_value().egld_or_single_esdt(); + let mut payment_to_next_task = payment.clone(); + + for task in tasks.into_iter() { + let (task_type, dest_addr, endpoint_name, endpoint_args, gas_limit) = task.into_tuple(); + + let payment_to_next_task = self + .send() + .contract_call::<(EsdtTokenPayment)>(dest_addr, endpoint_name) + .with_egld_or_single_esdt_transfer(payment) + .with_raw_arguments(endpoint_args.to_arg_buffer()) + .with_gas_limit(gas_limit) + .transfer_execute(); + } + // let result; + // for proxy_action_id in proxy_action_ids { + // let proxy_action = self.proxy_actions(proxy_action_id).get(); + + // // Payments options + // let payments = self.call_value().all_esdt_transfers(); + // // let payments = egld payment based on ContractCallType + // // let payments = use the previous result; + + // let contract_call = + // ContractCallNoPayment::new(proxy_address, call.endpoint_name, payments); + + // for arg in proxy_action.args { + // // or use previous result + // contract_call.proxy_arg(&arg); + // } + + // raw_result = match contract_call_type { + // ContractCallType::ContractCallWithAnyPayment => { + // contract_call + // .with_gas_limit(proxy_action.gas_limit) + // .transfer_execute(); + // } + // } + + // result = raw_result.decode() + // } + } + + #[payable("*")] + #[endpoint(composeTasks2)] + fn compose_tasks2(&self, tasks: MultiValueEncoded>) + //-> Result<(), EsdtTokenPayment> + { + let payment = self.call_value().egld_or_single_esdt(); + let mut payment_to_next_task = payment.clone(); + + for task in tasks.into_iter() { + let (task_type, dest_addr, gas_limit) = task.into_tuple(); + + let payment_to_next_task = match task_type { + TaskType::WrapEGLD => { + //TODO + } + TaskType::UnwrapEgld => { + //TODO + } + TaskType::Swap => { + // self + // .pair_proxy(dest_addr) + // .swap_tokens_fixed_input(requested_token_id, min_amount_out) + // .with_esdt_transfer(payment) + // .execute_on_dest_context(), + } + TaskType::SendEsdt => { + self.send() + .contract_call::<(EsdtTokenPayment)>(dest_addr, b"") + .with_egld_or_single_esdt_transfer(payment) + .with_gas_limit(gas_limit) + .transfer_execute(); + } + TaskType::ExitLP => { + //TODO + } + TaskType::None => { + // return Ok(()) + } + }; + } + // let result; + // for proxy_action_id in proxy_action_ids { + // let proxy_action = self.proxy_actions(proxy_action_id).get(); + + // // Payments options + // let payments = self.call_value().all_esdt_transfers(); + // // let payments = egld payment based on ContractCallType + // // let payments = use the previous result; + + // let contract_call = + // ContractCallNoPayment::new(proxy_address, call.endpoint_name, payments); + + // for arg in proxy_action.args { + // // or use previous result + // contract_call.proxy_arg(&arg); + // } + + // raw_result = match contract_call_type { + // ContractCallType::ContractCallWithAnyPayment => { + // contract_call + // .with_gas_limit(proxy_action.gas_limit) + // .transfer_execute(); + // } + // } + + // result = raw_result.decode() + // } + } + #[proxy] + fn pair_contract_proxy(&self, to: ManagedAddress) -> pair::Proxy; +} diff --git a/contracts/proxy-actions/tests/empty_scenario_go_test.rs b/contracts/proxy-actions/tests/empty_scenario_go_test.rs new file mode 100644 index 00000000..ef0a7f21 --- /dev/null +++ b/contracts/proxy-actions/tests/empty_scenario_go_test.rs @@ -0,0 +1,10 @@ +use multiversx_sc_scenario::*; + +fn world() -> ScenarioWorld { + ScenarioWorld::vm_go() +} + +#[test] +fn empty_go() { + world().run("scenarios/empty.scen.json"); +} diff --git a/contracts/proxy-actions/tests/empty_scenario_rs_test.rs b/contracts/proxy-actions/tests/empty_scenario_rs_test.rs new file mode 100644 index 00000000..2ab76705 --- /dev/null +++ b/contracts/proxy-actions/tests/empty_scenario_rs_test.rs @@ -0,0 +1,17 @@ +use multiversx_sc_scenario::*; + +fn world() -> ScenarioWorld { + let mut blockchain = ScenarioWorld::new(); + blockchain.set_current_dir_from_workspace("contracts/examples/empty"); + + blockchain.register_contract( + "file:output/proxy-actions.wasm", + proxy_actions::ContractBuilder, + ); + blockchain +} + +#[test] +fn empty_rs() { + world().run("scenarios/empty.scen.json"); +} diff --git a/contracts/proxy-actions/wasm/Cargo.lock b/contracts/proxy-actions/wasm/Cargo.lock new file mode 100644 index 00000000..5a9b6622 --- /dev/null +++ b/contracts/proxy-actions/wasm/Cargo.lock @@ -0,0 +1,209 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ahash" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", +] + +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "empty" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "empty-wasm" +version = "0.0.0" +dependencies = [ + "empty", + "multiversx-sc-wasm-adapter", +] + +[[package]] +name = "endian-type" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" + +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" +dependencies = [ + "ahash", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hex-literal" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ebdb29d2ea9ed0083cd8cece49bbd968021bd99b0849edb4a9a7ee0fdf6a4e0" + +[[package]] +name = "multiversx-sc" +version = "0.43.4" +dependencies = [ + "bitflags", + "hashbrown", + "hex-literal", + "multiversx-sc-codec", + "multiversx-sc-derive", + "num-traits", +] + +[[package]] +name = "multiversx-sc-codec" +version = "0.18.1" +dependencies = [ + "arrayvec", + "multiversx-sc-codec-derive", +] + +[[package]] +name = "multiversx-sc-codec-derive" +version = "0.18.1" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "multiversx-sc-derive" +version = "0.43.4" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "radix_trie", + "syn", +] + +[[package]] +name = "multiversx-sc-wasm-adapter" +version = "0.43.4" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "nibble_vec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" +dependencies = [ + "smallvec", +] + +[[package]] +name = "num-traits" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + +[[package]] +name = "proc-macro2" +version = "1.0.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "radix_trie" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" +dependencies = [ + "endian-type", + "nibble_vec", +] + +[[package]] +name = "smallvec" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" diff --git a/contracts/proxy-actions/wasm/Cargo.toml b/contracts/proxy-actions/wasm/Cargo.toml new file mode 100644 index 00000000..f6d4f011 --- /dev/null +++ b/contracts/proxy-actions/wasm/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "proxy-actions-wasm" +version = "0.0.0" +edition = "2018" +publish = false +authors = [ "you",] + +[lib] +crate-type = [ "cdylib",] + +[workspace] +members = [ ".",] + +[dev-dependencies] + +[profile.release] +codegen-units = 1 +opt-level = "z" +lto = true +debug = false +panic = "abort" + +[dependencies.proxy-actions] +path = ".." + +[dependencies.multiversx-sc-wasm-adapter] +version = "0.43.4" diff --git a/contracts/proxy-actions/wasm/src/lib.rs b/contracts/proxy-actions/wasm/src/lib.rs new file mode 100644 index 00000000..8ea5e5e0 --- /dev/null +++ b/contracts/proxy-actions/wasm/src/lib.rs @@ -0,0 +1,26 @@ +// Code generated by the multiversx-sc multi-contract system. DO NOT EDIT. + +//////////////////////////////////////////////////// +////////////////// AUTO-GENERATED ////////////////// +//////////////////////////////////////////////////// + +// Init: 1 +// Endpoints: 0 +// Async Callback (empty): 1 +// Total number of exported functions: 2 + +#![no_std] +#![allow(internal_features)] +#![feature(lang_items)] + +multiversx_sc_wasm_adapter::allocator!(); +multiversx_sc_wasm_adapter::panic_handler!(); + +multiversx_sc_wasm_adapter::endpoints! { + empty + ( + init => init + ) +} + +multiversx_sc_wasm_adapter::async_callback_empty! {} From 65e8795b83938d21913aaa582d62a30a45dcd3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Wed, 4 Oct 2023 09:21:19 +0300 Subject: [PATCH 2/2] Compilation fixes --- contracts/proxy-actions/src/proxy_actions.rs | 2 +- contracts/proxy-actions/src/structs.rs | 131 ++++++++----------- 2 files changed, 56 insertions(+), 77 deletions(-) diff --git a/contracts/proxy-actions/src/proxy_actions.rs b/contracts/proxy-actions/src/proxy_actions.rs index 055be105..64de31d8 100644 --- a/contracts/proxy-actions/src/proxy_actions.rs +++ b/contracts/proxy-actions/src/proxy_actions.rs @@ -6,7 +6,7 @@ mod structs; /// An empty contract. To be used as a template when starting a new contract from scratch. #[multiversx_sc::contract] -pub trait ProxyActionsContract: structs::ActionCall { +pub trait ProxyActionsContract: structs::TaskCall { #[init] fn init(&self) {} } diff --git a/contracts/proxy-actions/src/structs.rs b/contracts/proxy-actions/src/structs.rs index 957349ae..8fa5a102 100644 --- a/contracts/proxy-actions/src/structs.rs +++ b/contracts/proxy-actions/src/structs.rs @@ -11,6 +11,26 @@ pub enum TaskType { ExitLP, } +pub mod pair_proxy { + #[multiversx_sc::proxy] + pub trait PairProxy { + #[payable("*")] + #[endpoint(swapTokensFixedInput)] + fn swap_tokens_fixed_input( + &self, + token_out: TokenIdentifier, + amount_out_min: BigUint, + ) -> EsdtTokenPayment; + + #[view(getSafePriceByDefaultOffset)] + fn get_safe_price_by_default_offset( + &self, + pair_address: ManagedAddress, + input_payment: EsdtTokenPayment, + ) -> EsdtTokenPayment; + } +} + #[multiversx_sc::module] pub trait TaskCall { #[payable("*")] @@ -18,69 +38,41 @@ pub trait TaskCall { fn compose_tasks1( &self, tasks: MultiValueEncoded< - MultiValue5< - TaskType, - ManagedAddress, - ManagedBuffer, - MultiValueEncoded, - u64, - >, + MultiValue4, u64>, >, ) //-> Result<(), EsdtTokenPayment> { let payment = self.call_value().egld_or_single_esdt(); - let mut payment_to_next_task = payment.clone(); + let payment_to_next_task = payment.clone(); for task in tasks.into_iter() { - let (task_type, dest_addr, endpoint_name, endpoint_args, gas_limit) = task.into_tuple(); + let (dest_addr, endpoint_name, endpoint_args, gas_limit) = task.into_tuple(); let payment_to_next_task = self .send() - .contract_call::<(EsdtTokenPayment)>(dest_addr, endpoint_name) - .with_egld_or_single_esdt_transfer(payment) + .contract_call::(dest_addr, endpoint_name) + .with_egld_or_single_esdt_transfer(payment_to_next_task.clone()) .with_raw_arguments(endpoint_args.to_arg_buffer()) .with_gas_limit(gas_limit) .transfer_execute(); } - // let result; - // for proxy_action_id in proxy_action_ids { - // let proxy_action = self.proxy_actions(proxy_action_id).get(); - - // // Payments options - // let payments = self.call_value().all_esdt_transfers(); - // // let payments = egld payment based on ContractCallType - // // let payments = use the previous result; - - // let contract_call = - // ContractCallNoPayment::new(proxy_address, call.endpoint_name, payments); - - // for arg in proxy_action.args { - // // or use previous result - // contract_call.proxy_arg(&arg); - // } - - // raw_result = match contract_call_type { - // ContractCallType::ContractCallWithAnyPayment => { - // contract_call - // .with_gas_limit(proxy_action.gas_limit) - // .transfer_execute(); - // } - // } - - // result = raw_result.decode() - // } } #[payable("*")] #[endpoint(composeTasks2)] - fn compose_tasks2(&self, tasks: MultiValueEncoded>) + fn compose_tasks2( + &self, + tasks: MultiValueEncoded< + MultiValue4, u64>, + >, + ) //-> Result<(), EsdtTokenPayment> { let payment = self.call_value().egld_or_single_esdt(); - let mut payment_to_next_task = payment.clone(); + let payment_to_next_task = payment.clone(); for task in tasks.into_iter() { - let (task_type, dest_addr, gas_limit) = task.into_tuple(); + let (task_type, dest_addr, endpoint_args, gas_limit) = task.into_tuple(); let payment_to_next_task = match task_type { TaskType::WrapEGLD => { @@ -90,16 +82,30 @@ pub trait TaskCall { //TODO } TaskType::Swap => { - // self - // .pair_proxy(dest_addr) - // .swap_tokens_fixed_input(requested_token_id, min_amount_out) - // .with_esdt_transfer(payment) - // .execute_on_dest_context(), + let args = endpoint_args.into_vec_of_buffers(); + let token_out = TokenIdentifier::from(args.get(0).clone_value()); + let min_amount_out = BigUint::from(args.get(1).clone_value()); + let token_payment = payment_to_next_task + .token_identifier + .clone() + .into_esdt_option(); + if token_payment.is_none() { + return; + } + self.pair_contract_proxy(dest_addr) + .swap_tokens_fixed_input(token_out, min_amount_out) + .with_esdt_transfer(EsdtTokenPayment::new( + token_payment.unwrap(), + payment_to_next_task.token_nonce, + payment_to_next_task.amount.clone(), + )) + .with_gas_limit(gas_limit) + .transfer_execute(); } TaskType::SendEsdt => { self.send() - .contract_call::<(EsdtTokenPayment)>(dest_addr, b"") - .with_egld_or_single_esdt_transfer(payment) + .contract_call::(dest_addr, b"".into()) + .with_egld_or_single_esdt_transfer(payment_to_next_task.clone()) .with_gas_limit(gas_limit) .transfer_execute(); } @@ -111,34 +117,7 @@ pub trait TaskCall { } }; } - // let result; - // for proxy_action_id in proxy_action_ids { - // let proxy_action = self.proxy_actions(proxy_action_id).get(); - - // // Payments options - // let payments = self.call_value().all_esdt_transfers(); - // // let payments = egld payment based on ContractCallType - // // let payments = use the previous result; - - // let contract_call = - // ContractCallNoPayment::new(proxy_address, call.endpoint_name, payments); - - // for arg in proxy_action.args { - // // or use previous result - // contract_call.proxy_arg(&arg); - // } - - // raw_result = match contract_call_type { - // ContractCallType::ContractCallWithAnyPayment => { - // contract_call - // .with_gas_limit(proxy_action.gas_limit) - // .transfer_execute(); - // } - // } - - // result = raw_result.decode() - // } } #[proxy] - fn pair_contract_proxy(&self, to: ManagedAddress) -> pair::Proxy; + fn pair_contract_proxy(&self, to: ManagedAddress) -> pair_proxy::Proxy; }