Skip to content

Instantly share code, notes, and snippets.

@ryanio
Last active December 30, 2020 22:39
Show Gist options
  • Save ryanio/25d82d6127a23e50b035bc1734f72996 to your computer and use it in GitHub Desktop.
Save ryanio/25d82d6127a23e50b035bc1734f72996 to your computer and use it in GitHub Desktop.
Minting Dai in a Truffle Test Suite with Forked Mainnet
const { BN, ether, balance } = require("openzeppelin-test-helpers");
const { expect } = require("chai");
const { asyncForEach } = require("./utils");
// Artifacts
const ForceSend = artifacts.require("ForceSend");
// ABI
const erc20ABI = require("./abi/erc20");
// Dai
const daiAddress = "0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359";
const daiContract = new web3.eth.Contract(erc20ABI, daiAddress);
// Utils
const getDaiBalance = async account => {
return daiContract.methods.balanceOf(account).call();
};
contract("Truffle Mint Dai", async accounts => {
it("should send ether to the Dai contract", async () => {
// Send 1 eth to daiAddress to have gas to mint.
// Uses ForceSend contract, otherwise just sending
// a normal tx will revert.
const forceSend = await ForceSend.new();
await forceSend.go(daiAddress, { value: ether("1") });
const ethBalance = await balance.current(daiAddress);
expect(new BN(ethBalance)).to.be.bignumber.least(new BN(ether("1")));
});
it("should mint Dai for our first 5 generated accounts", async () => {
// Get 100 Dai for first 5 accounts
await asyncForEach(accounts.slice(0, 5), async account => {
// daiAddress is passed to ganache-cli with flag `--unlock`
// so we can use the `mint` method.
await daiContract.methods
.mint(account, ether("100").toString())
.send({ from: daiAddress });
const daiBalance = await getDaiBalance(account);
expect(new BN(daiBalance)).to.be.bignumber.least(ether("100"));
});
});
});
@ryanio
Copy link
Author

ryanio commented Nov 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment