Skip to content

Commit

Permalink
refacotr input encode.
Browse files Browse the repository at this point in the history
  • Loading branch information
YancyParker committed Dec 14, 2023
1 parent 1b63b47 commit b86c688
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ola-lang-abi"
version = "1.0.2"
version = "1.0.3"
edition = "2021"
license = "MIT"
description = "Ola Smart Contract ABI parsing library"
Expand Down
2 changes: 1 addition & 1 deletion examples/book_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {
serde_json::from_reader(file).expect("failed to parse ABI")
};

let data = vec![120553111, 7, 60, 5, 111, 108, 97, 118, 109];
let data = vec![60, 5, 111, 108, 97, 118, 109, 7, 120553111];

// Decode
let (func, decoded_data) = abi.decode_input_from_slice(&data).unwrap();
Expand Down
36 changes: 16 additions & 20 deletions src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ impl Abi {
let f = self
.functions
.iter()
.find(|f| f.method_id() == input[0])
.find(|f| f.method_id() == input[input.len()-1])
.ok_or_else(|| anyhow!("ABI function not found"))?;

// input = [method_id, param-len, param1, param2, ...]
let decoded_params = f.decode_input_from_slice(&input[2..])?;
// input = [param1, param2, .. , param-len, method_id,]

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

Ok((f, decoded_params))
}
Expand All @@ -53,23 +54,19 @@ impl Abi {
.find(|f| f.signature() == signature)
.ok_or_else(|| anyhow!("ABI function not found"))?;

let mut enc_input = vec![f.method_id()];

let params = Value::encode(params);
enc_input.push(params.len() as u64);
enc_input.extend(params);
let mut params = Value::encode(params);
params.push(params.len() as u64);
params.push(f.method_id());

Ok(enc_input)
Ok(params)
}

pub fn encode_input_values(&self, params: &[Value]) -> Result<Vec<u64>> {
let mut enc_input = vec![];

let params = Value::encode(params);
enc_input.push(params.len() as u64);
enc_input.extend(params);
let mut params = Value::encode(params);
params.push(params.len() as u64);

Ok(enc_input)
Ok(params)
}
}

Expand Down Expand Up @@ -340,13 +337,12 @@ mod test {
functions: vec![fun],
};

let mut enc_input = vec![abi.functions[0].method_id()];

let params = Value::encode(&input_values);
enc_input.push(params.len() as u64);
enc_input.extend(params);
let dec = abi
.decode_input_from_slice(&enc_input)
let mut params = Value::encode(&input_values);
params.push(params.len() as u64);
params.push(abi.functions[0].method_id());
let dec: (&Function, DecodedParams) = abi
.decode_input_from_slice(&params)
.expect("decode_input_from_slice failed");

let expected_decoded_params = DecodedParams::from(
Expand Down

0 comments on commit b86c688

Please sign in to comment.