Skip to content

Commit

Permalink
Merge pull request #94 from unstoppabledomains/qrtp/rpc-support-for-swap
Browse files Browse the repository at this point in the history
add ERC-20 and RPC support required for successful Uniswap transactions
  • Loading branch information
qrtp authored Nov 5, 2024
2 parents 9bbd7ae + 5244c33 commit f45713c
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 71 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 3.1.38
* Support for ERC-20 token management
* Support additional RPC methods to enable Uniswap transactions
* Support for service worker background signing
* Remove clutter from drop down menu
* Fix bug validating token decimals before transfer
* Modify activity panel color palette

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"@metamask/providers": "^17.1.2",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@unstoppabledomains/config": "0.0.13",
"@unstoppabledomains/ui-components": "0.0.51-browser-extension.6",
"@unstoppabledomains/config": "0.0.17",
"@unstoppabledomains/ui-components": "0.0.51-browser-extension.10",
"@unstoppabledomains/ui-kit": "0.3.24",
"abitype": "^1.0.6",
"async-mutex": "^0.5.0",
Expand Down
19 changes: 19 additions & 0 deletions src/lib/wallet/evm/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Web3} from "web3";

import config from "@unstoppabledomains/config";
import {getProviderUrl} from "@unstoppabledomains/ui-components/lib/wallet/evm/provider";

export const getWeb3Provider = (chainId: number) => {
const chainSymbol = Object.keys(config.BLOCKCHAINS).find(k => {
return (
config.BLOCKCHAINS[k as keyof typeof config.BLOCKCHAINS].CHAIN_ID ===
chainId
);
});
if (!chainSymbol) {
throw new Error(`Configuration not found for chainId: ${chainId}`);
}

const providerUrl = getProviderUrl(chainSymbol);
return new Web3(providerUrl);
};
21 changes: 0 additions & 21 deletions src/lib/wallet/evm/token.test.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/lib/wallet/evm/token.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/lib/wallet/isAscii.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {isAscii} from "./isAscii";

describe("isAscii", () => {
it("returns true for ASCII text", () => {
expect(isAscii("abc123")).toBeTruthy();
});
});
2 changes: 1 addition & 1 deletion src/pages/Wallet/Connect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const Connect: React.FC = () => {

useEffect(() => {
// wait for required fields to be loaded
if (!isMounted() || !preferences || !connections) {
if (!isMounted() || !preferences || !connections || isLoaded) {
return;
}

Expand Down
108 changes: 98 additions & 10 deletions src/scripts/liteWalletProvider/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {Logger} from "../../lib/logger";
import {isEthAddress} from "../../lib/sherlock/matcher";
import {ResolutionData} from "../../lib/sherlock/types";
import {announceProvider} from "../../lib/wallet/evm/eip6963";
import {getWeb3Provider} from "../../lib/wallet/evm/provider";
import {EIP_712_KEY, TypedMessage} from "../../types/wallet/eip712";
import {WalletPreferences} from "../../types/wallet/preferences";
import {
Expand Down Expand Up @@ -172,14 +173,25 @@ class LiteWalletProvider extends EventEmitter {
result = await this.handleTypedSign(clone(request.params));
break;
// Transaction methods
case "eth_sendTransaction":
result = await this.handleSendTransaction(clone(request.params));
break;
case "eth_blockNumber":
result = await this.handleGetBlockNumber();
break;
case "eth_call":
result = await this.handleReadOnlyCall(clone(request.params));
break;
case "eth_estimateGas":
result = await this.handleEstimateGas(clone(request.params));
break;
case "eth_getTransactionByHash":
// not implemented, but stubbed out with "not found" to prevent runtime
// errors on apps that call this method
result = null;
result = await this.handleGetTransactionByHash(clone(request.params));
break;
case "eth_getTransactionReceipt":
result = await this.handleGetTransactionReceipt(
clone(request.params),
);
break;
case "eth_sendTransaction":
result = await this.handleSendTransaction(clone(request.params));
break;
default:
throw new EthereumProviderError(
Expand All @@ -189,10 +201,15 @@ class LiteWalletProvider extends EventEmitter {
}

// result is successful
Logger.log(
"Request successful",
JSON.stringify({method: request.method, result}),
);
try {
Logger.log(
"Request successful",
JSON.stringify({method: request.method, result}),
);
} catch (e2: any) {
Logger.log("Request successful", request.method);
}

return result;
} catch (e: any) {
// result is failure
Expand Down Expand Up @@ -499,6 +516,77 @@ class LiteWalletProvider extends EventEmitter {
return [address];
}

private async handleGetBlockNumber() {
// retrieve the web3 provider for connected chain
const chainIdHex = await this.handleGetConnectedChainIds();
const web3 = getWeb3Provider(web3utils.hexToNumber(chainIdHex) as number);

// query the block number
const block = await web3.eth.getBlockNumber();
return web3utils.numberToHex(block);
}

private async handleEstimateGas(params: any[]) {
// retrieve the web3 provider for connected chain
const chainIdHex = await this.handleGetConnectedChainIds();
const web3 = getWeb3Provider(web3utils.hexToNumber(chainIdHex) as number);

// validate any Tx parameters have been passed
if (!params || params.length === 0) {
throw new EthereumProviderError(PROVIDER_CODE_USER_ERROR, InvalidTxError);
}

// query the estimated gas
return web3utils.numberToHex(await web3.eth.estimateGas(params[0]));
}

private async handleGetTransactionByHash(params: any[]) {
// retrieve the web3 provider for connected chain
const chainIdHex = await this.handleGetConnectedChainIds();
const web3 = getWeb3Provider(web3utils.hexToNumber(chainIdHex) as number);

// validate any Tx parameters have been passed
if (!params || params.length === 0) {
throw new EthereumProviderError(PROVIDER_CODE_USER_ERROR, InvalidTxError);
}

// query the requested transaction
return await web3.eth.getTransaction(params[0]);
}

private async handleGetTransactionReceipt(params: any[]) {
// retrieve the web3 provider for connected chain
const chainIdHex = await this.handleGetConnectedChainIds();
const web3 = getWeb3Provider(web3utils.hexToNumber(chainIdHex) as number);

// validate any Tx parameters have been passed
if (!params || params.length === 0) {
throw new EthereumProviderError(PROVIDER_CODE_USER_ERROR, InvalidTxError);
}

// query the requested transaction
return await web3.eth.getTransactionReceipt(params[0]);
}

private async handleReadOnlyCall(params: any[]) {
// retrieve the web3 provider for connected chain
const chainIdHex = await this.handleGetConnectedChainIds();
const web3 = getWeb3Provider(web3utils.hexToNumber(chainIdHex) as number);

// validate any Tx parameters have been passed
if (!params || params.length === 0) {
throw new EthereumProviderError(PROVIDER_CODE_USER_ERROR, InvalidTxError);
}

// query the requested transaction
return await web3.eth.call(
// call parameters
params[0],
// optional block number
params.length > 1 ? params[1] : undefined,
);
}

private async handleGetConnectedChainIds() {
if (this.networkVersion) {
// return hex formatted network version
Expand Down
3 changes: 3 additions & 0 deletions src/types/wallet/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export type ProviderMethod =
| "eth_sendTransaction"
| "eth_signTypedData_v4"
| "eth_blockNumber"
| "eth_getTransactionReceipt"
| "eth_call"
| "eth_estimateGas"
| "eth_getTransactionByHash"
| "personal_sign"
| "wallet_requestPermissions"
Expand Down
21 changes: 11 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6271,9 +6271,9 @@ __metadata:
languageName: node
linkType: hard

"@unstoppabledomains/config@npm:0.0.13, @unstoppabledomains/config@npm:latest":
version: 0.0.13
resolution: "@unstoppabledomains/config@npm:0.0.13"
"@unstoppabledomains/config@npm:0.0.17, @unstoppabledomains/config@npm:latest":
version: 0.0.17
resolution: "@unstoppabledomains/config@npm:0.0.17"
dependencies:
"@bugsnag/browser-performance": ^2.2.0
"@bugsnag/js": ^7.16.2
Expand All @@ -6282,7 +6282,7 @@ __metadata:
"@types/lodash.merge": ^4.6.7
lodash: ^4.17.21
lodash.merge: ^4.6.2
checksum: edf0b6a9fe30b61a4f7e731c7a205ac814b6757b74c6bbf164c47b92be308141c1ab397775bc5c6bda900f176f46c09fe04afc30a6429e41d14ccb684d1d35ca
checksum: 0b20b5e6b3ccad71b6ee0ed83efc696e9a4444c945ca63b0ba3f2636758a8de9c009652d859bbc488325c3f19bc74a6b10641ff3775b2d15b52e0a0e233c7b78
languageName: node
linkType: hard

Expand All @@ -6299,9 +6299,9 @@ __metadata:
languageName: node
linkType: hard

"@unstoppabledomains/ui-components@npm:0.0.51-browser-extension.6":
version: 0.0.51-browser-extension.6
resolution: "@unstoppabledomains/ui-components@npm:0.0.51-browser-extension.6"
"@unstoppabledomains/ui-components@npm:0.0.51-browser-extension.10":
version: 0.0.51-browser-extension.10
resolution: "@unstoppabledomains/ui-components@npm:0.0.51-browser-extension.10"
dependencies:
"@braintree/sanitize-url": ^6.0.4
"@emotion/server": ^11.4.0
Expand Down Expand Up @@ -6342,6 +6342,7 @@ __metadata:
"@xmtp/content-type-text": ^1.0.0
"@xmtp/proto": ^3.62.1
"@xmtp/xmtp-js": 12.1.0
abitype: ^1.0.6
bip322-js: ^1.1.0
bitcoin-address-validation: ^2.2.3
bluebird: ^3.7.2
Expand Down Expand Up @@ -6400,7 +6401,7 @@ __metadata:
"@unstoppabledomains/ui-kit": ^0.3.24
notistack: ^2.0.5
react-query: ^3.39.3
checksum: 9e57ff6e08a952c9180a45443df228999979b4ec17da7ced8d08918bd07ec084e36e4e00fe9e21715c68dbead776a75c9e74072a69996691c3687fc80016d749
checksum: 942daaa58b454b844d217096378b4c6ad8f9dc5abca48c3acda479cd98f0125e576c165eabc422fbe4fc0a840d85ad58c3641a7d77d2e8c7fc8910f3e3b88368
languageName: node
linkType: hard

Expand Down Expand Up @@ -22263,8 +22264,8 @@ __metadata:
"@types/react-dom": ^17.0.0
"@typescript-eslint/eslint-plugin": ^5.39.0
"@typescript-eslint/parser": ^5.39.0
"@unstoppabledomains/config": 0.0.13
"@unstoppabledomains/ui-components": 0.0.51-browser-extension.6
"@unstoppabledomains/config": 0.0.17
"@unstoppabledomains/ui-components": 0.0.51-browser-extension.10
"@unstoppabledomains/ui-kit": 0.3.24
abitype: ^1.0.6
assert: ^2.1.0
Expand Down

0 comments on commit f45713c

Please sign in to comment.