Etherlime
Search…
Migration from Truffle to Etherlime

Migration from Truffle to Etherlime

Install & Initialize Etherlime

1
npm i -g etherlime
Copied!
Install the global etherlime to allow you to run etherlime commands.
1
etherlime init
Copied!
The command will add to your project structure the following parts:
  • ./contracts/LimeFactory.sol
    • ./deployment/deploy.js
    • ./test/exampleTest.js
Note! These are added just to give you an example. You can remove them.

Write new scripts for deployment using the template provided

  • require etherlime module
  • require the compiled contract from ./build folder not the
    contract itself
with Truffle :
1
const LimeFactory = artifacts.require("./LimeFactory.sol");
Copied!
with Etherlime :
1
const etherlime = require('etherlime-lib')
2
const LimeFactory = require('../build/LimeFactory.json');
Copied!
  • set the deployer and then deploy the contract
Local deployment with Etherlime :
1
const etherlime = require('etherlime-lib')
2
const LimeFactory = require('../build/LimeFactory.json');
3
const InterfaceFactory = require('../build/InterfaceFactory.json')
4
5
const deployer = new etherlime.EtherlimeGanacheDeployer();
6
const limeFactory = await deployer.deploy(LimeFactory);
7
8
//example how to wrap deployed contract and to pass its address
9
const contractInstance = await etherlime.ContractAt(InterfaceFactory,
10
limeFactory.contractAddress)
Copied!
Find more examples for deployment here.

Modify tests

In order to modify the tests from Truffle to Etherlime, slight changes are needed to be done:
with Truffle :
1
const LimeFactory = artifacts.require("./LimeFactory.sol");
2
3
contract('LimeFactory tests', async (accounts) => {
4
5
let owner = accounts[0];
6
7
beforeEach(async function() {
8
limeFactory = await LimeFactory.new();
9
});
10
11
it('should do something', () => {
12
13
})
14
}
Copied!
with Etherlime :
1
// step1: require Etherlime module
2
const etherlime = require('etherlime-lib')
3
4
// step2: require compiled contract from ./build,
5
// not the .sol file (as in deployment scripts)
6
const LimeFactory = require('../build/LimeFactory.json')
7
8
9
// step4: replace 'contract' descriptor to 'describe',
10
// then remove (accounts) param in async function
11
describe('LimeFactory tests', async () => {
12
13
// step5: initialize account
14
let owner = accounts[0];
15
16
// step6: set the deployer in before/beforeEach
17
// and fix the deployment scripts as we did before
18
beforeEach(async function() {
19
20
deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
21
limeFactory = await deployer.deploy(LimeFactory);
22
23
});
24
25
it('should do something', () => {
26
27
})
28
})
Copied!
1
# Flexibility
Copied!
  • **in case you want to use an address of an account, you must extend
    it to** let owner = accounts[0].signer.address
  • **when a contract’s method is called, the default sender is set to
    accounts[0]. If you want to execute it from another account,
    replace** {from: anotherAccount} object with
    .from(anotherAccount).
with Truffle :
1
await limeFactory.createLime(newLime' 0, 10, 12, {from: accounts[1]})
Copied!
with Etherlime :
1
await limeFactory.from(2).createLime('newLime' 0, 10, 12);
2
3
// as a param you may also use:
4
await limeFactory.from(accounts[1]).createLime('newLime' 0, 10, 12);
5
await limeFactory.from(accounts[1].signer).createLime('newLime' 0, 10, 12);
6
await limeFactory.from(accounts[1].signer.address).createLime('newLime' 0, 10, 12);
7
await limeFactory.from(customSigner).createLime('newLime' 0, 10, 12);
Copied!
  • **when you need to execute payable function, pass the value as an
    object** contract.somePayableFunction(arg1, arg2, {value: 100})
  • don't use “.call” when calling view functions.
  • to timeTravel - replace web3 increaseTime with global options
    utils.timeTravel(provider, seconds)

Assertions and available utils

For more convenience Etherlime provides some additional assertions and global utils object:
assert it is an address :
1
it('should be valid address', async () => {
2
assert.isAddress(limeFactory.contractAddress, "The contract was not deployed");
3
})
Copied!
assert a function revert :
1
it('should revert if try to create lime with 0 carbohydrates', async () => {
2
let carbohydrates = 0;
3
await assert.revert(limeFactoryInstance.createLime("newLime2", carbohydrates, 8, 2),
4
"Carbohydrates are not set to 0");
5
});
Copied!
test an event
with Truffle:
1
let expectedEvent = 'FreshLime';
2
let result = await limeFactory.createLime('newLime' 8, 10, 12);
3
assert.lengthOf(result.logs, 1, "There should be 1 event emitted from new product!");
4
assert.strictEqual(result.logs[0].event, expectedEvent,
5
`The event emitted was ${result.logs[0].event} instead of ${expectedEvent}`);
Copied!
with Etherlime
1
let expectedEvent = 'FreshLime'
2
let transaction = await limeFactory.createLime('newLime' 8, 10, 12);
3
const transactionReceipt = await limeFactory.verboseWaitForTransaction(transaction)
4
5
// check the transaction has such an event
6
let isEmitted = utils.hasEvent(transactionReceipt, LimeFactory, expectedEvent);
7
assert(isEmitted, 'Event FreshLime was not emitted');
8
9
// parse logs
10
let logs = utils.parseLogs(transactionReceipt, LimeFactory, expectedEvent);
11
assert.equal(logs[0].name, 'newLime, "LimeFactory" with name "newLime" was not created');
Copied!
Find more test examples here.

Final steps:

  • delete ./migrations folder
  • delete truffle.js/truffle-config.js file
  • delete truffle from package.json
  • delete node_modules
  • run npm install
  • open a fresh terminal tab and enter etherlime ganache
  • run etherlime test