Skip to content

Commit

Permalink
Merge branch 'dev' into tsionyx-zks-235-api-client-crate
Browse files Browse the repository at this point in the history
  • Loading branch information
tsionyx committed Dec 24, 2020
2 parents 622861e + 5dee172 commit 5cc5737
Show file tree
Hide file tree
Showing 58 changed files with 1,664 additions and 372 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
!docker/exit-tool/exit-tool-entry.sh
!docker/exit-tool/configs
!docker/keybase-secrets/entrypoint.sh
!docker/ci-integration-test/entrypoint.sh
!etc/env/dev.env.example
!etc/tokens
!keys/packed
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
ci-all:
# We currently have two self-hosted runners, one of which is marked "DEV-CI" and other one is marder "MAIN".
# "MAIN" is the current CI runner, "DEV-CI" is currently used to experiment with CI optimizing.
runs-on: self-hosted
runs-on: [self-hosted, MAIN]

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:
rustup update stable
cargo install --version=0.2.0 sqlx-cli
cargo install diesel_cli --no-default-features --features postgres
docker pull matterlabs/ci-integration-test:zk-latest
docker pull matterlabs/ci-integration-test:latest
- name: zk
run: zk
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions bin/zcli
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

if [ -z "$1" ]; then
cd $ZKSYNC_HOME
yarn && yarn zcli build
else
# can't start this with yarn since it has quirks with `--` as an argument
node -- $ZKSYNC_HOME/infrastructure/zcli/build/index.js "$@"
fi
1 change: 1 addition & 0 deletions core/bin/zksync_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jsonwebtoken = "7"
metrics = "0.13.0-alpha.8"
lru-cache = "0.1.2"
once_cell = "1.4"
regex = "1"

[dev-dependencies]
zksync_test_account = { path = "../../tests/test_account" }
10 changes: 6 additions & 4 deletions core/bin/zksync_api/src/api_server/rest/v1/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use actix_web::{
Scope,
};

use types::AccountOpReceipt;
// Workspace uses
use zksync_config::ConfigurationOptions;
use zksync_storage::{QueryResult, StorageProcessor};
Expand All @@ -25,11 +24,14 @@ use self::types::{
pending_account_op_receipt_from_priority_op, search_direction_as_storage,
tx_receipt_from_response, validate_receipts_query,
},
AccountInfo, AccountQuery, AccountReceiptsQuery, AccountState, AccountTxReceipt,
PendingAccountOpReceipt, SearchDirection, TxLocation,
AccountReceiptsQuery, SearchDirection,
};
// Public uses
pub use self::types::convert::account_state_from_storage;
pub use self::types::{
convert::account_state_from_storage, AccountInfo, AccountOpReceipt, AccountQuery,
AccountReceipts, AccountState, AccountTxReceipt, DepositingBalances, DepositingFunds,
PendingAccountOpReceipt, TxLocation,
};

#[cfg(test)]
mod tests;
Expand Down
7 changes: 3 additions & 4 deletions core/bin/zksync_api/src/api_server/rest/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use actix_web::{

use Error as ApiError;
// Workspace uses
pub use zksync_api_client::rest::v1::{Client, ClientError, Pagination, PaginationQuery};
pub use zksync_api_client::rest::v1::{
Client, ClientError, Pagination, PaginationQuery, MAX_LIMIT,
};
use zksync_config::{ApiServerOptions, ConfigurationOptions};

// Local uses
Expand All @@ -28,9 +30,6 @@ mod test_utils;
mod tokens;
mod transactions;

/// Maximum limit value in the requests.
pub const MAX_LIMIT: u32 = 100;

type JsonResult<T> = std::result::Result<web::Json<T>, Error>;

pub(crate) fn api_scope(
Expand Down
4 changes: 2 additions & 2 deletions core/bin/zksync_api/src/api_server/rest/v1/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ mod tests {
);

let expected_data = PriorityOpData {
data: dummy_deposit_op(Address::default(), 1, 15, 2),
data: dummy_deposit_op(Address::default(), 1, 15, 2).op,
serial_id: VERIFIED_OP_SERIAL_ID,
eth_hash: verified_op_hash,
};
Expand Down Expand Up @@ -250,7 +250,7 @@ mod tests {
);

let expected_data = PriorityOpData {
data: dummy_full_exit_op(1, Address::default(), 16, 3),
data: dummy_full_exit_op(1, Address::default(), 16, 3).op,
serial_id: COMMITTED_OP_SERIAL_ID,
eth_hash: committed_eth_hash,
};
Expand Down
18 changes: 6 additions & 12 deletions core/bin/zksync_api/src/api_server/rest/v1/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,9 @@ impl TestServerConfig {
NewExecutedPriorityOperation {
block_number: 2,
block_index: 2,
operation: serde_json::to_value(dummy_deposit_op(
Address::default(),
1,
VERIFIED_OP_SERIAL_ID,
2,
))
operation: serde_json::to_value(
dummy_deposit_op(Address::default(), 1, VERIFIED_OP_SERIAL_ID, 2).op,
)
.unwrap(),
from_account: Default::default(),
to_account: Default::default(),
Expand All @@ -411,12 +408,9 @@ impl TestServerConfig {
NewExecutedPriorityOperation {
block_number: VERIFIED_BLOCKS_COUNT as i64 + 1,
block_index: 1,
operation: serde_json::to_value(dummy_full_exit_op(
1,
Address::default(),
COMMITTED_OP_SERIAL_ID,
3,
))
operation: serde_json::to_value(
dummy_full_exit_op(1, Address::default(), COMMITTED_OP_SERIAL_ID, 3).op,
)
.unwrap(),
from_account: Default::default(),
to_account: Default::default(),
Expand Down
104 changes: 104 additions & 0 deletions core/bin/zksync_api/src/bin/dev-liquidity-token-watcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//! Token watcher implementation for dev environment
//!
//! Implements Uniswap API for token which are deployed in localhost network
use std::{collections::HashMap, fs::File, io::BufReader, path::Path};

use actix_cors::Cors;
use actix_web::{middleware, web, App, HttpResponse, HttpServer, Result};
use bigdecimal::BigDecimal;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use zksync_api::fee_ticker::validator::watcher::{
GraphqlResponse, GraphqlTokenResponse, TokenResponse,
};

#[derive(Serialize, Deserialize, Clone, Debug)]
struct TokenData {
address: String,
name: String,
volume: BigDecimal,
}

type Tokens = HashMap<String, TokenData>;

#[derive(Debug, Serialize, Deserialize)]
struct GrqaphqlQuery {
query: String,
}

async fn handle_graphql(
params: web::Json<GrqaphqlQuery>,
tokens: web::Data<Tokens>,
) -> Result<HttpResponse> {
// Now, we support only one graphql query, we will add full
let query_parser = Regex::new(r#"\{token\(id:\s"(?P<address>.*?)"\).*"#).expect("Right regexp");
let caps = query_parser.captures(&params.query).unwrap();
let address = &caps["address"].to_ascii_lowercase();
let volume = if let Some(token) = tokens.get(address) {
token.volume.clone()
} else {
BigDecimal::from(0)
};
let response = GraphqlResponse {
data: GraphqlTokenResponse {
token: Some(TokenResponse {
trade_volume_usd: volume.to_string(),
}),
},
};
Ok(HttpResponse::Ok().json(response))
}

fn load_tokens(path: &Path) -> Tokens {
let file = File::open(path).unwrap();
let reader = BufReader::new(file);

let values: Vec<Value> = serde_json::from_reader(reader).unwrap();
let mut tokens: Tokens = Default::default();
for value in values {
if let Value::Object(value) = value {
let address = value["address"].as_str().unwrap().to_ascii_lowercase();
tokens.insert(
address.clone(),
TokenData {
address,
name: value["name"].to_string(),
volume: BigDecimal::from(500),
},
);
};
}
let phnx_token = TokenData {
address: "0x6b175474e89094c44da98b954eedeac495271d0f".to_string(),

name: "PHNX".to_string(),
volume: BigDecimal::from(30),
};
tokens.insert(phnx_token.address.clone(), phnx_token);
tokens
}

fn main() {
env_logger::init();

let mut runtime = actix_rt::System::new("dev-liquidity-token-watcher");

let tokens = load_tokens(Path::new(&"etc/tokens/localhost.json".to_string()));
runtime.block_on(async {
HttpServer::new(move || {
App::new()
.wrap(middleware::Logger::default())
.wrap(Cors::new().send_wildcard().max_age(3600).finish())
.data(tokens.clone())
.route("/graphql", web::post().to(handle_graphql))
})
.bind("0.0.0.0:9975")
.unwrap()
.shutdown_timeout(1)
.run()
.await
.expect("Server crashed");
});
}
14 changes: 8 additions & 6 deletions core/bin/zksync_api/src/fee_ticker/balancer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use zksync_storage::ConnectionPool;

use crate::{
fee_ticker::{
fee_token_validator::FeeTokenValidator,
ticker_api::{TickerApi, TokenPriceAPI},
ticker_info::FeeTickerInfo,
validator::{watcher::TokenWatcher, FeeTokenValidator},
FeeTicker, TickerConfig, TickerRequest,
},
utils::token_db_cache::TokenDBCache,
Expand All @@ -21,22 +21,23 @@ static TICKER_CHANNEL_SIZE: usize = 32000;

/// `TickerBalancer` is a struct used for scaling the ticker.
/// Create `n` tickers and balance the load between them.
pub(crate) struct TickerBalancer<API: TokenPriceAPI, INFO> {
tickers: Vec<FeeTicker<TickerApi<API>, INFO>>,
pub(crate) struct TickerBalancer<API: TokenPriceAPI, INFO, WATCHER> {
tickers: Vec<FeeTicker<TickerApi<API>, INFO, WATCHER>>,
channels: Vec<Sender<TickerRequest>>,
requests: Receiver<TickerRequest>,
}

impl<API, INFO> TickerBalancer<API, INFO>
impl<API, INFO, WATCHER> TickerBalancer<API, INFO, WATCHER>
where
API: TokenPriceAPI + Clone + Sync + Send + 'static,
INFO: FeeTickerInfo + Clone + Sync + Send + 'static,
WATCHER: TokenWatcher + Clone + Sync + Send + 'static,
{
pub fn new(
token_price_api: API,
ticker_info: INFO,
ticker_config: TickerConfig,
validator: FeeTokenValidator,
validator: FeeTokenValidator<WATCHER>,
requests: Receiver<TickerRequest>,
db_pool: ConnectionPool,
number_of_tickers: u8,
Expand Down Expand Up @@ -98,6 +99,7 @@ mod tests {
use super::TickerBalancer;
use crate::fee_ticker::ticker_api::coingecko::CoinGeckoAPI;
use crate::fee_ticker::ticker_info::TickerInfo;
use crate::fee_ticker::validator::watcher::UniswapTokenWatcher;
use crate::fee_ticker::TickerRequest;
use futures::{
channel::{mpsc, oneshot},
Expand All @@ -116,7 +118,7 @@ mod tests {
}
let (mut request_sender, request_receiver) = mpsc::channel(2);

let dispatcher = TickerBalancer::<CoinGeckoAPI, TickerInfo> {
let dispatcher = TickerBalancer::<CoinGeckoAPI, TickerInfo, UniswapTokenWatcher> {
tickers: vec![],
channels: senders,
requests: request_receiver,
Expand Down
Loading

0 comments on commit 5cc5737

Please sign in to comment.