Skip to content

Commit

Permalink
chore(wallet): migrate swap to brave-core (#18562)
Browse files Browse the repository at this point in the history
* chore(wallet): Migrate Swap V2 to Brave Core

* chore(wallet): Swap V2 integration fixes

* chore(wallet): Swap v2 android fixes

* chore(wallet): fix presubmit errors

* fix-icons

* fix-more-presubmit-errors

* add-reduce-network-name-util

* fix-noopener-error

* fix-button-color

* chore(wallet): add useSelectedAccountQuery slice(extra)

* fix(wallet): fix spot prices and cleanup types

* fix-lp-wrapping-and-additional-icons

* review(thypon): fix console.log statements

* review(thypon): convert fake SVGs to PNG files

* chore: fix presubmit error

* fix vulnerable regex

* fix vulnerable regex v2

* more presubmit fixes

* review(josheleonard): review fixes (part 1)

* presubmit fixes

* review(josheleonard): review fixes (part 2)

* fixed build for android

Signed-off-by: Vadym Struts <vstruts@brave.com>

---------

Signed-off-by: Vadym Struts <vstruts@brave.com>
Co-authored-by: Douglas Daniel <douglashdaniel@gmail.com>
Co-authored-by: Vadym Struts <vstruts@brave.com>
  • Loading branch information
3 people committed May 25, 2023
1 parent b844cff commit d293cd9
Show file tree
Hide file tree
Showing 149 changed files with 8,208 additions and 707 deletions.
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

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'
}
]

0 comments on commit d293cd9

Please sign in to comment.