From 4da64a5eb539cc71312d448b0ce557a4a46294f0 Mon Sep 17 00:00:00 2001 From: Stanislav Bezkorovainyi Date: Fri, 25 Dec 2020 16:26:00 +0200 Subject: [PATCH] Basic CLI util to transfer ETH --- infrastructure/eth_transfer/.gitignore | 1 + infrastructure/eth_transfer/package.json | 22 +++++++++++ infrastructure/eth_transfer/src/index.ts | 45 +++++++++++++++++++++++ infrastructure/eth_transfer/tsconfig.json | 15 ++++++++ 4 files changed, 83 insertions(+) create mode 100644 infrastructure/eth_transfer/.gitignore create mode 100644 infrastructure/eth_transfer/package.json create mode 100644 infrastructure/eth_transfer/src/index.ts create mode 100644 infrastructure/eth_transfer/tsconfig.json diff --git a/infrastructure/eth_transfer/.gitignore b/infrastructure/eth_transfer/.gitignore new file mode 100644 index 0000000000..796b96d1c4 --- /dev/null +++ b/infrastructure/eth_transfer/.gitignore @@ -0,0 +1 @@ +/build diff --git a/infrastructure/eth_transfer/package.json b/infrastructure/eth_transfer/package.json new file mode 100644 index 0000000000..976aa6717d --- /dev/null +++ b/infrastructure/eth_transfer/package.json @@ -0,0 +1,22 @@ +{ + "name": "eth_transfer", + "version": "0.0.1", + "description": "CLI tool for transfering ETH", + "main": "build/index.js", + "bin": "build/index.js", + "repository": "https://github.com/matter-labs/zksync-dev.git", + "author": "The Matter Labs Team", + "license": "MIT", + "scripts": { + "build": "tsc", + "watch": "tsc --watch", + "start": "node build/index.js" + }, + "devDependencies": { + "typescript": "^4.0.2" + }, + "dependencies": { + "commander": "^6.0.0", + "ethers": "^5.0.18" + } +} diff --git a/infrastructure/eth_transfer/src/index.ts b/infrastructure/eth_transfer/src/index.ts new file mode 100644 index 0000000000..0de5c9a7dc --- /dev/null +++ b/infrastructure/eth_transfer/src/index.ts @@ -0,0 +1,45 @@ +import { Command } from 'commander'; +import { ethers, Wallet } from 'ethers'; + +const program = new Command(); +program.version('0.0.1'); + +program + .option('-pk, --private-key ', 'private key of the sender') + .option('-t, --target ', 'address of the target account') + .option('-n, --network ', 'eth network') + .option('-a, --amount ', 'amount of the ETH to be sent'); + +program.parse(process.argv); + +function getProvider(network: string) { + if (network === 'localhost') { + return new ethers.providers.JsonRpcProvider('http://localhost:8545'); + } + + return ethers.providers.getDefaultProvider(network); +} + +async function main() { + const { privateKey, target, amount, network } = program; + + const provider = getProvider(network || 'mainnet'); + const wallet = new Wallet(privateKey).connect(provider); + + let tx = { + to: target, + value: ethers.utils.parseEther(amount) + }; + + try { + const txResponse = await wallet.sendTransaction(tx); + console.log('Transaction was sent! Hash: ', txResponse.hash); + } catch (err) { + console.log('Failed to send tx. Reason: ', err.message || err); + } +} + +main().catch((err: Error) => { + console.error('Error:', err.message || err); + process.exitCode = 1; +}); diff --git a/infrastructure/eth_transfer/tsconfig.json b/infrastructure/eth_transfer/tsconfig.json new file mode 100644 index 0000000000..f96df8d60e --- /dev/null +++ b/infrastructure/eth_transfer/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "target": "es2019", + "module": "commonjs", + "outDir": "build", + "strict": true, + "esModuleInterop": true, + "noEmitOnError": true, + "skipLibCheck": true, + "declaration": true + }, + "files": [ + "src/index.ts" + ] +}