1

To automate my development cycle I want to save the addresses of deployed contracts so that my DApp can read this information and know where to find the contracts on my private block chain:

for example, this is what my truffle migration script looks like:

const fs  = require('fs');
var  FunnyToken = artifacts.require("FunnyToken");
var  TokenSale = artifacts.require("TokenSale");
var storage = {};
module.exports  = function(deployer, network, accounts) {
    deployer.deploy(FunnyToken, 1000000000000)
        .then(function(){
            storage.TOKEN_ADDRESS = FunnyToken.address;
            deployer.deploy(TokenSale, 1, accounts[0], FunnyToken.address).then(function () {
                storage.SALE_ADDRESS = TokenSale.address;
            });
        });

    console.log("adresses:")
    console.log(storage)
    fs.writeFileSync('.address.json',JSON.stringify(storage), 'utf-8');
};

But the .address.json generated this way contains simply an empty object: {}.

On the DApp side I want to connect to the Contract's address

web3.eth.contract(address=contract_address, abi=contract_abi)

I should also mention that I am Using Python and web3.py to write the DApp. So that, ideally, the address information should be stored in a file to be read by the Python application.

What is the recommended pattern for this?

The code for the Python DApp (Including the solution proposed)

2

Truffle creates .json files inside the build/ directory. There's a file for each contract, they contain the address of the deployed contract. You have build/contracts/MetaCoin.json inside there's a section "networks" that is a dictionary that maps network id to contract address.

You can use your language of choice to process this files an generate an configuration file with the addreses you want.

Another option is to add a migration script that will run after all previous stages this script will write the addresses of the contract to a configuration file of your choice.

Create a file named 9_final_step.js in the migrations directory

var ConvertLib = artifacts.require("./ConvertLib.sol");
var MetaCoin = artifacts.require("./MetaCoin.sol");

module.exports = function(deployer) {
  // Output to console or a configuration file
  console.log({
    MetaCoin: MetaCoin.address,
    ConvertLib: ConvertLib.address,
  });
};
  • The json files inside build/contracts are the result of contract compilation no deployment. thus they contain no information about the adress to which the contract is deployed to. – fccoelho Oct 18 '18 at 16:04
  • I will update the example with an illustration of why this does not work – fccoelho Oct 18 '18 at 16:09
  • I use that feature all the time, during migrations the artifacts will update with deployment address. At the end of the artifacts there's a "networks" section that contain the deployment address. – Ismael Oct 18 '18 at 16:44
  • 1
    @fccoelho It might be there's a bug in your migrations scripts that causes truffle to fail to update contracts artifacts. – Ismael Oct 18 '18 at 16:48
  • you are right there is a bug in the deployment of the TokenSale contract which stops truffle from updating the artifacts. – fccoelho Oct 18 '18 at 17:29
1

Beside the fact that Truffle updates the JSON files, your .address.json contains only a {} because you fs.writeFileSync too early.

You see, the deployer thingy you wrote is a promise chain and the storage info is ready only when all .thens have fired. So you have to put your fs.writeFileSync after all have fired. Notice the many returns:


return deployer.deploy(FunnyToken, 1000000000000)
    .then(() => {
        storage.TOKEN_ADDRESS = FunnyToken.address;
        return deployer.deploy(TokenSale, 1,...);
    })
    .then(() => {
        storage.SALE_ADDRESS = TokenSale.address;
        fs.writeFileSync('.address.json',JSON.stringify(storage), 'utf-8');
    });
  • Yes, I noticed that after a while. In any case, I decided to use the JSON files instead of generating a new file. Thanks. – fccoelho Oct 19 '18 at 15:31

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.