Fix current-balances command
@@ -4,16 +4,23 @@ import * as ethers from 'ethers';
export async function currentBalances(network: types.Network, operator_address: string) {
const provider = await zksync.getDefaultProvider(network);
- const state = await provider.getState(operator_address);
+ const ethProvider = ethers.getDefaultProvider(network);
let balances: {[token: string] : {eth: number, usd: number}} = {"total": {"eth": 0, "usd": 0}};
+
const eth_price = await provider.getTokenPrice("ETH");
- for (const token in state.verified.balances) {
+ for (const token in await provider.getTokens()) {
+ if(token == "ETH")
+ continue;
+
const tokenPrice = await provider.getTokenPrice(token);
- const tokenAmount = state.verified.balances[token].toString();
+ const tokenAddress = await provider.tokenSet.resolveTokenAddress(token);
+ const erc20contract = new ethers.Contract(tokenAddress, zksync.utils.IERC20_INTERFACE, ethProvider);
+ const tokenAmount = await erc20contract.balanceOf(operator_address);
const numberOfTokens = Number(provider.tokenSet.formatToken(token, tokenAmount));
+
const usd_cost = tokenPrice * numberOfTokens;
const eth_cost = usd_cost / eth_price;
@@ -25,8 +32,6 @@ export async function currentBalances(network: types.Network, operator_address:
"usd": usd_cost
};
}
- await provider.disconnect();
-
return balances;
}
@@ -54,14 +59,10 @@ export async function collectedFees(network: types.Network, providerAddress: str
const response = await fetch(blockUrl);
const blocks = await response.json();
- if (blocks === null)
+ if (blocks == null)
break;
for(const block of blocks) {
- // skip unverified blocks
- if(block.verified_at == null)
- continue;
-
currentBlock = block.block_number;
currentBlockTimeStamp = Date.parse(block.verified_at);
@@ -69,18 +70,31 @@ export async function collectedFees(network: types.Network, providerAddress: str
break;
const commitTransaction = await ethProvider.getTransaction(block.commit_tx_hash);
- const verifyTransaction = await ethProvider.getTransaction(block.verify_tx_hash);
- const verifyTransactionRequest = await ethProvider.getTransactionReceipt(block.verify_tx_hash);
const commitTransactionRequest = await ethProvider.getTransactionReceipt(block.commit_tx_hash);
- if(commitTransaction == null || verifyTransactionRequest == null || verifyTransaction == null || commitTransactionRequest == null)
+ if(commitTransaction == null || commitTransactionRequest == null)
continue;
const commitTransactionFee = Number(ethers.utils.formatEther(commitTransactionRequest.gasUsed.mul(commitTransaction.gasPrice)));
+
+ statistics["SENDER_ACCOUNT"]["eth"] += commitTransactionFee;
+ statistics["SENDER_ACCOUNT"]["usd"] += commitTransactionFee * eth_price;
+
+ // skip unverified blocks
+ if(block.verified_at == null)
+ continue;
+
+ const verifyTransaction = await ethProvider.getTransaction(block.verify_tx_hash);
+ const verifyTransactionRequest = await ethProvider.getTransactionReceipt(block.verify_tx_hash);
+
+
+ if(verifyTransaction == null || verifyTransactionRequest == null)
+ continue;
+
const verifyTransactionFee = Number(ethers.utils.formatEther(verifyTransactionRequest.gasUsed.mul(commitTransaction.gasPrice)));
- statistics["SENDER_ACCOUNT"]["eth"] += verifyTransactionFee + commitTransactionFee;
- statistics["SENDER_ACCOUNT"]["usd"] += (verifyTransactionFee + commitTransactionFee) * eth_price;
+ statistics["SENDER_ACCOUNT"]["eth"] += verifyTransactionFee;
+ statistics["SENDER_ACCOUNT"]["usd"] += verifyTransactionFee * eth_price;
const transactionUrl = `${providerAddress}/api/v0.1/blocks/${currentBlock}/transactions`;
const response = await fetch(transactionUrl);
