Skip to content

Commit

Permalink
feat(wallet): Multichain Support in Portfolio UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglashdaniel committed Mar 16, 2022
1 parent 2114923 commit 88c5939
Show file tree
Hide file tree
Showing 55 changed files with 823 additions and 376 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ mojom::BlockchainTokenPtr ParseTokenInfo(const std::string& json) {

return mojom::BlockchainToken::New(eth_addr.ToChecksumAddress(), *name,
"" /* logo */, is_erc20, is_erc721,
*symbol, decimals, true, "", "");
*symbol, decimals, true, "", "", "");
}

} // namespace brave_wallet
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ TEST_F(AssetRatioServiceUnitTest, GetTokenInfo) {
GetTokenInfo("0xdac17f958d2ee523a2206206994597c13d831ec7",
mojom::BlockchainToken::New(
"0xdAC17F958D2ee523a2206206994597C13D831ec7", "Tether USD",
"", true, false, "USDT", 6, true, "", ""));
"", true, false, "USDT", 6, true, "", "", ""));

SetInterceptor("unexpected response");
GetTokenInfo("0xdac17f958d2ee523a2206206994597c13d831ec7", nullptr);
Expand Down
38 changes: 19 additions & 19 deletions components/brave_wallet/browser/brave_wallet_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,42 @@ constexpr int32_t kAutoLockMinutesMax = 10080;
// List of assets from Wyre, available to buy
static base::NoDestructor<std::vector<mojom::BlockchainToken>> kBuyTokens(
{{"0x0D8775F648430679A709E98d2b0Cb6250d2887EF", "Basic Attention Token",
"bat.png", true, false, "BAT", 18, true, "", ""},
{"", "Ethereum", "", false, false, "ETH", 18, true, "", ""},
"bat.png", true, false, "BAT", 18, true, "", "", ""},
{"", "Ethereum", "", false, false, "ETH", 18, true, "", "", ""},
{"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "USD Coin", "usdc.png",
true, false, "USDC", 6, true, "", ""},
true, false, "USDC", 6, true, "", "", ""},
{"0x6B175474E89094C44Da98b954EedeAC495271d0F", "DAI", "dai.png", true,
false, "DAI", 18, true, "", ""},
false, "DAI", 18, true, "", "", ""},
{"0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9", "AAVE", "AAVE.png", true,
false, "AAVE", 18, true, "", ""},
false, "AAVE", 18, true, "", "", ""},
{"0x4Fabb145d64652a948d72533023f6E7A623C7C53", "Binance USD", "busd.png",
true, false, "BUSD", 18, true, "", ""},
true, false, "BUSD", 18, true, "", "", ""},
{"0xc00e94Cb662C3520282E6f5717214004A7f26888", "Compound", "comp.png",
true, false, "Comp", 18, true, "", ""},
true, false, "Comp", 18, true, "", "", ""},
{"0xD533a949740bb3306d119CC777fa900bA034cd52", "Curve", "curve.png", true,
false, "CRV", 18, true, "", ""},
false, "CRV", 18, true, "", "", ""},
{"0x056Fd409E1d7A124BD7017459dFEa2F387b6d5Cd", "Gemini Dollar", "gusd.png",
true, false, "GUSD", 2, true, "", ""},
true, false, "GUSD", 2, true, "", "", ""},
{"0x514910771AF9Ca656af840dff83E8264EcF986CA", "Chainlink",
"chainlink.png", true, false, "LINK", 18, true, "", ""},
"chainlink.png", true, false, "LINK", 18, true, "", "", ""},
{"0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2", "Maker", "mkr.png", true,
false, "MKR", 18, true, "", ""},
false, "MKR", 18, true, "", "", ""},
{"0xC011a73ee8576Fb46F5E1c5751cA3B9Fe0af2a6F", "Synthetix",
"synthetix.png", true, false, "SNX", 18, true, "", ""},
"synthetix.png", true, false, "SNX", 18, true, "", "", ""},
{"0x04Fa0d235C4abf4BcF4787aF4CF447DE572eF828", "UMA", "UMA.png", true,
false, "UMA", 18, true, "", ""},
false, "UMA", 18, true, "", "", ""},
{"0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984", "Uniswap", "uni.png", true,
false, "UNI", 18, true, "", ""},
false, "UNI", 18, true, "", "", ""},
{"0xA4Bdb11dc0a2bEC88d24A3aa1E6Bb17201112eBe", "Stably Dollar", "usds.png",
true, false, "USDS", 6, true, "", ""},
true, false, "USDS", 6, true, "", "", ""},
{"0xdAC17F958D2ee523a2206206994597C13D831ec7", "Tether", "usdt.png", true,
false, "USDT", 6, true, "", ""},
false, "USDT", 6, true, "", "", ""},
{"0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599", "Wrapped Bitcoin",
"wbtc.png", true, false, "WBTC", 8, true, "", ""},
"wbtc.png", true, false, "WBTC", 8, true, "", "", ""},
{"0x0bc529c00C6401aEF6D220BE8C6Ea1667F6Ad93e", "Year.Finance", "yfi.png",
true, false, "YFI", 18, true, "", ""},
true, false, "YFI", 18, true, "", "", ""},
{"0x9043d4d51C9d2e31e3F169de4551E416970c27Ef", "Palm DAI", "pdai.png",
true, false, "PDAI", 18, true, "", ""}});
true, false, "PDAI", 18, true, "", "", ""}});

const char kWalletBaseDirectory[] = "BraveWallet";
const char kImageSourceHost[] = "erc-token-images";
Expand Down
4 changes: 3 additions & 1 deletion components/brave_wallet/browser/brave_wallet_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,10 @@ void BraveWalletService::GetUserAssets(const std::string& chain_id,
std::vector<mojom::BlockchainTokenPtr> result;
for (const auto& token : tokens->GetList()) {
mojom::BlockchainTokenPtr tokenPtr = ValueToBlockchainToken(token);
if (tokenPtr)
if (tokenPtr) {
tokenPtr->chain_id = chain_id;
result.push_back(std::move(tokenPtr));
}
}

std::move(callback).Run(std::move(result));
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/common/brave_wallet.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ struct BlockchainToken {
bool visible = true;
string token_id = ""; // Non-empty for ERC721 tokens.
string coingecko_id = "";
string chain_id;
};

// WebUI-side handler for requests from the browser.
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/common/eth_request_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ bool ParseWalletWatchAssetParams(const std::string& json,

*token = mojom::BlockchainToken::New(eth_addr.ToChecksumAddress(),
*symbol /* name */, logo, true, false,
*symbol, decimals, true, "", "");
*symbol, decimals, true, "", "", "");
return true;
}

Expand Down
27 changes: 6 additions & 21 deletions components/brave_wallet_ui/assets/asset-icons/sol-asset-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions components/brave_wallet_ui/common/actions/wallet_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ import {
import {
BraveWallet,
WalletAccountType,
GetAllNetworksList,
GetAllTokensReturnInfo,
GetNativeAssetBalancesReturnInfo,
GetNativeAssetBalancesPayload,
GetBlockchainTokenBalanceReturnInfo,
PortfolioTokenHistoryAndInfo,
SendTransactionParams,
Expand Down Expand Up @@ -60,7 +58,7 @@ export const selectAccount = createAction<WalletAccountType>('selectAccount')
export const selectNetwork = createAction<BraveWallet.NetworkInfo>('selectNetwork')
export const setNetwork = createAction<BraveWallet.NetworkInfo>('setNetwork')
export const getAllNetworks = createAction('getAllNetworks')
export const setAllNetworks = createAction<GetAllNetworksList>('getAllNetworks')
export const setAllNetworks = createAction<BraveWallet.NetworkInfo[]>('getAllNetworks')
export const chainChangedEvent = createAction<ChainChangedEventPayloadType>('chainChangedEvent')
export const isEip1559Changed = createAction<IsEip1559Changed>('isEip1559Changed')
export const keyringCreated = createAction('keyringCreated')
Expand All @@ -71,9 +69,9 @@ export const unlocked = createAction('unlocked')
export const backedUp = createAction('backedUp')
export const accountsChanged = createAction('accountsChanged')
export const selectedAccountChanged = createAction('selectedAccountChanged')
export const setAllTokensList = createAction<GetAllTokensReturnInfo>('setAllTokensList')
export const setAllTokensList = createAction<BraveWallet.BlockchainToken[]>('setAllTokensList')
export const getAllTokensList = createAction('getAllTokensList')
export const nativeAssetBalancesUpdated = createAction<GetNativeAssetBalancesReturnInfo>('nativeAssetBalancesUpdated')
export const nativeAssetBalancesUpdated = createAction<GetNativeAssetBalancesPayload>('nativeAssetBalancesUpdated')
export const tokenBalancesUpdated = createAction<GetBlockchainTokenBalanceReturnInfo>('tokenBalancesUpdated')
export const pricesUpdated = createAction<GetPriceReturnInfo>('tokenBalancesUpdated')
export const portfolioPriceHistoryUpdated = createAction<PortfolioTokenHistoryAndInfo[][]>('portfolioPriceHistoryUpdated')
Expand Down Expand Up @@ -115,3 +113,5 @@ export const speedupTransaction = createAction<BraveWallet.TransactionInfo>('spe
export const defaultCurrenciesUpdated = createAction<DefaultCurrencies>('defaultCurrenciesUpdated')
export const expandWalletNetworks = createAction('expandWalletNetworks')
export const refreshBalancesAndPriceHistory = createAction('refreshBalancesAndPriceHistory')
export const setSelectedCoin = createAction<BraveWallet.CoinType>('setSelectedCoin')
export const setDefaultNetworks = createAction<BraveWallet.NetworkInfo[]>('setDefaultNetworks')
64 changes: 47 additions & 17 deletions components/brave_wallet_ui/common/async/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
SwapErrorResponse,
WalletAccountType,
WalletState,
WalletInfo
WalletInfo,
SupportedCoinTypes
} from '../../constants/types'

// Utils
Expand Down Expand Up @@ -119,10 +120,6 @@ handler.on(WalletActions.initialize.getType(), async (store) => {
await refreshWalletInfo(store)
})

handler.on(WalletActions.chainChangedEvent.getType(), async (store: Store, payload: ChainChangedEventPayloadType) => {
await refreshWalletInfo(store)
})

handler.on(WalletActions.keyringCreated.getType(), async (store) => {
await refreshWalletInfo(store)
})
Expand Down Expand Up @@ -153,9 +150,12 @@ handler.on(WalletActions.accountsChanged.getType(), async (store) => {
await updateAccountInfo(store)
})

handler.on(WalletActions.selectedAccountChanged.getType(), async (store) => {
await refreshWalletInfo(store)
})
// Will use this observer selectedAccountChanged action again once
// selectedCoin is implemented here https://github.com/brave/brave-browser/issues/21465

// handler.on(WalletActions.selectedAccountChanged.getType(), async (store) => {
// await refreshWalletInfo(store)
// })

handler.on(WalletActions.defaultWalletChanged.getType(), async (store) => {
await refreshWalletInfo(store)
Expand Down Expand Up @@ -192,17 +192,31 @@ handler.on(WalletActions.removeFavoriteApp.getType(), async (store: Store, appIt
await refreshWalletInfo(store)
})

handler.on(WalletActions.chainChangedEvent.getType(), async (store: Store, payload: ChainChangedEventPayloadType) => {
const keyringService = getAPIProxy().keyringService
const state = getWalletState(store)
const { accounts } = state
const selectedAccountAddress = await keyringService.getSelectedAccount(payload.coin)
const selectedAccount = accounts.find((account) => account.address === selectedAccountAddress.address) ?? accounts[0]
store.dispatch(WalletActions.setSelectedAccount(selectedAccount))
store.dispatch(WalletActions.setSelectedCoin(payload.coin))
})

handler.on(WalletActions.selectNetwork.getType(), async (store: Store, payload: BraveWallet.NetworkInfo) => {
const jsonRpcService = getAPIProxy().jsonRpcService
await jsonRpcService.setNetwork(payload.chainId, BraveWallet.CoinType.ETH)
await refreshWalletInfo(store)
await jsonRpcService.setNetwork(payload.chainId, payload.coin)
store.dispatch(WalletActions.setNetwork(payload))
})

handler.on(WalletActions.selectAccount.getType(), async (store: Store, payload: WalletAccountType) => {
const { keyringService } = getAPIProxy()

const state = getWalletState(store)
const { defaultNetworks } = state
const defaultCoinTypesNetwork = defaultNetworks.find((network) => network.coin === payload.coin) ?? defaultNetworks[0]
await keyringService.setSelectedAccount(payload.address, payload.coin)
store.dispatch(WalletActions.setNetwork(defaultCoinTypesNetwork))
store.dispatch(WalletActions.setSelectedAccount(payload))
store.dispatch(WalletActions.setSelectedCoin(payload.coin))
await store.dispatch(refreshTransactionHistory(payload.address))
})

Expand Down Expand Up @@ -243,15 +257,31 @@ handler.on(WalletActions.initialized.getType(), async (store: Store, payload: Wa

handler.on(WalletActions.getAllNetworks.getType(), async (store) => {
const jsonRpcService = getAPIProxy().jsonRpcService
const fullList = await jsonRpcService.getAllNetworks(BraveWallet.CoinType.ETH)
store.dispatch(WalletActions.setAllNetworks(fullList))

const getFullNetworkList = await Promise.all(SupportedCoinTypes.map(async (coin: BraveWallet.CoinType) => {
const networkList = await jsonRpcService.getAllNetworks(coin)
return networkList.networks
}))
const networkList = getFullNetworkList.flat(1)
store.dispatch(WalletActions.setAllNetworks(networkList))
})

handler.on(WalletActions.getAllTokensList.getType(), async (store) => {
const { blockchainRegistry, jsonRpcService } = getAPIProxy()
const { chainId } = await jsonRpcService.getChainId(BraveWallet.CoinType.ETH)
const fullList = await blockchainRegistry.getAllTokens(chainId)
store.dispatch(WalletActions.setAllTokensList(fullList))
const state = getWalletState(store)
const { networkList } = state
const { blockchainRegistry } = getAPIProxy()
const getAllTokensList = await Promise.all(networkList.map(async (network) => {
const list = await blockchainRegistry.getAllTokens(network.chainId)
return list.tokens.map((token) => {
return {
...token,
chainId: network.chainId,
logo: `chrome://erc-token-images/${token.logo}`
}
})
}))
const allTokensList = getAllTokensList.flat(1)
store.dispatch(WalletActions.setAllTokensList(allTokensList))
})

handler.on(WalletActions.addUserAsset.getType(), async (store: Store, payload: AddUserAssetPayloadType) => {
Expand Down
Loading

0 comments on commit 88c5939

Please sign in to comment.