Skip to content

Commit

Permalink
Merge pull request #1564 from oasisprotocol/mz/ethAddress
Browse files Browse the repository at this point in the history
Keep eth address to fetch runtimes data
  • Loading branch information
buberdds authored Oct 17, 2024
2 parents c8f64a0 + 5a3a25d commit 449338c
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 177 deletions.
1 change: 1 addition & 0 deletions .changelog/1564.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Retain ETH address in details page for accounts with no EVM transactions
6 changes: 3 additions & 3 deletions playwright/tests/accounts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function setup(page: Page) {
},
)
await page.route(
'**/v1/sapphire/transactions?limit=10&offset=0&rel=oasis1qq2v39p9fqk997vk6742axrzqyu9v2ncyuqt8uek',
'**/v1/sapphire/transactions?limit=10&offset=0&rel=0x0000000000000000000000000000000000000000',
route => {
route.fulfill({
body: JSON.stringify({
Expand All @@ -26,7 +26,7 @@ async function setup(page: Page) {
})
},
)
await page.route('**/v1/sapphire/accounts/oasis1qq2v39p9fqk997vk6742axrzqyu9v2ncyuqt8uek', route => {
await page.route('**/v1/sapphire/accounts/0x0000000000000000000000000000000000000000', route => {
route.fulfill({
body: JSON.stringify({
address: 'oasis1qq2v39p9fqk997vk6742axrzqyu9v2ncyuqt8uek',
Expand All @@ -47,7 +47,7 @@ async function setup(page: Page) {
})
})
await page.route(
'**/v1/sapphire/events?limit=10&offset=0&rel=oasis1qq2v39p9fqk997vk6742axrzqyu9v2ncyuqt8uek',
'**/v1/sapphire/events?limit=10&offset=0&rel=0x0000000000000000000000000000000000000000',
route => {
route.fulfill({
body: JSON.stringify({
Expand Down
2 changes: 1 addition & 1 deletion playwright/tests/getPreciseNumberFormat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function setup(page: Page, balance: string, decimals: number) {
},
)

await page.route('**/v1/sapphire/accounts/oasis1qq2v39p9fqk997vk6742axrzqyu9v2ncyuqt8uek', route => {
await page.route('**/v1/sapphire/accounts/0x0000000000000000000000000000000000000000', route => {
route.fulfill({
body: JSON.stringify({
address: 'oasis1qq2v39p9fqk997vk6742axrzqyu9v2ncyuqt8uek',
Expand Down
31 changes: 7 additions & 24 deletions src/app/pages/RuntimeAccountDetailsPage/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { AppErrors } from '../../../types/errors'
import { useSearchParamsPagination } from '../../components/Table/useSearchParamsPagination'
import { NUMBER_OF_ITEMS_ON_SEPARATE_PAGE as limit } from '../../config'
import { SearchScope } from '../../../types/searchScope'
import { getOasisAddressOrNull } from '../../utils/helpers'

export const useAccount = (scope: SearchScope, address: string) => {
const { network, layer } = scope
Expand All @@ -34,19 +33,13 @@ export const useAccountTransactions = (scope: SearchScope, address: string) => {
// We should use useGetConsensusTransactions()
}

const oasisAddress = getOasisAddressOrNull(address)
const query = useGetRuntimeTransactions(
network,
layer, // This is OK since consensus has been handled separately
{
limit,
offset: offset,
rel: oasisAddress!,
},
{
query: {
enabled: !!oasisAddress,
},
rel: address,
},
)
const { isFetched, isLoading, data } = query
Expand Down Expand Up @@ -79,22 +72,12 @@ export const useAccountEvents = (scope: SearchScope, address: string) => {
// We should use useGetConsensusEvents()
}

const oasisAddress = getOasisAddressOrNull(address)
const query = useGetRuntimeEvents(
network,
layer,
{
limit,
offset: offset,
rel: oasisAddress!,
// TODO: implement filtering for non-transactional events
},
{
query: {
enabled: !!oasisAddress,
},
},
)
const query = useGetRuntimeEvents(network, layer, {
limit,
offset: offset,
rel: address,
// TODO: implement filtering for non-transactional events
})
const { isFetched, isLoading, isError, data } = query
const events = data?.data.events

Expand Down
79 changes: 17 additions & 62 deletions src/app/pages/TokenDashboardPage/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import { SearchScope } from '../../../types/searchScope'
import { useSearchParamsPagination } from '../../components/Table/useSearchParamsPagination'
import { NUMBER_OF_ITEMS_ON_SEPARATE_PAGE } from '../../config'
import { useComprehensiveSearchParamsPagination } from '../../components/Table/useComprehensiveSearchParamsPagination'
import { getOasisAddressOrNull } from '../../utils/helpers'

export const useTokenInfo = (scope: SearchScope, address: string, enabled = true) => {
const { network, layer } = scope
if (layer === Layer.consensus) {
// There can be no ERC-20 or ERC-721 tokens on consensus
throw AppErrors.UnsupportedLayer
}
const query = useGetRuntimeEvmTokensAddress(network, layer, address!, {
const query = useGetRuntimeEvmTokensAddress(network, layer, address, {
query: { enabled },
})
const token = query.data?.data
Expand All @@ -38,19 +37,14 @@ export const useTokenInfo = (scope: SearchScope, address: string, enabled = true
}

export const useTokenTransfers = (scope: SearchScope, params: { address: string }) => {
const oasisAddress = getOasisAddressOrNull(params.address)
return _useTokenTransfers(scope, oasisAddress ? { rel: oasisAddress } : undefined)
return _useTokenTransfers(scope, { rel: params.address })
}

export const useNFTInstanceTransfers = (
scope: SearchScope,
params: { nft_id: string; contract_address: string },
) => {
const oasisAddress = getOasisAddressOrNull(params.contract_address)
return _useTokenTransfers(
scope,
oasisAddress ? { nft_id: params.nft_id, contract_address: oasisAddress } : undefined,
)
return _useTokenTransfers(scope, { nft_id: params.nft_id, contract_address: params.contract_address })
}

export const _useTokenTransfers = (scope: SearchScope, params: undefined | GetRuntimeEventsParams) => {
Expand Down Expand Up @@ -109,21 +103,10 @@ export const useTokenHolders = (scope: SearchScope, address: string) => {
// There are no token holders on the consensus layer.
}

const oasisAddress = getOasisAddressOrNull(address)
const query = useGetRuntimeEvmTokensAddressHolders(
network,
layer,
oasisAddress!,
{
limit: NUMBER_OF_ITEMS_ON_SEPARATE_PAGE,
offset: offset,
},
{
query: {
enabled: !!oasisAddress,
},
},
)
const query = useGetRuntimeEvmTokensAddressHolders(network, layer, address, {
limit: NUMBER_OF_ITEMS_ON_SEPARATE_PAGE,
offset: offset,
})

const { isFetched, isLoading, data } = query

Expand Down Expand Up @@ -157,21 +140,10 @@ export const useTokenInventory = (scope: SearchScope, address: string) => {
throw AppErrors.UnsupportedLayer
// There are no tokens on the consensus layer.
}
const oasisAddress = getOasisAddressOrNull(address)
const query = useGetRuntimeEvmTokensAddressNfts(
network,
layer,
oasisAddress!,
{
limit: NUMBER_OF_INVENTORY_ITEMS,
offset: offset,
},
{
query: {
enabled: !!oasisAddress,
},
},
)
const query = useGetRuntimeEvmTokensAddressNfts(network, layer, address, {
limit: NUMBER_OF_INVENTORY_ITEMS,
offset: offset,
})
const { isFetched, isLoading, data } = query
const inventory = data?.data.evm_nfts

Expand Down Expand Up @@ -204,23 +176,11 @@ export const useAccountTokenInventory = (scope: SearchScope, address: string, to
// There are no tokens on the consensus layer.
}

const oasisAddress = getOasisAddressOrNull(address)
const oasisTokenAddress = getOasisAddressOrNull(tokenAddress)
const query = useGetRuntimeAccountsAddressNfts(
network,
layer,
oasisAddress!,
{
limit: NUMBER_OF_INVENTORY_ITEMS,
offset: offset,
token_address: oasisTokenAddress!,
},
{
query: {
enabled: !!oasisAddress && !!oasisTokenAddress,
},
},
)
const query = useGetRuntimeAccountsAddressNfts(network, layer, address, {
limit: NUMBER_OF_INVENTORY_ITEMS,
offset: offset,
token_address: tokenAddress,
})
const { isFetched, isLoading, data } = query
const inventory = data?.data.evm_nfts

Expand Down Expand Up @@ -250,12 +210,7 @@ export const useNFTInstance = (scope: SearchScope, address: string, id: string)
throw AppErrors.UnsupportedLayer
// There are no tokens on the consensus layer.
}
const oasisAddress = getOasisAddressOrNull(address)
const query = useGetRuntimeEvmTokensAddressNftsId(network, layer, oasisAddress!, id, {
query: {
enabled: !!oasisAddress,
},
})
const query = useGetRuntimeEvmTokensAddressNftsId(network, layer, address, id)

const { data, isError, isFetched, isLoading } = query
if (isError) {
Expand Down
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()
})
})
13 changes: 1 addition & 12 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,17 +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 (account.stats.num_txns <= 0 && 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
Loading

0 comments on commit 449338c

Please sign in to comment.