Skip to content

Commit

Permalink
History command
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenKor committed Apr 10, 2022
1 parent be15e06 commit 0053ca2
Show file tree
Hide file tree
Showing 6 changed files with 9,108 additions and 8,674 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NETWORK=ethereum
RPC_URL='https://kovan.poa.network'
RPC_URL='https://kovan.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161'
RELAYER_URL='https://relayer.thgkjlr.website/'
TOKEN_ADDRESS='0xd0c47706dcC088E3133ee0D58Ab6ba95A6ad362f'
CONTRACT_ADDRESS='0x3453A3FB52cE634d994fB498447C92D78AbBE49f'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zeropool-console",
"version": "0.3.0",
"version": "0.3.1",
"license": "MIT",
"author": "Dmitry Vdovin <[email protected]>",
"homepage": "https://github.com/zeropoolnetwork/zeropool-console",
Expand Down
7 changes: 6 additions & 1 deletion src/account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AES from 'crypto-js/aes';
import Utf8 from 'crypto-js/enc-utf8';
import { EthereumClient, PolkadotClient, Client as NetworkClient } from 'zeropool-support-js';
import { init, ZeropoolClient } from 'zeropool-client-js';
import { init, ZeropoolClient, HistoryRecord } from 'zeropool-client-js';
import bip39 from 'bip39-light';
import HDWalletProvider from '@truffle/hdwallet-provider';
import { deriveSpendingKey } from 'zeropool-client-js/lib/utils';
Expand Down Expand Up @@ -151,6 +151,11 @@ export default class Account {
return this.zpClient.rawState(TOKEN_ADDRESS);
}

public async getAllHistory(): Promise<HistoryRecord[]> {
return this.zpClient.getAllHistory(TOKEN_ADDRESS);
}


// TODO: Support multiple tokens
public async getTokenBalance(): Promise<string> {
return await this.client.getTokenBalance(TOKEN_ADDRESS);
Expand Down
49 changes: 33 additions & 16 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Account from './account';
import bip39 from 'bip39-light';
import { HistoryRecord, HistoryTransactionType } from 'zeropool-client-js';

export async function setSeed(seed: string, password: string) {
await this.account.login(seed, password);
Expand Down Expand Up @@ -42,22 +43,6 @@ Private balance:
this.resume();
}

// export async function getBalances() {
// const account: Account = this.account;
// const balances = await account.getBalances();
// let buf = '';

// for (const [coinType, coinBalances] of Object.entries(balances)) {
// buf += ` ${NetworkType[coinType]}:\n`;

// for (const balance of coinBalances) {
// buf += ` ${balance.address}: ${balance.balance}\n`;
// }
// }

// this.echo(`Balances:\n${buf}`);
// }

export async function getTokenBalance() {
return this.account.getTokenBalance();
}
Expand Down Expand Up @@ -102,6 +87,38 @@ export async function getInternalState() {
}
}

export async function printHistory() {
this.pause();
const history: HistoryRecord[] = await this.account.getAllHistory();
this.resume();
for (const tx of history) {
this.echo(`${humanReadable(tx, 1000000000, "TOKEN")}`);
}
}

function humanReadable(record: HistoryRecord, denominator: number, tokenname: string): string {
let dt = new Date(record.timestamp * 1000);

let mainPart: string;
if (record.type == HistoryTransactionType.Deposit) {
mainPart = `DEPOSITED ${Number(record.amount) / denominator} ${tokenname} FROM ${record.from}`;
} else if (record.type == HistoryTransactionType.TransferIn) {
mainPart = `RECEIVED ${Number(record.amount) / denominator} sh${tokenname} ON ${record.to}`;
} else if (record.type == HistoryTransactionType.TransferOut) {
mainPart = `SENDED ${Number(record.amount) / denominator} sh${tokenname} TO ${record.to}`;
} else if (record.type == HistoryTransactionType.Withdrawal) {
mainPart = `WITHDRAWED ${Number(record.amount) / denominator} sh${tokenname} TO ${record.to}`;
} else {
mainPart = `UNKNOWN TRANSACTION TYPE (${record.type})`
}

if (record.fee > 0) {
mainPart += `(fee = ${record.fee})`;
}

return `${dt.toLocaleString()} : ${mainPart} [txHash ${record.txHash}]`;
}

export function clear() {
this.clear();
}
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const COMMANDS: { [key: string]: [(...args) => void, string, string] } = {
'transfer-shielded': [c.transferShielded, '<shielded address> <amount>', 'move shielded tokens to the another zkBob address (inside a pool)'],
'deposit-shielded': [c.depositShielded, '<amount>', 'shield some tokens'],
'withdraw-shielded': [c.withdrawShielded, '<amount> [address]', 'withdraw shielded tokens to the native address (to the your account if addres is ommited)'],
'history': [c.printHistory, '', 'print all transactions related to your account'],
'internal-state': [c.getInternalState, '', 'print your account and incoming notes'],
'clear': [c.clear, '', 'clear terminal'],
'reset': [c.reset, '', 'log out from the current account'],
Expand Down Expand Up @@ -121,6 +122,8 @@ const COMMANDS: { [key: string]: [(...args) => void, string, string] } = {
<div class="command-example">withdraw-shielded 2000000000000000000 [optional_external_address]</div>
<div class="comment">// Check your shielded balance</div>
<div class="command-example">get-shielded-balance</div>
<div class="comment">// Print account history</div>
<div class="command-example">history</div>
</p>
<p>
Expand Down
Loading

0 comments on commit 0053ca2

Please sign in to comment.