Solidity – Using async/await Functions in Truffle Migrations

soliditytruffletruffle-migration

I followed the below links

http://y-nakajo.hatenablog.com/entry/2018/01/15/174743

Truffle post-deployment actions

and tried to create a migrations file as below.

const string_lib = artifacts.require('strings.sol');
const safemath_lib = artificats.require('safemath.sol');
const PublishService_contract = artifacts.require('PublishService.sol');

module.exports = function(deployer) {
    deployer.then(async () => {
        await deployer.deploy(string_lib);
        await deployer.deploy(safemath_lib);
    });
    deployer.deploy(PublishService_contract);
    deployer.link(string_lib, PublishService_contract);
    deployer.link(safemath_lib, PublishService_contract);
};

The publish service file here is dependent on strings and safemath libraries. But it gives the below error:

deployer.then(async () => {
^

SyntaxError: Unexpected token (

can someone help me with this?

Even I tried to do it this way, without async calls

const string_lib = artifacts.require('strings.sol');
const safemath_lib = artifacts.require('SafeMath.sol');
const PublishService_contract = artifacts.require('PublishService.sol');

module.exports = function(deployer) {
        deployer.deploy(string_lib).then(() => {  
        deployer.deploy(safemath_lib).then(() => {
                deployer.deploy(PublishService_contract);
        });
    });
    deployer.link(string_lib, PublishService_contract);
    deployer.link(safemath_lib, PublishService_contract);
};

But this also resulted in an error.

Error encountered, bailing. Network state unknown. Review successful
transactions manually. Error: Cannot link library: SafeMath has no
address. Has it been deployed?

Here is the complete console output.

Running migration: 1_initial_migration.js
  Replacing Migrations...
  ... 0xb60508071fa5b0ba282da1c1b8bce5ed2157fff096292e73550bf357f7468f5b
  Migrations: 0xf633f429e089a049cd29189c0505336e69e88173
Saving successful migration to network...
  ... 0x3ad495b3643e84b769a79cdeafe88c158d9f13653f773f6dc074b99bf1a688ee
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Replacing strings...
  ... 0x314516c05d9733925df2832cd8a411af8d0694f89b1050e3c0b2403f4af5c48a
  strings: 0x13e9875bb07843af3f360e4fd274626ba2ba66ef
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: Cannot link library: SafeMath has no address. Has it been deployed?
    at Object.link (/usr/lib/node_modules/truffle/build/webpack:/~/truffle-deployer/src/linker.js:25:1)
    at /usr/lib/node_modules/truffle/build/webpack:/~/truffle-deployer/src/actions/link.js:5:1
    at /usr/lib/node_modules/truffle/build/webpack:/~/truffle-deployer/src/deferredchain.js:20:1
    at process._tickCallback (internal/process/next_tick.js:109:7)

Best Answer

In your first attempt, async functions may not be used with your browser. Just try to stack promises.

Your second attempt it is just a javascript async and sync problem. promises are always async so you need to wait for them to finish then call the deployer.link :

const string_lib = artifacts.require('strings.sol');
const safemath_lib = artifacts.require('SafeMath.sol');
const PublishService_contract = artifacts.require('PublishService.sol');

module.exports = function(deployer) {
    deployer.deploy(string_lib).then(() => {
        deployer.deploy(safemath_lib).then(() => {
            deployer.deploy(PublishService_contract).then(()=>{
                deployer.link(string_lib, PublishService_contract);
                deployer.link(safemath_lib, PublishService_contract);
            });

        });
    });
};
Related Topic