Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ serde = { version = "1.0.152", features = ["derive"] }
serde_json = { version = "1.0.93", default-features = false, features = ["raw_value"] }
ethers-core = { version="=2.0.2", features = [] }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
num-traits = { version = "0.2", default-features = false }
42 changes: 24 additions & 18 deletions crates/utils/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ use ethers_core::{

use crate::error::EncodeError;
use std::str::FromStr;
use ethers_core::abi::Error;
use polywrap_wasm_rs::BigInt;
use num_traits::Num;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noob q: why is this num_traits needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For BigInt::from_str_radix


pub fn encode_params(types: Vec<String>, values: Vec<String>) -> Vec<u8> {
let tokens: Vec<Token> = values.iter()
.zip(types.iter())
.map(|(arg, t)| {
let kind = HumanReadableParser::parse_type(&t).unwrap();
if let ParamType::Array(items) = &kind {
if let ParamType::Address = items.as_ref() {
return LenientTokenizer::tokenize(&kind, arg.replace("\"", "").as_str()).unwrap();
}
}
if arg.starts_with("\"") && arg.ends_with("\"") {
return LenientTokenizer::tokenize(&kind, arg.replace("\"", "").as_str()).unwrap();
}
LenientTokenizer::tokenize(&kind, arg).unwrap()
tokenize(&kind, arg).unwrap()
})
.collect();
let bytes = encode(&tokens);
Expand Down Expand Up @@ -59,15 +54,7 @@ pub fn tokenize_values(values: &Vec<String>, params: &Vec<Param>) -> Vec<Token>
.iter()
.zip(values.iter())
.map(|(param, arg)| {
if let ParamType::Array(items) = &param.kind {
if let ParamType::Address = items.as_ref() {
return LenientTokenizer::tokenize(&param.kind, arg.replace("\"", "").as_str()).unwrap();
}
}
if arg.starts_with("\"") && arg.ends_with("\"") {
return LenientTokenizer::tokenize(&param.kind, arg.replace("\"", "").as_str()).unwrap();
}
LenientTokenizer::tokenize(&param.kind, arg).unwrap()
tokenize(&param.kind, arg).unwrap()
})
.collect()
}
Expand Down Expand Up @@ -101,3 +88,22 @@ pub fn encode_packed_bytes(bytes: String) -> String {
let encoded = encode_packed(&[token]).unwrap();
format!("{}", Bytes::from(encoded))
}

fn tokenize(kind: &ParamType, arg: &String) -> Result<Token, Error> {
if let ParamType::Array(items) = &kind {
if let ParamType::Address = items.as_ref() {
return LenientTokenizer::tokenize(&kind, arg.replace("\"", "").as_str());
}
}
if arg.starts_with("\"") && arg.ends_with("\"") {
return LenientTokenizer::tokenize(&kind, arg.replace("\"", "").as_str());
}
if let ParamType::Uint(_) = &kind {
if arg.chars().any(char::is_alphabetic) {
let hex = if arg.starts_with("0x") { arg.strip_prefix("0x").unwrap() } else { arg.as_str() };
let decimal = BigInt::from_str_radix(hex, 16).unwrap().to_string();
return LenientTokenizer::tokenize(&kind, &decimal);
}
}
LenientTokenizer::tokenize(&kind, arg)
}
3 changes: 1 addition & 2 deletions wraps/utils/tests/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe("Ethereum Wrapper", () => {
expect(response.value).toBe(expected);
});

it.only("encodeParams", async () => {
it("encodeParams uint256", async () => {
const response = await client.invoke<string>({
uri,
method: "encodeParams",
Expand All @@ -163,7 +163,6 @@ describe("Ethereum Wrapper", () => {
},
});

console.log(response)
if (!response.ok) throw response.error;

const expected = ethers.utils.defaultAbiCoder.encode(
Expand Down