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> <SFullWidthContainer>
<h6>Balances</h6> <h6>Balances</h6>
<Column center> <Column center>
{assets.map(asset => ( {assets.map(asset =>
<Asset key={asset.symbol} asset={asset} /> asset.symbol ? <Asset key={asset.symbol} asset={asset} /> : null,
))} )}
</Column> </Column>
</SFullWidthContainer> </SFullWidthContainer>
) : null} ) : null}

View File

@ -1,54 +1,147 @@
import axios, { AxiosInstance } from "axios"; 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", baseURL: "https://ethereum-api.xyz",
timeout: 30000, // 30 secs timeout: 10000, // 10 secs
headers: { headers: {
Accept: "application/json", Accept: "application/json",
"Content-Type": "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> { export async function apiGetAccountBalance(address: string, chainId: string): Promise<AssetData> {
const ethChainId = chainId.split(":")[1]; const ethChainId = chainId.split(":")[1];
const response = await ethereumApi.get( const rpc = rpcProvidersByChainId[Number(ethChainId)];
`/account-balance?address=${address}&chainId=${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; const { result } = response.data;
return result; const balance = parseInt(result, 16).toString();
} return { balance, ...token };
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;
} }
export const apiGetAccountNonce = async (address: string, chainId: string): Promise<number> => { export const apiGetAccountNonce = async (address: string, chainId: string): Promise<number> => {
const ethChainId = chainId.split(":")[1]; 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; const { result } = response.data;
return result; const nonce = parseInt(result, 16);
return nonce;
}; };
export const apiGetGasPrices = async (): Promise<GasPrices> => { export const apiGetGasPrice = async (chainId: string): Promise<string> => {
const response = await ethereumApi.get(`/gas-prices`); 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; const { result } = response.data;
return result; return result;
}; };

View File

@ -1,13 +1,6 @@
import * as encoding from "@walletconnect/encoding"; import * as encoding from "@walletconnect/encoding";
import { apiGetAccountNonce, apiGetGasPrices } from "./api"; import { apiGetAccountNonce, apiGetGasPrice } 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();
}
export async function formatTestTransaction(account: string) { export async function formatTestTransaction(account: string) {
const [namespace, reference, address] = account.split(":"); const [namespace, reference, address] = account.split(":");
@ -23,7 +16,7 @@ export async function formatTestTransaction(account: string) {
const nonce = encoding.sanitizeHex(encoding.numberToHex(_nonce)); const nonce = encoding.sanitizeHex(encoding.numberToHex(_nonce));
// gasPrice // gasPrice
const _gasPrice = await getGasPrice(chainId); const _gasPrice = await apiGetGasPrice(chainId);
const gasPrice = encoding.sanitizeHex(_gasPrice); const gasPrice = encoding.sanitizeHex(_gasPrice);
// gasLimit // gasLimit

View File

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