refactor(dapp): remove dependence on ethereum-api.xyz, use individual RPCs

This commit is contained in:
Ben Kremer 2022-08-04 11:29:38 +02:00
parent b59c1faa43
commit 6c23356a55
4 changed files with 131 additions and 46 deletions

View File

@ -152,9 +152,9 @@ const Blockchain: FC<PropsWithChildren<BlockchainProps>> = (
<SFullWidthContainer>
<h6>Balances</h6>
<Column center>
{assets.map(asset => (
<Asset key={asset.symbol} asset={asset} />
))}
{assets.map(asset =>
asset.symbol ? <Asset key={asset.symbol} asset={asset} /> : null,
)}
</Column>
</SFullWidthContainer>
) : null}

View File

@ -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<number, any> = {
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<AssetData[]> {
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<AssetData> {
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<ParsedTx[]> {
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<number> => {
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<GasPrices> => {
const response = await ethereumApi.get(`/gas-prices`);
export const apiGetGasPrice = async (chainId: string): Promise<string> => {
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;
};

View File

@ -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<string> {
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

View File

@ -3,8 +3,7 @@ import { ChainsMap } from "caip-api";
export interface AssetData {
symbol: string;
name: string;
decimals: string;
contractAddress: string;
contractAddress?: string;
balance?: string;
}