From dc38ca5e8db33bb55337c441e06a976802e085ae Mon Sep 17 00:00:00 2001 From: Dzmitry Lahunouski Date: Wed, 10 Jul 2024 19:23:14 +0100 Subject: [PATCH] fuel with alchemy --- .env.example | 3 ++- Makefile | 2 +- src/cli/tx.ts | 8 ------ src/libs/alchemy.ts | 30 +++++++++++++++++++++ src/libs/clients.ts | 41 +++++++++++++++++++++++++++++ src/libs/local-evm-client.ts | 21 --------------- src/libs/validations.ts | 2 +- src/packages/fuel/deposit/index.ts | 14 ++++++---- src/packages/scroll/report/index.ts | 2 +- src/packages/tx/index.ts | 19 ------------- 10 files changed, 85 insertions(+), 57 deletions(-) delete mode 100644 src/cli/tx.ts create mode 100644 src/libs/alchemy.ts create mode 100644 src/libs/clients.ts delete mode 100644 src/libs/local-evm-client.ts delete mode 100644 src/packages/tx/index.ts diff --git a/.env.example b/.env.example index f0884ad..db48fd9 100644 --- a/.env.example +++ b/.env.example @@ -2,4 +2,5 @@ PROXY_USER=FFFFFF PROXY_PASS=AAAAAAAAAAAA PROXY_HOST=xproxy.site PROXY_PORT=11111 -PROXY_REBOOT=https://changeip.mobileproxy.space/?proxy_key= \ No newline at end of file +PROXY_REBOOT=https://changeip.mobileproxy.space/?proxy_key= +ALCHEMY_RPC_KEY= \ No newline at end of file diff --git a/Makefile b/Makefile index 8de108f..15624e2 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ profiles :; node --import tsx/esm src/cli/profiles.ts recover :; node --import tsx/esm src/cli/recover.ts -tx :; node --import tsx/esm src/cli/tx.ts +fuel :; node --import tsx/esm src/cli/fuel.ts report :; node --import tsx/esm src/cli/report.ts diff --git a/src/cli/tx.ts b/src/cli/tx.ts deleted file mode 100644 index 45a3836..0000000 --- a/src/cli/tx.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { input } from "@inquirer/prompts"; -import { tx } from "src/packages/tx"; - -(async function main() { - const network = await input({ message: "enter network" }); - console.log("ntw", network); - return tx(network); -})(); diff --git a/src/libs/alchemy.ts b/src/libs/alchemy.ts new file mode 100644 index 0000000..96c68d9 --- /dev/null +++ b/src/libs/alchemy.ts @@ -0,0 +1,30 @@ +/* + * The supported networks by Alchemy. Note that some functions are not available + * on all networks. Please refer to the Alchemy documentation for which APIs are + * available on which networks + * {@link https://docs.alchemy.com/alchemy/apis/feature-support-by-chain} + * + * @public + */ +export enum Network { + ETH_MAINNET = "eth-mainnet", + ETH_GOERLI = "eth-goerli", + ETH_SEPOLIA = "eth-sepolia", + OPT_MAINNET = "opt-mainnet", + OPT_GOERLI = "opt-goerli", + OPT_SEPOLIA = "opt-sepolia", + ARB_MAINNET = "arb-mainnet", + ARB_GOERLI = "arb-goerli", + ARB_SEPOLIA = "arb-sepolia", + MATIC_MAINNET = "polygon-mainnet", + MATIC_MUMBAI = "polygon-mumbai", + MATIC_AMOY = "polygon-amoy", + ASTAR_MAINNET = "astar-mainnet", + POLYGONZKEVM_MAINNET = "polygonzkevm-mainnet", + POLYGONZKEVM_TESTNET = "polygonzkevm-testnet", + BASE_MAINNET = "base-mainnet", + BASE_GOERLI = "base-goerli", + BASE_SEPOLIA = "base-sepolia", + ZKSYNC_MAINNET = "zksync-mainnet", + ZKSYNC_SEPOLIA = "zksync-sepolia", +} diff --git a/src/libs/clients.ts b/src/libs/clients.ts new file mode 100644 index 0000000..b0255da --- /dev/null +++ b/src/libs/clients.ts @@ -0,0 +1,41 @@ +import { AxiosInstance } from "axios"; +import { createWalletClient, http, publicActions, custom, createPublicClient } from "viem"; +import * as chains from "viem/chains"; +import getEnv from "src/env"; +import { Network as AlchemyNetwork } from "src/libs/alchemy"; + +type ChainIdToAlchemyNetworksMap = { + [prop: number]: AlchemyNetwork; +}; + +const chainIdToAlchemyNetworksMap: ChainIdToAlchemyNetworksMap = { + [chains.mainnet.id]: AlchemyNetwork.ETH_MAINNET, +}; + +const transport = (chain: chains.Chain, proxy?: AxiosInstance) => { + if (!proxy) { + return http(); + } + + return custom({ + async request(body: { method: string; params: any[] }) { + const response = await proxy.post( + `https://${chainIdToAlchemyNetworksMap[chain.id]}.g.alchemy.com/v2/${getEnv("ALCHEMY_RPC_KEY")}`, + body, + ); + return response.data; + }, + }); +}; + +export const getPublicClient = (chain: chains.Chain) => + createPublicClient({ + chain, + transport: http(), + }); + +export const getClient = (chain: chains.Chain, proxy: AxiosInstance) => + createWalletClient({ + chain, + transport: transport(chain, proxy), + }).extend(publicActions); diff --git a/src/libs/local-evm-client.ts b/src/libs/local-evm-client.ts deleted file mode 100644 index c2391e0..0000000 --- a/src/libs/local-evm-client.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { createWalletClient, http, publicActions, custom } from "viem"; -import { Chain, mainnet } from "viem/chains"; - -const transport = (chain: Chain) => { - if (chain.id === mainnet.id) { - return http(); - } - return custom({ - async request({ method, params }) { - // const response = await customRpc.request(method, params); - // return response; - console.log(method, params); - }, - }); -}; - -export const getClient = (chain: Chain) => - createWalletClient({ - chain, - transport: transport(chain), - }).extend(publicActions); diff --git a/src/libs/validations.ts b/src/libs/validations.ts index 6973d85..b8e182f 100644 --- a/src/libs/validations.ts +++ b/src/libs/validations.ts @@ -9,7 +9,7 @@ import Joi from "joi"; export const validateMKey = (input: string) => { Joi.attempt( input, - Joi.string().pattern(new RegExp("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$")).required(), + Joi.string().pattern(new RegExp("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-.]).{8,}$")).required(), "minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character", ); return input; diff --git a/src/packages/fuel/deposit/index.ts b/src/packages/fuel/deposit/index.ts index 43225bc..c12d3f5 100644 --- a/src/packages/fuel/deposit/index.ts +++ b/src/packages/fuel/deposit/index.ts @@ -5,8 +5,9 @@ import { getContract, zeroAddress } from "viem"; import { privateKeyToAccount } from "viem/accounts"; import * as chains from "viem/chains"; import { abi } from "./abi"; +import { getClient, getPublicClient } from "src/libs/clients"; import { decryptMarkedFields } from "src/libs/crypt"; -import { getClient } from "src/libs/local-evm-client"; +import { refreshProxy } from "src/libs/proxify"; import { Profile } from "src/types/profile"; // export async function tx(network: string) { @@ -30,14 +31,18 @@ function getDecodedEVM(profiles: Profile, masterKey: string) { type EVMWallet = Profile["string"]["wallets"]["evm"]; -const checkAndDeposit = (client: ReturnType, evmWallet: EVMWallet) => async () => { +const checkAndDeposit = (evmWallet: EVMWallet) => async () => { const pk = evmWallet.pkᵻ as `0x${string}`; const account = privateKeyToAccount(pk); + const axiosInstance = await refreshProxy(); + const publicClient = getPublicClient(chains.mainnet); + const walletClient = getClient(chains.mainnet, axiosInstance); + const contract = getContract({ address: "0x19b5cc75846BF6286d599ec116536a333C4C2c14", abi, - client, + client: { public: publicClient, wallet: walletClient }, }); const userBalance = await contract.read.getBalance([account.address, zeroAddress]); @@ -54,9 +59,8 @@ const checkAndDeposit = (client: ReturnType, evmWallet: EVMWal export function deposit(masterKey: string) { const profiles = JSON.parse(readFileSync(resolve(".", ".profiles.json"), "utf-8")) as Profile; - const client = getClient(chains.mainnet); return [getDecodedEVM(profiles, masterKey)[0]].reduce((promise, accountData) => { - return promise.then(checkAndDeposit(client, accountData)); + return promise.then(checkAndDeposit(accountData)); }, Promise.resolve()); } diff --git a/src/packages/scroll/report/index.ts b/src/packages/scroll/report/index.ts index a496b6f..033a33c 100644 --- a/src/packages/scroll/report/index.ts +++ b/src/packages/scroll/report/index.ts @@ -9,7 +9,7 @@ import { Profile } from "src/types/profile"; const refreshAndCall = (index: string, address: string) => async (report: any) => { try { - const axiosInstance = await refreshProxy(20000); + const axiosInstance = await refreshProxy(); axiosRetry(axiosInstance, { retries: 3, diff --git a/src/packages/tx/index.ts b/src/packages/tx/index.ts deleted file mode 100644 index 4fa2365..0000000 --- a/src/packages/tx/index.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { parseEther } from "viem"; -import { privateKeyToAccount } from "viem/accounts"; -import * as chains from "viem/chains"; -import { getClient } from "src/libs/local-evm-client"; - -type SupportedNetworks = keyof typeof chains; - -export async function tx(network: string) { - const client = getClient(chains[network as SupportedNetworks] || chains.mainnet); - const account = privateKeyToAccount("0xc7d9d44df8fa6567e30963cecc0d91b3b798c08ebdee6c30b66ad63dff0eece7"); - - const hash = await client.sendTransaction({ - account, - to: "0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC", - value: parseEther("0.001"), - }); - - return hash; -}