Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release v1.0.2] - GUI API updates #24

Merged
merged 3 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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