Skip to content

Commit 6f9ded5

Browse files
authored
Merge pull request #127 from TrueBlocks/develop
Develop
2 parents 6ebb1b4 + afb5195 commit 6f9ded5

File tree

11 files changed

+90
-94
lines changed

11 files changed

+90
-94
lines changed

app/data_history.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import (
1212
sdk "github.com/TrueBlocks/trueblocks-sdk/v3"
1313
)
1414

15-
var historyMutex sync.Mutex
15+
var historyMutex sync.RWMutex
1616

1717
func (a *App) Reload(addr base.Address) {
1818
a.CancleContexts()
1919
historyMutex.Lock()
2020
delete(a.historyMap, addr)
2121
historyMutex.Unlock()
22+
a.HistoryPage(addr.String(), 0, 15)
2223
a.removeAddress(addr)
2324
a.Refresh()
2425
}
@@ -47,9 +48,9 @@ func (a *App) HistoryPage(addr string, first, pageSize int) types.HistoryContain
4748
return types.HistoryContainer{}
4849
}
4950

50-
historyMutex.Lock()
51+
historyMutex.RLock()
5152
_, exists := a.historyMap[address]
52-
historyMutex.Unlock()
53+
historyMutex.RUnlock()
5354

5455
if !exists {
5556
messages.Send(a.ctx,
@@ -77,13 +78,14 @@ func (a *App) HistoryPage(addr string, first, pageSize int) types.HistoryContain
7778
if !ok {
7879
continue
7980
}
80-
historyMutex.Lock()
81+
historyMutex.RLock()
8182
summary := a.historyMap[address]
83+
historyMutex.RUnlock()
84+
8285
summary.Address = address
8386
summary.Name = a.names.NamesMap[address].Name
8487
summary.Items = append(summary.Items, *tx)
85-
a.historyMap[address] = summary
86-
if len(a.historyMap[address].Items)%base.Max(pageSize, 1) == 0 {
88+
if len(summary.Items)%base.Max(pageSize, 1) == 0 {
8789
sort.Slice(summary.Items, func(i, j int) bool {
8890
if summary.Items[i].BlockNumber == summary.Items[j].BlockNumber {
8991
return summary.Items[i].TransactionIndex > summary.Items[j].TransactionIndex
@@ -92,12 +94,17 @@ func (a *App) HistoryPage(addr string, first, pageSize int) types.HistoryContain
9294
})
9395
messages.Send(a.ctx,
9496
messages.Progress,
95-
messages.NewProgressMsg(int64(len(a.historyMap[address].Items)), int64(nItems), address),
97+
messages.NewProgressMsg(int64(len(summary.Items)), int64(nItems), address),
9698
)
9799
}
100+
101+
historyMutex.Lock()
102+
a.historyMap[address] = summary
98103
historyMutex.Unlock()
104+
99105
case err := <-opts.RenderCtx.ErrorChan:
100106
messages.Send(a.ctx, messages.Error, messages.NewErrorMsg(err, address))
107+
101108
default:
102109
if opts.RenderCtx.WasCanceled() {
103110
return
@@ -106,12 +113,13 @@ func (a *App) HistoryPage(addr string, first, pageSize int) types.HistoryContain
106113
}
107114
}()
108115

109-
_, meta, err := opts.Export()
116+
_, meta, err := opts.Export() // blocks until forever loop above finishes
110117
if err != nil {
111118
messages.Send(a.ctx, messages.Error, messages.NewErrorMsg(err, address))
112119
return types.HistoryContainer{}
113120
}
114121
a.meta = *meta
122+
115123
historyMutex.Lock()
116124
sort.Slice(a.historyMap[address].Items, func(i, j int) bool {
117125
if a.historyMap[address].Items[i].BlockNumber == a.historyMap[address].Items[j].BlockNumber {
@@ -125,10 +133,12 @@ func (a *App) HistoryPage(addr string, first, pageSize int) types.HistoryContain
125133
messages.Completed,
126134
messages.NewProgressMsg(int64(len(a.historyMap[address].Items)), int64(len(a.historyMap[address].Items)), address),
127135
)
136+
137+
a.loadPortfolio(nil, nil)
128138
}
129139

130-
historyMutex.Lock()
131-
defer historyMutex.Unlock()
140+
historyMutex.RLock()
141+
defer historyMutex.RUnlock()
132142

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

150-
historyMutex.Lock()
151-
defer historyMutex.Unlock()
160+
historyMutex.RLock()
152161
l := len(a.historyMap[address].Items)
162+
historyMutex.RUnlock()
153163
if l > 0 {
154164
return l
155165
}
@@ -173,14 +183,14 @@ func (a *App) removeAddress(addr base.Address) {
173183
for i, item := range a.portfolio.Items {
174184
if item.Address == addr {
175185
a.portfolio.Items = append(a.portfolio.Items[:i], a.portfolio.Items[i+1:]...)
176-
a.portfolio.MyCount--
186+
// a.portfolio.MyCount--
177187
break
178188
}
179189
}
180190
for i, item := range a.monitors.Items {
181191
if item.Address == addr {
182192
a.monitors.Items = append(a.monitors.Items[:i], a.monitors.Items[i+1:]...)
183-
a.monitors.NItems--
193+
// a.monitors.NItems--
184194
break
185195
}
186196
}

app/data_history_export.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ func (a *App) ExportToCsv(addr string) {
1717
return
1818
}
1919

20-
historyMutex.Lock()
20+
historyMutex.RLock()
2121
_, exists := a.historyMap[address]
22-
historyMutex.Unlock()
22+
historyMutex.RUnlock()
2323

2424
if exists {
2525
fn := fmt.Sprintf("history_%s.csv", address)
@@ -39,7 +39,7 @@ func (a *App) ExportToCsv(addr string) {
3939
"Nonce",
4040
"Input",
4141
"TransactionType"))
42-
historyMutex.Lock()
42+
historyMutex.RLock()
4343
for _, item := range a.historyMap[address].Items {
4444
lines = append(lines, fmt.Sprintf("%d,%s,%d,%s,%s,%s,%d,%d,%d,%d,%d,%d,%s,%s",
4545
item.BlockNumber,
@@ -57,7 +57,7 @@ func (a *App) ExportToCsv(addr string) {
5757
item.Input,
5858
item.TransactionType))
5959
}
60-
historyMutex.Unlock()
60+
historyMutex.RUnlock()
6161
file.StringToAsciiFile(fn, strings.Join(lines, "\n")+"\n")
6262
utils.System(fmt.Sprintf("open %s", fn))
6363
}

app/proc_refresh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (a *App) Refresh(which ...string) {
6262
))
6363
}
6464
}
65-
a.loadPortfolio(nil, nil)
65+
// a.loadPortfolio(nil, nil)
6666

6767
// And then update everything else in the fullness of time
6868
wg := sync.WaitGroup{}

frontend/src/components/formatters/AppearanceFormatter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ export const AppearanceFormatter = ({ value, value2, className, size = "md" }: O
66
const [isPopupOpen, setPopupOpen] = useState(false);
77

88
const copyHash = useCallback(() => {
9-
ClipboardSetText(value2).then(() => {
9+
ClipboardSetText(String(value2)).then(() => {
1010
setPopupOpen(false);
1111
});
1212
}, [value2]);
1313

1414
const appPopup = (
1515
<AppearancePopup
16-
hash={value2}
16+
hash={String(value2)}
1717
onSubmit={() => setPopupOpen(false)}
1818
onClose={() => setPopupOpen(false)}
1919
onCopy={() => copyHash}

frontend/src/components/formatters/Formatter.tsx

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
EdMode,
1111
} from "@components";
1212
import { base } from "@gocode/models";
13-
import { useDateTime, useToEther, useToGas } from "@hooks";
13+
import { useAppState } from "@state";
1414
import { GetDebugColor } from ".";
1515

1616
export type knownType =
@@ -42,15 +42,19 @@ export type knownType =
4242
export type FormatterProps = {
4343
type: knownType;
4444
value: any;
45-
value2?: any;
45+
value2?: boolean | base.Hash | base.Address | string | undefined;
4646
className?: string;
4747
size?: TextProps["size"];
4848
};
4949

5050
export const Formatter = ({ type, value, value2, className, size = "md" }: FormatterProps) => {
51+
const { address } = useAppState();
52+
53+
const cn = GetDebugColor(type) || className;
5154
const n = value as number;
5255
const bi = value as bigint;
53-
const cn = GetDebugColor(type) || className;
56+
const bool = value2 as boolean;
57+
const from = value2 as unknown as base.Address;
5458

5559
switch (type) {
5660
case "boolean":
@@ -62,13 +66,13 @@ export const Formatter = ({ type, value, value2, className, size = "md" }: Forma
6266
case "tag":
6367
return <TagFormatter value={value} size={size} className={cn} />;
6468
case "gas":
65-
value = useToGas(bi, value2 as base.Address);
69+
value = from !== address ? "-" : formatEther(bi);
6670
break;
6771
case "ether":
68-
value = useToEther(bi);
72+
value = bool ? "" : formatEther(bi);
6973
break;
7074
case "timestamp":
71-
value = useDateTime(n);
75+
value = formatDateTime(n);
7276
break;
7377
case "date":
7478
value = value?.replace("T", " ");
@@ -95,7 +99,7 @@ export const Formatter = ({ type, value, value2, className, size = "md" }: Forma
9599
case "url":
96100
break;
97101
case "crud":
98-
return <CrudButton size="xs" value={value} isDeleted={value2} />;
102+
return <CrudButton size="xs" value={value} isDeleted={bool} />;
99103
case "address-editor":
100104
return (
101105
<AddressFormatter type={type} className={cn} size={size} value={value} value2={value2} mode={EdMode.All} />
@@ -138,3 +142,39 @@ const formatBytes = (bytes: number): string => {
138142
});
139143
return `${formattedValue} ${sizes[i]}`;
140144
};
145+
146+
const formatEther = (value: bigint | string) => {
147+
// from https://viem.sh/docs/utilities/formatUnits
148+
if (typeof value === "string" && value.includes(".")) {
149+
return value;
150+
}
151+
if (!value) return "-";
152+
153+
let display = value.toString();
154+
const negative = display.startsWith("-");
155+
if (negative) display = display.slice(1);
156+
display = display.padStart(18, "0");
157+
158+
const integer = display.slice(0, display.length - 18);
159+
let fraction = display.slice(display.length - 18);
160+
fraction = fraction.slice(0, 5).padEnd(5, "0");
161+
162+
const v = `${negative ? "-" : ""}${integer || "0"}.${fraction}`;
163+
// return display === "000000000000000000" ? "-" : v + " --- " + display;
164+
if (v === "0.00000") return "-";
165+
return v;
166+
};
167+
168+
const formatDateTime = (timestamp: number): string => {
169+
const date = new Date(timestamp * 1000); // Convert timestamp from seconds to milliseconds
170+
171+
const year = date.getFullYear();
172+
const month = String(date.getMonth() + 1).padStart(2, "0");
173+
const day = String(date.getDate()).padStart(2, "0");
174+
const hours = String(date.getHours()).padStart(2, "0");
175+
const minutes = String(date.getMinutes()).padStart(2, "0");
176+
const seconds = String(date.getSeconds()).padStart(2, "0");
177+
178+
const formatted = `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;
179+
return formatted;
180+
};

frontend/src/hooks/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
export * from "./useKeyboardPaging";
22
export * from "./useViewName";
3-
export * from "./useToEther";
4-
export * from "./useDateTime";
53
export * from "./useEnvironment";

frontend/src/hooks/useDateTime.tsx

Lines changed: 0 additions & 21 deletions
This file was deleted.

frontend/src/hooks/useToEther.tsx

Lines changed: 0 additions & 34 deletions
This file was deleted.

frontend/src/views/History/HistoryTable.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export const tableColumns: CustomColumnDef<types.Transaction, any>[] = [
3030
}),
3131
columnHelper.accessor("value", {
3232
header: () => "Ether",
33-
cell: (info) => <Formatter type="ether" value={info.renderValue()} />,
33+
cell: (info) => {
34+
const { value, isError } = info.row.original;
35+
return <Formatter type="ether" value={value} value2={isError} />;
36+
},
3437
meta: { className: "medium cell" },
3538
}),
3639
columnHelper.accessor("gasUsed", {

frontend/wailsjs/go/models.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,12 @@ export namespace types {
16931693

16941694
export namespace wizard {
16951695

1696+
export enum Step {
1697+
RESET = "Reset",
1698+
PREVIOUS = "Previous",
1699+
NEXT = "Next",
1700+
FINISH = "Finish",
1701+
}
16961702
export enum State {
16971703
NOTOKAY = "notOkay",
16981704
TOMLOKAY = "tomlOkay",
@@ -1701,12 +1707,6 @@ export namespace wizard {
17011707
INDEXOKAY = "indexOkay",
17021708
OKAY = "okay",
17031709
}
1704-
export enum Step {
1705-
RESET = "Reset",
1706-
PREVIOUS = "Previous",
1707-
NEXT = "Next",
1708-
FINISH = "Finish",
1709-
}
17101710
export class Wizard {
17111711
state: State;
17121712

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ replace (
88
)
99

1010
require (
11-
github.com/TrueBlocks/trueblocks-core/src/apps/chifra v0.0.0-20240901212707-3b0228642466
12-
github.com/TrueBlocks/trueblocks-sdk/v3 v3.3.1
11+
github.com/TrueBlocks/trueblocks-core/src/apps/chifra v0.0.0-20240906195947-7a9af20ce595
12+
github.com/TrueBlocks/trueblocks-sdk/v3 v3.3.2
1313
github.com/joho/godotenv v1.5.1
1414
github.com/wailsapp/wails/v2 v2.8.2
1515
)

0 commit comments

Comments
 (0)