Skip to content

Commit

Permalink
bring back recipient
Browse files Browse the repository at this point in the history
  • Loading branch information
tolak committed Sep 15, 2023
1 parent 7f031b6 commit 622ec45
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 31 deletions.
26 changes: 26 additions & 0 deletions contracts/index_executor/src/abi/handler.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bytes",
"name": "recipient",
"type": "bytes"
}
],
"name": "Deposited",
Expand Down Expand Up @@ -194,6 +200,11 @@
"name": "token",
"type": "address"
},
{
"internalType": "bytes",
"name": "recipient",
"type": "bytes"
},
{
"internalType": "uint256",
"name": "amount",
Expand Down Expand Up @@ -278,6 +289,11 @@
"name": "amount",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "recipient",
"type": "bytes"
},
{
"internalType": "address",
"name": "worker",
Expand Down Expand Up @@ -334,6 +350,11 @@
"name": "token",
"type": "address"
},
{
"internalType": "bytes",
"name": "recipient",
"type": "bytes"
},
{
"internalType": "uint256",
"name": "amount",
Expand Down Expand Up @@ -408,6 +429,11 @@
"name": "token",
"type": "address"
},
{
"internalType": "bytes",
"name": "recipient",
"type": "bytes"
},
{
"internalType": "uint256",
"name": "amount",
Expand Down
5 changes: 3 additions & 2 deletions contracts/index_executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod index_executor {
use crate::registry::Registry;
use crate::storage::StorageClient;
use crate::task::{Task, TaskId, TaskStatus};
use crate::task_deposit::Solution;
use crate::task_fetcher::ActivedTaskFetcher;
use alloc::{string::String, vec, vec::Vec};
use ink::storage::traits::StorageLayout;
Expand Down Expand Up @@ -426,9 +427,9 @@ mod index_executor {

let solution_id = [b"solution".to_vec(), id.to_vec()].concat();
Ok(client
.read_storage::<Vec<u8>>(&solution_id)
.read_storage::<Solution>(&solution_id)
.map_err(|_| Error::FailedToReadStorage)?
.map(|(solution, _)| solution))
.map(|(solution, _)| solution.encode()))
}

/// Returs the interior registry, callable to all
Expand Down
4 changes: 2 additions & 2 deletions contracts/index_executor/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Registry {
native_asset: hex::decode("0000000000000000000000000000000000000802")
.expect("InvalidLocation"),
foreign_asset: None,
handler_contract: hex::decode("0C6182a4435a75d6871143f938426eFBDd0d11C9")
handler_contract: hex::decode("556B97f1F706B22716B7BD16a8005640615826A0")
.expect("InvalidLocation"),
tx_indexer: "https://squid.subsquid.io/graph-moonbeam/graphql".to_string(),
},
Expand All @@ -61,7 +61,7 @@ impl Registry {
native_asset: hex::decode("0000000000000000000000000000000000000000")
.expect("InvalidLocation"),
foreign_asset: None,
handler_contract: hex::decode("678D1D26dA14C6FcC32057401993A6989a6F477d")
handler_contract: hex::decode("50f98D5e488aF6665f444aE751501683de3FDAb2")
.expect("InvalidLocation"),
tx_indexer: "https://squid.subsquid.io/graph-astar/graphql".to_string(),
},
Expand Down
3 changes: 2 additions & 1 deletion contracts/index_executor/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use pink_web3::{
};
use scale::{Decode, Encode};

#[derive(Clone, Decode, Encode, PartialEq)]
#[derive(Clone, Debug, Decode, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct StepInput {
pub exe: String,
pub source_chain: String,
Expand Down
50 changes: 28 additions & 22 deletions contracts/index_executor/src/task_deposit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::step::StepInput;
use crate::task::Task;
use alloc::{string::String, vec, vec::Vec};
use alloc::{string::String, vec::Vec};
use pink_web3::{
contract::{tokens::Detokenize, Error as PinkError},
ethabi::Token,
Expand All @@ -9,13 +9,16 @@ use pink_web3::{
use scale::{Decode, Encode};
use xcm::v3::AssetId as XcmAssetId;

pub type Solution = Vec<StepInput>;

#[derive(Debug)]
#[allow(dead_code)]
pub struct EvmDepositData {
sender: Address,
token: Address,
recipient: Vec<u8>,
amount: U256,
pub task: Option<Vec<u8>>,
pub solution: Option<Vec<u8>>,
}

impl Detokenize for EvmDepositData {
Expand All @@ -31,15 +34,20 @@ impl Detokenize for EvmDepositData {
deposit_data[0].clone(),
deposit_data[1].clone(),
deposit_data[2].clone(),
deposit_data[3].clone(),
) {
(Token::Address(sender), Token::Address(token), Token::Uint(amount)) => {
Ok(EvmDepositData {
sender,
token,
amount,
task: None,
})
}
(
Token::Address(sender),
Token::Address(token),
Token::Bytes(recipient),
Token::Uint(amount),
) => Ok(EvmDepositData {
sender,
token,
recipient,
amount,
solution: None,
}),
_ => Err(PinkError::InvalidOutputType(String::from(
"Return type dismatch",
))),
Expand All @@ -62,7 +70,7 @@ pub struct SubDepositData {
pub asset: XcmAssetId,
pub amount: u128,
pub recipient: Vec<u8>,
pub task: Vec<u8>,
pub solution: Vec<u8>,
}

// Define the structures to parse deposit data json
Expand All @@ -72,7 +80,7 @@ pub struct DepositData {
sender: Vec<u8>,
amount: u128,
recipient: Vec<u8>,
task: Vec<u8>,
solution: Vec<u8>,
}

impl TryFrom<EvmDepositData> for DepositData {
Expand All @@ -81,8 +89,8 @@ impl TryFrom<EvmDepositData> for DepositData {
Ok(Self {
sender: value.sender.as_bytes().into(),
amount: value.amount.try_into().expect("Amount overflow"),
recipient: vec![],
task: value.task.ok_or("MiisingSolution")?,
recipient: value.recipient,
solution: value.solution.ok_or("MiisingSolution")?,
})
}
}
Expand All @@ -93,7 +101,7 @@ impl From<SubDepositData> for DepositData {
sender: value.sender.into(),
amount: value.amount,
recipient: value.recipient,
task: value.task,
solution: value.solution,
}
}
}
Expand All @@ -107,13 +115,13 @@ impl DepositData {
) -> Result<Task, &'static str> {
pink_extension::debug!("Trying to parse task data from json string");

let execution_plan: ExecutionPlan =
Decode::decode(&mut self.task.as_slice()).map_err(|_| "InvalidTask")?;
let solution: Solution =
Decode::decode(&mut self.solution.as_slice()).map_err(|_| "InvalidTask")?;
pink_extension::debug!(
"Parse task data successfully, found {:?} operations",
execution_plan.len()
solution.len()
);
if execution_plan.is_empty() {
if solution.is_empty() {
return Err("EmptyTask");
}
pink_extension::debug!("Trying to convert task data to task");
Expand All @@ -128,7 +136,7 @@ impl DepositData {
..Default::default()
};

for step_input in execution_plan.iter() {
for step_input in solution.iter() {
uninitialized_task
.steps
.push(step_input.clone().try_into()?);
Expand All @@ -137,5 +145,3 @@ impl DepositData {
Ok(uninitialized_task)
}
}

type ExecutionPlan = Vec<StepInput>;
9 changes: 5 additions & 4 deletions contracts/index_executor/src/task_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::account::AccountInfo;
use crate::chain::{Chain, ChainType};
use crate::storage::StorageClient;
use crate::task::Task;
use crate::task_deposit::{DepositData, EvmDepositData, SubDepositData};
use crate::task_deposit::{DepositData, EvmDepositData, Solution, SubDepositData};
use alloc::vec::Vec;
use pink_extension::ResultExt;
use pink_subrpc::{
Expand All @@ -16,6 +16,7 @@ use pink_web3::{
transports::{resolve_ready, PinkHttp},
types::{Address, H160},
};
use scale::Encode;

/// Fetch actived tasks from blockchains and construct a `Task` from it.
/// If the given chain is EVM based, fetch tasks from solidity-based smart contract storage through RPC task.
Expand Down Expand Up @@ -89,16 +90,16 @@ impl ActivedTaskFetcher {
// Read solution from db
let solution_id = [b"solution".to_vec(), task_id.to_vec()].concat();
let (solution, _) = client
.read_storage::<Vec<u8>>(&solution_id)
.read_storage::<Solution>(&solution_id)
.map_err(|_| "FailedToReadStorage")?
.ok_or("NoSolutionFound")?;
pink_extension::debug!(
"Found solution data associate to task {:?}, solution: {:?}",
&hex::encode(task_id),
&hex::encode(&solution),
&solution,
);

evm_deposit_data.task = Some(solution);
evm_deposit_data.solution = Some(solution.encode());
let deposit_data: DepositData = evm_deposit_data.try_into()?;
let task = deposit_data.to_task(&chain.name, task_id, self.worker.account32)?;
Ok(Some(task))
Expand Down

0 comments on commit 622ec45

Please sign in to comment.