Skip to content

Commit

Permalink
Best version yet
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Jul 19, 2024
1 parent 5cbefbe commit a87df27
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 67 deletions.
57 changes: 31 additions & 26 deletions app/history.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
package app

import (
"github.com/TrueBlocks/trueblocks-core/sdk/v3"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
)

var addrToHistoryMap = map[base.Address][]types.Transaction{}

func (a *App) GetHistory(addr string, first, pageSize int) []types.Transaction {
address := base.HexToAddress(addr)
if address.IsZero() {
return []types.Transaction{
{
TransactionIndex: 1,
BlockNumber: 1,
BlockHash: base.HexToHash("0x730724cb08a6eb17bf6b3296359d261570d343ea7944a17a9d7287d77900db01"),
},
{
TransactionIndex: 2,
BlockNumber: 2,
BlockHash: base.HexToHash("0x730724cb08a6eb17bf6b3296359d261570d343ea7944a17a9d7287d77900db02"),
},
{
TransactionIndex: 3,
BlockNumber: 3,
BlockHash: base.HexToHash("0x730724cb08a6eb17bf6b3296359d261570d343ea7944a17a9d7287d77900db03"),
if len(addrToHistoryMap[address]) == 0 {
opts := sdk.ExportOptions{
Addrs: []string{addr},
Articulate: true,
Globals: sdk.Globals{
Cache: true,
},
}
}

var ret []types.Transaction
if len(addrToHistoryMap[address]) == 0 {
return ret
monitors, _, err := opts.Export()
if err != nil {
// EventEmitter.Emit("error", err)
logger.Info(err)
return []types.Transaction{}
} else if len(monitors) == 0 {
logger.Info("none")
return []types.Transaction{}
} else {
logger.Info("got em", len(monitors))
addrToHistoryMap[address] = monitors
}
}
first = base.Max(0, base.Min(first, len(addrToHistoryMap[address])-1))
last := base.Min(len(addrToHistoryMap[address]), first+pageSize)
return addrToHistoryMap[address][first:last]
}

func (a *App) GetHistoryCnt(addr string) int {
address := base.HexToAddress(addr)
if address.IsZero() {
return 3
func (a *App) GetHistoryCnt(addr string) int64 {
opts := sdk.ListOptions{
Addrs: []string{addr},
}
monitors, _, err := opts.ListCount()
if err != nil {
// EventEmitter.Emit("error", err)
return 0
} else if len(monitors) == 0 {
return 0
}
return len(addrToHistoryMap[address])
return monitors[0].NRecords
}
73 changes: 58 additions & 15 deletions frontend/src/views/History/HistoryView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,74 @@ import React, { useState, useEffect } from "react";
import { GetHistory, GetHistoryCnt } from "@gocode/app/App";
import classes from "@/App.module.css";
import { View, ViewStatus } from "@/components/view";
import { Stack, Title } from "@mantine/core";
import { Group, Stack, Title, Text } from "@mantine/core";
import { types } from "@gocode/models";
import { useHotkeys } from "react-hotkeys-hook";

export function HistoryView() {
const [address, setAddress] = useState<string>("");
const [cnt, setCnt] = useState<number>(0);
const [txs, setTxs] = useState<types.Transaction[]>([]);
const [address, setAddress] = useState<string>("0x054993ab0f2b1acc0fdc65405ee203b4271bebe6");
const [items, setItems] = useState<types.Transaction[]>([]);
const [nItems, setNItems] = useState<number>(0);
const [curItem, setCurItem] = useState<number>(0);
const perPage = 20;

useHotkeys("left", (event) => {
setCurItem((cur) => Math.max(cur - 1, 0));
event.preventDefault();
});
useHotkeys("up", (event) => {
setCurItem((cur) => Math.max(cur - perPage, 0));
event.preventDefault();
});
useHotkeys("home", (event) => {
setCurItem(0);
event.preventDefault();
});

useHotkeys("right", (event) => {
var max = Math.max(nItems - perPage, 0);
setCurItem((cur) => Math.min(max, cur + 1));
event.preventDefault();
});
useHotkeys("down", (event) => {
var max = Math.max(nItems - perPage, 0);
setCurItem((cur) => Math.min(max, cur + perPage));
event.preventDefault();
});
useHotkeys("end", (event) => {
var max = Math.max(nItems - perPage, 0);
setCurItem(max);
event.preventDefault();
});

useEffect(() => {
const fetchHistory = async () => {
const fetchedItems = await GetHistory(address, curItem, perPage);
setItems(fetchedItems);
const cnt = await GetHistoryCnt(address);
setNItems(cnt);
};
fetchHistory();
}, [address, curItem, perPage]);

useEffect(() => {
setAddress("0xf503017d7baf7fbc0fff7492b751025c6a78179b");
GetHistoryCnt(address).then((cnt: number) => {
setCnt(cnt);
});
GetHistory(address, 0, 20).then((txs: types.Transaction[]) => {
setTxs(txs);
});
}, []);
setCurItem(0);
}, [address]);

return (
<View>
<Title order={3}>History Title</Title>
<Stack className={classes.mainContent}>
<div>{address}</div>
<div>{cnt}</div>
<div>{JSON.stringify(txs)}</div>
<Text>
{address} at record {curItem} of {nItems} records
</Text>
<Group>
{items.map((item, idx) => (
<Text key={idx}>
{item.blockNumber} {item.from} {item.to} {item.value}
</Text>
))}
</Group>
</Stack>
<ViewStatus>Status / Progress</ViewStatus>
</View>
Expand Down
51 changes: 28 additions & 23 deletions frontend/src/views/Names/NamesView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,64 @@ import { View, ViewStatus } from "@/components/view";
const columnHelper = createColumnHelper<types.Name>();

export function NamesView() {
const [names, _setData] = React.useState<types.Name[]>(() => []);
const [nNames, setNamesCount] = useState<number>(0);
const [curName, setCurName] = useState<number>(-1);
const [perPage, setPerPage] = useState<number>(20);
const [items, setItems] = useState<types.Name[]>([]);
const [nItems, setNItems] = useState<number>(0);
const [curItem, setCurItem] = useState<number>(0);
const perPage = 20;

useHotkeys("left", (event) => {
setCurName(curName - 1 < 0 ? 0 : curName - 1);
setCurItem((cur) => Math.max(cur - 1, 0));
event.preventDefault();
});
useHotkeys("up", (event) => {
setCurName(curName - 20 < 0 ? 0 : curName - 20);
setCurItem((cur) => Math.max(cur - perPage, 0));
event.preventDefault();
});
useHotkeys("right", (event) => {
setCurName(curName + 1 >= nNames ? nNames : curName + 1);
useHotkeys("home", (event) => {
setCurItem(0);
event.preventDefault();
});
useHotkeys("down", (event) => {
setCurName(curName + 20 >= nNames ? nNames - 20 : curName + 20);

useHotkeys("right", (event) => {
var max = Math.max(nItems - perPage, 0);
setCurItem((cur) => Math.min(max, cur + 1));
event.preventDefault();
});
useHotkeys("home", (event) => {
setCurName(0);
useHotkeys("down", (event) => {
var max = Math.max(nItems - perPage, 0);
setCurItem((cur) => Math.min(max, cur + perPage));
event.preventDefault();
});
useHotkeys("end", (event) => {
setCurName(nNames <= 20 ? 20 : nNames - 20);
var max = Math.max(nItems - perPage, 0);
setCurItem(max);
event.preventDefault();
});

useEffect(() => {
GetNames(curName, perPage).then((names: types.Name[]) => {
_setData(names);
GetNamesCnt().then((cnt) => {
setNamesCount(cnt);
});
});
}, [curName]);
const fetchHistory = async () => {
const fetchedItems = await GetNames(curItem, perPage);
setItems(fetchedItems);
const cnt = await GetNamesCnt();
setNItems(cnt);
};
fetchHistory();
}, [curItem, perPage]);

useEffect(() => {
setCurName(0);
setCurItem(0);
}, []);

const table = useReactTable({
data: names,
data: items,
columns: namesColumns,
getCoreRowModel: getCoreRowModel(),
});

return (
<View>
<Stack className={classes.mainContent}>
<Title order={3}>Names Title {curName}</Title>
<Title order={3}>Names Title {curItem}</Title>
<Table>
<Table.Thead>
{table.getHeaderGroups().map((headerGroup) => (
Expand Down
15 changes: 15 additions & 0 deletions frontend/wailsjs/go/types/Appearance.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {types} from '../models';

export function Date():Promise<string>;

export function FinishUnmarshal():Promise<void>;

export function GetKey():Promise<string>;

export function Model(arg1:string,arg2:string,arg3:boolean,arg4:{[key: string]: any}):Promise<types.Model>;

export function Orig():Promise<string>;

export function String():Promise<string>;
27 changes: 27 additions & 0 deletions frontend/wailsjs/go/types/Appearance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT

export function Date() {
return window['go']['types']['Appearance']['Date']();
}

export function FinishUnmarshal() {
return window['go']['types']['Appearance']['FinishUnmarshal']();
}

export function GetKey() {
return window['go']['types']['Appearance']['GetKey']();
}

export function Model(arg1, arg2, arg3, arg4) {
return window['go']['types']['Appearance']['Model'](arg1, arg2, arg3, arg4);
}

export function Orig() {
return window['go']['types']['Appearance']['Orig']();
}

export function String() {
return window['go']['types']['Appearance']['String']();
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22
toolchain go1.22.0

replace (
github.com/TrueBlocks/trueblocks-core/sdk => ../trueblocks-core/sdk
github.com/TrueBlocks/trueblocks-core/sdk/v3 => ../trueblocks-core/sdk
github.com/TrueBlocks/trueblocks-core/src/apps/chifra => ../trueblocks-core/src/apps/chifra
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/TrueBlocks/trueblocks-core/sdk/v3 v3.0.0-20240718213142-3cffd1bf17f0 h1:wWeaGbu8mv+NLwDJ3EKPNpoX4UxYxL8jjtFNQdhP2tg=
github.com/TrueBlocks/trueblocks-core/sdk/v3 v3.0.0-20240718213142-3cffd1bf17f0/go.mod h1:r01Zts5Fl/rEdb5iU94jg5cMu+fPn8msgiAL3u1MIxg=
github.com/VictoriaMetrics/fastcache v1.6.0/go.mod h1:0qHz5QP0GMX4pfmMA/zt5RgfNuXJrTP0zS7DqpHGGTw=
github.com/VictoriaMetrics/fastcache v1.12.1 h1:i0mICQuojGDL3KblA7wUNlY5lOK6a4bwt3uRKnkZU40=
github.com/VictoriaMetrics/fastcache v1.12.1/go.mod h1:tX04vaqcNoQeGLD+ra5pU5sWkuxnzWhEzLwhP9w653o=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func main() {
a,
&types.Name{},
&types.Transaction{},
&types.Appearance{},
},
StartHidden: true,
AssetServer: &assetserver.Options{
Expand Down

0 comments on commit a87df27

Please sign in to comment.