Skip to content

Commit

Permalink
Merge pull request #127 from TrueBlocks/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tjayrush authored Sep 14, 2024
2 parents 6ebb1b4 + afb5195 commit 6f9ded5
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 94 deletions.
38 changes: 24 additions & 14 deletions app/data_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
sdk "github.com/TrueBlocks/trueblocks-sdk/v3"
)

var historyMutex sync.Mutex
var historyMutex sync.RWMutex

func (a *App) Reload(addr base.Address) {
a.CancleContexts()
historyMutex.Lock()
delete(a.historyMap, addr)
historyMutex.Unlock()
a.HistoryPage(addr.String(), 0, 15)
a.removeAddress(addr)
a.Refresh()
}
Expand Down Expand Up @@ -47,9 +48,9 @@ func (a *App) HistoryPage(addr string, first, pageSize int) types.HistoryContain
return types.HistoryContainer{}
}

historyMutex.Lock()
historyMutex.RLock()
_, exists := a.historyMap[address]
historyMutex.Unlock()
historyMutex.RUnlock()

if !exists {
messages.Send(a.ctx,
Expand Down Expand Up @@ -77,13 +78,14 @@ func (a *App) HistoryPage(addr string, first, pageSize int) types.HistoryContain
if !ok {
continue
}
historyMutex.Lock()
historyMutex.RLock()
summary := a.historyMap[address]
historyMutex.RUnlock()

summary.Address = address
summary.Name = a.names.NamesMap[address].Name
summary.Items = append(summary.Items, *tx)
a.historyMap[address] = summary
if len(a.historyMap[address].Items)%base.Max(pageSize, 1) == 0 {
if len(summary.Items)%base.Max(pageSize, 1) == 0 {
sort.Slice(summary.Items, func(i, j int) bool {
if summary.Items[i].BlockNumber == summary.Items[j].BlockNumber {
return summary.Items[i].TransactionIndex > summary.Items[j].TransactionIndex
Expand All @@ -92,12 +94,17 @@ func (a *App) HistoryPage(addr string, first, pageSize int) types.HistoryContain
})
messages.Send(a.ctx,
messages.Progress,
messages.NewProgressMsg(int64(len(a.historyMap[address].Items)), int64(nItems), address),
messages.NewProgressMsg(int64(len(summary.Items)), int64(nItems), address),
)
}

historyMutex.Lock()
a.historyMap[address] = summary
historyMutex.Unlock()

case err := <-opts.RenderCtx.ErrorChan:
messages.Send(a.ctx, messages.Error, messages.NewErrorMsg(err, address))

default:
if opts.RenderCtx.WasCanceled() {
return
Expand All @@ -106,12 +113,13 @@ func (a *App) HistoryPage(addr string, first, pageSize int) types.HistoryContain
}
}()

_, meta, err := opts.Export()
_, meta, err := opts.Export() // blocks until forever loop above finishes
if err != nil {
messages.Send(a.ctx, messages.Error, messages.NewErrorMsg(err, address))
return types.HistoryContainer{}
}
a.meta = *meta

historyMutex.Lock()
sort.Slice(a.historyMap[address].Items, func(i, j int) bool {
if a.historyMap[address].Items[i].BlockNumber == a.historyMap[address].Items[j].BlockNumber {
Expand All @@ -125,10 +133,12 @@ func (a *App) HistoryPage(addr string, first, pageSize int) types.HistoryContain
messages.Completed,
messages.NewProgressMsg(int64(len(a.historyMap[address].Items)), int64(len(a.historyMap[address].Items)), address),
)

a.loadPortfolio(nil, nil)
}

historyMutex.Lock()
defer historyMutex.Unlock()
historyMutex.RLock()
defer historyMutex.RUnlock()

first = base.Max(0, base.Min(first, len(a.historyMap[address].Items)-1))
last := base.Min(len(a.historyMap[address].Items), first+pageSize)
Expand All @@ -147,9 +157,9 @@ func (a *App) getHistoryCnt(addr string) int {
return 0
}

historyMutex.Lock()
defer historyMutex.Unlock()
historyMutex.RLock()
l := len(a.historyMap[address].Items)
historyMutex.RUnlock()
if l > 0 {
return l
}
Expand All @@ -173,14 +183,14 @@ func (a *App) removeAddress(addr base.Address) {
for i, item := range a.portfolio.Items {
if item.Address == addr {
a.portfolio.Items = append(a.portfolio.Items[:i], a.portfolio.Items[i+1:]...)
a.portfolio.MyCount--
// a.portfolio.MyCount--
break
}
}
for i, item := range a.monitors.Items {
if item.Address == addr {
a.monitors.Items = append(a.monitors.Items[:i], a.monitors.Items[i+1:]...)
a.monitors.NItems--
// a.monitors.NItems--
break
}
}
Expand Down
8 changes: 4 additions & 4 deletions app/data_history_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func (a *App) ExportToCsv(addr string) {
return
}

historyMutex.Lock()
historyMutex.RLock()
_, exists := a.historyMap[address]
historyMutex.Unlock()
historyMutex.RUnlock()

if exists {
fn := fmt.Sprintf("history_%s.csv", address)
Expand All @@ -39,7 +39,7 @@ func (a *App) ExportToCsv(addr string) {
"Nonce",
"Input",
"TransactionType"))
historyMutex.Lock()
historyMutex.RLock()
for _, item := range a.historyMap[address].Items {
lines = append(lines, fmt.Sprintf("%d,%s,%d,%s,%s,%s,%d,%d,%d,%d,%d,%d,%s,%s",
item.BlockNumber,
Expand All @@ -57,7 +57,7 @@ func (a *App) ExportToCsv(addr string) {
item.Input,
item.TransactionType))
}
historyMutex.Unlock()
historyMutex.RUnlock()
file.StringToAsciiFile(fn, strings.Join(lines, "\n")+"\n")
utils.System(fmt.Sprintf("open %s", fn))
}
Expand Down
2 changes: 1 addition & 1 deletion app/proc_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (a *App) Refresh(which ...string) {
))
}
}
a.loadPortfolio(nil, nil)
// a.loadPortfolio(nil, nil)

// And then update everything else in the fullness of time
wg := sync.WaitGroup{}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/formatters/AppearanceFormatter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export const AppearanceFormatter = ({ value, value2, className, size = "md" }: O
const [isPopupOpen, setPopupOpen] = useState(false);

const copyHash = useCallback(() => {
ClipboardSetText(value2).then(() => {
ClipboardSetText(String(value2)).then(() => {
setPopupOpen(false);
});
}, [value2]);

const appPopup = (
<AppearancePopup
hash={value2}
hash={String(value2)}
onSubmit={() => setPopupOpen(false)}
onClose={() => setPopupOpen(false)}
onCopy={() => copyHash}
Expand Down
54 changes: 47 additions & 7 deletions frontend/src/components/formatters/Formatter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
EdMode,
} from "@components";
import { base } from "@gocode/models";
import { useDateTime, useToEther, useToGas } from "@hooks";
import { useAppState } from "@state";
import { GetDebugColor } from ".";

export type knownType =
Expand Down Expand Up @@ -42,15 +42,19 @@ export type knownType =
export type FormatterProps = {
type: knownType;
value: any;
value2?: any;
value2?: boolean | base.Hash | base.Address | string | undefined;
className?: string;
size?: TextProps["size"];
};

export const Formatter = ({ type, value, value2, className, size = "md" }: FormatterProps) => {
const { address } = useAppState();

const cn = GetDebugColor(type) || className;
const n = value as number;
const bi = value as bigint;
const cn = GetDebugColor(type) || className;
const bool = value2 as boolean;
const from = value2 as unknown as base.Address;

switch (type) {
case "boolean":
Expand All @@ -62,13 +66,13 @@ export const Formatter = ({ type, value, value2, className, size = "md" }: Forma
case "tag":
return <TagFormatter value={value} size={size} className={cn} />;
case "gas":
value = useToGas(bi, value2 as base.Address);
value = from !== address ? "-" : formatEther(bi);
break;
case "ether":
value = useToEther(bi);
value = bool ? "" : formatEther(bi);
break;
case "timestamp":
value = useDateTime(n);
value = formatDateTime(n);
break;
case "date":
value = value?.replace("T", " ");
Expand All @@ -95,7 +99,7 @@ export const Formatter = ({ type, value, value2, className, size = "md" }: Forma
case "url":
break;
case "crud":
return <CrudButton size="xs" value={value} isDeleted={value2} />;
return <CrudButton size="xs" value={value} isDeleted={bool} />;
case "address-editor":
return (
<AddressFormatter type={type} className={cn} size={size} value={value} value2={value2} mode={EdMode.All} />
Expand Down Expand Up @@ -138,3 +142,39 @@ const formatBytes = (bytes: number): string => {
});
return `${formattedValue} ${sizes[i]}`;
};

const formatEther = (value: bigint | string) => {
// from https://viem.sh/docs/utilities/formatUnits
if (typeof value === "string" && value.includes(".")) {
return value;
}
if (!value) return "-";

let display = value.toString();
const negative = display.startsWith("-");
if (negative) display = display.slice(1);
display = display.padStart(18, "0");

const integer = display.slice(0, display.length - 18);
let fraction = display.slice(display.length - 18);
fraction = fraction.slice(0, 5).padEnd(5, "0");

const v = `${negative ? "-" : ""}${integer || "0"}.${fraction}`;
// return display === "000000000000000000" ? "-" : v + " --- " + display;
if (v === "0.00000") return "-";
return v;
};

const formatDateTime = (timestamp: number): string => {
const date = new Date(timestamp * 1000); // Convert timestamp from seconds to milliseconds

const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
const seconds = String(date.getSeconds()).padStart(2, "0");

const formatted = `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;
return formatted;
};
2 changes: 0 additions & 2 deletions frontend/src/hooks/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from "./useKeyboardPaging";
export * from "./useViewName";
export * from "./useToEther";
export * from "./useDateTime";
export * from "./useEnvironment";
21 changes: 0 additions & 21 deletions frontend/src/hooks/useDateTime.tsx

This file was deleted.

34 changes: 0 additions & 34 deletions frontend/src/hooks/useToEther.tsx

This file was deleted.

5 changes: 4 additions & 1 deletion frontend/src/views/History/HistoryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export const tableColumns: CustomColumnDef<types.Transaction, any>[] = [
}),
columnHelper.accessor("value", {
header: () => "Ether",
cell: (info) => <Formatter type="ether" value={info.renderValue()} />,
cell: (info) => {
const { value, isError } = info.row.original;
return <Formatter type="ether" value={value} value2={isError} />;
},
meta: { className: "medium cell" },
}),
columnHelper.accessor("gasUsed", {
Expand Down
12 changes: 6 additions & 6 deletions frontend/wailsjs/go/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,12 @@ export namespace types {

export namespace wizard {

export enum Step {
RESET = "Reset",
PREVIOUS = "Previous",
NEXT = "Next",
FINISH = "Finish",
}
export enum State {
NOTOKAY = "notOkay",
TOMLOKAY = "tomlOkay",
Expand All @@ -1701,12 +1707,6 @@ export namespace wizard {
INDEXOKAY = "indexOkay",
OKAY = "okay",
}
export enum Step {
RESET = "Reset",
PREVIOUS = "Previous",
NEXT = "Next",
FINISH = "Finish",
}
export class Wizard {
state: State;

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ replace (
)

require (
github.com/TrueBlocks/trueblocks-core/src/apps/chifra v0.0.0-20240901212707-3b0228642466
github.com/TrueBlocks/trueblocks-sdk/v3 v3.3.1
github.com/TrueBlocks/trueblocks-core/src/apps/chifra v0.0.0-20240906195947-7a9af20ce595
github.com/TrueBlocks/trueblocks-sdk/v3 v3.3.2
github.com/joho/godotenv v1.5.1
github.com/wailsapp/wails/v2 v2.8.2
)
Expand Down

0 comments on commit 6f9ded5

Please sign in to comment.