Skip to content

Commit

Permalink
refactor: walletd upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Nov 28, 2023
1 parent e02f29f commit f1da1d9
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 201 deletions.
14 changes: 11 additions & 3 deletions apps/walletd/contexts/events/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ValueCopyable,
LoadingDots,
ValueScFiat,
ValueSf,
} from '@siafoundation/design-system'
import { humanDate } from '@siafoundation/sia-js'
import { EventData, TableColumnId } from './types'
Expand Down Expand Up @@ -106,11 +107,18 @@ export const columns: EventsTableColumn[] = [
label: 'amount',
category: 'general',
contentClassName: 'w-[120px] justify-end',
render: ({ data: { amount } }) => {
if (!amount) {
render: ({ data: { amountSc, amountSf } }) => {
if (!amountSc) {
return null
}
return <ValueScFiat displayBoth size="12" value={amount} />
return (
<div className="flex flex-col gap-2">
{!amountSc.isZero() && (
<ValueScFiat displayBoth size="12" value={amountSc} />
)}
{!!amountSf && <ValueSf size="12" value={amountSf} />}
</div>
)
},
},
{
Expand Down
57 changes: 32 additions & 25 deletions apps/walletd/contexts/events/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
useServerFilters,
} from '@siafoundation/design-system'
import {
WalletEventMinerPayout,
WalletEventTransaction,
useWalletEvents,
useWalletSubscribe,
useWalletTxPool,
Expand Down Expand Up @@ -81,19 +83,36 @@ export function useEventsMain() {
amount: new BigNumber(e.received).minus(e.sent),
}))
const dataEvents: EventData[] = responseEvents.data.map((e, index) => {
let amount = new BigNumber(0)
if (e.type === 'siacoin transfer') {
const inputsTotal =
e.val?.inputs?.reduce(
(acc, o) => acc.plus(o.value),
let amountSc = new BigNumber(0)
let amountSf = 0
if (e.type === 'transaction') {
const inputsScTotal =
e.val?.siacoinInputs?.reduce(
(acc, o) => acc.plus(o.siacoinOutput.value),
new BigNumber(0)
) || new BigNumber(0)
const outputsTotal =
e.val?.outputs?.reduce(
(acc, o) => acc.plus(o.value),
const outputsScTotal =
e.val?.siacoinOutputs?.reduce(
(acc, o) => acc.plus(o.siacoinOutput.value),
new BigNumber(0)
) || new BigNumber(0)
amount = outputsTotal.minus(inputsTotal)
amountSc = outputsScTotal.minus(inputsScTotal)

const inputsSfTotal =
e.val?.siafundInputs?.reduce(
(acc, o) => acc + o.siafundElement.siafundOutput.value,
0
) || 0
const outputsSfTotal =
e.val?.siafundOutputs?.reduce(
(acc, o) => acc + o.siafundOutput.value,
0
) || 0
amountSf = outputsSfTotal - inputsSfTotal
}

if (e.type === 'miner payout') {
amountSc = new BigNumber(e.val.siacoinOutput.siacoinOutput.value)
}

const id = String(index)
Expand All @@ -103,31 +122,19 @@ export function useEventsMain() {
timestamp: new Date(e.timestamp).getTime(),
height: e.index.height,
pending: false,
amount,
}
if ('maturityHeight' in e.val) {
res.maturityHeight = e.val.maturityHeight
amountSc,
amountSf,
}
if ('fee' in e.val) {
res.fee = new BigNumber(e.val.fee)
}
if ('contractID' in e.val) {
res.contractId = e.val.contractID
if ('fileContract' in e.val) {
res.contractId = e.val.fileContract.id
}
if ('transactionID' in e.val) {
res.id += e.val.transactionID
res.transactionId = e.val.transactionID
}
if ('outputID' in e.val) {
res.id += e.val.outputID
res.outputId = e.val.outputID
}
if ('netAddress' in e.val) {
res.netAddress = e.val.netAddress
}
if ('publicKey' in e.val) {
res.publicKey = e.val.publicKey
}
return res
})
return [...dataTxPool.reverse(), ...dataEvents]
Expand Down
11 changes: 2 additions & 9 deletions apps/walletd/contexts/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,25 @@ export type EventData = {
id: string
timestamp: number
height?: number
maturityHeight?: number
pending: boolean
type: string
fee?: BigNumber
amount?: BigNumber
amountSc?: BigNumber
amountSf?: number
transactionId?: string
contractId?: string
outputId?: string
netAddress?: string
publicKey?: string
}

export type TableColumnId =
// | 'actions'
// | 'id'
| 'type'
| 'height'
| 'maturityHeight'
| 'timestamp'
| 'amount'
| 'fee'
| 'transactionId'
| 'contractId'
| 'outputId'
| 'netAddress'
| 'publicKey'

export const columnsDefaultVisible: TableColumnId[] = [
// 'actions',
Expand Down
54 changes: 36 additions & 18 deletions apps/walletd/dialogs/WalletAddressesGenerateLedgerDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,6 @@ export function WalletAddressesGenerateLedgerDialog({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [nextIndex])

const { handleOpenChange, closeAndReset } = useDialogFormHelpers({
form,
onOpenChange,
defaultValues,
})

const formIndex = form.watch('index')
const formCount = form.watch('count')

Expand Down Expand Up @@ -230,7 +224,15 @@ export function WalletAddressesGenerateLedgerDialog({
return indiciesWithAddresses
}, [existingAddresses, indices])

const newAddresses = useMemo(
const newIncompleteAddresses = useMemo(
() =>
Object.entries(indiciesWithAddresses)
.filter(([index, item]) => item.isNew && !item.address)
.map(([index, item]) => item),
[indiciesWithAddresses]
)

const newGeneratedAddresses = useMemo(
() =>
Object.entries(indiciesWithAddresses)
.filter(([index, item]) => item.isNew && item.address)
Expand All @@ -239,8 +241,11 @@ export function WalletAddressesGenerateLedgerDialog({
)

const saveAddresses = useCallback(async () => {
const count = newAddresses.length
for (const [i, { address, publicKey, index }] of newAddresses.entries()) {
const count = newGeneratedAddresses.length
for (const [
i,
{ address, publicKey, index },
] of newGeneratedAddresses.entries()) {
const response = await addressAdd.put({
params: {
id: walletId,
Expand Down Expand Up @@ -270,19 +275,27 @@ export function WalletAddressesGenerateLedgerDialog({
triggerSuccessToast(`Successfully added ${count} addresses.`)
}
return
}, [addressAdd, walletId, newAddresses])
}, [addressAdd, walletId, newGeneratedAddresses])

const { handleOpenChange, closeAndReset } = useDialogFormHelpers({
form,
onOpenChange: (open: boolean) => {
setIndicies({})
onOpenChange(open)
},
defaultValues,
})

const onSubmit = useCallback(async () => {
if (newAddresses.length === 0) {
if (newIncompleteAddresses.length === 0) {
triggerErrorToast(
'Add and generate addresses with your Ledger device to continue.'
)
return
}
await saveAddresses()
setIndicies({})
closeAndReset()
}, [newAddresses, saveAddresses, closeAndReset])
}, [newIncompleteAddresses, saveAddresses, closeAndReset])

return (
<Dialog
Expand All @@ -295,11 +308,16 @@ export function WalletAddressesGenerateLedgerDialog({
}}
onSubmit={form.handleSubmit(onSubmit)}
controls={
<div className="flex justify-end">
<FormSubmitButton form={form} variant="accent" size="medium">
Save {newAddresses.length}{' '}
{newAddresses.length === 1 ? 'address' : 'addresses'}
</FormSubmitButton>
<div className="flex gap-1 justify-end">
<Button size="medium" variant="gray" onClick={closeAndReset}>
Close
</Button>
{newGeneratedAddresses.length > 0 && (
<FormSubmitButton form={form} variant="accent" size="medium">
Save {newGeneratedAddresses.length}{' '}
{newGeneratedAddresses.length === 1 ? 'address' : 'addresses'}
</FormSubmitButton>
)}
</div>
}
>
Expand Down
12 changes: 1 addition & 11 deletions libs/react-walletd/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
SiafundOutputID,
Transaction,
WalletEvent,
GatewayPeer,
} from './siaTypes'

// consensus
Expand Down Expand Up @@ -78,17 +79,6 @@ export function useEstimatedNetworkBlockHeight(): number {

// syncer

type GatewayPeer = {
addr: string
inbound: boolean
version: string

firstSeen: string
connectedSince: string
syncedBlocks: number
syncDuration: number
}

export const syncerPeersKey = '/syncer/peers'

export function useSyncerPeers(args?: HookArgsSwr<void, GatewayPeer[]>) {
Expand Down
Loading

0 comments on commit f1da1d9

Please sign in to comment.