diff --git a/package.json b/package.json index a4ff438..60746c8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@guardianui/test", - "version": "1.0.1", + "version": "1.0.2", "description": "

\"guardianui

", "main": "./dist/index.js", "exports": { diff --git a/src/models/GUI.ts b/src/models/GUI.ts index 2f1a013..67e6c1d 100644 --- a/src/models/GUI.ts +++ b/src/models/GUI.ts @@ -26,6 +26,11 @@ export class GUI { this.page = page; } + /** + * Gets the GuardianUI RPC cache URL for a specific chain ID if it exists + * @param chainId - Chain ID to get the RPC cache URL for + * @returns RPC cache URL if it exists, undefined otherwise + */ getCacheUrl(chainId: number): string | undefined { const chainIdEnvVar = this.rpcCacheEnvVars[chainId as keyof typeof this.rpcCacheEnvVars]; @@ -34,6 +39,83 @@ export class GUI { } } + /** + * Gets the address of the currently injected wallet + * @returns The address of the currently injected wallet + */ + async getWalletAddress(): Promise { + return await this.page.evaluate("window.ethereum.signer.address"); + } + + /** + * Gets the ETH balance of a specific address + * @param address - The address to get the ETH balance of + * @returns The ETH balance of the address + */ + async getEthBalance(address: string): Promise { + // Pull provider URL from the page + const providerUrl: string = await this.page.evaluate("window.ethereum.provider.connection.url"); + + // Pull chain ID from the page + const chainId: string = await this.page.evaluate("window.ethereum.chainId"); + + // Create provider + const provider = new ethers.providers.JsonRpcProvider(providerUrl, parseInt(chainId)); + + // Get the ETH balance of the address + const balance = await provider.getBalance(address); + return balance.toString(); + } + + /** + * Gets the balance of a specific token for a specific address + * @param token - The token to get the balance of + * @param address - The address to get the balance of + * @returns The balance of the token for the address + */ + async getBalance(token: string, address: string): Promise { + // Pull provider URL from the page + const providerUrl: string = await this.page.evaluate("window.ethereum.provider.connection.url"); + + // Pull chain ID from the page + const chainId: string = await this.page.evaluate("window.ethereum.chainId"); + + // Create provider + const provider = new ethers.providers.JsonRpcProvider(providerUrl, parseInt(chainId)); + + // Create ERC20 contract object + const erc20Contract = new ethers.Contract(token, erc20TokenAbi, provider); + + // Get token balance + const balance = await erc20Contract.balanceOf(address); + return balance.toString(); + } + + /** + * Gets the allowance of a specific token for specific owner and spender addresses + * @param token - The token to get the allowance of + * @param ownerAddress - The address of the owner of the tokens to get the allowance for + * @param spenderAddress - The address of the spender of the tokens to get the allowance for + * @returns The allowance of the token for the owner and spender addresses + */ + async getAllowance(token: string, ownerAddress: string, spenderAddress: string): Promise { + // Pull provider URL from the page + const providerUrl: string = await this.page.evaluate("window.ethereum.provider.connection.url"); + + // Pull chain ID from the page + const chainId: string = await this.page.evaluate("window.ethereum.chainId"); + + // Create provider + const provider = new ethers.providers.JsonRpcProvider(providerUrl, parseInt(chainId)); + + // Create ERC20 contract object + const erc20Contract = new ethers.Contract(token, erc20TokenAbi, provider); + + // Get token allowance + const allowance = await erc20Contract.allowance(ownerAddress, spenderAddress); + return allowance.toString(); + } + /** * Spawn a forked chain using Anvil using a specific chain and optionally a block number * @param chainId - Chain ID to fork