Skip to content

Commit

Permalink
Move Fee, BatchFee to zksync_types and add txs fee method to rest api…
Browse files Browse the repository at this point in the history
… client
  • Loading branch information
Deniallugo committed Jan 27, 2021
1 parent 2ba3f73 commit 511f1a8
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 139 deletions.
69 changes: 0 additions & 69 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions core/bin/zksync_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ jsonrpc-ws-server = "14.0.3"
jsonrpc-http-server = "14.0.3"
jsonrpc-derive = "14.0.3"

tokio_new = { package = "tokio", version = "1.0" , features = ["rt-multi-thread"]}

tokio = { version = "0.2", features = ["full"] }
tokio_old = { package = "tokio", version = "0.1.22" }
futures = { version = "0.3", features = ["compat"] }
Expand Down Expand Up @@ -72,7 +70,6 @@ regex = "1"
[dev-dependencies]
zksync_test_account = { path = "../../tests/test_account" }
criterion = {version = "0.3.4", features = ["async_tokio", "async_futures"]}
async-jsonrpc-client = "0.1.3"

[[bench]]
name = "api_service"
Expand Down
29 changes: 18 additions & 11 deletions core/bin/zksync_api/src/api_server/rest/v1/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@ use actix_web::{
web::{self, Json},
Scope,
};
use num::BigUint;

// Workspace uses
use zksync_api_client::rest::v1::IncomingTxForFee;
pub use zksync_api_client::rest::v1::{
FastProcessingQuery, IncomingTx, IncomingTxBatch, IncomingTxBatchForFee, Receipt, TxData,
};
use zksync_storage::{
chain::operations_ext::records::TxReceiptResponse, QueryResult, StorageProcessor,
};
use zksync_types::{tx::TxHash, BlockNumber, SignedZkSyncTx};

use crate::api_server::tx_sender::{SubmitError, TxSender};
use crate::fee_ticker::{BatchFee, Fee};
use zksync_types::{tx::TxHash, BatchFee, BlockNumber, Fee, SignedZkSyncTx};

// Local uses
use super::{ApiError, JsonResult, Pagination, PaginationQuery};
use zksync_api_client::rest::v1::IncomingTxForFee;
use zksync_types::helpers::closest_packable_fee_amount;
use crate::api_server::tx_sender::{SubmitError, TxSender};

#[derive(Debug, Clone, Copy)]
pub enum SumbitErrorCode {
Expand Down Expand Up @@ -293,7 +289,7 @@ async fn get_txs_batch_fee_in_wei(
.await
.map_err(ApiError::from)?;

Ok(Json(BatchFee { total_fee: fee }))
Ok(Json(fee))
}

pub fn api_scope(tx_sender: TxSender) -> Scope {
Expand All @@ -318,6 +314,7 @@ pub fn api_scope(tx_sender: TxSender) -> Scope {
mod tests {
use actix_web::App;
use bigdecimal::BigDecimal;
use ethabi::Address;
use futures::{channel::mpsc, StreamExt};
use num::{BigUint, Zero};

Expand All @@ -327,19 +324,20 @@ mod tests {
use zksync_types::{
tokens::TokenLike,
tx::{PackedEthSignature, TxEthSignature},
AccountId, BlockNumber, Nonce, TokenId, TxFeeTypes, ZkSyncTx,
AccountId, BlockNumber, Fee, Nonce,
OutputFeeType::Withdraw,
TokenId, TxFeeTypes, ZkSyncTx,
};

use crate::{
api_server::helpers::try_parse_tx_hash,
core_api_client::CoreApiClient,
fee_ticker::{Fee, OutputFeeType::Withdraw, TickerRequest},
fee_ticker::TickerRequest,
signature_checker::{VerifiedTx, VerifyTxSignatureRequest},
};

use super::super::test_utils::{TestServerConfig, TestTransactions};
use super::*;
use ethabi::Address;

fn submit_txs_loopback() -> (CoreApiClient, actix_web::test::TestServer) {
async fn send_tx(_tx: Json<SignedZkSyncTx>) -> Json<Result<(), ()>> {
Expand Down Expand Up @@ -508,6 +506,15 @@ mod tests {
try_parse_tx_hash(&transactions[0].tx_hash).unwrap()
};

let fee = client
.get_txs_fee(
TxFeeTypes::Withdraw,
Address::random(),
TokenLike::Id(TokenId(0)),
)
.await?;
assert_ne!(fee.total_fee, BigUint::zero());

let fee = client
.get_batched_txs_fee(
vec![TxFeeTypes::Withdraw, TxFeeTypes::Transfer],
Expand Down
7 changes: 3 additions & 4 deletions core/bin/zksync_api/src/api_server/rpc_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use zksync_storage::{
},
ConnectionPool, StorageProcessor,
};
use zksync_types::{tx::TxHash, Address, TokenLike, TxFeeTypes};
use zksync_types::{tx::TxHash, Address, BatchFee, Fee, TokenLike, TxFeeTypes};

// Local uses
use crate::{
fee_ticker::{Fee, TickerRequest, TokenPriceRequestType},
fee_ticker::{TickerRequest, TokenPriceRequestType},
signature_checker::VerifyTxSignatureRequest,
utils::shared_lru_cache::SharedLruCache,
};
Expand All @@ -37,7 +37,6 @@ pub mod types;
pub use self::rpc_trait::Rpc;
use self::types::*;
use super::tx_sender::TxSender;
use num::BigUint;

#[derive(Clone)]
pub struct RpcApp {
Expand Down Expand Up @@ -267,7 +266,7 @@ impl RpcApp {
mut ticker_request_sender: mpsc::Sender<TickerRequest>,
transactions: Vec<(TxFeeTypes, Address)>,
token: TokenLike,
) -> Result<BigUint> {
) -> Result<BatchFee> {
let req = oneshot::channel();
ticker_request_sender
.send(TickerRequest::GetBatchTxFee {
Expand Down
16 changes: 4 additions & 12 deletions core/bin/zksync_api/src/api_server/rpc_server/rpc_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ use bigdecimal::BigDecimal;
use jsonrpc_core::{Error, Result};
// Workspace uses
use zksync_types::{
helpers::closest_packable_fee_amount,
tx::{TxEthSignature, TxHash},
Address, Token, TokenLike, TxFeeTypes, ZkSyncTx,
Address, BatchFee, Fee, Token, TokenLike, TxFeeTypes, ZkSyncTx,
};

// Local uses
use crate::{
api_server::tx_sender::SubmitError,
fee_ticker::{BatchFee, Fee, TokenPriceRequestType},
};
use crate::{api_server::tx_sender::SubmitError, fee_ticker::TokenPriceRequestType};

use super::{error::*, types::*, RpcApp};

Expand Down Expand Up @@ -245,14 +241,10 @@ impl RpcApp {

let transactions: Vec<(TxFeeTypes, Address)> =
(tx_types.iter().cloned().zip(addresses.iter().cloned())).collect();
let mut total_fee =
Self::ticker_batch_fee_request(ticker, transactions, token.clone()).await?;

// Sum of transactions can be unpackable
total_fee = closest_packable_fee_amount(&total_fee);
let total_fee = Self::ticker_batch_fee_request(ticker, transactions, token.clone()).await?;

metrics::histogram!("api.rpc.get_txs_batch_fee_in_wei", start.elapsed());
Ok(BatchFee { total_fee })
Ok(total_fee)
}

pub async fn _impl_get_token_price(self, token: TokenLike) -> Result<BigDecimal> {
Expand Down
7 changes: 3 additions & 4 deletions core/bin/zksync_api/src/api_server/rpc_server/rpc_trait.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use std::collections::HashMap;
// External uses
use bigdecimal::BigDecimal;
use futures::{FutureExt, TryFutureExt};
use jsonrpc_core::Error;
use jsonrpc_derive::rpc;

// Workspace uses
use zksync_types::{
tx::{TxEthSignature, TxHash},
Address, Token, TokenLike, TxFeeTypes, ZkSyncTx,
Address, BatchFee, Fee, Token, TokenLike, TxFeeTypes, ZkSyncTx,
};

// Local uses
use crate::fee_ticker::{BatchFee, Fee};
use bigdecimal::BigDecimal;

use super::{types::*, RpcApp};

pub type FutureResp<T> = Box<dyn futures01::Future<Item = T, Error = Error> + Send>;
Expand Down
13 changes: 6 additions & 7 deletions core/bin/zksync_api/src/api_server/tx_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use futures::{
channel::{mpsc, oneshot},
prelude::*,
};
use num::bigint::ToBigInt;
use num::{bigint::ToBigInt, BigUint, Zero};
use thiserror::Error;

// Workspace uses
Expand All @@ -19,18 +19,17 @@ use zksync_storage::ConnectionPool;
use zksync_types::{
tx::EthSignData,
tx::{SignedZkSyncTx, TxEthSignature, TxHash},
Address, Token, TokenId, TokenLike, TxFeeTypes, ZkSyncTx,
Address, BatchFee, Fee, Token, TokenId, TokenLike, TxFeeTypes, ZkSyncTx,
};

// Local uses
use crate::{
core_api_client::CoreApiClient,
fee_ticker::{Fee, TickerRequest, TokenPriceRequestType},
fee_ticker::{TickerRequest, TokenPriceRequestType},
signature_checker::{TxVariant, VerifiedTx, VerifyTxSignatureRequest},
tx_error::TxAddError,
utils::token_db_cache::TokenDBCache,
};
use num::{BigUint, Zero};

#[derive(Clone)]
pub struct TxSender {
Expand Down Expand Up @@ -305,7 +304,7 @@ impl TxSender {
.await?;

let required_total_usd_fee =
BigDecimal::from(required_eth_fee.to_bigint().unwrap()) * &eth_price_in_usd;
BigDecimal::from(required_eth_fee.total_fee.to_bigint().unwrap()) * &eth_price_in_usd;

// Scaling the fee required since the price may change between signing the transaction and sending it to the server.
let scaled_provided_fee_in_usd = scale_user_fee_up(provided_total_usd_fee.clone());
Expand Down Expand Up @@ -381,7 +380,7 @@ impl TxSender {
&self,
transactions: Vec<(TxFeeTypes, Address)>,
token: TokenLike,
) -> Result<BigUint, SubmitError> {
) -> Result<BatchFee, SubmitError> {
Self::ticker_batch_fee_request(self.ticker_requests.clone(), transactions, token).await
}

Expand Down Expand Up @@ -468,7 +467,7 @@ impl TxSender {
mut ticker_request_sender: mpsc::Sender<TickerRequest>,
transactions: Vec<(TxFeeTypes, Address)>,
token: TokenLike,
) -> Result<BigUint, SubmitError> {
) -> Result<BatchFee, SubmitError> {
let req = oneshot::channel();
ticker_request_sender
.send(TickerRequest::GetBatchTxFee {
Expand Down
Loading

0 comments on commit 511f1a8

Please sign in to comment.