diff --git a/dapps/react-dapp-v2/src/components/Blockchain.tsx b/dapps/react-dapp-v2/src/components/Blockchain.tsx index dd6869a..0852402 100644 --- a/dapps/react-dapp-v2/src/components/Blockchain.tsx +++ b/dapps/react-dapp-v2/src/components/Blockchain.tsx @@ -152,9 +152,9 @@ const Blockchain: FC> = (
Balances
- {assets.map(asset => ( - - ))} + {assets.map(asset => + asset.symbol ? : null, + )}
) : null} diff --git a/dapps/react-dapp-v2/src/helpers/api.ts b/dapps/react-dapp-v2/src/helpers/api.ts index d6ca62e..ad61229 100644 --- a/dapps/react-dapp-v2/src/helpers/api.ts +++ b/dapps/react-dapp-v2/src/helpers/api.ts @@ -1,54 +1,147 @@ import axios, { AxiosInstance } from "axios"; -import { AssetData, GasPrices, ParsedTx } from "./types"; +import { AssetData } from "./types"; -const ethereumApi: AxiosInstance = axios.create({ +const rpcProvidersByChainId: Record = { + 1: { + name: "Ethereum Mainnet", + baseURL: "https://mainnet.infura.io/v3/5dc0df7abe4645dfb06a9a8c39ede422", + token: { + name: "Ether", + symbol: "ETH", + }, + }, + 42: { + name: "Ethereum Kovan", + baseURL: "https://kovan.infura.io/v3/5dc0df7abe4645dfb06a9a8c39ede422", + token: { + name: "Ether", + symbol: "ETH", + }, + }, + 137: { + name: "Polygon Mainnet", + baseURL: "https://polygon-rpc.com", + token: { + name: "Matic", + symbol: "MATIC", + }, + }, + 80001: { + name: "Polygon Mumbai", + baseURL: "https://rpc-mumbai.maticvigil.com", + token: { + name: "Matic", + symbol: "MATIC", + }, + }, + 10: { + name: "Optimism", + baseURL: "https://mainnet.optimism.io", + token: { + name: "Ether", + symbol: "ETH", + }, + }, + 69: { + name: "Optimism Kovan", + baseURL: "https://kovan.optimism.io", + token: { + name: "Ether", + symbol: "ETH", + }, + }, + 42161: { + name: "Arbitrum", + baseURL: "https://arb1.arbitrum.io/rpc", + token: { + name: "Ether", + symbol: "ETH", + }, + }, + 421611: { + name: "Arbitrum Rinkeby", + baseURL: "https://rinkeby.arbitrum.io/rpc", + token: { + name: "Ether", + symbol: "ETH", + }, + }, + 100: { + name: "xDAI", + baseURL: "https://xdai-archive.blockscout.com", + token: { + name: "xDAI", + symbol: "xDAI", + }, + }, + 42220: { + name: "Celo", + baseURL: "https://forno.celo.org", + token: { + name: "CELO", + symbol: "CELO", + }, + }, + 44787: { + name: "Celo", + baseURL: "https://alfajores-forno.celo-testnet.org", + token: { + name: "CELO", + symbol: "CELO", + }, + }, +}; + +const api: AxiosInstance = axios.create({ baseURL: "https://ethereum-api.xyz", - timeout: 30000, // 30 secs + timeout: 10000, // 10 secs headers: { Accept: "application/json", "Content-Type": "application/json", }, }); -export async function apiGetAccountAssets(address: string, chainId: string): Promise { - const ethChainId = chainId.split(":")[1]; - const response = await ethereumApi.get( - `/account-assets?address=${address}&chainId=${ethChainId}`, - ); - const { result } = response.data; - return result; -} - export async function apiGetAccountBalance(address: string, chainId: string): Promise { const ethChainId = chainId.split(":")[1]; - const response = await ethereumApi.get( - `/account-balance?address=${address}&chainId=${ethChainId}`, - ); + const rpc = rpcProvidersByChainId[Number(ethChainId)]; + if (!rpc) { + return { balance: "", symbol: "", name: "" }; + } + const { baseURL, token } = rpc; + const response = await api.post(baseURL, { + jsonrpc: "2.0", + method: "eth_getBalance", + params: [address, "latest"], + id: 1, + }); const { result } = response.data; - return result; -} - -export async function apiGetAccountTransactions( - address: string, - chainId: string, -): Promise { - const ethChainId = chainId.split(":")[1]; - const response = await ethereumApi.get( - `/account-transactions?address=${address}&chainId=${ethChainId}`, - ); - const { result } = response.data; - return result; + const balance = parseInt(result, 16).toString(); + return { balance, ...token }; } export const apiGetAccountNonce = async (address: string, chainId: string): Promise => { const ethChainId = chainId.split(":")[1]; - const response = await ethereumApi.get(`/account-nonce?address=${address}&chainId=${ethChainId}`); + const { baseURL } = rpcProvidersByChainId[Number(ethChainId)]; + const response = await api.post(baseURL, { + jsonrpc: "2.0", + method: "eth_getTransactionCount", + params: [address, "latest"], + id: 1, + }); const { result } = response.data; - return result; + const nonce = parseInt(result, 16); + return nonce; }; -export const apiGetGasPrices = async (): Promise => { - const response = await ethereumApi.get(`/gas-prices`); +export const apiGetGasPrice = async (chainId: string): Promise => { + const ethChainId = chainId.split(":")[1]; + const { baseURL } = rpcProvidersByChainId[Number(ethChainId)]; + const response = await api.post(baseURL, { + jsonrpc: "2.0", + method: "eth_gasPrice", + params: [], + id: 1, + }); const { result } = response.data; return result; }; diff --git a/dapps/react-dapp-v2/src/helpers/tx.ts b/dapps/react-dapp-v2/src/helpers/tx.ts index 4f5fc5f..4abc0f0 100644 --- a/dapps/react-dapp-v2/src/helpers/tx.ts +++ b/dapps/react-dapp-v2/src/helpers/tx.ts @@ -1,13 +1,6 @@ import * as encoding from "@walletconnect/encoding"; -import { apiGetAccountNonce, apiGetGasPrices } from "./api"; -import { toWad } from "./utilities"; - -export async function getGasPrice(chainId: string): Promise { - if (chainId === "eip155:1") return toWad("20", 9).toHexString(); - const gasPrices = await apiGetGasPrices(); - return toWad(`${gasPrices.slow.price}`, 9).toHexString(); -} +import { apiGetAccountNonce, apiGetGasPrice } from "./api"; export async function formatTestTransaction(account: string) { const [namespace, reference, address] = account.split(":"); @@ -23,7 +16,7 @@ export async function formatTestTransaction(account: string) { const nonce = encoding.sanitizeHex(encoding.numberToHex(_nonce)); // gasPrice - const _gasPrice = await getGasPrice(chainId); + const _gasPrice = await apiGetGasPrice(chainId); const gasPrice = encoding.sanitizeHex(_gasPrice); // gasLimit diff --git a/dapps/react-dapp-v2/src/helpers/types.ts b/dapps/react-dapp-v2/src/helpers/types.ts index 1f2bcf9..c1b6b6d 100644 --- a/dapps/react-dapp-v2/src/helpers/types.ts +++ b/dapps/react-dapp-v2/src/helpers/types.ts @@ -3,8 +3,7 @@ import { ChainsMap } from "caip-api"; export interface AssetData { symbol: string; name: string; - decimals: string; - contractAddress: string; + contractAddress?: string; balance?: string; }