Skip to content

Commit

Permalink
[Release v1.0.2] - GUI API updates (#24)
Browse files Browse the repository at this point in the history
* Add getters for ETH balance, token balances, and token approvals (#22)

* Bump version
  • Loading branch information
0xLienid committed May 24, 2023
1 parent 7b645b2 commit 6648366
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guardianui/test",
"version": "1.0.1",
"version": "1.0.2",
"description": "<p align=\"center\"> <picture> <source media=\"(prefers-color-scheme: dark)\" srcset=\"https://github.com/GuardianUI/landing-page/blob/main/assets/images/logo.png\"> <img alt=\"guardianui logo\" src=\"https://github.com/GuardianUI/landing-page/blob/main/assets/images/logo.png\" width=\"auto\" height=\"75\"> </picture> </p>",
"main": "./dist/index.js",
"exports": {
Expand Down
82 changes: 82 additions & 0 deletions src/models/GUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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<string> {
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<string> {
// 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<string> {
// 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<string> {
// 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
Expand Down

0 comments on commit 6648366

Please sign in to comment.