From 4abe9e81557faff3fcb3b0e6b9fce013b6537dcf Mon Sep 17 00:00:00 2001 From: Daniel Whiffing Date: Thu, 5 Sep 2024 11:29:22 -0400 Subject: [PATCH 1/5] Adds polygonscan-api patch to add amoy support --- package-lock.json | 229 ++++++++++++++++++++++++++++ package.json | 2 + patches/polygonscan-api+1.0.4.patch | 13 ++ 3 files changed, 244 insertions(+) create mode 100644 patches/polygonscan-api+1.0.4.patch diff --git a/package-lock.json b/package-lock.json index 9b68349..68054b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,7 @@ "": { "name": "magic-chat-prototype-next", "version": "0.0.0", + "hasInstallScript": true, "dependencies": { "@langchain/community": "^0.2.23", "@langchain/core": "^0.2.20", @@ -53,6 +54,7 @@ }, "devDependencies": { "abitype": "^1.0.6", + "patch-package": "^8.0.0", "prettier": "3.0.0" }, "engines": { @@ -2741,6 +2743,12 @@ "license": "MIT", "peer": true }, + "node_modules/@yarnpkg/lockfile": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz", + "integrity": "sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ==", + "dev": true + }, "node_modules/abitype": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/abitype/-/abitype-1.0.6.tgz", @@ -3153,6 +3161,15 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", "license": "MIT" }, + "node_modules/at-least-node": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz", + "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==", + "dev": true, + "engines": { + "node": ">= 4.0.0" + } + }, "node_modules/autoprefixer": { "version": "10.4.14", "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.14.tgz", @@ -3564,6 +3581,21 @@ "node": ">= 6" } }, + "node_modules/ci-info": { + "version": "3.9.0", + "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-3.9.0.tgz", + "integrity": "sha512-NIxF55hv4nSqQswkAeiOi1r83xy8JldOFDTWiug55KBu9Jnblncd2U6ViHmYgHf01TPZS77NJBhBMKdWj9HQMQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/sibiraj-s" + } + ], + "engines": { + "node": ">=8" + } + }, "node_modules/cipher-base": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/cipher-base/-/cipher-base-1.0.4.tgz", @@ -5212,6 +5244,15 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/find-yarn-workspace-root": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/find-yarn-workspace-root/-/find-yarn-workspace-root-2.0.0.tgz", + "integrity": "sha512-1IMnbjt4KzsQfnhnzNd8wUEgXZ44IzZaZmnLYx7D5FZlaHt2gW20Cri8Q+E/t5tIj4+epTBub+2Zxu/vNILzqQ==", + "dev": true, + "dependencies": { + "micromatch": "^4.0.2" + } + }, "node_modules/flat": { "version": "5.0.2", "resolved": "https://registry.npmjs.org/flat/-/flat-5.0.2.tgz", @@ -6018,6 +6059,21 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-docker": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz", + "integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==", + "dev": true, + "bin": { + "is-docker": "cli.js" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/is-extglob": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", @@ -6300,6 +6356,18 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-wsl": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-2.2.0.tgz", + "integrity": "sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==", + "dev": true, + "dependencies": { + "is-docker": "^2.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/isarray": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/isarray/-/isarray-2.0.5.tgz", @@ -6409,6 +6477,24 @@ "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==", "license": "MIT" }, + "node_modules/json-stable-stringify": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/json-stable-stringify/-/json-stable-stringify-1.1.1.tgz", + "integrity": "sha512-SU/971Kt5qVQfJpyDveVhQ/vya+5hvrjClFOcr8c0Fq5aODJjMwutrOfCU+eCnVD5gpx1Q3fEqkyom77zH1iIg==", + "dev": true, + "dependencies": { + "call-bind": "^1.0.5", + "isarray": "^2.0.5", + "jsonify": "^0.0.1", + "object-keys": "^1.1.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/json-stable-stringify-without-jsonify": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz", @@ -6465,6 +6551,15 @@ "graceful-fs": "^4.1.6" } }, + "node_modules/jsonify": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.1.tgz", + "integrity": "sha512-2/Ki0GcmuqSrgFyelQq9M05y7PS0mEwuIzrf3f1fPqkVDVRvZrPZtVSMHxdgo8Aq0sxAOb/cr2aqqA3LeWHVPg==", + "dev": true, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/jsonpointer": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/jsonpointer/-/jsonpointer-5.0.1.tgz", @@ -6513,6 +6608,15 @@ "json-buffer": "3.0.1" } }, + "node_modules/klaw-sync": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/klaw-sync/-/klaw-sync-6.0.0.tgz", + "integrity": "sha512-nIeuVSzdCCs6TDPTqI8w1Yre34sSq7AkZ4B3sfOBbI2CgVSB4Du4aLQijFU2+lhAFCwt9+42Hel6lQNIv6AntQ==", + "dev": true, + "dependencies": { + "graceful-fs": "^4.1.11" + } + }, "node_modules/langchain": { "version": "0.2.17", "resolved": "https://registry.npmjs.org/langchain/-/langchain-0.2.17.tgz", @@ -7540,6 +7644,22 @@ "wrappy": "1" } }, + "node_modules/open": { + "version": "7.4.2", + "resolved": "https://registry.npmjs.org/open/-/open-7.4.2.tgz", + "integrity": "sha512-MVHddDVweXZF3awtlAS+6pgKLlm/JgxZ90+/NBurBoQctVOOB/zDdVjcyPzQ+0laDGbsWgrRkflI65sQeOgT9Q==", + "dev": true, + "dependencies": { + "is-docker": "^2.0.0", + "is-wsl": "^2.1.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/openai": { "version": "4.57.0", "resolved": "https://registry.npmjs.org/openai/-/openai-4.57.0.tgz", @@ -7609,6 +7729,15 @@ "node": ">= 0.8.0" } }, + "node_modules/os-tmpdir": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "integrity": "sha512-D2FR03Vir7FIu45XBY20mTb+/ZSWB00sjU9jdQXt83gDrI4Ztz5Fs7/yy74g2N5SVQY4xY1qDr4rNddwYRVX0g==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/p-finally": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz", @@ -7722,6 +7851,94 @@ "integrity": "sha512-ft3iAoLOB/MlwbNXgzy43SWGP6sQki2jQvAyBg/zDFAgr9bfNWZIUj42Kw2eJIl8kEi4PbgE6U1Zau/HwI75HA==", "license": "MIT" }, + "node_modules/patch-package": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/patch-package/-/patch-package-8.0.0.tgz", + "integrity": "sha512-da8BVIhzjtgScwDJ2TtKsfT5JFWz1hYoBl9rUQ1f38MC2HwnEIkK8VN3dKMKcP7P7bvvgzNDbfNHtx3MsQb5vA==", + "dev": true, + "dependencies": { + "@yarnpkg/lockfile": "^1.1.0", + "chalk": "^4.1.2", + "ci-info": "^3.7.0", + "cross-spawn": "^7.0.3", + "find-yarn-workspace-root": "^2.0.0", + "fs-extra": "^9.0.0", + "json-stable-stringify": "^1.0.2", + "klaw-sync": "^6.0.0", + "minimist": "^1.2.6", + "open": "^7.4.2", + "rimraf": "^2.6.3", + "semver": "^7.5.3", + "slash": "^2.0.0", + "tmp": "^0.0.33", + "yaml": "^2.2.2" + }, + "bin": { + "patch-package": "index.js" + }, + "engines": { + "node": ">=14", + "npm": ">5" + } + }, + "node_modules/patch-package/node_modules/fs-extra": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", + "integrity": "sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==", + "dev": true, + "dependencies": { + "at-least-node": "^1.0.0", + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/patch-package/node_modules/jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "dev": true, + "dependencies": { + "universalify": "^2.0.0" + }, + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, + "node_modules/patch-package/node_modules/rimraf": { + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", + "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", + "deprecated": "Rimraf versions prior to v4 are no longer supported", + "dev": true, + "dependencies": { + "glob": "^7.1.3" + }, + "bin": { + "rimraf": "bin.js" + } + }, + "node_modules/patch-package/node_modules/slash": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/slash/-/slash-2.0.0.tgz", + "integrity": "sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A==", + "dev": true, + "engines": { + "node": ">=6" + } + }, + "node_modules/patch-package/node_modules/universalify": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.1.tgz", + "integrity": "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==", + "dev": true, + "engines": { + "node": ">= 10.0.0" + } + }, "node_modules/path-exists": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", @@ -9492,6 +9709,18 @@ "node": ">=0.10.0" } }, + "node_modules/tmp": { + "version": "0.0.33", + "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", + "integrity": "sha512-jRCJlojKnZ3addtTOjdIqoRuPEKBvNXcGYqzO6zWZX8KfKEpnGY5jfggJQ3EjKuu8D4bJRr0y+cYJFmYbImXGw==", + "dev": true, + "dependencies": { + "os-tmpdir": "~1.0.2" + }, + "engines": { + "node": ">=0.6.0" + } + }, "node_modules/to-fast-properties": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz", diff --git a/package.json b/package.json index 28f524b..fedcb51 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "build": "next build", "start": "next start", "lint": "next lint", + "postinstall": "patch-package", "format": "prettier --write \"app\"" }, "engines": { @@ -58,6 +59,7 @@ }, "devDependencies": { "abitype": "^1.0.6", + "patch-package": "^8.0.0", "prettier": "3.0.0" }, "resolutions": { diff --git a/patches/polygonscan-api+1.0.4.patch b/patches/polygonscan-api+1.0.4.patch new file mode 100644 index 0000000..b27f1d6 --- /dev/null +++ b/patches/polygonscan-api+1.0.4.patch @@ -0,0 +1,13 @@ +diff --git a/node_modules/polygonscan-api/lib/get-request.js b/node_modules/polygonscan-api/lib/get-request.js +index be397d5..8d0cd9d 100755 +--- a/node_modules/polygonscan-api/lib/get-request.js ++++ b/node_modules/polygonscan-api/lib/get-request.js +@@ -15,7 +15,7 @@ function pickChainUrl(chain) { + + const MAIN_API_URL = 'https://api.polygonscan.com'; + const TESTNET_API_URL_MAP = { +- mumbai: 'https://api-testnet.polygonscan.com', ++ amoy: 'https://api-amoy.polygonscan.com', + }; + + module.exports = function(chain, timeout) { From faf0c28d3a35d05172dd79c53a41de4cb71c075d Mon Sep 17 00:00:00 2001 From: Daniel Whiffing Date: Thu, 5 Sep 2024 13:33:33 -0400 Subject: [PATCH 2/5] Ensures correct rpc uri based on chainId --- constants/index.ts | 9 ++++++++- utils/tee.ts | 7 +++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/constants/index.ts b/constants/index.ts index e40c68b..62c3aaa 100644 --- a/constants/index.ts +++ b/constants/index.ts @@ -4,15 +4,22 @@ export const CHAINS = { 11155111: { name: "ETH Sepolia", explorerURI: "https://sepolia.etherscan.io/tx/", + rpcURI: "https://eth-sepolia.g.alchemy.com/v2/", + }, + 1: { + name: "ETH Mainnet", + explorerURI: "https://etherscan.io/tx/", + rpcURI: "https://eth-mainnet.g.alchemy.com/v2/", }, - 1: { name: "ETH Mainnet", explorerURI: "https://etherscan.io/tx/" }, 137: { name: "POLY Mainnet", explorerURI: "https://polygonscan.com/tx/", + rpcURI: "https://polygon-mainnet.g.alchemy.com/v2/", }, 80002: { name: "POLY Amoy", explorerURI: "https://amoy.polygonscan.com/tx/", + rpcURI: "https://polygon-amoy.g.alchemy.com/v2/", }, }; diff --git a/utils/tee.ts b/utils/tee.ts index 5f099cc..969829a 100644 --- a/utils/tee.ts +++ b/utils/tee.ts @@ -1,9 +1,10 @@ import axios from "axios"; import * as ethers from "ethers"; -import { TransactionError, NetworkError, SigningError } from "./errors"; +import { TransactionError, SigningError } from "./errors"; import { KVCache } from "./kvCache"; import { getAbi } from "./abi"; import { ChainIdEnum } from "@/types"; +import { CHAINS } from "@/constants"; type IWalletTxPayload = { type: number; @@ -167,7 +168,9 @@ export async function getTransactionReceipt({ getWalletUUIDandAccessKey(publicAddress), ]); - const provider = new ethers.JsonRpcProvider(process.env.RPC_URL); + const provider = new ethers.JsonRpcProvider( + `${CHAINS[chainId].rpcURI}${process.env.ALCHEMY_API_KEY}`, + ); const contract = new ethers.Contract( contractAddress, abi as ethers.InterfaceAbi, From 7ffcd01052c1affd88fe5c666c4af8228749a77b Mon Sep 17 00:00:00 2001 From: Daniel Whiffing Date: Thu, 5 Sep 2024 13:34:53 -0400 Subject: [PATCH 3/5] Removes RPC url --- .env.example | 1 - utils/tee.ts | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index a311e39..4342d87 100644 --- a/.env.example +++ b/.env.example @@ -11,4 +11,3 @@ OPENAI_API_KEY="YOUR_API_KEY" ETHERSCAN_API_KEY="Your etherscan api key" -RPC_URL="Your RPC url" diff --git a/utils/tee.ts b/utils/tee.ts index 969829a..c52b309 100644 --- a/utils/tee.ts +++ b/utils/tee.ts @@ -168,9 +168,8 @@ export async function getTransactionReceipt({ getWalletUUIDandAccessKey(publicAddress), ]); - const provider = new ethers.JsonRpcProvider( - `${CHAINS[chainId].rpcURI}${process.env.ALCHEMY_API_KEY}`, - ); + const RPC_URL = `${CHAINS[chainId].rpcURI}${process.env.ALCHEMY_API_KEY}`; + const provider = new ethers.JsonRpcProvider(RPC_URL); const contract = new ethers.Contract( contractAddress, abi as ethers.InterfaceAbi, From 75d838c4ee15fff0c8da8e7970836c3c5cdf3990 Mon Sep 17 00:00:00 2001 From: Daniel Whiffing Date: Thu, 5 Sep 2024 13:37:51 -0400 Subject: [PATCH 4/5] Adds try/catch to gas estimate --- utils/tee.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/utils/tee.ts b/utils/tee.ts index c52b309..2f68a48 100644 --- a/utils/tee.ts +++ b/utils/tee.ts @@ -190,7 +190,7 @@ export async function getTransactionReceipt({ const [nonce, feeData, gasEstimate] = await Promise.all([ provider.getTransactionCount(wallet_address), provider.getFeeData(), - provider.estimateGas(payload), + getGasEstimate(provider, payload), ]); payload = { @@ -231,3 +231,15 @@ export async function getTransactionReceipt({ throw error; } } + +const getGasEstimate = async ( + provider: ethers.ethers.JsonRpcProvider, + payload: ethers.ethers.TransactionRequest, +) => { + try { + return await provider.estimateGas(payload); + } catch (e) { + // Default gas + return BigInt(100_000); + } +}; From d501cec187845e72a0fd73f2da7d2bd872b0e82a Mon Sep 17 00:00:00 2001 From: Daniel Whiffing Date: Thu, 5 Sep 2024 13:40:19 -0400 Subject: [PATCH 5/5] Adds missing keys to env sample --- .env.example | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 4342d87..96b995b 100644 --- a/.env.example +++ b/.env.example @@ -10,4 +10,5 @@ KV_URL="Retrieved from vercel" OPENAI_API_KEY="YOUR_API_KEY" ETHERSCAN_API_KEY="Your etherscan api key" - +POLYGONSCAN_API_KEY="Your polygonscan api key" +ALCHEMY_API_KEY="Your alchemy api key" \ No newline at end of file