Skip to content

Instantly share code, notes, and snippets.

@ferdiunal
Created December 14, 2023 06:39
Show Gist options
  • Save ferdiunal/b28ab68c4b2a91b8902f721182f1c214 to your computer and use it in GitHub Desktop.
Save ferdiunal/b28ab68c4b2a91b8902f721182f1c214 to your computer and use it in GitHub Desktop.
Web3BrowserHelper
import { ethers } from "ethers";
import { RuntimeError } from "../errors";
interface NetworkConfig {
chainId: string;
rpcUrls: string[];
chainName: string;
nativeCurrency: {
name: string;
symbol: string;
decimals: number;
};
blockExplorerUrls: string[];
}
export class Browser {
constructor() {
if (typeof window === "undefined") {
throw new RuntimeError("Web3 is not available in this environment");
}
}
async addChain(params: NetworkConfig) {
try {
return await this.switchChain(params.chainId);
} catch (switchError: any) {
if (switchError.code === 4902) {
try {
await window.ethereum.request({
method: "wallet_addEthereumChain",
params: [params],
})
} catch (e) {
throw new RuntimeError("Failed to add chain");
}
}
throw new RuntimeError("Failed to add chain");
}
}
async switchChain(chainId: string | number) {
chainId = ethers.toQuantity(chainId);
return window.ethereum.request({
method: "wallet_switchEthereumChain",
params: [{ chainId }],
})
}
async requestPermissions() {
return window.ethereum.request({
method: "wallet_requestPermissions",
params: [{ eth_accounts: {} }],
});
}
async getPermissions() {
return window.ethereum.request({
method: "wallet_getPermissions",
});
}
personalSign(message: string, address: string) {
return window.ethereum.request({
method: "personal_sign",
params: [message, address],
});
}
requestAccounts() {
return window.ethereum.request({
method: "eth_requestAccounts",
});
}
accounts() {
return window.ethereum.request({
method: "eth_accounts",
});
}
messageSign(message: string, address: string) {
return window.ethereum.request({
method: "eth_sign",
params: [address, message],
});
}
async getNetwork() {
return window.ethereum.request({
method: "net_version",
});
}
async getBlockNumber() {
return window.ethereum.request({
method: "eth_blockNumber",
});
}
async getBalance(address: string) {
return window.ethereum.request({
method: "eth_getBalance",
params: [address, "latest"],
});
}
async getTransactionCount(address: string) {
return window.ethereum.request({
method: "eth_getTransactionCount",
params: [address, "latest"],
});
}
async getGasPrice() {
return window.ethereum.request({
method: "eth_gasPrice",
});
}
async estimateGas(transaction: any) {
return window.ethereum.request({
method: "eth_estimateGas",
params: [transaction],
});
}
async sendTransaction(transaction: any) {
return window.ethereum.request({
method: "eth_sendTransaction",
params: [transaction],
});
}
async call(transaction: any) {
return window.ethereum.request({
method: "eth_call",
params: [transaction, "latest"],
});
}
async getTransactionReceipt(hash: string) {
return window.ethereum.request({
method: "eth_getTransactionReceipt",
params: [hash],
});
}
async getLogs(filter: any) {
return window.ethereum.request({
method: "eth_getLogs",
params: [filter],
});
}
async getBlock(blockHashOrBlockNumber: string | number) {
return window.ethereum.request({
method: "eth_getBlockByNumber",
params: [blockHashOrBlockNumber, true],
});
}
async getBlockWithTransactions(blockHashOrBlockNumber: string | number) {
return window.ethereum.request({
method: "eth_getBlockByNumber",
params: [blockHashOrBlockNumber, true],
});
}
async getTransaction(hash: string) {
return window.ethereum.request({
method: "eth_getTransactionByHash",
params: [hash],
});
}
async getCode(address: string) {
return window.ethereum.request({
method: "eth_getCode",
params: [address, "latest"],
});
}
async getStorageAt(address: string, position: string) {
return window.ethereum.request({
method: "eth_getStorageAt",
params: [address, position, "latest"],
});
}
onAccountsChanged(callback: (accounts: string[]) => void) {
return window.ethereum.on("accountsChanged", callback);
}
onChainChanged(callback: (chainId: string) => void) {
return window.ethereum.on("chainChanged", callback);
}
onConnect(callback: (chainId: string) => void) {
return window.ethereum.on("connect", callback);
}
onDisconnect(callback: (chainId: string) => void) {
return window.ethereum.on("disconnect", callback);
}
onMessage(callback: (message: any) => void) {
return window.ethereum.on("message", callback);
}
}
declare global {
interface Window {
ethereum: ethers.BrowserProvider & ethers.Eip1193Provider & {
isConnected(): boolean;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment