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

chore(wallet): migrate swap to brave-core #18562

Merged
merged 22 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion components/brave_wallet_ui/common/slices/api.slice.extra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ import type {
import {
useGetAccountInfosRegistryQuery,
useGetNetworkQuery,
useGetSelectedAccountAddressQuery,
useGetTokensRegistryQuery,
useGetTransactionsQuery,
useGetUserTokensRegistryQuery,
} from './api.slice'

// entities
import {
accountInfoEntityAdaptorInitialState
} from './entities/account-info.entity'

// utils
import {
selectAllUserAssetsFromQueryResult,
Expand Down Expand Up @@ -50,6 +56,25 @@ export const useAccountQuery = (
)
}

export const useSelectedAccountQuery = () => {
const {
data: accountInfosRegistry = accountInfoEntityAdaptorInitialState,
isLoading: isLoadingAccounts
} = useGetAccountInfosRegistryQuery(undefined)

const { data: selectedAccountAddress, isLoading: isLoadingSelectedAddress } =
useGetSelectedAccountAddressQuery(isLoadingAccounts ? skipToken : undefined)

const selectedAccount = selectedAccountAddress
? findAccountFromRegistry(selectedAccountAddress, accountInfosRegistry)
: undefined

return {
isLoading: isLoadingAccounts || isLoadingSelectedAddress,
data: selectedAccount
}
}

export const useGetCombinedTokensListQuery = (
arg?: undefined,
opts?: { skip?: boolean }
Expand Down Expand Up @@ -80,7 +105,7 @@ export const useGetCombinedTokensListQuery = (
if (isLoadingUserTokens || isLoadingKnownTokens) {
return {
isLoading: true,
data: []
data: [] as BraveWallet.BlockchainToken[]
}
}
const combinedList = selectCombinedTokensList(knownTokens, userTokens)
Expand Down
25 changes: 22 additions & 3 deletions components/brave_wallet_ui/common/slices/api.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,9 @@ export function createWalletApi (
]
}),
setNetwork: mutation<
Pick<BraveWallet.NetworkInfo, 'chainId' | 'coin'>,
Pick<BraveWallet.NetworkInfo, 'chainId' | 'coin'> & {
selectedAccount?: AccountInfoEntity
},
Pick<BraveWallet.NetworkInfo, 'chainId' | 'coin'>
>({
queryFn: async (
Expand All @@ -733,7 +735,7 @@ export function createWalletApi (
baseQuery
) => {
try {
const { braveWalletService } = baseQuery(undefined).data
const { data: { braveWalletService }, cache } = baseQuery(undefined)

await dispatch(
walletApi.endpoints.setSelectedCoin.initiate(coin)
Expand All @@ -746,8 +748,25 @@ export function createWalletApi (
'braveWalletService.SetChainIdForActiveOrigin failed'
)
}

// FIXME(josheleonard): could be written in a more efficient way.
// Clients setting networks must be aware of the selected account
// corresponding to the new network.
const accountsRegistry = await cache.getAccountsRegistry()
cache.clearSelectedAccount()
const selectedAccountAddress =
await cache.getSelectedAccountAddress()

const selectedAccount = selectedAccountAddress
? accountsRegistry.entities[
accountInfoEntityAdaptor.selectId({
address: selectedAccountAddress
})
]
: undefined
onyb marked this conversation as resolved.
Show resolved Hide resolved

return {
data: { chainId, coin }
data: { chainId, coin, selectedAccount }
}
} catch (error) {
console.error(error)
Expand Down
27 changes: 24 additions & 3 deletions components/brave_wallet_ui/constants/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TimeDelta } from 'gen/mojo/public/mojom/base/time.mojom.m.js'
import * as BraveWallet from 'gen/brave/components/brave_wallet/common/brave_wallet.mojom.m.js'
import { HardwareWalletResponseCodeType } from '../common/hardware/types'
import { NftsPinningStatusType } from '../page/constants/action_types'
import { AccountInfoEntity } from '../common/slices/entities/account-info.entity'

// Re-export BraveWallet for use in other modules, to avoid hard-coding the
// path of generated mojom files.
Expand Down Expand Up @@ -421,7 +422,8 @@ export interface PortfolioTokenHistoryAndInfo {

interface BaseTransactionParams {
network: BraveWallet.NetworkInfo
fromAccount: WalletAccountType
// FIXME(josheleonard): Should be just AccountInfoEntity
fromAccount: WalletAccountType | AccountInfoEntity
to: string
value: string
coin: BraveWallet.CoinType
Expand Down Expand Up @@ -847,7 +849,7 @@ export interface NFTAtrribute {
traitType: string
value: string,
traitRarity?: string
}
}

export interface NFTMetadataReturnType {
metadataUrl?: string
Expand Down Expand Up @@ -1078,6 +1080,25 @@ const BitcoinKeyringTypes = [
export type BitcoinKeyring = typeof BitcoinKeyringTypes[number]

const BitcoinNetworkTypes = [
BraveWallet.BITCOIN_MAINNET, BraveWallet.BITCOIN_TESTNET
BraveWallet.BITCOIN_MAINNET, BraveWallet.BITCOIN_TESTNET
] as const
export type BitcoinNetwork = typeof BitcoinNetworkTypes[number]


export type GasFeeOption = {
id: string
name: string
icon: string
}

export type GasEstimate = {
gasFee: string
gasFeeGwei?: string
gasFeeFiat?: string
time?: string
}

export type SwapAndSend = {
label: string
name: string
}
25 changes: 25 additions & 0 deletions components/brave_wallet_ui/options/gas-fee-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

// Types
import { GasFeeOption } from '../constants/types'

export const gasFeeOptions: GasFeeOption[] = [
{
id: 'slow',
name: 'braveSwapSlow',
icon: 'send'
},
{
id: 'average',
name: 'braveSwapAverage',
icon: 'network-speed-average'
},
{
id: 'fast',
name: 'braveSwapFast',
icon: 'rocket'
}
]
18 changes: 18 additions & 0 deletions components/brave_wallet_ui/options/swap-and-send-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

// Types
import { SwapAndSend } from '../constants/types'

export const SwapAndSendOptions: SwapAndSend[] = [
{
label: 'braveSwapToAccount',
name: 'to-account'
},
{
label: 'braveSwapToAddress',
name: 'to-address'
}
]