Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: send screen performance refactor #20219

Merged
merged 23 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1f047d0
chore: relocate & rename send screen components
josheleonard Sep 14, 2023
e97aacc
chore: move send screen hooks into send component
josheleonard Sep 14, 2023
6ccd1aa
perf: remove useAssets hook
josheleonard Sep 14, 2023
4d9fb77
pref: remove unneeded component nesting
josheleonard Sep 14, 2023
032c37b
feat: add useAccountFromAddressQuery
josheleonard Sep 14, 2023
7951722
chore: simplify send page routing hash logic
josheleonard Sep 14, 2023
8adc263
chore: refactor off-chain ENS warnings UI
josheleonard Sep 14, 2023
3df3504
feat: add address endpoints
josheleonard Sep 16, 2023
146d42a
feat: add useModal hook
josheleonard Sep 19, 2023
c1a775b
fix: consolidate name service lookup queries
josheleonard Sep 19, 2023
daad241
fix: remove useSend hook & speedup send screen
josheleonard Sep 19, 2023
fc2e6e3
chore: add cache tags for name service lookups
josheleonard Sep 19, 2023
eaf9b48
chore: reuse common computed variables
josheleonard Sep 19, 2023
823b328
chore: add mocks and storybook
josheleonard Sep 21, 2023
6b989e7
fix: use resolved name service address for sending
josheleonard Sep 21, 2023
b07c26b
chore: pre-submit
josheleonard Sep 21, 2023
247f8e1
perf: pre-fetch token balances on send screen
josheleonard Sep 21, 2023
34ad005
perf: speedup finding asset from router params
josheleonard Sep 21, 2023
147746f
pref: speedup address validation
josheleonard Sep 21, 2023
c588c1f
fix: do not lowercase resolved NameService address
josheleonard Sep 21, 2023
6e7265e
fix: remove token prefetching
josheleonard Sep 22, 2023
6060540
chore: remove unneeded memo & fix broken import
josheleonard Oct 10, 2023
fb20316
fix: log correct error message
josheleonard Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 152 additions & 26 deletions components/brave_wallet_ui/common/async/__mocks__/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,29 @@ import type WalletApiProxy from '../../wallet_api_proxy'
// utils
import { getCoinFromTxDataUnion } from '../../../utils/network-utils'
import { deserializeTransaction } from '../../../utils/model-serialization-utils'
import { getAssetIdKey } from '../../../utils/asset-utils'

// mocks
import { mockWalletState } from '../../../stories/mock-data/mock-wallet-state'
import { mockedMnemonic } from '../../../stories/mock-data/user-accounts'
import {
NativeAssetBalanceRegistry,
TokenBalanceRegistry,
mockAccount,
mockErc721Token,
mockEthAccountInfo,
mockFilecoinAccountInfo,
mockFilecoinMainnetNetwork,
mockOnRampCurrencies,
mockSolanaAccountInfo,
mockSolanaMainnetNetwork,
mockSplNft
} from '../../constants/mocks'
import { mockEthMainnet, mockNetworks } from '../../../stories/mock-data/mock-networks'
import {
mockAccountAssetOptions,
mockBasicAttentionToken,
mockErc20TokensList,
mockErc721Token,
mockSplNft,
} from '../../../stories/mock-data/mock-asset-options'
import {
mockFilSendTransaction,
Expand Down Expand Up @@ -74,23 +77,6 @@ export const makeMockedStoreWithSpy = () => {
return { store }
}

type NativeAssetBalanceRegistry = Record<
string, // account address
| Record<
string, // chainId
string // balance
>
| undefined
>

type TokenBalanceRegistry = Record<
string, // account address
Record<
string, // asset identifier
string // balance
>
>

export interface WalletApiDataOverrides {
selectedCoin?: BraveWallet.CoinType
selectedAccountId?: BraveWallet.AccountId
Expand Down Expand Up @@ -235,6 +221,9 @@ export class MockedWalletApiProxy {
deserializeTransaction(mockedErc20ApprovalTransaction)
]

// name service lookups
requireOffchainConsent: number = BraveWallet.ResolveMethod.kAsk

constructor(overrides?: WalletApiDataOverrides | undefined) {
this.applyOverrides(overrides)
}
Expand Down Expand Up @@ -359,7 +348,48 @@ export class MockedWalletApiProxy {
},
getNetworkForSelectedAccountOnActiveOrigin: async () => {
return { network: this.selectedNetwork }
}
},
isBase58EncodedSolanaPubkey: async (key) => {
return {
result: true
}
},
getBalanceScannerSupportedChains: async () => {
return {
chainIds: this.networks.map((n) => n.chainId)
}
},
ensureSelectedAccountForChain: async (coin, chainId) => {
const foundAccount = findAccountByUniqueKey(
this.accountInfos,
this.selectedAccountId.uniqueKey,
)

return {
accountId:
foundAccount?.accountId.coin === coin
? foundAccount.accountId
: this.accountInfos.find((a) => a.accountId.coin === coin)
?.accountId ?? null
}
},
setNetworkForSelectedAccountOnActiveOrigin: async (chainId) => {
if (this.selectedNetwork.chainId === chainId) {
return {
success: true
}
}

const net = this.networks.find(n => n.chainId === chainId)

if (net) {
this.selectedNetwork = net
}

return {
success: !!net
}
},
}

swapService: Partial<InstanceType<typeof BraveWallet.SwapServiceInterface>> =
Expand Down Expand Up @@ -434,7 +464,27 @@ export class MockedWalletApiProxy {
return password === 'password'
? { mnemonic: mockedMnemonic }
: { mnemonic: '' }
}
},
getChecksumEthAddress: async (address) => {
return {
checksumAddress: address.toLocaleLowerCase()
}
},
setSelectedAccount: async (accountId) => {
const validId = !!this.accountInfos.find(
(a) => a.accountId.uniqueKey === accountId.uniqueKey
)

if (validId) {
this.selectedAccountId = accountId
} else {
console.log('invalid id: ' + accountId.uniqueKey)
}

return {
success: validId
}
},
}

ethTxManagerProxy: Partial<
Expand Down Expand Up @@ -523,16 +573,18 @@ export class MockedWalletApiProxy {
}
},
getSolanaBalance: async (pubkey: string, chainId: string) => {
const balance = BigInt(this.nativeBalanceRegistry[pubkey]?.[chainId] ?? 0)
return {
balance: BigInt(this.nativeBalanceRegistry[pubkey]?.[chainId] || 0),
balance,
error: 0,
errorMessage: ''
}
},
// Token balances
getERC20TokenBalance: async (contract, address, chainId) => {
return {
balance:
this.nativeBalanceRegistry[address]?.[
this.tokenBalanceRegistry[address]?.[
blockchainTokenEntityAdaptor.selectId({
coin: BraveWallet.CoinType.ETH,
chainId,
Expand All @@ -554,7 +606,7 @@ export class MockedWalletApiProxy {
) => {
return {
balance:
this.nativeBalanceRegistry[accountAddress]?.[
this.tokenBalanceRegistry[accountAddress]?.[
blockchainTokenEntityAdaptor.selectId({
coin: BraveWallet.CoinType.ETH,
chainId,
Expand All @@ -576,7 +628,7 @@ export class MockedWalletApiProxy {
) => {
return {
balance:
this.nativeBalanceRegistry[accountAddress]?.[
this.tokenBalanceRegistry[accountAddress]?.[
blockchainTokenEntityAdaptor.selectId({
coin: BraveWallet.CoinType.ETH,
chainId,
Expand All @@ -597,7 +649,7 @@ export class MockedWalletApiProxy {
) => {
return {
amount:
this.nativeBalanceRegistry[walletAddress]?.[
this.tokenBalanceRegistry[walletAddress]?.[
blockchainTokenEntityAdaptor.selectId({
coin: BraveWallet.CoinType.ETH,
chainId,
Expand All @@ -613,6 +665,53 @@ export class MockedWalletApiProxy {
errorMessage: ''
}
},
getSPLTokenBalances: async (pubkey, chainId) => {
const balances = Object.keys(this.tokenBalanceRegistry?.[pubkey])
.filter((tokenId) => tokenId.includes(chainId))
.map((tokenIdentifier) => {
const token = this.blockchainTokens.find(
(t) => getAssetIdKey(t) === tokenIdentifier
)

const amount =
this.tokenBalanceRegistry[pubkey][tokenIdentifier] || '0'

return {
amount: this.tokenBalanceRegistry[pubkey][tokenIdentifier] || '0',
decimals: token?.decimals ?? 1,
mint: token?.contractAddress ?? '',
uiAmount: amount
}
})
return {
balances,
error: 0,
errorMessage: '',
}
},
getERC20TokenBalances: async (contracts, address, chainId) => {
const balances = Object.keys(this.tokenBalanceRegistry?.[address])
.filter((tokenId) => tokenId.includes(chainId))
.map((tokenIdentifier) => {
const token = this.blockchainTokens.find(
(t) => getAssetIdKey(t) === tokenIdentifier
)

const amount =
this.tokenBalanceRegistry[address][tokenIdentifier] || '0'

return {
balance: amount,
contractAddress: token?.contractAddress || ''
}
})
return {
balances,
error: 0,
errorMessage: ''
}
},
// NFT Metadata
getERC721Metadata: async (contract, tokenId, chainId) => {
const mockedMetadata =
mockNFTMetadata.find(
Expand Down Expand Up @@ -662,6 +761,33 @@ export class MockedWalletApiProxy {
name: mockedMetadata.contractInformation.name
} as CommonNftMetadata)
}
},
// name service lookups
setEnsOffchainLookupResolveMethod(method) {
this.requireOffchainConsent = method
},
ensGetEthAddr: async (domain) => {
return {
address: `0x1234abcd1234${domain}`,
error: 0,
errorMessage: '',
requireOffchainConsent:
this.requireOffchainConsent !== BraveWallet.ResolveMethod.kEnabled
}
},
snsGetSolAddr: async (domain) => {
return {
address: `s1abcd1234567890${domain}`,
error: 0,
errorMessage: ''
}
},
unstoppableDomainsGetWalletAddr: async (domain, token) => {
return {
address: `0x${token?.chainId}abcd${domain}`,
error: 0,
errorMessage: ''
}
}
}

Expand Down
Loading