Buidler, Waffle & Ethers
Tags: ethereum
Published: 2020-08-06 16:26:39

---
title: Buidler, Waffle & Ethers
published: true
date: 2020-07-27 12:58:54 UTC
tags: Ethereum,ethereum
canonical_url: https://blog.coincodecap.com/buidler-waffle-ethers?utm_source=rss&utm_medium=rss&utm_campaign=buidler-waffle-ethers
---

Lately, at [Balancer](https://balancer.finance/) we’ve moved from the Truffle development environment to using Buidler, Waffle, and Ethers. The main benefit is being able to use console.log in Solidity during debugging — it’s amazing how much of a difference this makes and for this alone the change over is worth it. Here are some notes I made during the switch over.

### Ethers

The ethers.js library aims to be a complete and compact library for interacting with the [Ethereum](https://blog.coincodecap.com/tag/ethreuem) Blockchain and its ecosystem.

Documentation is here: [https://docs.ethers.io](https://docs.ethers.io/) and this [Web3.js vs Ethers.js guide](https://github.com/adrianmcli/web3-vs-ethers) was useful.

The following gist demonstrates some basic usage of Ethers that creates an instance of a deployed contract and then running some calls against it:

<script src="https://gist.github.com/johngrantuk/e14773dd466c8cd1bfcc1a4a94fe477c.js"></script><noscript><a href="https://gist.github.com/johngrantuk/e14773dd466c8cd1bfcc1a4a94fe477c">View this gist on GitHub</a></noscript>

### Buidler & Waffle

Buidler is described as a ‘task runner’. I think its easiest to see it as a swap for Truffle/Ganache. It has lots of different [plugins](https://buidler.dev/plugins/) that make it really useful and its [documentation](https://buidler.dev/getting-started/) was refreshingly good.

The [Quickstart](https://buidler.dev/getting-started/#quick-start) shows you how to install and how to run common tasks. It also uses [Waffle](https://getwaffle.io/) for testing. Waffle is a simple smart contract testing library built on top of Ethers.js. Tests in Waffle are written using Mocha alongside with Chai and from my experience everything just worked. The docs are [here](https://ethereum-waffle.readthedocs.io/en/latest/). And its worth digging in to see some of the useful things it offers such as [Chai Matchers](https://ethereum-waffle.readthedocs.io/en/latest/matchers.html) which allow you to test things like reverts, events, etc.

Buidler commands I found I used a lot:

- Run the local Buidler EVM: `$ npx buidler node`
- Compile project contracts: `$ npx buidler compile`
- Run tests: `$ npx buidler test ./test/testfile.ts`

Here’s an example test file I used that demonstrates a few useful things:

<script src="https://gist.githubusercontent.com/johngrantuk/30d41d88b9da7f6412b766f2fa988b3c.js"></script><noscript><a href="https://gist.githubusercontent.com/johngrantuk/30d41d88b9da7f6412b766f2fa988b3c">View this gist on GitHub</a></noscript>

### Static Calls

`let poolAddr = await factory.callStatic.newBPool();` – The contract [callStatic](https://docs.ethers.io/v5/api/contract/contract/#contract-callStatic) pretends that a call is not state-changing and returns the result. This does not actually change any state and is free.

### Connecting Different Accounts

`await _pools[1].connect(newUserSigner).approve(PROXY, MAX);` – Using contract [connect(signer)](https://docs.ethers.io/v5/api/contract/contract/#Contract-connect) calls the contract via the signer specified.

### Gas Costs

```
await proxy.connect(newUserSigner).exitswapExternAmountOut(
            \_POOLS[1],
            MKR,
            amountOut,
            startingBptBalance,
            {
              gasPrice: 0
            }
        );
```

Setting the gasPrice to 0 like above allows me to run the transaction without spending any Eth on it. This was useful when checking Eth balance changes without having to worry about gas costs.

### Custom accounts & balances

```
const config: BuidlerConfig = {
  solc: {
    version: "0.5.12",
    optimizer: {
      enabled: true,
      runs: 200,
    },
  },
  networks: {
    buidlerevm: {
      blockGasLimit: 20000000,
      accounts: [
        { privateKey: '0xPrefixedPrivateKey1', balance: '1000000000000000000000000000000' },
        { privateKey: '0xPrefixedPrivateKey2', balance: '1000000000000000000000000000000' }
      ]
    },
  },
};
```

I needed the test accounts to have more than the 1000Eth balance set by default. In [buidler.config.ts](https://buidler.dev/config/) you can add accounts with custom balances like above.

### Deploying

[Deploying](https://buidler.dev/guides/deploying.html) is done using scripts. First I updated my [buidler.config.ts](https://buidler.dev/config/) with the account/key for Kovan that will be used to deploy (i.e. must have Eth):

```
const config: BuidlerConfig = {
  solc: {
    version: "0.5.12",
    optimizer: {
      enabled: true,
      runs: 200,
    },
  },
  networks: {
    buidlerevm: {
      blockGasLimit: 20000000,
    }
    kovan: {
      url: `https://kovan.infura.io/v3/${process.env.INFURA}`,
      accounts: [`${process.env.KEY}`]
    }
  },
};
```

Then I wrote a deploy-script.js:

```
async function main() {
  // We get the contract to deploy
  const ExchangeProxy = await ethers.getContractFactory("ExchangeProxy");
  const WETH = '0xd0A1E359811322d97991E03f863a0C30C2cF029C';
  const exchangeProxy = await ExchangeProxy.deploy(WETH); await exchangeProxy.deployed(); console.log("Proxy deployed to:", exchangeProxy.address);
}main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });
```

Then run this using: `npx buidler run --network kovan deploy-script.js`

### 🎉 Console Logging 🎉

One of the holy grails of Solidity development and so easy to setup in this case! There are also Solidity stack traces and error messages but unfortunately there was a bug that caused this not to work for our contracts.

To get this going all you need to do is add: `import "@nomiclabs/buidler/console.sol";` at the top of your contract then use console.log. More details on what kind of outputs, etc it supports are [here](https://buidler.dev/buidler-evm/#console-log). Lifesaver!

Hope some of this was helpful and you enjoy using it as much as me.

**Also Read**

- [Beacon Chain: Heart of Ethereum 2.0](https://blog.coincodecap.com/ethereum-beacon-chain)
- [Ethereum Smart Contracts and the Oracle Problem](https://blog.coincodecap.com/ethereum-smart-contracts-and-the-oracle-problem)
- [How to Generate Random Numbers on Ethereum Using VRF](https://blog.coincodecap.com/how-to-generate-random-numbers-on-ethereum-using-vrf)
- [MarkerDAO: Getting Started with Dai Stable Coin](https://blog.coincodecap.com/markerdao-getting-started-with-dai-stable-coin)
- [Development Analysis Of ARK Project](https://blog.coincodecap.com/development-analysis-of-ark-project-new)
- [Development analysis of the Fantom Project](https://blog.coincodecap.com/development-analysis-of-the-fantom-cryptocurrency)
- [Proposing Future Ethereum Access Control](https://blog.coincodecap.com/proposing-future-ethereum-access-control)
- [Sharding: The brain of Ethereum 2.0](https://blog.coincodecap.com/sharding-in-ethereum)

The post [Buidler, Waffle & Ethers](https://blog.coincodecap.com/buidler-waffle-ethers) appeared first on [CoinCodeCap Blog](https://blog.coincodecap.com).