Jayant Krishnamurthy 2 سال پیش
والد
کامیت
3b96beea74
2فایلهای تغییر یافته به همراه218 افزوده شده و 10 حذف شده
  1. 147 6
      package-lock.json
  2. 71 4
      target_chains/ethereum/proxy/src/index.ts

+ 147 - 6
package-lock.json

@@ -16,6 +16,7 @@
         "target_chains/aptos/sdk/js",
         "target_chains/cosmwasm/sdk/js",
         "target_chains/ethereum/contracts",
+        "target_chains/ethereum/proxy",
         "target_chains/ethereum/sdk/js",
         "target_chains/ethereum/sdk/solidity",
         "third_party/pyth/p2w-relay",
@@ -11080,6 +11081,10 @@
         "ieee754": "^1.2.1"
       }
     },
+    "node_modules/@pythnetwork/evm-proxy": {
+      "resolved": "target_chains/ethereum/proxy",
+      "link": true
+    },
     "node_modules/@pythnetwork/price-service-client": {
       "resolved": "price_service/client/js",
       "link": true
@@ -18033,6 +18038,14 @@
       "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz",
       "integrity": "sha512-SZs7ekbP8CN0txVG2xVRH6EgKmEm31BOxA07vkFaETzZz1xh+cbt8BcI0slpymvwhx5dlFnQG2rTlPVQn+iRPQ=="
     },
+    "node_modules/@types/http-proxy": {
+      "version": "1.17.10",
+      "resolved": "https://registry.npmjs.org/@types/http-proxy/-/http-proxy-1.17.10.tgz",
+      "integrity": "sha512-Qs5aULi+zV1bwKAg5z1PWnDXWmsn+LxIvUGv6E2+OOMYhclZMO+OXd9pYVf2gLykf2I7IV2u7oTHwChPNsvJ7g==",
+      "dependencies": {
+        "@types/node": "*"
+      }
+    },
     "node_modules/@types/istanbul-lib-coverage": {
       "version": "2.0.4",
       "resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz",
@@ -29552,6 +29565,19 @@
       "resolved": "https://registry.npmjs.org/http-https/-/http-https-1.0.0.tgz",
       "integrity": "sha512-o0PWwVCSp3O0wS6FvNr6xfBCHgt0m1tvPLFOCc2iFDKTRAXhB7m8klDf7ErowFH8POa6dVdGatKU5I1YYwzUyg=="
     },
+    "node_modules/http-proxy": {
+      "version": "1.18.1",
+      "resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.1.tgz",
+      "integrity": "sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==",
+      "dependencies": {
+        "eventemitter3": "^4.0.0",
+        "follow-redirects": "^1.0.0",
+        "requires-port": "^1.0.0"
+      },
+      "engines": {
+        "node": ">=8.0.0"
+      }
+    },
     "node_modules/http-proxy-agent": {
       "version": "5.0.0",
       "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-5.0.0.tgz",
@@ -29566,6 +29592,40 @@
         "node": ">= 6"
       }
     },
+    "node_modules/http-proxy-middleware": {
+      "version": "2.0.6",
+      "resolved": "https://registry.npmjs.org/http-proxy-middleware/-/http-proxy-middleware-2.0.6.tgz",
+      "integrity": "sha512-ya/UeJ6HVBYxrgYotAZo1KvPWlgB48kUJLDePFeneHsVujFaW5WNj2NgWCAE//B1Dl02BIfYlpNgBy8Kf8Rjmw==",
+      "dependencies": {
+        "@types/http-proxy": "^1.17.8",
+        "http-proxy": "^1.18.1",
+        "is-glob": "^4.0.1",
+        "is-plain-obj": "^3.0.0",
+        "micromatch": "^4.0.2"
+      },
+      "engines": {
+        "node": ">=12.0.0"
+      },
+      "peerDependencies": {
+        "@types/express": "^4.17.13"
+      },
+      "peerDependenciesMeta": {
+        "@types/express": {
+          "optional": true
+        }
+      }
+    },
+    "node_modules/http-proxy-middleware/node_modules/is-plain-obj": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-3.0.0.tgz",
+      "integrity": "sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA==",
+      "engines": {
+        "node": ">=10"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
     "node_modules/http-signature": {
       "version": "1.2.0",
       "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",
@@ -40973,8 +41033,7 @@
     "node_modules/requires-port": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
-      "integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==",
-      "dev": true
+      "integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ=="
     },
     "node_modules/reselect": {
       "version": "4.1.7",
@@ -47862,7 +47921,7 @@
       "dependencies": {
         "@injectivelabs/sdk-ts": "^1.0.457",
         "@pythnetwork/price-service-client": "*",
-        "@pythnetwork/pyth-sdk-solidity": "^2.2.0",
+        "@pythnetwork/pyth-sdk-solidity": "*",
         "@truffle/hdwallet-provider": "^2.1.3",
         "joi": "^17.6.0",
         "web3": "^1.8.1",
@@ -49678,6 +49737,31 @@
         "node": ">=12"
       }
     },
+    "target_chains/ethereum/proxy": {
+      "name": "@pythnetwork/evm-proxy",
+      "version": "1.2.0",
+      "license": "Apache-2.0",
+      "dependencies": {
+        "@pythnetwork/price-service-client": "*",
+        "buffer": "^6.0.3",
+        "express": "^4.18.2",
+        "http-proxy-middleware": "^2.0.6"
+      },
+      "devDependencies": {
+        "@types/jest": "^29.4.0",
+        "@types/node": "^18.11.18",
+        "@types/web3-provider-engine": "^14.0.1",
+        "@types/yargs": "^17.0.10",
+        "@typescript-eslint/eslint-plugin": "^5.21.0",
+        "@typescript-eslint/parser": "^5.21.0",
+        "eslint": "^8.14.0",
+        "jest": "^29.4.1",
+        "prettier": "^2.6.2",
+        "ts-jest": "^29.0.5",
+        "typescript": "^4.6.3",
+        "yargs": "^17.4.1"
+      }
+    },
     "target_chains/ethereum/sdk/js": {
       "name": "@pythnetwork/pyth-evm-js",
       "version": "1.2.0",
@@ -57431,6 +57515,27 @@
         }
       }
     },
+    "@pythnetwork/evm-proxy": {
+      "version": "file:target_chains/ethereum/proxy",
+      "requires": {
+        "@pythnetwork/price-service-client": "*",
+        "@types/jest": "^29.4.0",
+        "@types/node": "^18.11.18",
+        "@types/web3-provider-engine": "^14.0.1",
+        "@types/yargs": "^17.0.10",
+        "@typescript-eslint/eslint-plugin": "^5.21.0",
+        "@typescript-eslint/parser": "^5.21.0",
+        "buffer": "^6.0.3",
+        "eslint": "^8.14.0",
+        "express": "^4.18.2",
+        "http-proxy-middleware": "^2.0.6",
+        "jest": "^29.4.1",
+        "prettier": "^2.6.2",
+        "ts-jest": "^29.0.5",
+        "typescript": "^4.6.3",
+        "yargs": "^17.4.1"
+      }
+    },
     "@pythnetwork/price-service-client": {
       "version": "file:price_service/client/js",
       "requires": {
@@ -58605,7 +58710,7 @@
       "requires": {
         "@injectivelabs/sdk-ts": "^1.0.457",
         "@pythnetwork/price-service-client": "*",
-        "@pythnetwork/pyth-sdk-solidity": "^2.2.0",
+        "@pythnetwork/pyth-sdk-solidity": "*",
         "@truffle/hdwallet-provider": "^2.1.3",
         "@types/ethereum-protocol": "^1.0.2",
         "@types/jest": "^27.4.1",
@@ -66090,6 +66195,14 @@
       "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz",
       "integrity": "sha512-SZs7ekbP8CN0txVG2xVRH6EgKmEm31BOxA07vkFaETzZz1xh+cbt8BcI0slpymvwhx5dlFnQG2rTlPVQn+iRPQ=="
     },
+    "@types/http-proxy": {
+      "version": "1.17.10",
+      "resolved": "https://registry.npmjs.org/@types/http-proxy/-/http-proxy-1.17.10.tgz",
+      "integrity": "sha512-Qs5aULi+zV1bwKAg5z1PWnDXWmsn+LxIvUGv6E2+OOMYhclZMO+OXd9pYVf2gLykf2I7IV2u7oTHwChPNsvJ7g==",
+      "requires": {
+        "@types/node": "*"
+      }
+    },
     "@types/istanbul-lib-coverage": {
       "version": "2.0.4",
       "resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz",
@@ -75411,6 +75524,16 @@
       "resolved": "https://registry.npmjs.org/http-https/-/http-https-1.0.0.tgz",
       "integrity": "sha512-o0PWwVCSp3O0wS6FvNr6xfBCHgt0m1tvPLFOCc2iFDKTRAXhB7m8klDf7ErowFH8POa6dVdGatKU5I1YYwzUyg=="
     },
+    "http-proxy": {
+      "version": "1.18.1",
+      "resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.1.tgz",
+      "integrity": "sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==",
+      "requires": {
+        "eventemitter3": "^4.0.0",
+        "follow-redirects": "^1.0.0",
+        "requires-port": "^1.0.0"
+      }
+    },
     "http-proxy-agent": {
       "version": "5.0.0",
       "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-5.0.0.tgz",
@@ -75422,6 +75545,25 @@
         "debug": "4"
       }
     },
+    "http-proxy-middleware": {
+      "version": "2.0.6",
+      "resolved": "https://registry.npmjs.org/http-proxy-middleware/-/http-proxy-middleware-2.0.6.tgz",
+      "integrity": "sha512-ya/UeJ6HVBYxrgYotAZo1KvPWlgB48kUJLDePFeneHsVujFaW5WNj2NgWCAE//B1Dl02BIfYlpNgBy8Kf8Rjmw==",
+      "requires": {
+        "@types/http-proxy": "^1.17.8",
+        "http-proxy": "^1.18.1",
+        "is-glob": "^4.0.1",
+        "is-plain-obj": "^3.0.0",
+        "micromatch": "^4.0.2"
+      },
+      "dependencies": {
+        "is-plain-obj": {
+          "version": "3.0.0",
+          "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-3.0.0.tgz",
+          "integrity": "sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA=="
+        }
+      }
+    },
     "http-signature": {
       "version": "1.2.0",
       "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",
@@ -85413,8 +85555,7 @@
     "requires-port": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
-      "integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==",
-      "dev": true
+      "integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ=="
     },
     "reselect": {
       "version": "4.1.7",

+ 71 - 4
target_chains/ethereum/proxy/src/index.ts

@@ -1,17 +1,83 @@
+import express from "express";
+import {
+  createProxyMiddleware,
+  Filter,
+  Options,
+  RequestHandler,
+  responseInterceptor,
+} from "http-proxy-middleware";
 import * as http from "http";
+import { ClientRequest } from "http";
+
+const UNDERLYING_URL = "https://goerli.optimism.io";
+
+const proxy = createProxyMiddleware({
+  target: UNDERLYING_URL,
+  changeOrigin: true, // for vhosted sites
+
+  selfHandleResponse: true, // res.end() will be called internally by responseInterceptor()
+
+  onProxyReq: (proxyReq: ClientRequest, req, res, options) => {
+    const exchange = `[-->] ${req.method} ${req.url} -> ${proxyReq.method} ${proxyReq.host} ${proxyReq.path}`;
+    console.log(exchange);
+
+    let body = "";
+    req.on("data", (chunk) => {
+      body += chunk;
+    });
+    req.on("end", () => {
+      console.log(body);
+
+      // Modify the body of the request here
+      // const modifiedBody = body; // JSON.stringify({ data: body });
+      // proxyReq.setHeader('content-type', 'application/json');
+      // proxyReq.setHeader('content-length', Buffer.byteLength(modifiedBody));
+      // proxyReq.write(modifiedBody);
+      // proxyReq.end();
+    });
+  },
+
+  onProxyRes: responseInterceptor(
+    async (responseBuffer, proxyRes, req, res) => {
+      // log original request and proxied request info
+      const exchange = `[<--] ${req.method} ${req.url} -> ${proxyRes.url} [${proxyRes.statusCode}]`;
+      console.log(exchange); // [DEBUG] GET / -> http://www.example.com [200]
+
+      // log complete response
+      const response = responseBuffer.toString("utf8");
+      console.log(response); // log response body
+
+      return responseBuffer;
+    }
+  ),
+});
+
+const app = express();
+
+app.use("/", proxy);
+// app.use(bodyParser.json());
+// app.use(bodyParser.urlencoded());
+
+app.listen(8080);
+
+/*
+import * as http from "http";
+import * as https from "https";
 
 const server = http.createServer(
   (req: http.IncomingMessage, res: http.ServerResponse) => {
     const { method, url, headers } = req;
     console.log(`${method} request to ${url}`);
 
-    const proxy = http.request(
-      url!,
+    const underlyingRpc = "https://eth.llamarpc.com"
+
+    const proxy = https.request(
+      new URL(url!, underlyingRpc),
       { method, headers },
       (proxyRes: http.IncomingMessage) => {
         console.log(`Proxy response status code: ${proxyRes.statusCode}`);
-        res.writeHead(proxyRes.statusCode || 200, proxyRes.headers);
-        proxyRes.pipe(res);
+        // res.writeHead(proxyRes.statusCode || 200, proxyRes.headers);
+        // proxyRes.pipe(res);
       }
     );
 
@@ -24,3 +90,4 @@ const PORT = process.env.PORT || 8080;
 server.listen(PORT, () => {
   console.log(`Proxy server listening on port ${PORT}`);
 });
+*/