Skip to content

Commit

Permalink
Basic CLI util to transfer ETH
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavBreadless committed Dec 25, 2020
1 parent 2526ccc commit 4da64a5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions infrastructure/eth_transfer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
22 changes: 22 additions & 0 deletions infrastructure/eth_transfer/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
45 changes: 45 additions & 0 deletions infrastructure/eth_transfer/src/index.ts
Original file line number Diff line number Diff line change
@@ -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>', 'private key of the sender')
.option('-t, --target <target>', 'address of the target account')
.option('-n, --network <network>', 'eth network')
.option('-a, --amount <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;
});
15 changes: 15 additions & 0 deletions infrastructure/eth_transfer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit 4da64a5

Please sign in to comment.