From d55e39b189dddf0c787c2f2cc2ad47134f927ebd Mon Sep 17 00:00:00 2001 From: TrooperCrypto Date: Wed, 31 May 2023 21:48:56 +0200 Subject: [PATCH] add ethereumRPC for custom rpc's --- README.md | 11 +++++------ config.json.EXAMPLE | 1 + marketmaker.js | 24 +++++++++++++++++++++++- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 687b702..f069923 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ node marketmaker.js ## Configuration Via Environment Variables -It is __recommended__ to use environment variables to set your private keys. You can set `ETH_PRIVKEY`, `CRYPTOWATCH_API_KEY` and `INFURA` using them. You can set them using `ETH_PRIVKEY=0x____`. For more informations on private keys read [this](https://linuxize.com/post/how-to-set-and-list-environment-variables-in-linux/). +It is __recommended__ to use environment variables to set your private keys. You can set `ETH_PRIVKEY`, `CRYPTOWATCH_API_KEY` and `RPC` using them. You can set them using `ETH_PRIVKEY=0x____`. For more informations on private keys read [this](https://linuxize.com/post/how-to-set-and-list-environment-variables-in-linux/). If your hosting service requires you to pass in configs via environment variables you can compress `config.json`: @@ -139,10 +139,9 @@ Example: ``` ###### Chainlink -With chainlink you have access to price oracles via blockchain. The requests are read-calls to a smart contract. The public ethers provider might be too slow for a higher number of pairs or at times of high demand. Therefore, it might be needed to have access to an Infura account (100000 Requests/Day for free). You can get an endpoint for your market maker (like https://mainnet.infura.io/v3/...), You can add this with the `infuraUrl` field in `config.json`, like this: +With chainlink you have access to price oracles via blockchain. The requests are read-calls to a smart contract. The public ethers provider might be too slow for a higher number of pairs or at times of high demand. Therefore, it might be needed to have access to an RPC provider account like quicknode, alchemy, ... (some offer free calls). You can get an endpoint for your market maker (like "https://eth.llamarpc.com"), You can add this with the `ethereumRPC` field in `config.json`, like this: ``` - -"infuraUrl": "https://mainnet.infura.io/v3/xxxxxxxx", +"ethereumRPC": "https://eth.llamarpc.com", "pairs": { "ETH-USDC": { "zigzagChainId": 1, @@ -161,9 +160,9 @@ You can get the available market contracts [here.](https://docs.chain.link/docs/ ``` ###### UniswapV3 -With uniswapV3 you have access to price feed's via blockchain. The requests are read-calls to a smart contract. The public ethers provider might be too slow for a higher number of pairs or at times of high demand. Therefore, it might be needed to have access to an Infura account (100000 Requests/Day for free). You can get an endpoint for your market maker (like https://mainnet.infura.io/v3/...), You can add this with the `infuraUrl` field in `config.json`, like this: +With uniswapV3 you have access to price feed's via blockchain. The requests are read-calls to a smart contract. The public ethers provider might be too slow for a higher number of pairs or at times of high demand. Therefore, it might be needed to have access to an RPC provider account like quicknode, alchemy, ... (some offer free calls). You can get an endpoint for your market maker (like "https://eth.llamarpc.com"), You can add this with the `ethereumRPC` field in `config.json`, like this: ``` -"infuraUrl": "https://mainnet.infura.io/v3/xxxxxxxx", +"ethereumRPC": "https://eth.llamarpc.com", "pairs": { "ETH-USDC": { "zigzagChainId": 1, diff --git a/config.json.EXAMPLE b/config.json.EXAMPLE index 9c76b35..57dff94 100644 --- a/config.json.EXAMPLE +++ b/config.json.EXAMPLE @@ -6,6 +6,7 @@ ], "zigzagChainId": 1, "zigzagWsUrl": "wss://zigzag-exchange.herokuapp.com", + "ethereumRPC": "https://eth.llamarpc.com", "pairs": { "ETH-USDC": { "priceFeedPrimary": "cryptowatch:6631", diff --git a/marketmaker.js b/marketmaker.js index cda9afa..e3e92f0 100644 --- a/marketmaker.js +++ b/marketmaker.js @@ -47,14 +47,36 @@ console.log("ACTIVE PAIRS", activePairs); const CHAIN_ID = parseInt(MM_CONFIG.zigzagChainId); const ETH_NETWORK = (CHAIN_ID === 1) ? "mainnet" : "goerli"; const infureKey = (process.env.infura || MM_CONFIG.infura); +const ethereumRPC = process.env.infuraUrl || process.env.RPC || process.env.ethereumRPC || MM_CONFIG.infuraUrl || MM_CONFIG.RPC || MM_CONFIG.ethereumRPC; let ethersProvider = null; if (infureKey) { ethersProvider = new ethers.providers.InfuraProvider( "mainnet", infureKey ); +} else if (ethereumRPC) { + ethersProvider = new ethers.providers.JsonRpcProvider(ethereumRPC); } else { - ethersProvider = new ethers.getDefaultProvider("mainnet"); + console.error(` + You did not provider an rpc url with "ethereumRPC" inside your config + or with ETHEREUM_RPC in the environment variables. + Please add a custom one. There are some providers with free plans. + + Using a public provider, there is no guarantee that it is stable, for a stable market-maker create a custom RPC.` + ) + ethersProvider = new ethers.providers.JsonRpcProvider('https://eth.llamarpc.com'); +} + +const resProvider = await Promise.race([ + ethersProvider.ready, + new Promise((_, reject) => { + setTimeout(() => reject(new Error('Request timed out')), 10_000) + }) +]) + +// check if provider is working +if (Number(resProvider.chainId) !== 1) { + throw new Error(`Cant connect provider, use "ethereumRPC" in the config to add an ethereumRPC`) } // Start price feeds