0

I am developing a back-end DApp using truffle and nodejs on a private network. My application call Contract.deployed() and print the address of the obtained Contract. When I call truffle migrate --reset, my app still get the old contract. So here is the test:

  1. I deploy the the contract
  2. I start my application
  3. I migrate --reset the contract
  4. My application execute Contract.deployed() and gets the old version of the contract.

So here is the code :

For the example, I used a simple contract provided with the truffle toolbox.

truffle unbox metacoin 
truffle deploy

   Deploying 'MetaCoin'
   --------------------
   > transaction hash:    0xf8c0dbc30008e48a023bbb5678773b8220563520f73a38cdfbe5fbef59c19253
   > Blocks: 0            Seconds: 0
   > contract address:    0xeE5d7863B98eB793f6d2b77d51FC0fD2474fE55f
   > account:             0xed9d02e382b34818e88B88a309c7fe71E65f419d
   > balance:             1000000000
   > gas used:            358572
   > gas price:           0 gwei
   > value sent:          0 ETH
   > total cost:          0 ETH

   > Saving artifacts
   -------------------------------------
   > Total cost:                   0 ETH

And I have a small software that query the Metacoin.deployed() address every 5 second. I added a prompt to debug and see if I can update the address manually

const WEB3_PROVIDER = "http://localhost:22000";

let Web3        = require('web3'),
    contract    = require("truffle-contract"),
    path        = require('path'),
    MetaCoinAbi = require(path.join(__dirname, "../smartContract/build/contracts/MetaCoin"));

let provider    = new Web3.providers.HttpProvider(WEB3_PROVIDER);
let MetaCoin = contract(MetaCoinAbi);
MetaCoin.setProvider(provider);
let metaCoinAddress;

const readline = require('readline');

function status(){

    let metaCoinPromise;
    if (typeof metaCoinAddress !== 'undefined'){
        metaCoinPromise = MetaCoin.at(metaCoinAddress);
    }else{
        metaCoinPromise = MetaCoin.deployed();
    }

    metaCoinPromise.then(async metaCoin=>{
        console.log("The deployed MetaCoin contract is at "+metaCoin.address)
    }).catch((error) => {
        console.error(error);
    });
}

// query the metacoin address every 5 sec
setInterval(status, 5000);

// command promt
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    prompt: '> '
});


rl.prompt("");
rl.on('line', (line) => {
  switch (line.trim().split(" ")[0]) {
    case 'address':
        metaCoinAddress = line.trim().split(" ")[1]
        console.log("metaCoinAddress is set at "+metaCoinAddress);
        break;
    default:
      console.log(`Unknown command '${line.trim()}'`);
      break;
  }
  rl.prompt();
}).on('close', () => {
  console.log('Closing');
  process.exit(0);
});

my package.json in case of

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "test.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "ethlint": "^1.2.3",
    "truffle-contract": "^4.0.4",
    "truffle-hdwallet-provider": "^1.0.4",
    "web3": "1.0.0-beta.37",
  }
}

Here is the output of the test

The deployed MetaCoin contract is at 0xeE5d7863B98eB793f6d2b77d51FC0fD2474fE55f
The deployed MetaCoin contract is at 0xeE5d7863B98eB793f6d2b77d51FC0fD2474fE55f
The deployed MetaCoin contract is at 0xeE5d7863B98eB793f6d2b77d51FC0fD2474fE55f
The deployed MetaCoin contract is at 0xeE5d7863B98eB793f6d2b77d51FC0fD2474fE55f
>> deploy the new contract with truffle deploy --reset >> output new contract at 0xaD0a14caC7eb984a7b7F1436Ce2ED79Bc7c2fF90
>> it should be the new address?
The deployed MetaCoin contract is at 0xeE5d7863B98eB793f6d2b77d51FC0fD2474fE55f
The deployed MetaCoin contract is at 0xeE5d7863B98eB793f6d2b77d51FC0fD2474fE55f
The deployed MetaCoin contract is at 0xeE5d7863B98eB793f6d2b77d51FC0fD2474fE55f

>> set the new Contract address to prove it can access it
>> address 0xaD0a14caC7eb984a7b7F1436Ce2ED79Bc7c2fF90
The deployed MetaCoin contract is at 0xaD0a14caC7eb984a7b7F1436Ce2ED79Bc7c2fF90
The deployed MetaCoin contract is at 0xaD0a14caC7eb984a7b7F1436Ce2ED79Bc7c2fF90
The deployed MetaCoin contract is at 0xaD0a14caC7eb984a7b7F1436Ce2ED79Bc7c2fF90

When I stop and restart my application it start with the new address. (0xaD0a14caC7eb984a7b7F1436Ce2ED79Bc7c2fF90)

Is it an expected behavior? Am I using it wrong?

0

This is how things are supposed to work. The contract's ABI is read only once at the beginning.

MetaCoinAbi = require(path.join(__dirname, "../smartContract/build/contracts/MetaCoin"));

Further changes are ignored.

For a dynamic behavior you have to re-read the ABI file and recreate the MetaCoin object.

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.