Skip to content

Commit

Permalink
Store only id, timestamp and txDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Jul 7, 2023
1 parent a49cfdb commit ff992d1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 29 deletions.
28 changes: 17 additions & 11 deletions src/components/common/BatchSidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { SyntheticEvent } from 'react'
import type { TransactionDetails } from '@safe-global/safe-gateway-typescript-sdk'
import { type MetaTransactionData, OperationType } from '@safe-global/safe-core-sdk-types'
import { useCallback, useContext } from 'react'
import { Button, Divider, Drawer, SvgIcon, Typography } from '@mui/material'
import { useDraftBatch, useUpdateBatch } from '@/hooks/useDraftBatch'
Expand All @@ -9,6 +11,15 @@ import BatchTxItem from './BatchTxItem'
import ConfirmBatchFlow from '@/components/tx-flow/flows/ConfirmBatch'
import PlusIcon from '@/public/images/common/plus.svg'

const getData = (txDetails: TransactionDetails): MetaTransactionData => {
return {
to: txDetails.txData?.to.value ?? '',
value: txDetails.txData?.value ?? '0',
data: txDetails.txData?.hexData ?? '0x',
operation: OperationType.Call, // only calls can be batched
}
}

const BatchSidebar = ({ isOpen, onToggle }: { isOpen: boolean; onToggle: (open: boolean) => void }) => {
const { setTxFlow } = useContext(TxModalContext)
const batchTxs = useDraftBatch()
Expand All @@ -29,7 +40,7 @@ const BatchSidebar = ({ isOpen, onToggle }: { isOpen: boolean; onToggle: (open:
e.preventDefault()
if (!batchTxs.length) return
closeSidebar()
setTxFlow(<ConfirmBatchFlow calls={batchTxs.map((item) => item.txData)} />)
setTxFlow(<ConfirmBatchFlow calls={batchTxs.map((item) => getData(item.txDetails))} />)
},
[setTxFlow, batchTxs, closeSidebar],
)
Expand All @@ -48,16 +59,11 @@ const BatchSidebar = ({ isOpen, onToggle }: { isOpen: boolean; onToggle: (open:
<Divider />

<div className={css.txs}>
{batchTxs.length
? batchTxs.map((item, index) => (
<BatchTxItem
key={index}
count={index + 1}
{...item}
onDelete={() => onDeleteClick(item.txDetails.txId)}
/>
))
: 'No transactions added yet'}
{!batchTxs.length && 'No transactions added yet'}

{batchTxs.map((item, index) => (
<BatchTxItem key={index} count={index + 1} {...item} onDelete={() => onDeleteClick(item.txDetails.txId)} />
))}
</div>

<Button onClick={onAddClick}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const SingleTxDecoded = ({
<CodeIcon color="border" fontSize="small" />
<Typography>{actionTitle}</Typography>
<Typography ml="8px">
<b>{method}</b>
<b>{method || 'native transfer'}</b>
</Typography>
</div>
</AccordionSummary>
Expand Down
3 changes: 1 addition & 2 deletions src/components/tx/SignOrExecuteForm/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ export const useTxActions = (): TxActions => {
const addToBatch: TxActions['addToBatch'] = async (safeTx, origin) => {
assertTx(safeTx)
assertWallet(wallet)
assertOnboard(onboard)

const id = await proposeTx(wallet.address, safeTx, undefined, origin)
addTxToBatch(safeTx, id)
addTxToBatch(id)
return id
}

Expand Down
13 changes: 1 addition & 12 deletions src/hooks/useDraftBatch.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
import { useCallback } from 'react'
import type { MetaTransactionData, SafeTransaction } from '@safe-global/safe-core-sdk-types'
import { useAppDispatch, useAppSelector } from '@/store'
import useChainId from './useChainId'
import useSafeAddress from './useSafeAddress'
import type { DraftBatchItem } from '@/store/batchSlice'
import { selectBatchBySafe, addTx, removeTx } from '@/store/batchSlice'
import { getTransactionDetails } from '@safe-global/safe-gateway-typescript-sdk'

const getMetaData = (safeTx: SafeTransaction): MetaTransactionData => {
return {
to: safeTx.data.to,
value: safeTx.data.value,
data: safeTx.data.data,
operation: safeTx.data.operation,
}
}

export const useUpdateBatch = () => {
const chainId = useChainId()
const safeAddress = useSafeAddress()
const dispatch = useAppDispatch()

const onAdd = useCallback(
(safeTx: SafeTransaction, id: string) => {
(id: string) => {
getTransactionDetails(chainId, id).then((tx) => {
dispatch(
addTx({
chainId,
safeAddress,
timestamp: Date.now(),
txData: getMetaData(safeTx),
txDetails: tx,
}),
)
Expand Down
3 changes: 0 additions & 3 deletions src/store/batchSlice.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { createSelector, createSlice, type PayloadAction } from '@reduxjs/toolkit'
import type { MetaTransactionData } from '@safe-global/safe-core-sdk-types'
import type { TransactionDetails } from '@safe-global/safe-gateway-typescript-sdk'
import type { RootState } from '.'

export type DraftBatchItem = {
timestamp: number
txData: MetaTransactionData
txDetails: TransactionDetails
}

Expand All @@ -28,7 +26,6 @@ export const batchSlice = createSlice({
chainId: string
safeAddress: string
timestamp: number
txData: MetaTransactionData
txDetails: TransactionDetails
}>,
) => {
Expand Down

0 comments on commit ff992d1

Please sign in to comment.