1

I am trying to call a function of an already deployed contract from within another contract.

import "./TutorialToken.sol";
contract Sellers
{ 
  TutorialToken _tt;

  constructor(address _f) public
  {
      _tt = TutorialToken(_f);
  }

  _tt.f();
}

However when I try and truffle migrate I get an error "Sellers" -- Invalid number of parameters for "undefined". Got 0 expected 1!

I am confused on how I call the function of an already deployed contract from within another contract.

EDIT

initial deployment

const Migrations = artifacts.require('./Migrations.sol')

module.exports = function (deployer) {
  deployer.deploy(Migrations)
}

deploying contracts

const TutorialToken = artifacts.require('./TutorialToken.sol')
const Sellers = artifacts.require('./Sellers.sol')

module.exports = function (deployer) {
  deployer.deploy(TutorialToken),
  deployer.deploy(Sellers)
}

Migrations.sol

pragma solidity ^0.5.0;

contract Migrations {
address public owner;
uint public last_completed_migration;

modifier restricted() {
    if (msg.sender == owner) _;
}

constructor() public {
    owner = msg.sender;
}

function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
}

function upgrade(address new_address) public restricted {
    Migrations upgraded = Migrations(new_address);
    upgraded.setCompleted(last_completed_migration);
}

}

  • What is this incomplete piece of code? Please provide a minimal working example, including both contracts and the function from which _tt.f() is invoked. Your code here will not compile! – goodvibration Jan 8 at 15:59
  • Also, in order to answer your question, what we would really need to see is your Truffle migration script, which is not even present here. – goodvibration Jan 8 at 16:00
  • I am aware this won't compile, I have taken code out in order to simplify the question. It doesn't matter what the contract TutorialToken is or what the function it is calling is, my question is about how in general one calls a method from a contract that has been already deployed, rather than making a new instance of the contract. I have updated my question to show my initial migration script and the contract deployments. – Chris Johnston Jan 8 at 16:05
  • You are trying to deploy Sellers without passing constructor arguments. – goodvibration Jan 8 at 16:11
2

You are trying to deploy the Sellers without passing constructor arguments.

You also have a suspicious comma in that piece of code up there.

Change this:

deployer.deploy(TutorialToken), // what's this comma for?
deployer.deploy(Sellers)

To this:

deployer.deploy(TutorialToken).then(function() {
  return deployer.deploy(Sellers, TutorialToken.address);
});

See Truffle documentation for more details.

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.