Skip to content

Commit

Permalink
Merge #1286
Browse files Browse the repository at this point in the history
1286: Basic CLI util to transfer ETH r=popzxc a=StanislavBreadless

A tool that takes the following parameters:

`-pk, --private-key <private-key>` -- the `0x`-prefixed private key of the sender.
`-t, --target <target>` -- the `0x`-prefixed Ethereum address of the recipient.
`-n, --network <network>` -- the Ethereum network.
`-a, --amount <amount>` -- the amount of the Ethereum to be sent.

Tested on Rinkeby. It works.

Co-authored-by: Stanislav Bezkorovainyi <[email protected]>
  • Loading branch information
2 parents 2526ccc + 7fbce3e commit 176362d
Show file tree
Hide file tree
Showing 5 changed files with 84 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"
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"infrastructure/zcli",
"infrastructure/explorer",
"infrastructure/zk",
"infrastructure/eth_transfer",
"core/tests/ts-tests"
],
"nohoist": [
Expand Down

0 comments on commit 176362d

Please sign in to comment.