Skip to content

Commit

Permalink
Merge pull request #105 from unstoppabledomains/qrtp/new-rpc-methods
Browse files Browse the repository at this point in the history
Add Ethereum provider methods: eth_getBalance, eth_getCode, eth_gasPrice
  • Loading branch information
qrtp authored Jan 4, 2025
2 parents 9cc5cff + 4151e32 commit 9d85567
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
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

0 comments on commit 9d85567

Please sign in to comment.