-
Notifications
You must be signed in to change notification settings - Fork 6
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
Composable tasks to perform complex actions #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
*/target/ | ||
|
||
# The mxpy output | ||
/output*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "proxy-actions" | ||
version = "0.0.0" | ||
authors = [ "MultiversX <[email protected]>" ] | ||
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" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
multiversx_sc_meta::cli_main::<proxy_actions::AbiProvider>(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"language": "rust" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "*" | ||
} | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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::TaskCall { | ||
#[init] | ||
fn init(&self) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
multiversx_sc::imports!(); | ||
multiversx_sc::derive_imports!(); | ||
|
||
#[derive(TypeAbi, TopEncode, TopDecode, PartialEq, ManagedVecItem)] | ||
pub enum TaskType { | ||
None, | ||
WrapEGLD, | ||
UnwrapEgld, | ||
Swap, | ||
SendEsdt, | ||
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("*")] | ||
#[endpoint(composeTasks1)] | ||
fn compose_tasks1( | ||
&self, | ||
tasks: MultiValueEncoded< | ||
MultiValue4<ManagedAddress, ManagedBuffer, MultiValueEncoded<ManagedBuffer>, u64>, | ||
>, | ||
) //-> Result<(), EsdtTokenPayment> | ||
{ | ||
let payment = self.call_value().egld_or_single_esdt(); | ||
let payment_to_next_task = payment.clone(); | ||
Check warning on line 46 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L46
Raw output
Check warning on line 46 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L46
Raw output
|
||
|
||
for task in tasks.into_iter() { | ||
let (dest_addr, endpoint_name, endpoint_args, gas_limit) = task.into_tuple(); | ||
|
||
let payment_to_next_task = self | ||
Check warning on line 51 in contracts/proxy-actions/src/structs.rs GitHub Actions / Contracts / Wasm tests
Check warning on line 51 in contracts/proxy-actions/src/structs.rs GitHub Actions / Contracts / Wasm tests
Check warning on line 51 in contracts/proxy-actions/src/structs.rs GitHub Actions / Contracts / Wasm tests
Check warning on line 51 in contracts/proxy-actions/src/structs.rs GitHub Actions / Contracts / Rust tests
Check warning on line 51 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L51
Raw output
Check warning on line 51 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L51
Raw output
Check warning on line 51 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L51
Raw output
Check warning on line 51 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L51
Raw output
|
||
.send() | ||
.contract_call::<EsdtTokenPayment>(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(); | ||
} | ||
} | ||
|
||
#[payable("*")] | ||
#[endpoint(composeTasks2)] | ||
fn compose_tasks2( | ||
&self, | ||
tasks: MultiValueEncoded< | ||
MultiValue4<TaskType, ManagedAddress, MultiValueEncoded<ManagedBuffer>, u64>, | ||
>, | ||
) | ||
//-> Result<(), EsdtTokenPayment> | ||
{ | ||
let payment = self.call_value().egld_or_single_esdt(); | ||
let payment_to_next_task = payment.clone(); | ||
Check warning on line 72 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L72
Raw output
Check warning on line 72 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L72
Raw output
|
||
|
||
for task in tasks.into_iter() { | ||
let (task_type, dest_addr, endpoint_args, gas_limit) = task.into_tuple(); | ||
|
||
let payment_to_next_task = match task_type { | ||
Check warning on line 77 in contracts/proxy-actions/src/structs.rs GitHub Actions / Contracts / Wasm tests
Check warning on line 77 in contracts/proxy-actions/src/structs.rs GitHub Actions / Contracts / Wasm tests
Check warning on line 77 in contracts/proxy-actions/src/structs.rs GitHub Actions / Contracts / Wasm tests
Check warning on line 77 in contracts/proxy-actions/src/structs.rs GitHub Actions / Contracts / Rust tests
Check warning on line 77 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L77
Raw output
Check warning on line 77 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L77
Raw output
Check warning on line 77 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L77
Raw output
Check warning on line 77 in contracts/proxy-actions/src/structs.rs GitHub Actions / clippy[clippy] contracts/proxy-actions/src/structs.rs#L77
Raw output
|
||
TaskType::WrapEGLD => { | ||
//TODO | ||
} | ||
TaskType::UnwrapEgld => { | ||
//TODO | ||
} | ||
TaskType::Swap => { | ||
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"".into()) | ||
.with_egld_or_single_esdt_transfer(payment_to_next_task.clone()) | ||
.with_gas_limit(gas_limit) | ||
.transfer_execute(); | ||
} | ||
TaskType::ExitLP => { | ||
//TODO | ||
} | ||
TaskType::None => { | ||
// return Ok(()) | ||
} | ||
}; | ||
} | ||
} | ||
#[proxy] | ||
fn pair_contract_proxy(&self, to: ManagedAddress) -> pair_proxy::Proxy<Self::Api>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use multiversx_sc_scenario::*; | ||
|
||
fn world() -> ScenarioWorld { | ||
ScenarioWorld::vm_go() | ||
} | ||
|
||
#[test] | ||
fn empty_go() { | ||
world().run("scenarios/empty.scen.json"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to call the router ? it can do all kinds of swaps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And you would know the router address by default. So it is a setting in the contract. The same is for wrap contract address.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pair contract addresses can be written in storage.