Skip to content

Commit 9f3d613

Browse files
committed
feat: unlock, lock and withdraw funds in sdk
1 parent fe21a4a commit 9f3d613

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

crates/sdk/src/eth/batcher_payment_service.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Arc;
22

3-
use ethers::prelude::*;
3+
use ethers::{core::k256::ecdsa::SigningKey, prelude::*};
44

55
use crate::common::errors::VerificationError;
66

@@ -11,6 +11,9 @@ abigen!(
1111

1212
pub type BatcherPaymentService = BatcherPaymentServiceContract<Provider<Http>>;
1313

14+
pub type SignerMiddlewareT = SignerMiddleware<Provider<Http>, Wallet<SigningKey>>;
15+
pub type BatcherPaymentServiceWithSigner = BatcherPaymentServiceContract<SignerMiddlewareT>;
16+
1417
pub async fn batcher_payment_service(
1518
provider: Provider<Http>,
1619
contract_address: H160,

crates/sdk/src/verification_layer/mod.rs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
DEFAULT_MAX_FEE_BATCH_SIZE, GAS_PRICE_PERCENTAGE_MULTIPLIER,
66
INSTANT_MAX_FEE_BATCH_SIZE, PERCENTAGE_DIVIDER,
77
},
8-
errors::{self, GetNonceError},
8+
errors::{self, GetNonceError, PaymentError},
99
types::{
1010
AlignedVerificationData, ClientMessage, FeeEstimationType, GetNonceResponseMessage,
1111
Network, ProvingSystemId, VerificationData,
@@ -19,7 +19,7 @@ use crate::{
1919
},
2020
eth::{
2121
aligned_service_manager::aligned_service_manager,
22-
batcher_payment_service::batcher_payment_service,
22+
batcher_payment_service::{batcher_payment_service, BatcherPaymentServiceWithSigner},
2323
},
2424
};
2525

@@ -750,6 +750,73 @@ pub async fn get_balance_in_aligned(
750750
}
751751
}
752752

753+
pub async fn unlock_balance_in_aligned(
754+
signer: &SignerMiddleware<Provider<Http>, LocalWallet>,
755+
network: Network,
756+
) -> Result<ethers::types::TransactionReceipt, errors::PaymentError> {
757+
let payment_service_address = network.get_batcher_payment_service_address();
758+
let payment_service =
759+
BatcherPaymentServiceWithSigner::new(payment_service_address, signer.clone().into());
760+
761+
let receipt = payment_service
762+
.unlock()
763+
.send()
764+
.await
765+
.map_err(|e| PaymentError::SendError(e.to_string()))?
766+
.await
767+
.map_err(|e| PaymentError::SubmitError(e.to_string()))?;
768+
769+
match receipt {
770+
Some(hash) => Ok(hash),
771+
None => Err(PaymentError::SubmitError("".into())),
772+
}
773+
}
774+
775+
pub async fn lock_balance_in_aligned(
776+
signer: &SignerMiddleware<Provider<Http>, LocalWallet>,
777+
network: Network,
778+
) -> Result<ethers::types::TransactionReceipt, errors::PaymentError> {
779+
let payment_service_address = network.get_batcher_payment_service_address();
780+
let payment_service =
781+
BatcherPaymentServiceWithSigner::new(payment_service_address, signer.clone().into());
782+
783+
let receipt = payment_service
784+
.lock()
785+
.send()
786+
.await
787+
.map_err(|e| PaymentError::SendError(e.to_string()))?
788+
.await
789+
.map_err(|e| PaymentError::SubmitError(e.to_string()))?;
790+
791+
match receipt {
792+
Some(hash) => Ok(hash),
793+
None => Err(PaymentError::SubmitError("".into())),
794+
}
795+
}
796+
797+
pub async fn withdraw_balance_from_aligned(
798+
signer: &SignerMiddleware<Provider<Http>, LocalWallet>,
799+
network: Network,
800+
amount: U256,
801+
) -> Result<ethers::types::TransactionReceipt, errors::PaymentError> {
802+
let payment_service_address = network.get_batcher_payment_service_address();
803+
let payment_service =
804+
BatcherPaymentServiceWithSigner::new(payment_service_address, signer.clone().into());
805+
806+
let receipt = payment_service
807+
.withdraw(amount)
808+
.send()
809+
.await
810+
.map_err(|e| PaymentError::SendError(e.to_string()))?
811+
.await
812+
.map_err(|e| PaymentError::SubmitError(e.to_string()))?;
813+
814+
match receipt {
815+
Some(hash) => Ok(hash),
816+
None => Err(PaymentError::SubmitError("".into())),
817+
}
818+
}
819+
753820
/// Saves AlignedVerificationData in a file.
754821
///
755822
/// # Arguments

0 commit comments

Comments
 (0)