Oz-console 2.0 released (Ethers based console)

oz-console has been significantly updated, and now is able to use the OZ network configuration for simplicity.

Previous, the network configuration was a pain. Now it more deeply integrates with OZ CLI to pull from the OZ network config.

For example:

$ oz-console -n mainnet

And it will launch a console populated with Ethers.js instance of interfaces, contracts, etc. A signer will be provided that corresponds to the OZ ‘from’ address, or one can be passed in.

2 Likes

Hi @asselstine,

Thanks for sharing :pray:

Is there an example of how to use it? e.g. how to interact with a deployed contract.

Yes! I’ve updated the readme with some examples. I’ll add it here as well:

OpenZeppelin Ethers Console

Provides an Ethers.js based console to interact with your contracts.

Installation

$ yarn add oz-console

Usage

oz-console can be used from the command line or programmatically.

For example: let’s assume you have an ERC20 called MyToken that is deployed to mainnet.

Let’s interact with the contract:

# Contracts should be deployed and compiled
$ oz-console -n mainnet

Now we’re in oz-console:

// contracts contains Ethers.js Contract instance of all deployed contracts
mainnet> await contracts.MyToken.totalSupply()
// await here is redundant, but demonstrates that the syntax is supported

Or get the ether balance:

// provider and contracts are available in the environment
mainnet> provider.getBalance(contracts.MyToken.address)

Create contracts on-the-fly:

// ethers, signers, and artifacts are available in the enviroment
mainnet> let MyToken2 = new ethers.Contract('0x1234...', artifacts.MyToken.abi, signer)

For a complete reference of what variables are available in the global context see below.

CLI Help

Usage: oz-console [options]

Provides an Ethers.js based console to interact with your OpenZeppelin SDK project.  Supports await syntax.  Includes global variables:
  
  artifacts: Every project contract discovered, including ProxyAdmin
  interfaces: An Ethers interface for each artifact discovered
  contracts: An Ethers contract for each *deployed* artifact.  Includes ProxyAdmin.
  provider: an ethers provider
  signer: a signer for the configured OZ 'from' address
  ethers: the ethers lib

Options:
  -n, --network <network name>                  selects the openzeppelin network to use (default: "mainnet")
  -p, --projectConfig <oz project config path>  sets the project config path (default: ".openzeppelin/project.json")
  -v, --verbose                                 enable verbose logging.  useful for diagnosing errors
  -e, --exec <js file path>                     executes a javascript file instead of running a REPL
  -a, --address <from address>                  use the address as the signer
  -h, --help                                    shows this help

Programmatic Usage

Use buildContext to setup the environment programmatically

const { buildContext } = require('oz-console')

const context = buildContext({
  network: 'mainnet'
})

// Ethers
context.ethers
// OpenZeppelin CLI ProjectFile object
context.projectFile
// Artifact JSON blobs
context.artifacts
// Ethers Interfaces for each artifact
context.interfaces
// Ethers Contract for each deployed contract
context.contracts
// Ethers provider
context.provider
// Ethers signer for the OZ 'from' address
context.signer
// OpenZeppelin CLI NetworkFile object
context.networkFile
// OpenZeppelin CLi NetworkConfig object
context.networkConfig
1 Like

Hi @asselstine,

This is really awesome. :pray:

Using my favourite Box contract (https://docs.openzeppelin.com/learn/developing-smart-contracts#setting-up-a-solidity-project)

Box.sol

// contracts/Box.sol
pragma solidity ^0.5.0;

contract Box {
    uint256 private value;

    // Emitted when the stored value changes
    event ValueChanged(uint256 newValue);

    // Stores a new value in the contract
    function store(uint256 newValue) public {
        value = newValue;
        emit ValueChanged(newValue);
    }

    // Reads the last stored value
    function retrieve() public view returns (uint256) {
        return value;
    }
}

Deploy using the CLI

$ npx oz deploy
✓ Compiled contracts with solc 0.5.17 (commit.d19bba13)
? Choose the kind of deployment regular
? Pick a network development
? Pick a contract to deploy Box
✓ Deployed instance of Box
0xe78A0F7E598Cc8b0Bb87894B0F60dD2a88d6a8Ab

Interact using oz-console

Store and retrieve a value

$ npx oz-console -n development
development> await contracts.Box.store(42)
{ hash:
   '0x35495e4ce3c766bb08a985a4c1faa6df9f7b774bd6a3910fba04cbea38bf83c4',
  blockHash:
   '0xb283ae90984b48f2ede5f5177fc146af22bb14053c896f3eb6cc8ad5da185379',
  blockNumber: 2,
  transactionIndex: 0,
  confirmations: 1,
  from: '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1',
  gasPrice: BigNumber { _hex: '0x4a817c800' },
  gasLimit: BigNumber { _hex: '0xa611' },
  to: '0xe78A0F7E598Cc8b0Bb87894B0F60dD2a88d6a8Ab',
  value: BigNumber { _hex: '0x0' },
  nonce: 1,
  data:
   '0x6057361d000000000000000000000000000000000000000000000000000000000000002a',
  r:
   '0x12e7d0d272eb12e3f7a857a312c7035372f82faedbe378975b1f04e4f79b5db0',
  s:
   '0x00c1f20252e39d884a667c3955c744b5dc06f9af56a4d67eda8c9ac6937971f5',
  v: 38,
  creates: null,
  raw:
   '0xf887018504a817c80082a61194e78a0f7e598cc8b0bb87894b0f60dd2a88d6a8ab80a46057361d000000000000000000000000000000000000000000000000000000000000002a26a012e7d0d272eb12e3f7a857a312c7035372f82faedbe378975b1f04e4f79b5db09fc1f20252e39d884a667c3955c744b5dc06f9af56a4d67eda8c9ac6937971f5',
  networkId: 1,
  chainId: 1,
  wait: [Function] }
development> (await contracts.Box.retrieve()).toString()
'42'
1 Like

Easy peasy! I’m glad you like it. I find it to be an essential tool.

1 Like

Hi @asselstine,

I wanted to check the network ID of a ganache-cli quickly, so I installed and used oz-console.

$ npx oz-console -n development
development> provider.getNetwork()
{ chainId: 1588737945659, name: 'unknown' }

It is a great addition to my tool box. Thank you. :pray:
It looks like I was the first stargazer: https://github.com/pooltogether/oz-console/stargazers :star:

1 Like