Skip to content
Draft
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;

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)
}
2 changes: 1 addition & 1 deletion wraps/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ edition = "2021"

[dependencies]
thiserror = "1.0.38"
polywrap_msgpack_serde = "0.0.2-beta.5"
polywrap_msgpack_serde = "0.0.2-beta.7"
polywrap-wasm-rs = "0.11.2"
serde = { version = "1.0.152", features = ["derive"] }
serde_bytes = "0.11.5"
Expand Down
1 change: 1 addition & 0 deletions wraps/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde = { version = "1.0.152", features = ["derive"] }
ethers-core = { version="=2.0.2" }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
ethers-utils = { path = "../../crates/utils" }
polywrap_msgpack_serde = "0.0.2-beta.7"

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
4 changes: 2 additions & 2 deletions wraps/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"deploy": "npx polywrap deploy -o deployment.json"
},
"devDependencies": {
"@polywrap/client-js": "~0.12.0-pre.0",
"@polywrap/client-js": "0.12.2",
"@types/jest": "27.0.3",
"ethers": "5.7.2",
"jest": "26.6.3",
"js-sha3": "0.8.0",
"polywrap": "~0.11.0-pre.0",
"polywrap": "0.11.2",
"ts-jest": "26.5.4",
"typescript": "4.9.5"
}
Expand Down
5 changes: 3 additions & 2 deletions wraps/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ethers_utils::{
encode_function as utils_encode_function, encode_params as utils_encode_params,
solidity_pack as utils_solidity_pack, to_eth as utils_to_eth, to_wei as utils_to_wei,
};
use polywrap_msgpack_serde::BigIntWrapper;
use polywrap_wasm_rs::BigInt;
use std::str::FromStr;

Expand Down Expand Up @@ -41,15 +42,15 @@ impl ModuleTrait for Module {
let mut op_bytes: [u8; 1] = [0];

if let Some(op) = args.operation {
if BigInt::from(1) == op {
if BigIntWrapper(BigInt::from(1)) == op {
op_bytes[0] = 1;
}
}

let operation = Token::FixedBytes(op_bytes.into());
let to = args.to.parse::<Address>().unwrap();

let value = utils_encode_params(vec!["uint256".into()], vec![args.value.to_string()]);
let value = utils_encode_params(vec!["uint256".into()], vec![args.value.0.to_string()]);

let data = Bytes::from_str(&args.data).unwrap();
let data_len = utils_encode_params(vec!["uint256".into()], vec![data.len().to_string()]);
Expand Down
21 changes: 20 additions & 1 deletion wraps/utils/tests/e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PolywrapClientConfigBuilder, PolywrapClient } from "@polywrap/client-js";
import { PolywrapClient, PolywrapClientConfigBuilder } from "@polywrap/client-js";
import * as path from "path";
import { ethers, utils } from "ethers";

Expand Down Expand Up @@ -153,6 +153,25 @@ describe("Ethereum Wrapper", () => {
expect(response.value).toBe(expected);
});

it("encodeParams uint256", async () => {
const response = await client.invoke<string>({
uri,
method: "encodeParams",
args: {
types: ["uint256"],
values: ["0xb1073742015cbcf5a3a4d9d1ae33ecf619439710b89475f92e2abd2117e90f90"],
},
});

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

const expected = ethers.utils.defaultAbiCoder.encode(
["uint256"],
["0xb1073742015cbcf5a3a4d9d1ae33ecf619439710b89475f92e2abd2117e90f90"]
);
expect(response.value).toBe(expected);
});

it("encodeFunction", async () => {
const response = await client.invoke<string>({
uri,
Expand Down
Loading