Skip to content

Commit

Permalink
Fixed serialization on requests with Map objects
Browse files Browse the repository at this point in the history
  • Loading branch information
DrHongos committed Jan 11, 2024
1 parent 6e49916 commit 80a23e9
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 77 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ web-sys = {version="0.3.65", features = ["Window", "Event", "EventTarget"]}
yew = { version="0.21", features=["csr"] }
serde_json = "1.0.108"

alloy-web = { path = "../../alloy-web" }
alloy-web = { git = "https://github.com/DrHongos/alloy-web" }
#{ path = "../../alloy-web" }
alloy-chains = "0.1.5"
# alloy crates
alloy-providers = { git = "https://github.com/alloy-rs/alloy" }
Expand Down
60 changes: 44 additions & 16 deletions src/components/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@ use yew::prelude::*;
use crate::contexts::ethereum::UseEthereum;
use wasm_bindgen_futures::spawn_local;
use crate::helpers::log;
use std::ops::Deref;
use crate::components::{
block_selector::BlockSelector,
blockid_input::BlockIdInput,
call_request_input::CallRequestInput,
};
use std::ops::BitXorAssign;
use alloy_rpc_types::{BlockId, CallRequest};
/*
TODO:
- fake transactionrequest
ie: DAI totalSupply()
to: 0x6B175474E89094C44Da98b954EedeAC495271d0F
data: 0x18160ddd (to get more use: cast calldata "method(args)" args)
*/
use std::{
ops::{Deref, BitXorAssign},
str::FromStr,
};
use alloy_rpc_types::{BlockId, CallRequest, CallInput};
use alloy_primitives::{address, Bytes};

#[function_component(Call)]
pub fn call() -> Html {
Expand All @@ -31,14 +26,12 @@ pub fn call() -> Html {
let on_block_entry: Callback<BlockId> = {
let b = block.clone();
Callback::from(move |inp: BlockId| {
//log(format!("Received blockId {:#?}", inp).as_str());
b.set(Some(inp));
})
};
let on_tx: Callback<CallRequest> = {
let t = tx.clone();
Callback::from(move |txr| {
//log(format!("Received CallRequest {:#?}", txr).as_str());
t.set(Some(txr));
})
};
Expand All @@ -63,7 +56,40 @@ pub fn call() -> Html {
}
})
};

let test_dai_supply = {
let b = (*block).clone();
let client = client.clone();
Callback::from(move |_: MouseEvent| {
if let Some(client) = client.deref() {
let client = client.clone();
let b = b.clone();
spawn_local(async move {
let tx = CallRequest {
from: None,
to: Some(address!("6B175474E89094C44Da98b954EedeAC495271d0F")), // DAI [mainnet]
gas_price: None,
max_fee_per_gas: None,
max_priority_fee_per_gas: None,
gas: None,
value: None,
input: CallInput::new(Bytes::from_str("0x18160ddd").expect("Error parsing bytes")), // totalSupply()
nonce: None,
access_list: None,
transaction_type: None,
chain_id: None,
max_fee_per_blob_gas: None,
blob_versioned_hashes: None,
};
//log(format!("tx is {:#?}", tx).as_str());
let c = client.call(tx, b);
match c.await {
Ok(bn) => log(format!("Call result: {}", bn).as_str()),
Err(rv) => log(format!("Error: {:#?}", rv).as_str())
}
})
}
})
};
let toggle_comp = {
let o = open.clone();
let rv = (*o).clone();
Expand All @@ -80,10 +106,12 @@ pub fn call() -> Html {
html!{
<div class={"getCode"}>
if ethereum.is_connected() {
<button onclick={toggle_comp} class={"button-tool"}>
<button onclick={toggle_comp} class={"button"}>
{button_label}
</button>
if (*open).clone() {
<button onclick={test_dai_supply} class={"button"}>{"mainnet::DAI::totalSupply()"}</button>
<hr />
<CallRequestInput
on_tx={on_tx}
/>
Expand All @@ -101,4 +129,4 @@ pub fn call() -> Html {
</div>
}

}
}
9 changes: 3 additions & 6 deletions src/components/call_request_input.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use yew::prelude::*;
use alloy_primitives::{Address, U128, U256, Bytes, U8, U64};
use alloy_rpc_types::{CallRequest, AccessList, CallInput};
use alloy_primitives::{Address, Bytes};
use alloy_rpc_types::{CallRequest, CallInput};
use std::str::FromStr;
//use crate::helpers::log;
use crate::contexts::ethereum::UseEthereum;
use web_sys::HtmlInputElement;
use crate::components::{
address_input::AddressInput,
blockid_input::BlockIdInput,
};
use crate::components::address_input::AddressInput;
/*
TODO:
- create parameter encoder (enters method signature and allows to enter arguments, returns Bytes (for txrequest::data))
Expand Down
13 changes: 3 additions & 10 deletions src/components/send_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
use yew::prelude::*;
use alloy_primitives::Address;
use alloy_rpc_types::{BlockId, TransactionRequest};
use alloy_rpc_types::TransactionRequest;
use std::ops::Deref;
use wasm_bindgen_futures::spawn_local;
use std::ops::BitXorAssign;
use crate::helpers::log;
use crate::contexts::ethereum::UseEthereum;
use crate::components::{
address_input::AddressInput,
blockid_input::BlockIdInput,
transaction_request_input::TransactionRequestInput,
};
use crate::components::transaction_request_input::TransactionRequestInput;
/*
TODO:
- create TransactionRequest
- sign and send a transaction
- add all input fields
- handle error cases
*/

#[function_component(SendTransaction)]
Expand Down
5 changes: 1 addition & 4 deletions src/components/sign_typed_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ pub fn sign_typed_data() -> Html {
let client = provider.clone();
spawn_local(async move {
let tdata = json!(data).to_string();
let params1: Cow<'static, String> = Cow::Owned(account);
let params2: Cow<'static, String> = Cow::Owned(tdata);

let req: RpcCall<_, Vec<Cow<'static, String>>, String> = client.inner().prepare("eth_signTypedData_v4", vec![params1, params2]);
let req: RpcCall<_, Cow<(String, String)>, String> = client.inner().prepare("eth_signTypedData_v4", Cow::Owned((account, tdata)));

if let Ok(signed) = req.await {
let signature = Signature::from_str(&signed).expect("Could not parse Signature");
Expand Down
24 changes: 8 additions & 16 deletions src/components/switch_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use strum::IntoEnumIterator;
use serde::Serialize;
/*
TODO:
- error.. figure it out! -> metamask request an object as argument
- handle chain not known ("you should add this chain first")
- initial state (use_effect)
- disable current chain
- handle error cases
Expand All @@ -37,18 +37,14 @@ pub fn switch_chain() -> Html {
let obj = SwitchChainObj {
chain_id: (*chain).clone()
};
let tdata = serde_json::json!(obj).to_string();
let params: Cow<'static, String> = Cow::Owned(tdata);
let params: Cow<'static, SwitchChainObj> = Cow::Owned(obj);
let client = client.clone();
spawn_local(async move {
//if let Ok(client) = client {
//log(format!("{:#?}", &params).as_str());
let req: RpcCall<_, Cow<'static, String>, ()> = client.inner().prepare("wallet_switchEthereumChain", params);
match req.await {
Ok(()) => log(format!("Switched").as_str()),
Err(e) => log(format!("Error! {:#?}",e).as_str())
}
//} else { log("not connected") }
let req: RpcCall<_, Vec<Cow<'static, SwitchChainObj>>, ()> = client.inner().prepare("wallet_switchEthereumChain", vec![params]);
match req.await {
Ok(()) => log(format!("Switched").as_str()),
Err(e) => log(format!("Error! {:#?}",e).as_str())
}
})
}
})
Expand All @@ -65,7 +61,6 @@ pub fn switch_chain() -> Html {

let enum_iter = NamedChain::iter();
let list: Vec<NamedChain> = enum_iter.collect();
// let ontest = Callback::from(move |_: MouseEvent| {log(format!("{:#?}", enum_vector).as_str())});

html!{
<div class={"getCode"}>
Expand All @@ -79,10 +74,7 @@ pub fn switch_chain() -> Html {
}).collect::<Html>()
}
</select>
//<p>{chain.to_string()}</p>
<button onclick={switch_chain} class="select-chain-button">{"Switch (NWY)"}</button>
/* if chain != current_chain {
} */
<button onclick={switch_chain} class="select-chain-button">{"Switch chain"}</button>
}
</div>
}
Expand Down
50 changes: 26 additions & 24 deletions src/components/transaction_request_input.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use yew::prelude::*;
use alloy_primitives::{Address, U128, U256, Bytes, U8, U64};
use alloy_rpc_types::{
TransactionRequest,
AccessList,
CallInput,
json_u256
};
use alloy_primitives::{Address, U256, Bytes};
use alloy_rpc_types::TransactionRequest;
use std::str::FromStr;
use crate::contexts::ethereum::UseEthereum;
use web_sys::HtmlInputElement;
Expand All @@ -14,8 +9,7 @@ use crate::components::{
};
/*
TODO:
- error on Address encoding (no checksum?)
- if a do to_checksum() ends up in the same result? how does TransactionRequest encodes?
- add all input fields
- handle error cases
*/
Expand All @@ -42,21 +36,7 @@ pub fn transaction_request_input(props: &Props) -> Html {
let ethereum = use_context::<UseEthereum>().expect(
"No ethereum found. You must wrap your components in an <EthereumContextProvider />",
);
//let chain = ethereum.chain();
/*
let set_block_hash = {
let bh = block_hash.clone();
Callback::from(move |e: Event| {
let v: HtmlInputElement = e.target_unchecked_into();
let p = FixedBytes::from_str(&v.value());
if let Ok(p) = p {
bh.set(B256::from(p))
} else {
bh.set(B256::from(FixedBytes::ZERO))
}
})
};
*/

let create_tx_req = {
let f = ethereum.account().clone();
let t = to.clone();
Expand All @@ -81,6 +61,27 @@ pub fn transaction_request_input(props: &Props) -> Html {
)
})
};
let test_fake_req = {
let p = props.on_request.clone();
let f = ethereum.account().clone();
Callback::from(move |_| {
p.emit(
TransactionRequest {
from: Some(f),
to: Some(Address::from_str("0x1e5870eb2843119b0a7bedb80e0fbf23e294de34").unwrap()),
gas_price: None,
max_fee_per_gas: None,
max_priority_fee_per_gas: None,
gas: None,
value: Some(U256::from_str("100000000000000000").unwrap()),
data: None,
nonce: None,
access_list: None,
transaction_type: None,
}
)
})
};
let on_to: Callback<Address> = {
let t = to.clone();
Callback::from(move |a| {
Expand Down Expand Up @@ -113,6 +114,7 @@ pub fn transaction_request_input(props: &Props) -> Html {
html!{
<div class={"filter"}>
if ethereum.is_connected() {
<button onclick={test_fake_req}>{"Test"}</button>
<small>{"To"}</small>
<AddressInput
on_add={on_to}
Expand Down

0 comments on commit 80a23e9

Please sign in to comment.