2

On this Truffle web page you see the following code to deploy a smart contract with Web3.js:

http://truffleframework.com/docs/advanced/build_processes

// Step 1: Get a contract into my application
var json = require("./build/contracts/MyContract.json");

// Step 2: Turn that contract into an abstraction I can use
var contract = require("truffle-contract");
var MyContract = contract(json);

// Step 3: Provision the contract with a web3 provider
MyContract.setProvider(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));

// Step 4: Use the contract!
MyContract.deployed().then(function(deployed) {
  return deployed.someFunction();
});

What would this code look like if MyContract.json was dependent on another smart contract? For example, given a 2_deploy_contracts.js Truffle deployment script file like this:

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

module.exports = function(deployer) {
  deployer.deploy(ConvertLib);
  deployer.link(ConvertLib, MetaCoin);
  deployer.deploy(MetaCoin);
  // Deploy the voting smart contract.
  deployer.deploy(Voting);
};

My questions are:

  • I assume I would need to repeat the code involved with Steps 1-3 in the code above for each of my smart contracts? (i.e. - ConvertLib/MetaCoin/Voting).

  • Are there any other changes that I would need to make so that dependent code is available to the smart contracts that depend on it? For example, making sure the ConvertLib contract code is available to the MetaCoin contract code?

  • What is the Javascript equivalent of the deployer.link() statement?

1

I assume I would need to repeat the code involved with Steps 1-3 in the code above for each of my smart contracts? (i.e. - ConvertLib/MetaCoin/Voting).

If your Voting contract is dependent on MetaCoin contract, let's say if your Voting contract needed to MetaCoin to be already deployed and use it address in the Voting constructor, so the answer is: yes, I suggest you create another deploy file for each contract.

Are there any other changes that I would need to make so that dependent code is available to the smart contracts that depend on it? For example, making sure the ConvertLib contract code is available to the MetaCoin contract code?

In that case, it works for libraries AFAIK because the MetaCoin needs the Library to be deployed before, so the linking will basic assign an address of library to your contract. But truffle does some handling to make sure that assigning works. Interesting post about is library-driven-development-in-solidity.

What is the Javascript equivalent of the deployer.link() statement?

Let's say you have a third file called 3_deploy_voting.js and it is dependent on MetaCoin to be deployed.

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

module.exports = function(deployer) {
  deployer.deploy(Voting, MetaCoin.address);
};

Considering the previous migration already deployed the MetaCoin the address is already available to use and your Voting contract constructor looks like:

function Voting(address metaCoinAddress) public {...

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.