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

Add Ethereum provider methods: eth_getBalance, eth_getCode, eth_gasPrice #105

Merged
merged 1 commit into from
Jan 4, 2025
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 3.1.46
* Add Ethereum RPC methods: eth_getBalance, eth_getCode, eth_gasPrice

## 3.1.45
* Bug fixes

## 3.1.44
* Enable Solana wallet provider in the browser

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ud-extension",
"version": "3.1.45",
"version": "3.1.46",
"license": "MIT",
"author": {
"email": "eng@unstoppabledomains.com",
Expand Down
18 changes: 18 additions & 0 deletions src/scripts/liteWalletProvider/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,24 @@ const handleRpcRequest = async (
await web3.eth.estimateGas(rpcParams[0]),
);
break;
case "gasPrice":
result = web3utils.numberToHex(await web3.eth.getGasPrice());
break;
case "getBalance":
result = await web3.eth.getBalance(
rpcParams[0],
rpcParams.length > 1 ? rpcParams[1] : undefined,
);
if (typeof result !== "string" || !result.startsWith("0x")) {
result = web3utils.numberToHex(result);
}
break;
case "getCode":
result = web3.eth.getCode(
rpcParams[0],
rpcParams.length > 1 ? rpcParams[1] : undefined,
);
break;
case "getTransaction":
result = await web3.eth.getTransaction(rpcParams[0]);
break;
Expand Down
30 changes: 30 additions & 0 deletions src/scripts/liteWalletProvider/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ class LiteWalletProvider extends EventEmitter {
case "eth_estimateGas":
result = await this.handleRpcEstimateGas(clone(request.params));
break;
case "eth_gasPrice":
result = await this.handleRpcGasPrice();
break;
case "eth_getBalance":
result = await this.handleRpcGetBalance(clone(request.params));
break;
case "eth_getCode":
result = await this.handleRpcGetCode(clone(request.params));
break;
case "eth_getTransactionByHash":
result = await this.handleRpcGetTransaction(clone(request.params));
break;
Expand Down Expand Up @@ -536,6 +545,27 @@ class LiteWalletProvider extends EventEmitter {
return await this.handleRpcMethod(chainId, "estimateGas", params);
}

private async handleRpcGasPrice() {
// retrieve the web3 provider for connected chain
const chainIdHex = await this.handleGetConnectedChainIds();
const chainId = web3utils.hexToNumber(chainIdHex) as number;
return await this.handleRpcMethod(chainId, "gasPrice", []);
}

private async handleRpcGetBalance(params: any[]) {
// retrieve the web3 provider for connected chain
const chainIdHex = await this.handleGetConnectedChainIds();
const chainId = web3utils.hexToNumber(chainIdHex) as number;
return await this.handleRpcMethod(chainId, "getBalance", params);
}

private async handleRpcGetCode(params: any[]) {
// retrieve the web3 provider for connected chain
const chainIdHex = await this.handleGetConnectedChainIds();
const chainId = web3utils.hexToNumber(chainIdHex) as number;
return await this.handleRpcMethod(chainId, "getCode", params);
}

private async handleRpcGetTransaction(params: any[]) {
const chainIdHex = await this.handleGetConnectedChainIds();
const chainId = web3utils.hexToNumber(chainIdHex) as number;
Expand Down
6 changes: 6 additions & 0 deletions src/types/wallet/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export type ProviderMethod =
| "eth_sendTransaction"
| "eth_signTypedData_v4"
| "eth_blockNumber"
| "eth_getBalance"
| "eth_getCode"
| "eth_gasPrice"
| "eth_getTransactionReceipt"
| "eth_call"
| "eth_estimateGas"
Expand All @@ -39,6 +42,9 @@ export const ProviderMethodsWithPrompt: ProviderMethod[] = [
export type RpcRequest =
| "call"
| "estimateGas"
| "gasPrice"
| "getCode"
| "getBalance"
| "getBlockNumber"
| "getTransaction"
| "getTransactionReceipt";
Expand Down
Loading