Skip to content

Commit

Permalink
add function output decode.
Browse files Browse the repository at this point in the history
  • Loading branch information
YancyParker committed Jan 25, 2024
1 parent 4d49530 commit ad9c88b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ impl Abi {
Ok((f, decoded_params))
}

// Decode function input from slice.
pub fn decode_output_from_slice<'a>(
&'a self,
output: &[u64],
) -> Result<(&'a Function, DecodedParams)> {
let f = self
.functions
.iter()
.find(|f| f.method_id() == output[output.len()-1])
.ok_or_else(|| anyhow!("ABI function not found"))?;

// output = [param1, param2, .. , param-len, method_id,]

let decoded_params = f.decode_input_from_slice(&output[0..output.len()-2])?;

Ok((f, decoded_params))
}

pub fn encode_input_with_signature(
&self,
signature: &str,
Expand All @@ -61,7 +79,7 @@ impl Abi {
Ok(params)
}

pub fn encode_input_values(&self, params: &[Value]) -> Result<Vec<u64>> {
pub fn encode_values(&self, params: &[Value]) -> Result<Vec<u64>> {

let mut params = Value::encode(params);
params.push(params.len() as u64);
Expand Down Expand Up @@ -150,6 +168,23 @@ impl Function {
.collect::<Vec<_>>(),
))
}

// Decode function output from slice.
pub fn decode_output_from_slice(&self, output: &[u64]) -> Result<DecodedParams> {
let ouputs_types = self
.outputs
.iter()
.map(|f_output| f_output.type_.clone())
.collect::<Vec<_>>();

Ok(DecodedParams::from(
self.outputs
.iter()
.cloned()
.zip(Value::decode_from_slice(output, &ouputs_types)?)
.collect::<Vec<_>>(),
))
}
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down

0 comments on commit ad9c88b

Please sign in to comment.