Skip to content

Commit

Permalink
adjust call builder according to handler update
Browse files Browse the repository at this point in the history
  • Loading branch information
tolak committed Sep 12, 2023
1 parent 8d427f0 commit 6db467e
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 508 deletions.
8 changes: 4 additions & 4 deletions contracts/index_executor/src/actions/acala/dex.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::asset::{AcalaAssets, AggregatedSwapPath, CurrencyId, TokenSymbol};
use alloc::{format, vec, vec::Vec};
use alloc::{format, vec};
use pink_extension::ResultExt;
use scale::{Compact, Decode, Encode};
use xcm::v3::prelude::*;
Expand All @@ -21,7 +21,7 @@ impl AcalaSwap {
}

impl CallBuilder for AcalaSwap {
fn build_call(&self, step: Step) -> Result<Vec<Call>, &'static str> {
fn build_call(&self, step: Step) -> Result<Call, &'static str> {
let amount_out = Compact(1_u8);
let amount_in = Compact(step.spend_amount.ok_or("MissingSpendAmount")?);

Expand Down Expand Up @@ -58,7 +58,7 @@ impl CallBuilder for AcalaSwap {
]);
let path = vec![taiga_path, dex_path];

Ok(vec![Call {
Ok(Call {
params: CallParams::Sub(SubCall {
calldata: SubExtrinsic {
// the call index of acala dex module
Expand All @@ -72,6 +72,6 @@ impl CallBuilder for AcalaSwap {
}),
input_call: None,
call_index: None,
}])
})
}
}
11 changes: 5 additions & 6 deletions contracts/index_executor/src/actions/acala/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use super::asset::{AcalaAssets, CurrencyId, TokenType as AcalaTokenType};
use crate::call::{Call, CallBuilder, CallParams, SubCall, SubExtrinsic};
use crate::step::Step;
use crate::utils::ToArray;
use alloc::{vec, vec::Vec};
use scale::{Compact, Decode, Encode};

type MultiAddress = sp_runtime::MultiAddress<AccountId, u32>;
Expand All @@ -23,7 +22,7 @@ impl AcalaTransactor {
}

impl CallBuilder for AcalaTransactor {
fn build_call(&self, step: Step) -> Result<Vec<Call>, &'static str> {
fn build_call(&self, step: Step) -> Result<Call, &'static str> {
let asset_location = MultiLocation::decode(&mut step.spend_asset.as_slice())
.map_err(|_| "FailedToScaleDecode")?;
let bytes: [u8; 32] = step.recipient.ok_or("MissingRecipient")?.to_array();
Expand All @@ -35,7 +34,7 @@ impl CallBuilder for AcalaTransactor {

match asset_type {
AcalaTokenType::Utility => {
Ok(vec![Call {
Ok(Call {
params: CallParams::Sub(SubCall {
calldata: SubExtrinsic {
// Balance
Expand All @@ -47,7 +46,7 @@ impl CallBuilder for AcalaTransactor {
}),
input_call: None,
call_index: None,
}])
})
}
_ => {
let currency_id = match asset_type {
Expand All @@ -57,7 +56,7 @@ impl CallBuilder for AcalaTransactor {
}
_ => currency_id,
};
Ok(vec![Call {
Ok(Call {
params: CallParams::Sub(SubCall {
calldata: SubExtrinsic {
// Currencies
Expand All @@ -69,7 +68,7 @@ impl CallBuilder for AcalaTransactor {
}),
input_call: None,
call_index: None,
}])
})
}
}
}
Expand Down
26 changes: 13 additions & 13 deletions contracts/index_executor/src/actions/astar/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::asset::AstarAssets;
use crate::call::{Call, CallBuilder, CallParams, SubCall, SubExtrinsic};
use crate::step::Step;
use crate::utils::ToArray;
use alloc::{string::String, vec, vec::Vec};
use alloc::{string::String, vec::Vec};
use pink_subrpc::hasher::{Blake2_256, Hasher};
use scale::{Compact, Decode, Encode};

Expand Down Expand Up @@ -34,7 +34,7 @@ impl AstarSubToEvmTransactor {
}

impl CallBuilder for AstarSubToEvmTransactor {
fn build_call(&self, step: Step) -> Result<Vec<Call>, &'static str> {
fn build_call(&self, step: Step) -> Result<Call, &'static str> {
let bytes: [u8; 20] = step.recipient.clone().ok_or("MissingRecipient")?.to_array();
let mut new_step = step;
new_step.recipient = Some(self.h160_to_sr25519_pub(&bytes).to_vec());
Expand All @@ -57,15 +57,15 @@ impl AstarTransactor {
}

impl CallBuilder for AstarTransactor {
fn build_call(&self, step: Step) -> Result<Vec<Call>, &'static str> {
fn build_call(&self, step: Step) -> Result<Call, &'static str> {
let asset_location = MultiLocation::decode(&mut step.spend_asset.as_slice())
.map_err(|_| "FailedToScaleDecode")?;
let bytes: [u8; 32] = step.recipient.ok_or("MissingRecipient")?.to_array();
let recipient = MultiAddress::Id(AccountId::from(bytes));
let amount = Compact(step.spend_amount.ok_or("MissingSpendAmount")?);

if step.spend_asset == self.native {
Ok(vec![Call {
Ok(Call {
params: CallParams::Sub(SubCall {
calldata: SubExtrinsic {
// Balance
Expand All @@ -77,12 +77,12 @@ impl CallBuilder for AstarTransactor {
}),
input_call: None,
call_index: None,
}])
})
} else {
let asset_id = AstarAssets::new()
.get_assetid(&String::from("Astar"), &asset_location)
.ok_or("AssetNotFound")?;
Ok(vec![Call {
Ok(Call {
params: CallParams::Sub(SubCall {
calldata: SubExtrinsic {
// palletAsset
Expand All @@ -94,7 +94,7 @@ impl CallBuilder for AstarTransactor {
}),
input_call: None,
call_index: None,
}])
})
}
}
}
Expand Down Expand Up @@ -123,7 +123,7 @@ mod tests {
let secret_bytes = hex::decode(secret_key).unwrap();
let signer: [u8; 32] = secret_bytes.to_array();

let calls = transactor
let call = transactor
.build_call(Step {
exe_type: String::from(""),
exe: String::from(""),
Expand All @@ -139,7 +139,7 @@ mod tests {
nonce: None,
})
.unwrap();
match &calls[0].params {
match &call.params {
CallParams::Sub(sub_call) => {
let signed_tx = create_transaction_with_calldata(
&signer,
Expand Down Expand Up @@ -182,7 +182,7 @@ mod tests {
let secret_bytes = hex::decode(secret_key).unwrap();
let signer: [u8; 32] = secret_bytes.to_array();

let calls = transactor
let call = transactor
.build_call(Step {
exe_type: String::from(""),
exe: String::from(""),
Expand All @@ -198,7 +198,7 @@ mod tests {
nonce: None,
})
.unwrap();
match &calls[0].params {
match &call.params {
CallParams::Sub(sub_call) => {
let signed_tx = create_transaction_with_calldata(
&signer,
Expand Down Expand Up @@ -239,7 +239,7 @@ mod tests {
let secret_bytes = hex::decode(secret_key).unwrap();
let signer: [u8; 32] = secret_bytes.to_array();

let calls = transactor
let call = transactor
.build_call(Step {
exe_type: String::from("bridge"),
exe: String::from(""),
Expand All @@ -255,7 +255,7 @@ mod tests {
nonce: None,
})
.unwrap();
match &calls[0].params {
match &call.params {
CallParams::Sub(sub_call) => {
let signed_tx = create_transaction_with_calldata(
&signer,
Expand Down
25 changes: 14 additions & 11 deletions contracts/index_executor/src/actions/base/native_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use alloc::{vec, vec::Vec};
use pink_web3::{
api::{Eth, Namespace},
contract::{tokens::Tokenize, Contract},
Expand Down Expand Up @@ -28,7 +27,7 @@ impl NativeWrapper {
}

impl CallBuilder for NativeWrapper {
fn build_call(&self, step: Step) -> Result<Vec<Call>, &'static str> {
fn build_call(&self, step: Step) -> Result<Call, &'static str> {
let spend_asset = Address::from_slice(&step.spend_asset);
let receive_asset = Address::from_slice(&step.receive_asset);
let spend_amount = U256::from(step.spend_amount.ok_or("MissingSpendAmount")?);
Expand All @@ -43,7 +42,7 @@ impl CallBuilder for NativeWrapper {
let deposit_calldata = deposit_func
.encode_input(&[])
.map_err(|_| "EncodeParamError")?;
Ok(vec![Call {
Ok(Call {
params: CallParams::Evm(EvmCall {
target: self.weth9.address(),
calldata: deposit_calldata,
Expand All @@ -52,13 +51,15 @@ impl CallBuilder for NativeWrapper {
need_settle: true,
update_offset: U256::from(0),
update_len: U256::from(0),
// No spender
spender: Address::from(&[0; 20]),
spend_asset,
spend_amount,
receive_asset,
}),
input_call: None,
call_index: None,
}])
})
} else if spend_asset == self.weth9.address() && receive_asset == self.native {
// Withdraw
let withdraw_func = self
Expand All @@ -69,7 +70,7 @@ impl CallBuilder for NativeWrapper {
let withdraw_calldata = withdraw_func
.encode_input(&spend_amount.into_tokens())
.map_err(|_| "EncodeParamError")?;
Ok(vec![Call {
Ok(Call {
params: CallParams::Evm(EvmCall {
target: self.weth9.address(),
calldata: withdraw_calldata,
Expand All @@ -78,13 +79,15 @@ impl CallBuilder for NativeWrapper {
need_settle: true,
update_offset: U256::from(4),
update_len: U256::from(32),
// No spender
spender: Address::from(&[0; 20]),
spend_asset,
spend_amount,
receive_asset,
}),
input_call: None,
call_index: None,
}])
})
} else {
Err("UnrecognizedArguments")
}
Expand Down Expand Up @@ -178,12 +181,12 @@ mod tests {
.unwrap();

// Apply index mannually
deposit_call[0].input_call = Some(0);
deposit_call[0].call_index = Some(0);
withdraw_call[0].input_call = Some(0);
withdraw_call[0].call_index = Some(1);
deposit_call.input_call = Some(0);
deposit_call.call_index = Some(0);
withdraw_call.input_call = Some(0);
withdraw_call.call_index = Some(1);

let calls = [deposit_call, withdraw_call].concat().to_vec();
let calls = [deposit_call, withdraw_call].to_vec();

// Estiamte gas before submission
let gas = resolve_ready(handler.estimate_gas(
Expand Down
68 changes: 18 additions & 50 deletions contracts/index_executor/src/actions/base/uniswapv2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::{vec, vec::Vec};
use alloc::vec;
use pink_web3::{
api::{Eth, Namespace},
contract::{tokens::Tokenize, Contract},
Expand Down Expand Up @@ -31,7 +31,7 @@ impl UniswapV2 {
}

impl CallBuilder for UniswapV2 {
fn build_call(&self, step: Step) -> Result<Vec<Call>, &'static str> {
fn build_call(&self, step: Step) -> Result<Call, &'static str> {
let asset0 = Address::from_slice(&step.spend_asset);
let asset1 = Address::from_slice(&step.receive_asset);
let to = Address::from_slice(&step.recipient.ok_or("MissingRecipient")?);
Expand All @@ -51,54 +51,22 @@ impl CallBuilder for UniswapV2 {
.encode_input(&swap_params.into_tokens())
.map_err(|_| "EncodeParamError")?;

let token = Contract::from_json(
self.eth.clone(),
asset0,
include_bytes!("../../abi/erc20.json"),
)
.expect("Bad abi data");
let approve_params = (self.router.address(), amount_in);
let approve_func = token
.abi()
.function("approve")
.map_err(|_| "NoFunctionFound")?;
let approve_calldata = approve_func
.encode_input(&approve_params.into_tokens())
.map_err(|_| "EncodeParamError")?;

Ok(vec![
Call {
params: CallParams::Evm(EvmCall {
target: asset0,
calldata: approve_calldata,
value: U256::from(0),

need_settle: false,
update_offset: U256::from(36),
update_len: U256::from(32),
spend_asset: asset0,
spend_amount: amount_in,
receive_asset: asset0,
}),
input_call: None,
call_index: None,
},
Call {
params: CallParams::Evm(EvmCall {
target: self.router.address(),
calldata: swap_calldata,
value: U256::from(0),
Ok(Call {
params: CallParams::Evm(EvmCall {
target: self.router.address(),
calldata: swap_calldata,
value: U256::from(0),

need_settle: true,
update_offset: U256::from(4),
update_len: U256::from(32),
spend_asset: asset0,
spend_amount: amount_in,
receive_asset: asset1,
}),
input_call: None,
call_index: None,
},
])
need_settle: true,
update_offset: U256::from(4),
update_len: U256::from(32),
spender: self.router.address(),
spend_asset: asset0,
spend_amount: amount_in,
receive_asset: asset1,
}),
input_call: None,
call_index: None,
})
}
}
Loading

0 comments on commit 6db467e

Please sign in to comment.