Skip to content

Commit 15bf1af

Browse files
committed
feat: sdk function
1 parent f0d9115 commit 15bf1af

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

batcher/aligned-sdk/src/core/errors.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,12 @@ impl fmt::Display for FileError {
352352
}
353353
}
354354
}
355+
356+
pub enum BumpError {
357+
WebSocketConnectionError(tokio_tungstenite::tungstenite::Error),
358+
InvalidBumpUnit,
359+
SerializationError(String),
360+
ConnectionFailed(String),
361+
EthRpcError(String),
362+
InvalidRequest(String),
363+
}

batcher/aligned-sdk/src/core/types.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ pub enum ClientMessage {
231231
// Needs to be wrapped in box as the message is 3x bigger than the others
232232
// see https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
233233
SubmitProof(Box<SubmitProofMessage>),
234+
BumpFee(BumpUnit, U256, usize), // BumpUnit, value, proof_qty
234235
}
235236

236237
impl Display for ClientMessage {
@@ -239,6 +240,7 @@ impl Display for ClientMessage {
239240
match self {
240241
ClientMessage::GetNonceForAddress(_) => write!(f, "GetNonceForAddress"),
241242
ClientMessage::SubmitProof(_) => write!(f, "SubmitProof"),
243+
ClientMessage::BumpFee(_, _, _) => write!(f, "BumpFee"),
242244
}
243245
}
244246
}
@@ -396,6 +398,23 @@ pub enum GetNonceResponseMessage {
396398
InvalidRequest(String),
397399
}
398400

401+
#[derive(Debug, Clone, Serialize, Deserialize)]
402+
pub enum BumpFeeResponseMessage {
403+
Ok(),
404+
EthRpcError(String),
405+
InvalidRequest(String),
406+
}
407+
408+
#[derive(Debug, Serialize, Deserialize, Clone)]
409+
pub enum BumpUnit {
410+
Percentage,
411+
Wei,
412+
Gwei,
413+
Eth,
414+
NewMaxFee,
415+
BatchSize,
416+
}
417+
399418
#[derive(Debug, Clone, Copy)]
400419
pub enum Network {
401420
Devnet,

batcher/aligned-sdk/src/sdk.rs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use crate::{
1010
ADDITIONAL_SUBMISSION_GAS_COST_PER_PROOF, CONSTANT_GAS_COST,
1111
MAX_FEE_BATCH_PROOF_NUMBER, MAX_FEE_DEFAULT_PROOF_NUMBER,
1212
},
13-
errors::{self, GetNonceError},
13+
errors::{self, BumpError, GetNonceError},
1414
types::{
15-
AlignedVerificationData, ClientMessage, GetNonceResponseMessage, Network,
16-
PriceEstimate, ProvingSystemId, VerificationData,
15+
AlignedVerificationData, BumpFeeResponseMessage, BumpUnit, ClientMessage,
16+
GetNonceResponseMessage, Network, PriceEstimate, ProvingSystemId, VerificationData,
1717
},
1818
},
1919
eth::{
@@ -466,6 +466,64 @@ pub async fn submit(
466466
}
467467
}
468468

469+
pub async fn bump_user_fees(
470+
network: Network,
471+
bump_unit: BumpUnit,
472+
bump_amount: U256,
473+
proof_qty: usize, // Number of proofs to bump fees, from oldest to newest
474+
// maybe also i will need a signature here
475+
) -> Result<(), errors::BumpError> {
476+
// let batcher_url = network.get_batcher_url(); // This is in a PR
477+
let batcher_url = "network.get_batcher_url()";
478+
// let (ws_stream, _) = match connect_async(batcher_url).await {
479+
// Ok((ws_stream, response)) => (ws_stream, response),
480+
// Err(e) => return Err(BumpError::WebSocketConnectionError(e)),
481+
// };
482+
let (ws_stream, _) = connect_async(batcher_url)
483+
.await
484+
.map_err(|e| BumpError::WebSocketConnectionError(e))?;
485+
debug!("WebSocket handshake has been successfully completed");
486+
let (mut ws_write, ws_read) = ws_stream.split();
487+
488+
let msg = ClientMessage::BumpFee(bump_unit, bump_amount, proof_qty);
489+
490+
let msg_bin = cbor_serialize(&msg)
491+
.map_err(|_| BumpError::SerializationError("Failed to serialize msg".to_string()))?;
492+
493+
ws_write
494+
.send(Message::Binary(msg_bin.clone()))
495+
.await
496+
.map_err(|_| {
497+
BumpError::ConnectionFailed(
498+
"Ws connection failed to send message to batcher".to_string(),
499+
)
500+
})?;
501+
502+
let mut response_stream: ResponseStream =
503+
ws_read.try_filter(|msg| futures_util::future::ready(msg.is_binary()));
504+
505+
let response_msg = match response_stream.next().await {
506+
Some(Ok(msg)) => msg,
507+
_ => {
508+
return Err(BumpError::ConnectionFailed(
509+
"Connection was closed without close message before receiving all messages"
510+
.to_string(),
511+
));
512+
}
513+
};
514+
515+
let _ = ws_write.close().await;
516+
517+
match cbor_deserialize(response_msg.into_data().as_slice()) {
518+
Ok(BumpFeeResponseMessage::Ok()) => Ok(()),
519+
Ok(BumpFeeResponseMessage::EthRpcError(e)) => Err(BumpError::EthRpcError(e)),
520+
Ok(BumpFeeResponseMessage::InvalidRequest(e)) => Err(BumpError::InvalidRequest(e)),
521+
Err(_) => Err(BumpError::SerializationError(
522+
"Failed to deserialize batcher response message".to_string(),
523+
)),
524+
}
525+
}
526+
469527
/// Checks if the proof has been verified with Aligned and is included in the batch.
470528
/// # Arguments
471529
/// * `aligned_verification_data` - The aligned verification data obtained when submitting the proofs.

0 commit comments

Comments
 (0)