Skip to content

Commit 3497509

Browse files
committed
feat: add client
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent e1554a2 commit 3497509

File tree

4 files changed

+104
-17
lines changed

4 files changed

+104
-17
lines changed

tap_aggregator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ alloy.workspace = true
2121
anyhow.workspace = true
2222
tokio.workspace = true
2323
serde_json.workspace = true
24-
jsonrpsee = { workspace = true, features = ["server", "macros"] }
24+
jsonrpsee = { workspace = true, features = ["server", "macros", "client"] }
2525
clap = { version = "4.5.15", features = ["derive", "env"] }
2626
strum = { version = "0.26.3", features = ["derive"] }
2727
tracing-subscriber = "0.3.17"

tap_aggregator/src/bin/client.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,79 @@
1+
use std::str::FromStr;
12

2-
fn main() {
3+
use alloy::{
4+
primitives::{Address, B256},
5+
signers::local::PrivateKeySigner,
6+
};
7+
8+
use jsonrpsee::{core::client::ClientT, http_client::HttpClientBuilder, rpc_params};
9+
use tap_aggregator::{
10+
jsonrpsee_helpers::JsonRpcResponse,
11+
tap_aggregator::{tap_aggregator_client::TapAggregatorClient, RavRequest},
12+
};
13+
use tap_core::{
14+
rav::ReceiptAggregateVoucher, receipt::Receipt, signed_message::EIP712SignedMessage,
15+
tap_eip712_domain,
16+
};
17+
18+
#[tokio::main]
19+
async fn main() {
320
println!("Hello world");
21+
22+
let domain_separator = tap_eip712_domain(1, Address::ZERO);
23+
24+
let mut client = TapAggregatorClient::connect("http://localhost:8080")
25+
.await
26+
.unwrap();
27+
28+
let wallet = PrivateKeySigner::from_bytes(
29+
&B256::from_str("8990ccae49d8fff982bfcd2b3f83d6b14cb272fcd29e49f7a335accd906005dc")
30+
.unwrap(),
31+
)
32+
.unwrap();
33+
34+
let allocation_id = Address::from_str("0xabababababababababababababababababababab").unwrap();
35+
36+
// Create receipts
37+
let mut receipts = Vec::new();
38+
for value in 50..60 {
39+
receipts.push(
40+
EIP712SignedMessage::new(
41+
&domain_separator,
42+
Receipt::new(allocation_id, value).unwrap(),
43+
&wallet,
44+
)
45+
.unwrap(),
46+
);
47+
}
48+
49+
let rav_request = RavRequest {
50+
receipts: receipts.clone().into_iter().map(Into::into).collect(),
51+
previous_rav: None,
52+
};
53+
let res = client.aggregate_receipts(rav_request).await.unwrap();
54+
let signed_rav: tap_core::rav::SignedRAV =
55+
res.into_inner().rav.take().unwrap().try_into().unwrap();
56+
println!("Response: {signed_rav:?}");
57+
58+
let sender_aggregator = HttpClientBuilder::default()
59+
.build("http://localhost:8080")
60+
.unwrap();
61+
62+
let previous_rav: Option<tap_core::rav::SignedRAV> = None;
63+
64+
let response: JsonRpcResponse<EIP712SignedMessage<ReceiptAggregateVoucher>> = sender_aggregator
65+
.request(
66+
"aggregate_receipts",
67+
rpc_params!(
68+
"0.0", // TODO: Set the version in a smarter place.
69+
receipts,
70+
previous_rav
71+
),
72+
)
73+
.await
74+
.unwrap();
75+
let response = response.data;
76+
assert_eq!(signed_rav, response);
77+
78+
println!("Response: {response:?}");
479
}

tap_aggregator/src/main.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33

44
#![doc = include_str!("../README.md")]
55

6-
use std::borrow::Cow;
76
use std::collections::HashSet;
87
use std::str::FromStr;
98

109
use alloy::dyn_abi::Eip712Domain;
1110
use alloy::primitives::Address;
12-
use alloy::primitives::FixedBytes;
1311
use alloy::signers::local::PrivateKeySigner;
1412
use anyhow::Result;
1513
use axum::error_handling::HandleError;
@@ -18,7 +16,7 @@ use axum::BoxError;
1816
use axum::Router;
1917
use clap::Parser;
2018
use hyper::StatusCode;
21-
use ruint::aliases::U256;
19+
use tap_core::tap_eip712_domain;
2220
use tokio::net::TcpListener;
2321

2422
use log::{debug, info};
@@ -181,7 +179,7 @@ async fn main() -> Result<()> {
181179
info!("Shutting down...");
182180

183181
// Stop the server and wait for it to finish gracefully.
184-
handle.stop()?;
182+
let _ = handle.stop();
185183
handle.stopped().await;
186184

187185
debug!("Goodbye!");
@@ -215,14 +213,11 @@ fn create_eip712_domain(args: &Args) -> Result<Eip712Domain> {
215213
// Transfrom the args into the types expected by Eip712Domain::new().
216214

217215
// Transform optional strings into optional Cow<str>.
218-
let name = args.domain_name.clone().map(Cow::Owned);
219-
let version = args.domain_version.clone().map(Cow::Owned);
220-
221216
// Transform optional strings into optional U256.
222217
if args.domain_chain_id.is_some() {
223218
debug!("Parsing domain chain ID...");
224219
}
225-
let chain_id: Option<U256> = args
220+
let chain_id: Option<u64> = args
226221
.domain_chain_id
227222
.as_ref()
228223
.map(|s| s.parse())
@@ -231,17 +226,14 @@ fn create_eip712_domain(args: &Args) -> Result<Eip712Domain> {
231226
if args.domain_salt.is_some() {
232227
debug!("Parsing domain salt...");
233228
}
234-
let salt: Option<FixedBytes<32>> = args.domain_salt.as_ref().map(|s| s.parse()).transpose()?;
235229

236230
// Transform optional strings into optional Address.
237231
let verifying_contract: Option<Address> = args.domain_verifying_contract;
238232

239233
// Create the EIP-712 domain separator.
240-
Ok(Eip712Domain::new(
241-
name,
242-
version,
243-
chain_id,
244-
verifying_contract,
245-
salt,
234+
//
235+
Ok(tap_eip712_domain(
236+
chain_id.unwrap_or(1),
237+
verifying_contract.unwrap_or_default(),
246238
))
247239
}

tap_aggregator/src/tap_aggregator.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ impl TryFrom<SignedReceipt> for tap_core::receipt::SignedReceipt {
2828
}
2929
}
3030

31+
impl From<tap_core::receipt::Receipt> for Receipt {
32+
fn from(value: tap_core::receipt::Receipt) -> Self {
33+
Self {
34+
allocation_id: value.allocation_id.as_slice().to_vec(),
35+
timestamp_ns: value.timestamp_ns,
36+
nonce: value.nonce,
37+
value: Some(value.value.into()),
38+
}
39+
}
40+
}
41+
42+
impl From<tap_core::receipt::SignedReceipt> for SignedReceipt {
43+
fn from(value: tap_core::receipt::SignedReceipt) -> Self {
44+
Self {
45+
message: Some(value.message.into()),
46+
signature: value.signature.as_bytes().to_vec(),
47+
}
48+
}
49+
}
50+
3151
impl TryFrom<SignedRav> for EIP712SignedMessage<tap_core::rav::ReceiptAggregateVoucher> {
3252
type Error = anyhow::Error;
3353
fn try_from(voucher: SignedRav) -> Result<Self, Self::Error> {

0 commit comments

Comments
 (0)