diff --git a/contracts/index_executor/src/actions/acala/dex.rs b/contracts/index_executor/src/actions/acala/dex.rs index 0386bde..dcb0859 100644 --- a/contracts/index_executor/src/actions/acala/dex.rs +++ b/contracts/index_executor/src/actions/acala/dex.rs @@ -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::*; @@ -21,7 +21,7 @@ impl AcalaSwap { } impl CallBuilder for AcalaSwap { - fn build_call(&self, step: Step) -> Result, &'static str> { + fn build_call(&self, step: Step) -> Result { let amount_out = Compact(1_u8); let amount_in = Compact(step.spend_amount.ok_or("MissingSpendAmount")?); @@ -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 @@ -72,6 +72,6 @@ impl CallBuilder for AcalaSwap { }), input_call: None, call_index: None, - }]) + }) } } diff --git a/contracts/index_executor/src/actions/acala/transfer.rs b/contracts/index_executor/src/actions/acala/transfer.rs index d3a8012..7bd01b7 100644 --- a/contracts/index_executor/src/actions/acala/transfer.rs +++ b/contracts/index_executor/src/actions/acala/transfer.rs @@ -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; @@ -23,7 +22,7 @@ impl AcalaTransactor { } impl CallBuilder for AcalaTransactor { - fn build_call(&self, step: Step) -> Result, &'static str> { + fn build_call(&self, step: Step) -> Result { 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(); @@ -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 @@ -47,7 +46,7 @@ impl CallBuilder for AcalaTransactor { }), input_call: None, call_index: None, - }]) + }) } _ => { let currency_id = match asset_type { @@ -57,7 +56,7 @@ impl CallBuilder for AcalaTransactor { } _ => currency_id, }; - Ok(vec![Call { + Ok(Call { params: CallParams::Sub(SubCall { calldata: SubExtrinsic { // Currencies @@ -69,7 +68,7 @@ impl CallBuilder for AcalaTransactor { }), input_call: None, call_index: None, - }]) + }) } } } diff --git a/contracts/index_executor/src/actions/astar/sub.rs b/contracts/index_executor/src/actions/astar/sub.rs index 948f19f..532cdeb 100644 --- a/contracts/index_executor/src/actions/astar/sub.rs +++ b/contracts/index_executor/src/actions/astar/sub.rs @@ -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}; @@ -34,7 +34,7 @@ impl AstarSubToEvmTransactor { } impl CallBuilder for AstarSubToEvmTransactor { - fn build_call(&self, step: Step) -> Result, &'static str> { + fn build_call(&self, step: Step) -> Result { 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()); @@ -57,7 +57,7 @@ impl AstarTransactor { } impl CallBuilder for AstarTransactor { - fn build_call(&self, step: Step) -> Result, &'static str> { + fn build_call(&self, step: Step) -> Result { 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(); @@ -65,7 +65,7 @@ impl CallBuilder for AstarTransactor { 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 @@ -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 @@ -94,7 +94,7 @@ impl CallBuilder for AstarTransactor { }), input_call: None, call_index: None, - }]) + }) } } } @@ -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(""), @@ -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, @@ -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(""), @@ -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, @@ -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(""), @@ -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, diff --git a/contracts/index_executor/src/actions/base/native_wrapper.rs b/contracts/index_executor/src/actions/base/native_wrapper.rs index 26de900..f7fd4e3 100644 --- a/contracts/index_executor/src/actions/base/native_wrapper.rs +++ b/contracts/index_executor/src/actions/base/native_wrapper.rs @@ -1,4 +1,3 @@ -use alloc::{vec, vec::Vec}; use pink_web3::{ api::{Eth, Namespace}, contract::{tokens::Tokenize, Contract}, @@ -28,7 +27,7 @@ impl NativeWrapper { } impl CallBuilder for NativeWrapper { - fn build_call(&self, step: Step) -> Result, &'static str> { + fn build_call(&self, step: Step) -> Result { 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")?); @@ -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, @@ -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 @@ -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, @@ -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") } @@ -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( diff --git a/contracts/index_executor/src/actions/base/uniswapv2.rs b/contracts/index_executor/src/actions/base/uniswapv2.rs index 9b88667..f7d1891 100644 --- a/contracts/index_executor/src/actions/base/uniswapv2.rs +++ b/contracts/index_executor/src/actions/base/uniswapv2.rs @@ -1,4 +1,4 @@ -use alloc::{vec, vec::Vec}; +use alloc::vec; use pink_web3::{ api::{Eth, Namespace}, contract::{tokens::Tokenize, Contract}, @@ -31,7 +31,7 @@ impl UniswapV2 { } impl CallBuilder for UniswapV2 { - fn build_call(&self, step: Step) -> Result, &'static str> { + fn build_call(&self, step: Step) -> Result { 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")?); @@ -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, + }) } } diff --git a/contracts/index_executor/src/actions/base/uniswapv3.rs b/contracts/index_executor/src/actions/base/uniswapv3.rs index 0ec6f8a..61d49a3 100644 --- a/contracts/index_executor/src/actions/base/uniswapv3.rs +++ b/contracts/index_executor/src/actions/base/uniswapv3.rs @@ -1,4 +1,3 @@ -use alloc::{vec, vec::Vec}; use pink_web3::{ api::{Eth, Namespace}, contract::{tokens::Tokenize, Contract}, @@ -31,7 +30,7 @@ impl UniswapV3 { } impl CallBuilder for UniswapV3 { - fn build_call(&self, step: Step) -> Result, &'static str> { + fn build_call(&self, step: Step) -> Result { 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")?); @@ -51,55 +50,23 @@ impl CallBuilder for UniswapV3 { .encode_input(&[Token::Tuple(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), - - need_settle: true, - update_offset: U256::from(132), - update_len: U256::from(32), - spend_asset: asset0, - spend_amount: amount_in, - receive_asset: asset1, - }), - input_call: None, - call_index: None, - }, - ]) + Ok(Call { + params: CallParams::Evm(EvmCall { + target: self.router.address(), + calldata: swap_calldata, + value: U256::from(0), + + need_settle: true, + update_offset: U256::from(132), + 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, + }) } } @@ -113,20 +80,22 @@ mod tests { use pink_web3::{ api::{Eth, Namespace}, contract::{Contract, Options}, + keys::pink::KeyPair, transports::{resolve_ready, PinkHttp}, }; #[test] + #[ignore] fn should_work() { dotenv().ok(); pink_extension_runtime::mock_ext::mock_all_ext(); use pink_web3::types::Address; - // let secret_key = std::env::vars().find(|x| x.0 == "SECRET_KEY"); - // let secret_key = secret_key.unwrap().1; - // let secret_bytes = hex::decode(secret_key).unwrap(); - // let _signer: [u8; 32] = secret_bytes.to_array(); + let secret_key = std::env::vars().find(|x| x.0 == "SECRET_KEY"); + let secret_key = secret_key.unwrap().1; + let secret_bytes = hex::decode(secret_key).unwrap(); + let signer: [u8; 32] = secret_bytes.to_array(); // Handler on Moonbeam let handler_address: H160 = @@ -145,7 +114,7 @@ mod tests { "https://rpc.api.moonbeam.network", stellaswap_routerv3.into(), ); - let mut calls = stellaswap_v3 + let mut call = stellaswap_v3 .build_call(Step { exe_type: String::from(""), exe: String::from(""), @@ -165,15 +134,13 @@ mod tests { .unwrap(); // Apply index mannually - calls[0].input_call = Some(0); - calls[0].call_index = Some(0); - calls[1].input_call = Some(0); - calls[1].call_index = Some(1); + call.input_call = Some(0); + call.call_index = Some(0); // Estiamte gas before submission - let _gas = resolve_ready(handler.estimate_gas( + let gas = resolve_ready(handler.estimate_gas( "batchCall", - calls.clone(), + call.clone(), Address::from_slice(&hex::decode("bf526928373748b00763875448ee905367d97f96").unwrap()), Options::default(), )) @@ -184,20 +151,18 @@ mod tests { .unwrap(); // Uncomment if wanna send it to blockchain - // let _tx_id = resolve_ready(handler.signed_call( - // "batchCall", - // calls, - // Options::with(|opt| { - // opt.gas = Some(gas); - // }), - // KeyPair::from(signer), - // )) - // .map_err(|e| { - // println!( - // "Failed to submit step execution tx with error: {:?}", - // e - // ); - // "FailedToSubmitTransaction" - // }).unwrap(); + let _tx_id = resolve_ready(handler.signed_call( + "batchCall", + call, + Options::with(|opt| { + opt.gas = Some(gas); + }), + KeyPair::from(signer), + )) + .map_err(|e| { + println!("Failed to submit step execution tx with error: {:?}", e); + "FailedToSubmitTransaction" + }) + .unwrap(); } } diff --git a/contracts/index_executor/src/actions/moonbeam/xtoken.rs b/contracts/index_executor/src/actions/moonbeam/xtoken.rs index 3e950e6..8783f74 100644 --- a/contracts/index_executor/src/actions/moonbeam/xtoken.rs +++ b/contracts/index_executor/src/actions/moonbeam/xtoken.rs @@ -12,7 +12,7 @@ use crate::step::Step; #[derive(Clone)] pub struct XTokenBridge { - eth: Eth, + _eth: Eth, xtoken: Contract, dest_chain_id: u32, } @@ -28,7 +28,7 @@ impl XTokenBridge { .expect("Bad abi data"); Self { - eth, + _eth: eth, xtoken, dest_chain_id, } @@ -36,7 +36,7 @@ impl XTokenBridge { } impl CallBuilder for XTokenBridge { - fn build_call(&self, step: Step) -> Result, &'static str> { + fn build_call(&self, step: Step) -> Result { let spend_asset = Address::from_slice(&step.spend_asset); // We don't use it let receive_asset = Address::from_slice(&[0; 20]); @@ -73,7 +73,6 @@ impl CallBuilder for XTokenBridge { ]), ]); let bridge_params = (spend_asset, spend_amount, location, weight); - let bridge_func = self .xtoken .abi() @@ -83,56 +82,24 @@ impl CallBuilder for XTokenBridge { .encode_input(&bridge_params.into_tokens()) .map_err(|_| "EncodeParamError")?; - let token = Contract::from_json( - self.eth.clone(), - spend_asset, - include_bytes!("../../abi/erc20.json"), - ) - .expect("Bad abi data"); - let approve_params = (self.xtoken.address(), spend_amount); - 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: spend_asset, - calldata: approve_calldata, - value: U256::from(0), - - need_settle: false, - update_offset: U256::from(36), - update_len: U256::from(32), - spend_asset, - spend_amount, - receive_asset, - }), - input_call: None, - call_index: None, - }, - Call { - params: CallParams::Evm(EvmCall { - target: self.xtoken.address(), - calldata: bridge_calldata, - value: U256::from(0), + Ok(Call { + params: CallParams::Evm(EvmCall { + target: self.xtoken.address(), + calldata: bridge_calldata, + value: U256::from(0), - // Bridge operation do not need do settlement on source chain, because it must be the - // last step on source chain - need_settle: false, - update_offset: U256::from(36), - update_len: U256::from(32), - spend_asset, - spend_amount, - receive_asset, - }), - input_call: None, - call_index: None, - }, - ]) + // Bridge operation do not need do settlement on source chain, because it must be the + // last step on source chain + need_settle: false, + update_offset: U256::from(36), + update_len: U256::from(32), + spender: self.xtoken.address(), + spend_asset, + spend_amount, + receive_asset, + }), + input_call: None, + call_index: None, + }) } } diff --git a/contracts/index_executor/src/actions/phala/xtransfer.rs b/contracts/index_executor/src/actions/phala/xtransfer.rs index e7a2a53..fdf19a8 100644 --- a/contracts/index_executor/src/actions/phala/xtransfer.rs +++ b/contracts/index_executor/src/actions/phala/xtransfer.rs @@ -1,4 +1,3 @@ -use alloc::{vec, vec::Vec}; use scale::{Decode, Encode}; use crate::call::{Call, CallBuilder, CallParams, SubCall, SubExtrinsic}; @@ -29,7 +28,7 @@ impl XTransferXcm { } impl CallBuilder for XTransferXcm { - fn build_call(&self, step: Step) -> Result, &'static str> { + fn build_call(&self, step: Step) -> Result { let recipient = step.recipient.ok_or("MissingRecipient")?; let asset_location: MultiLocation = Decode::decode(&mut step.spend_asset.as_slice()).map_err(|_| "InvalidMultilocation")?; @@ -61,7 +60,7 @@ impl CallBuilder for XTransferXcm { ); let dest_weight: Weight = Weight::from_parts(6000000000_u64, 1000000_u64); - Ok(vec![Call { + Ok(Call { params: CallParams::Sub(SubCall { calldata: SubExtrinsic { pallet_id: 0x52u8, @@ -72,7 +71,7 @@ impl CallBuilder for XTransferXcm { }), input_call: None, call_index: None, - }]) + }) } } @@ -87,7 +86,7 @@ mod tests { // dest chain account type account_type: AccountType::Account32, }; - let calls = xtransfer + let call = xtransfer .build_call(Step { exe_type: String::from(""), exe: String::from(""), @@ -107,7 +106,7 @@ mod tests { }) .unwrap(); - match &calls[0].params { + match &call.params { CallParams::Sub(sub_call) => { println!("calldata: {:?}", hex::encode(&sub_call.calldata)) } diff --git a/contracts/index_executor/src/call.rs b/contracts/index_executor/src/call.rs index f7fd3a9..6ef14a9 100644 --- a/contracts/index_executor/src/call.rs +++ b/contracts/index_executor/src/call.rs @@ -22,6 +22,7 @@ pub struct EvmCall { pub need_settle: bool, pub update_offset: U256, pub update_len: U256, + pub spender: Address, pub spend_asset: Address, pub spend_amount: U256, pub receive_asset: Address, @@ -41,6 +42,7 @@ pub struct SubCall { pub calldata: Vec, } +#[allow(clippy::large_enum_variant)] #[derive(Clone, Decode, Encode, Eq, PartialEq, Ord, PartialOrd, Debug)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum CallParams { @@ -73,6 +75,7 @@ impl Tokenizable for Call { tokens.push(evm_call.need_settle.into_token()); tokens.push(evm_call.update_offset.into_token()); tokens.push(evm_call.update_len.into_token()); + tokens.push(evm_call.spender.into_token()); tokens.push(evm_call.spend_asset.into_token()); tokens.push(evm_call.spend_amount.into_token()); tokens.push(evm_call.receive_asset.into_token()); @@ -88,6 +91,6 @@ impl Tokenizable for Call { impl TokenizableItem for Call {} pub trait CallBuilder: DynClone { - fn build_call(&self, step: Step) -> Result, &'static str>; + fn build_call(&self, step: Step) -> Result; } dyn_clone::clone_trait_object!(CallBuilder); diff --git a/contracts/index_executor/src/step.rs b/contracts/index_executor/src/step.rs index 8ce59f7..8d20baf 100644 --- a/contracts/index_executor/src/step.rs +++ b/contracts/index_executor/src/step.rs @@ -101,7 +101,8 @@ impl Step { "Trying to build calldata for according to step data: {:?}", self, ); - action.build_call(self.clone()) + let call = action.build_call(self.clone())?; + Ok(vec![call]) } pub fn is_bridge_step(&self) -> bool { diff --git a/contracts/index_executor/src/task.rs b/contracts/index_executor/src/task.rs index a9e9cc3..e78c9dc 100644 --- a/contracts/index_executor/src/task.rs +++ b/contracts/index_executor/src/task.rs @@ -618,7 +618,6 @@ mod tests { use crate::utils::ToArray; use dotenv::dotenv; use hex_literal::hex; - use pink_web3::contract::tokens::Tokenize; use primitive_types::H160; #[test] @@ -1356,306 +1355,32 @@ mod tests { step.set_spend(0xf0f1f2f3f4f5f6f7f8f9); calls.append(&mut step.derive_calls(&context).unwrap()); } - assert_eq!(calls.len(), 2 * 3 + 1 + 1 + 2 * 2); + assert_eq!(calls.len(), 3 + 1 + 1 + 2); // Origin Step means Steps before merge // ========== First Merged Step ============= - // calls[0] and calls[1] build according to origin Step 0, + // calls[0] build according to origin Step 0, // and origin Step 0 don't relay any previous steps happened // on the same chain assert_eq!(calls[0].input_call, Some(0)); - assert_eq!(calls[1].input_call, Some(0)); - // calls[2] and calls[3] build according to origin Step 1, + // calls[1] build according to origin Step 1, // and origin Step 1 relay Step 0 as input, so take last call // of Step 0 as input call - assert_eq!(calls[2].input_call, Some(1)); - assert_eq!(calls[3].input_call, Some(1)); - // calls[4] and calls[5] build according to origin Step 2, + assert_eq!(calls[1].input_call, Some(0)); + // calls[2] build according to origin Step 2, // and origin Step 2 relay Step 1 as input, so take last call // of Step 1 as input call - assert_eq!(calls[4].input_call, Some(3)); - assert_eq!(calls[5].input_call, Some(3)); + assert_eq!(calls[2].input_call, Some(1)); // ========== Second Merged Step ============= - // calls[0] and calls[1] build according to origin Step 5, + // calls[5] build according to origin Step 5, // and origin Step 5 don't relay any previous steps heppened // on the same chain - assert_eq!(calls[0].input_call, Some(0)); - assert_eq!(calls[1].input_call, Some(0)); - // calls[2] and calls[3] build according to origin Step 6, + assert_eq!(calls[5].input_call, Some(0)); + // calls[6] build according to origin Step 6, // and origin Step 6 relay Step 5 as input, so take last call // of Step 5 as input call - assert_eq!(calls[2].input_call, Some(1)); - assert_eq!(calls[3].input_call, Some(1)); - } - - #[test] - fn test_calldata() { - dotenv().ok(); - pink_extension_runtime::mock_ext::mock_all_ext(); - - use crate::call::{Call, CallParams, EvmCall}; - use pink_web3::types::{Address, U256}; - - let handler: H160 = - H160::from_slice(&hex::decode("B30A27eE79514614dc363CE0aABb0B939b9deAeD").unwrap()); - let transport = Eth::new(PinkHttp::new("https://rpc.api.moonbeam.network")); - let handler = - Contract::from_json(transport, handler, include_bytes!("./abi/handler.json")).unwrap(); - let task_id: [u8; 32] = - hex::decode("1125000000000000000000000000000000000000000000000000000000000000") - .unwrap() - .to_array(); - // We call claimAndBatchCall so that first step will be executed along with the claim operation - - let params = ( - task_id, - vec![ - Call { - params: CallParams::Evm(EvmCall { - target: Address::from_slice( - hex::decode("acc15dc74880c9944775448304b263d191c6077f") - .unwrap() - .as_slice(), - ), - calldata: vec![ - 9, 94, 167, 179, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 8, 90, 9, - 211, 13, 111, 140, 78, 207, 110, 225, 1, 32, 209, 132, 115, 131, 187, - 87, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 13, 224, 182, 179, 167, 100, 0, 0, - ], - value: U256::from(0), - - need_settle: false, - update_offset: U256::from(36), - update_len: U256::from(32), - spend_asset: Address::from_slice( - hex::decode("acc15dc74880c9944775448304b263d191c6077f") - .unwrap() - .as_slice(), - ), - spend_amount: U256::from(1000000000000000000_u128), - receive_asset: Address::from_slice( - hex::decode("acc15dc74880c9944775448304b263d191c6077f") - .unwrap() - .as_slice(), - ), - }), - input_call: Some(0), - call_index: Some(0), - }, - Call { - params: CallParams::Evm(EvmCall { - target: Address::from_slice( - hex::decode("70085a09d30d6f8c4ecf6ee10120d1847383bb57") - .unwrap() - .as_slice(), - ), - calldata: vec![ - 56, 237, 23, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 13, 224, 182, 179, 167, 100, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, - 94, 168, 104, 4, 32, 15, 128, 193, 110, 168, 237, 220, 60, 116, 154, - 84, 169, 195, 125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101, 14, 240, 187, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 172, 193, 93, 199, 72, 128, - 201, 148, 71, 117, 68, 131, 4, 178, 99, 209, 145, 198, 7, 127, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 31, 202, 203, 210, 24, - 237, 192, 235, 162, 15, 194, 48, 140, 119, 128, 128, - ], - value: U256::from(0), - - need_settle: true, - update_offset: U256::from(4), - update_len: U256::from(32), - spend_asset: Address::from_slice( - hex::decode("acc15dc74880c9944775448304b263d191c6077f") - .unwrap() - .as_slice(), - ), - spend_amount: U256::from(1000000000000000000_u128), - receive_asset: Address::from_slice( - hex::decode("ffffffff1fcacbd218edc0eba20fc2308c778080") - .unwrap() - .as_slice(), - ), - }), - input_call: Some(0), - call_index: Some(1), - }, - Call { - params: CallParams::Evm(EvmCall { - target: Address::from_slice( - hex::decode("ffffffff1fcacbd218edc0eba20fc2308c778080") - .unwrap() - .as_slice(), - ), - calldata: vec![ - 9, 94, 167, 179, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 112, 8, 90, 9, - 211, 13, 111, 140, 78, 207, 110, 225, 1, 32, 209, 132, 115, 131, 187, - 87, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ], - value: U256::from(0), - - need_settle: false, - update_offset: U256::from(36), - update_len: U256::from(32), - spend_asset: Address::from_slice( - hex::decode("ffffffff1fcacbd218edc0eba20fc2308c778080") - .unwrap() - .as_slice(), - ), - spend_amount: U256::from(0), - receive_asset: Address::from_slice( - hex::decode("ffffffff1fcacbd218edc0eba20fc2308c778080") - .unwrap() - .as_slice(), - ), - }), - input_call: Some(1), - call_index: Some(2), - }, - Call { - params: CallParams::Evm(EvmCall { - target: Address::from_slice( - hex::decode("70085a09d30d6f8c4ecf6ee10120d1847383bb57") - .unwrap() - .as_slice(), - ), - calldata: vec![ - 56, 237, 23, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 94, 168, - 104, 4, 32, 15, 128, 193, 110, 168, 237, 220, 60, 116, 154, 84, 169, - 195, 125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 101, 14, 240, 189, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 31, 202, 203, 210, - 24, 237, 192, 235, 162, 15, 194, 48, 140, 119, 128, 128, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 99, 210, 78, 204, 142, 184, - 167, 181, 208, 128, 62, 144, 15, 123, 108, 237, - ], - value: U256::from(0), - - need_settle: true, - update_offset: U256::from(4), - update_len: U256::from(32), - spend_asset: Address::from_slice( - hex::decode("ffffffff1fcacbd218edc0eba20fc2308c778080") - .unwrap() - .as_slice(), - ), - spend_amount: U256::from(0), - receive_asset: Address::from_slice( - hex::decode("ffffffff63d24ecc8eb8a7b5d0803e900f7b6ced") - .unwrap() - .as_slice(), - ), - }), - input_call: Some(1), - call_index: Some(3), - }, - Call { - params: CallParams::Evm(EvmCall { - target: Address::from_slice( - hex::decode("ffffffff63d24ecc8eb8a7b5d0803e900f7b6ced") - .unwrap() - .as_slice(), - ), - calldata: vec![ - 9, 94, 167, 179, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ], - value: U256::from(0), - - need_settle: false, - update_offset: U256::from(36), - update_len: U256::from(32), - spend_asset: Address::from_slice( - hex::decode("ffffffff63d24ecc8eb8a7b5d0803e900f7b6ced") - .unwrap() - .as_slice(), - ), - spend_amount: U256::from(0), - receive_asset: Address::from_slice( - hex::decode("ffffffff63d24ecc8eb8a7b5d0803e900f7b6ced") - .unwrap() - .as_slice(), - ), - }), - input_call: Some(3), - call_index: Some(4), - }, - Call { - params: CallParams::Evm(EvmCall { - target: Address::from_slice( - hex::decode("0000000000000000000000000000000000000804") - .unwrap() - .as_slice(), - ), - calldata: vec![ - 185, 248, 19, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, - 255, 99, 210, 78, 204, 142, 184, 167, 181, 208, 128, 62, 144, 15, 123, - 108, 237, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 101, 160, 188, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 7, 243, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 34, 1, 4, 219, 160, 103, 127, 194, 116, 255, 172, 204, 15, 161, - 3, 10, 102, 177, 113, 209, 218, 146, 38, 210, 187, 157, 21, 38, 84, - 230, 167, 70, 242, 118, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ], - value: U256::from(0), - - need_settle: false, - update_offset: U256::from(36), - update_len: U256::from(32), - spend_asset: Address::from_slice( - hex::decode("ffffffff63d24ecc8eb8a7b5d0803e900f7b6ced") - .unwrap() - .as_slice(), - ), - spend_amount: U256::from(0), - receive_asset: Address::from_slice( - hex::decode("0000000000000000000000000000000000000000") - .unwrap() - .as_slice(), - ), - }), - input_call: Some(3), - call_index: Some(5), - }, - ], - ); - - let claim_func = handler - .abi() - .function("claimAndBatchCall") - .map_err(|_| "NoFunctionFound") - .unwrap(); - let calldata = claim_func - .encode_input(¶ms.into_tokens()) - .map_err(|_| "EncodeParamError") - .unwrap(); - println!("claim calldata: {:?}", hex::encode(calldata)); + assert_eq!(calls[6].input_call, Some(0)); } }