Skip to content

Commit

Permalink
Replace getEthAddressForAccount with fallbackEthAddress helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaw3d committed Oct 17, 2024
1 parent 245634d commit 5bbd54f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 46 deletions.
33 changes: 0 additions & 33 deletions src/app/utils/__tests__/getEthAccountAddress.test.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/app/utils/__tests__/getEthAccountAddressFromPreimage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getEthAccountAddressFromPreimage } from '../helpers'
import { suggestedEmptyAccount, suggestedParsedAccount } from '../test-fixtures'

describe('getEthAccountAddressFromPreimage', () => {
it('should convert preimage to evm addresses', () => {
expect(getEthAccountAddressFromPreimage(suggestedParsedAccount.address_preimage)).toEqual(
suggestedParsedAccount.address_eth,
)
})

it('should return undefined on empty account', () => {
expect(getEthAccountAddressFromPreimage(suggestedEmptyAccount.address_preimage)).toBeUndefined()
})
})
12 changes: 1 addition & 11 deletions src/app/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Buffer } from 'buffer'
import * as oasis from '@oasisprotocol/client'
import * as oasisRT from '@oasisprotocol/client-rt'
// eslint-disable-next-line no-restricted-imports
import { AddressPreimage, RuntimeAccount } from '../../oasis-nexus/generated/api'
import { AddressPreimage } from '../../oasis-nexus/generated/api'
import { validateMnemonic } from 'bip39'
import { sha512_256 } from 'js-sha512'

Expand Down Expand Up @@ -94,16 +94,6 @@ export function getEthAccountAddressFromPreimage(preimage: AddressPreimage | und
return getEthAccountAddressFromBase64(preimage.address_data)
}

export function getEthAddressForAccount(
account: RuntimeAccount,
possibleEthAddress?: string,
): string | undefined {
// In case of an empty account
if (possibleEthAddress && isValidEthAddress(possibleEthAddress)) return possibleEthAddress

return getEthAccountAddressFromPreimage(account.address_preimage)
}

export function uniq<T>(input: T[] | undefined): T[] {
return input === undefined ? [] : [...new Set(input)]
}
Expand Down
6 changes: 4 additions & 2 deletions src/oasis-nexus/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from './generated/api'
import {
getAccountSize,
getEthAddressForAccount,
getEthAccountAddressFromPreimage,
getOasisAddressOrNull,
isValidEthAddress,
} from '../app/utils/helpers'
Expand Down Expand Up @@ -448,7 +448,9 @@ export const useGetRuntimeAccountsAddress: typeof generated.useGetRuntimeAccount
if (status !== 200) return data
return groupAccountTokenBalances({
...data,
address_eth: getEthAddressForAccount(data, address),
address_eth:
getEthAccountAddressFromPreimage(data.address_preimage) ||
fallbackEthAddress(data.address, address),
evm_contract: data.evm_contract && {
...data.evm_contract,
eth_creation_tx: data.evm_contract.eth_creation_tx
Expand Down

0 comments on commit 5bbd54f

Please sign in to comment.