Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
perekopskiy committed Aug 12, 2021
1 parent c6f8ce2 commit 736136a
Show file tree
Hide file tree
Showing 8 changed files with 757 additions and 21 deletions.
15 changes: 8 additions & 7 deletions core/bin/zksync_api/src/api_server/web3/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ use tiny_keccak::keccak256;
use zksync_storage::StorageProcessor;
use zksync_types::{Nonce, Token, TokenId, ZkSyncOp, NFT};
// Local uses
use super::converter::{log, u256_from_biguint};
use super::types::{Bytes, CommonLogData, Event, Log, H160, H256, U256};
use super::{
converter::{log, u256_from_biguint},
types::{Bytes, CommonLogData, Event, Log, H160, H256, U256},
NFT_FACTORY_ADDRESS, ZKSYNC_PROXY_ADDRESS,
};
use crate::utils::token_db_cache::TokenDBCache;

#[derive(Debug, Clone)]
pub struct LogsHelper {
topic_by_event: HashMap<Event, H256>,
tokens: TokenDBCache,
pub zksync_proxy_address: H160,
zksync_proxy_address: H160,
nft_factory_address: H160,
}

Expand Down Expand Up @@ -46,10 +49,8 @@ impl LogsHelper {
Self {
topic_by_event,
tokens: TokenDBCache::new(),
zksync_proxy_address: H160::from_str("1000000000000000000000000000000000000000")
.unwrap(),
nft_factory_address: H160::from_str("2000000000000000000000000000000000000000")
.unwrap(),
zksync_proxy_address: H160::from_str(ZKSYNC_PROXY_ADDRESS).unwrap(),
nft_factory_address: H160::from_str(NFT_FACTORY_ADDRESS).unwrap(),
}
}

Expand Down
3 changes: 3 additions & 0 deletions core/bin/zksync_api/src/api_server/web3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ mod rpc_trait;
mod tests;
mod types;

pub const ZKSYNC_PROXY_ADDRESS: &str = "1000000000000000000000000000000000000000";
pub const NFT_FACTORY_ADDRESS: &str = "2000000000000000000000000000000000000000";

#[derive(Clone)]
pub struct Web3RpcApp {
runtime_handle: tokio::runtime::Handle,
Expand Down
2 changes: 2 additions & 0 deletions core/bin/zksync_api/src/api_server/web3/rpc_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ impl Web3RpcApp {
.any(|(i, topic)| i > 0 && topic.is_some());
if has_not_first {
return Ok(Vec::new());
} else if topics.is_empty() {
Vec::new()
} else {
topics.remove(0).unwrap_or_default().0
}
Expand Down
4 changes: 2 additions & 2 deletions core/bin/zksync_api/src/api_server/web3/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zksync_types::{
use super::{
converter::transaction_from_tx_data,
types::{BlockInfo, Event, Log, Transaction, TransactionReceipt, H160, H256, U256, U64},
Web3RpcApp,
Web3RpcApp, ZKSYNC_PROXY_ADDRESS,
};
use crate::api_server::rest::v02::test_utils::TestServerConfig;

Expand Down Expand Up @@ -949,7 +949,7 @@ async fn get_logs() -> anyhow::Result<()> {
.unwrap();
addresses.push(token.address);
}
addresses.push(rpc_app.logs_helper.zksync_proxy_address);
addresses.push(H160::from_str(ZKSYNC_PROXY_ADDRESS).unwrap());

let fut = {
let (client, server) = local_client().await?;
Expand Down
14 changes: 7 additions & 7 deletions core/lib/storage/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -4646,13 +4646,13 @@
"Left": []
},
"nullable": [
false,
false,
false,
false,
false,
false,
false
true,
true,
true,
true,
true,
true,
true
]
}
},
Expand Down
3 changes: 2 additions & 1 deletion core/tests/ts-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"mocha-steps": "^1.3.0",
"node-fetch": "^2.6.1",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
"typescript": "^4.0.5",
"web3": "^1.5.1"
}
}
58 changes: 57 additions & 1 deletion core/tests/ts-tests/tests/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Wallet, RestProvider, getDefaultRestProvider, types } from 'zksync';
import { Wallet, RestProvider, getDefaultRestProvider, types, utils } from 'zksync';
import { Tester } from './tester';
import Web3 from 'web3';
import { BigNumber } from 'ethers';
import './priority-ops';
import './change-pub-key';
import './transfer';
Expand Down Expand Up @@ -202,3 +204,57 @@ describe('ZkSync REST API V0.2 tests', () => {
expect(batchInfo.batchHash).to.eql(submitBatchResponse.batchHash);
});
});

describe('ZkSync web3 API tests', () => {
let tester: Tester;
let alice: Wallet;
let token: string = 'ETH';
let amount: BigNumber;

before('create tester and test wallets', async () => {
tester = await Tester.init('localhost', 'HTTP', 'RPC');
alice = await tester.fundedWallet('1.0');
let bob = await tester.emptyWallet();
amount = tester.syncProvider.tokenSet.parseToken(token, '1000');
await tester.testDeposit(alice, token, amount, true);
await tester.testChangePubKey(alice, token, false);
await tester.testTransfer(alice, bob, token, amount.div(4));
await tester.testWithdraw(alice, token, amount.div(5));
});

it('should check getLogs', async () => {
let web3 = new Web3('http://localhost:3002');
let restProvider = await getDefaultRestProvider('localhost');
let txs = await restProvider.accountTxs(alice.address(), { from: 'latest', direction: 'older', limit: 4 });
let fromBlock = txs.list[3].blockNumber!;
let toBlock = txs.list[0].blockNumber!;
let blockInfo: types.ApiBlockInfo;
do {
await utils.sleep(1000);
blockInfo = await restProvider.blockByPosition(toBlock);
} while (!blockInfo);

let logs = await web3.eth.getPastLogs({ fromBlock, toBlock });
let address = alice.address().substr(2).toLowerCase();
let count: Map<string, number> = new Map();
for (let log of logs) {
if (log.data.includes(address)) {
let countBefore = count.get(log.topics[0]);
if (!countBefore) {
countBefore = 0;
}
count.set(log.topics[0], countBefore + 1);
}
}
let zksyncDepositCount = count.get('0x2662d0a3ad9ff1caf7988e9ff1b359522c4c35d29e07035460b1e25c963ad3a0');
let zksyncChangePubKeyCount = count.get('0xe4ff40ee5f4c5ece5c0a6a50f5888e1a053fe2432370efd4f7d68a1629fe0a2c');
let zksyncTransferCount = count.get('0x5658042ff5bd6fc1d9418fb7a395205c554d2f0214fe1a13c193ee60fba3fbe2');
let zksyncWithdrawCount = count.get('0xc7937e7c6cc38e3d8cde2d3b9e942ae00eda070b394e346d232ed907c2d53a25');
let erc20TransferCount = count.get('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef');
expect(zksyncDepositCount).to.eql(1);
expect(zksyncChangePubKeyCount).to.eql(1);
expect(zksyncTransferCount).to.eql(1);
expect(zksyncWithdrawCount).to.eql(1);
expect(erc20TransferCount).to.eql(6); // 1 one Deposit, 1 one ChangePubKey, 2 for Transfer, 2 for Withdraw.
});
});
Loading

0 comments on commit 736136a

Please sign in to comment.