eth-cli enables users to interact with contracts in any network regardless of how they were initially deployed.

Installing eth-cli

eth-cli is a collection of commands that help to develop Dapps for Ethereum and interact with contracts deployed in any blockchain network.

To install eth-cli, we need to run npm install -g eth-cli . This will expose the eth command that starts an interaction with eth-cli.

By running eth , you will see the list of available commands, with the most useful ones listed below:

eth randomAdress generates a random address and a private key pair. Optionally, you can pass an int argument to generate more than one pair.

generates a random address and a private key pair. Optionally, you can pass an argument to generate more than one pair. eth contract:deploy allows you to deploy a contract in any blockchain network. It needs the compiled version of the (.bin) contract and a private key.

allows you to deploy a contract in any blockchain network. It needs the compiled version of the contract and a private key. eth networks shows information about the most known networks.

shows information about the most known networks. eth repl is the most important one. This command is invoked with a contract’s address and ABI. It will provide you a web3.js contract instance that you can interact with.

Interacting with a deployed contract

eth-cli is a fantastic tool for interacting with any deployed contract in any blockchain network. You will only need the contract’s ABI version and its address.

Each time you deploy a contract, you need to save the address where it is deployed. Almost every existing tool for deploying contracts provides you with the address of the contract after it is created.

Let’s interact with a contract deployed on Ropsten. Its functionality is very simple. It will persist a phrase for any sender. For this example, we will deploy the contract and ABI from this Gist.

First, let’s run the command below. Note that ./MyPhrase.abi refers to the path of the contract’s ABI, and @address indicates where the contract is deployed.

eth repl --ropsten ./MyPhrase.abi@0x46C637aaF2aFfb2661561DBbDfCa4EA5C56ecfAB

Now, we have entered the repl mode. Let’s continue by running the command below.

myPhrase.methods.phrases(“0xDe75665F3BE46D696e5579628fA17b662e6fC04e”).call()

myPhrase is a web3.eth.Contract instance of our deployed contract. It will return the eth-cli just works string. Great! We have interacted with a deployed contract in a really simple way.

Now, we can send a transaction to the setPhrase method to persist in our own phrase. To do so, we need to exit from the current repl mode using Ctrl+d. Now, we can enter into repl again, but this time unlocking an account.

This time, we’ll change the contract state. This is why we need to unlock an account to prove that we are the owner, if not, anyone could act on our behalf and change the contract state from an address they do not own. In order to send the transaction, the account related to the private key provided to repl must have ether that will be used to pay for the transaction.

Remember that we can get an account and its private key by running the eth randomAddress command. Then, we have to send ether to that account.

If you want to try this example in Ropsten, you can charge your account through the Ropsten Ethereum Faucet. Enter the newly created address to charge your account.

eth repl --ropsten --pk 0x3480AD3FB5FD2BA5DDAFC274B8E2A8CF655FB17C6956FCD2E89C2E2F68456C37 ./MyPhrase.abi@0x46C637aaF2aFfb2661561DBbDfCa4EA5C56ecfAB

Note that we need to specify the private key starting with 0x . Now, we can send our transaction.