Skip to content

Commit

Permalink
fix: detect native transfers (#3402)
Browse files Browse the repository at this point in the history
- fix: detect native transfers
- add unit tests for decoded tx
  • Loading branch information
schmanu authored Mar 6, 2024
1 parent 4c090ed commit 2f9073c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { render } from '@/tests/test-utils'
import SingleTxDecoded from '.'
import { Operation } from '@safe-global/safe-gateway-typescript-sdk'
import { faker } from '@faker-js/faker'
import { parseUnits } from 'ethers'
import { ERC20__factory } from '@/types/contracts'

describe('SingleTxDecoded', () => {
it('should show native transfers', () => {
const receiver = faker.finance.ethereumAddress()
const result = render(
<SingleTxDecoded
actionTitle="0"
showDelegateCallWarning
tx={{
data: '0x',
operation: Operation.CALL,
to: receiver,
value: parseUnits('1').toString(),
}}
txData={{
to: { value: receiver },
operation: Operation.CALL,
trustedDelegateCallTarget: false,
addressInfoIndex: {},
value: parseUnits('1').toString(),
}}
/>,
)

expect(result.queryByText('native transfer')).not.toBeNull()
})

it('should show unknown contract interactions', () => {
const unknownToken = faker.finance.ethereumAddress()
const spender = faker.finance.ethereumAddress()
const result = render(
<SingleTxDecoded
actionTitle="0"
showDelegateCallWarning
tx={{
data: ERC20__factory.createInterface().encodeFunctionData('approve', [spender, '100000']),
operation: Operation.CALL,
to: unknownToken,
}}
txData={{
to: { value: unknownToken },
operation: Operation.CALL,
trustedDelegateCallTarget: false,
addressInfoIndex: {},
}}
/>,
)

expect(result.queryByText('Unknown contract interaction')).not.toBeNull()
})

it('should show decoded data ', () => {
const unknownToken = faker.finance.ethereumAddress()
const spender = faker.finance.ethereumAddress()
const result = render(
<SingleTxDecoded
actionTitle="0"
showDelegateCallWarning
tx={{
data: ERC20__factory.createInterface().encodeFunctionData('approve', [spender, '100000']),
operation: Operation.CALL,
to: unknownToken,
dataDecoded: {
method: 'approve',
parameters: [
{
name: 'spender',
type: 'address',
value: spender,
},
{
name: 'value',
type: 'uint256',
value: '100000',
},
],
},
}}
txData={{
to: { value: unknownToken },
operation: Operation.CALL,
trustedDelegateCallTarget: false,
addressInfoIndex: {},
dataDecoded: {
method: 'approve',
parameters: [
{
name: 'spender',
type: 'address',
value: spender,
},
{
name: 'value',
type: 'uint256',
value: '100000',
},
],
},
}}
/>,
)

expect(result.queryAllByText('approve')).not.toHaveLength(0)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const SingleTxDecoded = ({
onChange,
}: SingleTxDecodedProps) => {
const chain = useCurrentChain()
const isNativeTransfer = tx.value !== '0' && tx.data && isEmptyHexData(tx.data)
const isNativeTransfer = tx.value !== '0' && (!tx.data || isEmptyHexData(tx.data))
const method = tx.dataDecoded?.method || (isNativeTransfer ? 'native transfer' : 'Unknown contract interaction')
const { decimals, symbol } = chain?.nativeCurrency || {}
const amount = tx.value ? formatVisualAmount(tx.value, decimals) : 0
Expand Down

0 comments on commit 2f9073c

Please sign in to comment.