diff --git a/Cargo.lock b/Cargo.lock index 70b15bc..a3bb86d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -298,6 +298,7 @@ dependencies = [ [[package]] name = "alloy-web" version = "0.1.0" +source = "git+https://github.com/DrHongos/alloy-web#ac6ce7df81fcffb729340e993a2a0dea78f6fee1" dependencies = [ "alloy-chains", "alloy-json-rpc", diff --git a/Cargo.toml b/Cargo.toml index 3162c7b..351fa22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/src/components/call.rs b/src/components/call.rs index 4bfc5a2..644805e 100644 --- a/src/components/call.rs +++ b/src/components/call.rs @@ -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 { @@ -31,14 +26,12 @@ pub fn call() -> Html { let on_block_entry: Callback = { let b = block.clone(); Callback::from(move |inp: BlockId| { - //log(format!("Received blockId {:#?}", inp).as_str()); b.set(Some(inp)); }) }; let on_tx: Callback = { let t = tx.clone(); Callback::from(move |txr| { - //log(format!("Received CallRequest {:#?}", txr).as_str()); t.set(Some(txr)); }) }; @@ -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(); @@ -80,10 +106,12 @@ pub fn call() -> Html { html!{
if ethereum.is_connected() { - if (*open).clone() { + +
@@ -101,4 +129,4 @@ pub fn call() -> Html {
} -} \ No newline at end of file +} diff --git a/src/components/call_request_input.rs b/src/components/call_request_input.rs index 5ed9409..2aba1b5 100644 --- a/src/components/call_request_input.rs +++ b/src/components/call_request_input.rs @@ -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)) diff --git a/src/components/send_transaction.rs b/src/components/send_transaction.rs index 5c88b83..b9d9803 100644 --- a/src/components/send_transaction.rs +++ b/src/components/send_transaction.rs @@ -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)] diff --git a/src/components/sign_typed_data.rs b/src/components/sign_typed_data.rs index a1d796d..bcdb7ac 100644 --- a/src/components/sign_typed_data.rs +++ b/src/components/sign_typed_data.rs @@ -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>, 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"); diff --git a/src/components/switch_chain.rs b/src/components/switch_chain.rs index 9fcec85..339d48f 100644 --- a/src/components/switch_chain.rs +++ b/src/components/switch_chain.rs @@ -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 @@ -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!("{:#?}", ¶ms).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>, ()> = client.inner().prepare("wallet_switchEthereumChain", vec![params]); + match req.await { + Ok(()) => log(format!("Switched").as_str()), + Err(e) => log(format!("Error! {:#?}",e).as_str()) + } }) } }) @@ -65,7 +61,6 @@ pub fn switch_chain() -> Html { let enum_iter = NamedChain::iter(); let list: Vec = enum_iter.collect(); -// let ontest = Callback::from(move |_: MouseEvent| {log(format!("{:#?}", enum_vector).as_str())}); html!{
@@ -79,10 +74,7 @@ pub fn switch_chain() -> Html { }).collect::() } - //

{chain.to_string()}

- - /* if chain != current_chain { - } */ + }
} diff --git a/src/components/transaction_request_input.rs b/src/components/transaction_request_input.rs index f96e9dc..e0621f7 100644 --- a/src/components/transaction_request_input.rs +++ b/src/components/transaction_request_input.rs @@ -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; @@ -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 */ @@ -42,21 +36,7 @@ pub fn transaction_request_input(props: &Props) -> Html { let ethereum = use_context::().expect( "No ethereum found. You must wrap your components in an ", ); - //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(); @@ -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
= { let t = to.clone(); Callback::from(move |a| { @@ -113,6 +114,7 @@ pub fn transaction_request_input(props: &Props) -> Html { html!{
if ethereum.is_connected() { + {"To"}