0

Please look at my simple example below. Is migration script 3 being executed, such that it sets the value on the blockchain -- I think not, because there is no speak about a txHash or cost...

How to make truffle do a transaction for migration script 3.

contracts/Test.sol

pragma solidity 0.5.8;

contract Test {

    uint256 c; 

    function read() public view returns ( uint256 ) {
      return c;
    }

    function write( uint256 _c ) public {
      c = _c;
    } 
}

Migrations scripts

$ cat migrations/1_initial_migration.js 
const Migrations = artifacts.require("Migrations");

module.exports = function(deployer) {
  deployer.deploy(Migrations);
};
$ cat migrations/2_deploy_contracts.js 
var Test = artifacts.require("Test");

module.exports = function(deployer) {
  deployer.deploy(Test);
};
$ cat migrations/3_set.js 
var Test = artifacts.require("Test");

module.exports = function(deployer) {

  Test.deployed().then( t => {
    t.write(1)
  })

};

Truffle output

truffle migrate --network goerli 

Compiling your contracts...
===========================
> Compiling ./contracts/Test.sol
> Artifacts written to test_sc/build/contracts
> Compiled successfully using:
   - solc: 0.5.8+commit.23d335f2.Emscripten.clang


Starting migrations...
======================
> Network name:    'goerli'
> Network id:      5
> Block gas limit: 0x7a1200


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0x778fd1772305c7425141ed0bd871e59c5c95ea07ac3e608dd40c7bdb30d8b3e1
   > Blocks: 0            Seconds: 12
   > contract address:    0x44f40AAFF065A1bAf4d9Dd204e22B5C419F1DC8f
   > block number:        1210809
   > block timestamp:     1567180517
   > account:             0x1ae222dCbEEC5c581F44d8bCC87e74fcfAB083Cb
   > balance:             0.063013755
   > gas used:            261393
   > gas price:           5 gwei
   > value sent:          0 ETH
   > total cost:          0.001306965 ETH

   Pausing for 2 confirmations...
   ------------------------------
   > confirmation number: 1 (block: 1210810)
   > confirmation number: 2 (block: 1210811)

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:         0.001306965 ETH


2_deploy_contracts.js
=====================

   Deploying 'Test'
   ----------------
   > transaction hash:    0x2a1cd709be9eaf1fefba8382a3e6c0a825fca72555beca76b4acdf868556996d
   > Blocks: 0            Seconds: 12
   > contract address:    0x74BdaE85cC50E3554Ae16cC7d09a83dAe503Ca41
   > block number:        1210813
   > block timestamp:     1567180577
   > account:             0x1ae222dCbEEC5c581F44d8bCC87e74fcfAB083Cb
   > balance:             0.062278565
   > gas used:            105015
   > gas price:           5 gwei
   > value sent:          0 ETH
   > total cost:          0.000525075 ETH

   Pausing for 2 confirmations...
   ------------------------------
   > confirmation number: 1 (block: 1210814)
   > confirmation number: 2 (block: 1210815)

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:         0.000525075 ETH


3_set.js
========

   > Saving migration to chain.
   -------------------------------------
   > Total cost:                   0 ETH

Look at that last line, didn't do it.

  • Change t => {t.write(1)} to async (t) => {await t.write(1)}. – goodvibration Aug 31 at 9:17
0

Part of your problem with your third script is that deployed() is async and you are not waiting for it to finish. Wrapping it in an async function and awaiting should make it work.

var Test = artifacts.require("Test");

module.exports = function(deployer) {
  deployer.then(async () => {
    const t = await Test.deployed();
    await t.write(1);
    console.log('Completed!');
  });
};

The other problem is that Truffle does not track operations that are not contract deployments. Even if the call has succeeded it will appear as it used 0 eth.

The console.log at the end will show a message if the transaction was successful.

1

Tou should do this in the second migration script, like:

var Test = artifacts.require("Test");

module.exports = function(deployer) {
  deployer.deploy(Test).then( (instance) => 
    instance.write(1);
)};

deploy function returns the contract instance (truffle-contract object). So, call its function after it is deployed.

P.S. This won't be shown in the console, but you can see tx on etherscan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.