0

I have a contract that works fine when only using remix. I needed to test the block size and block number. here someone suggested me to use ganache which I did but now I don't get the output I'm supposed to get in this function:

function promiseRentalCompleting() public
         atCFact(C_facts.Requested)
         onlyBy(initiator)
         transitionNext(true)
         returns (address){
             rental.car=rental.carGroup.freeCars[0];
             DepositPaying depositPaying = new DepositPaying(address(this));
             return address(depositPaying);
         }

Basically I need this function to give me an address where I will deploy the DepositPaying contract but the decoded output at remix is coming out empty what was not happening before I added ganache. Should I be doing something different to accommodate the web3 provider?

(using remix on the browser chrome, got ganache from here, not using de command line)

1 Answer 1

1

You're overlooking the mining step.

Contract deployment is the result of a transaction, but first a transaction has to mine. In the interim, the first response from sending a transaction, which new DepositPaying(address(this)) does, is a transaction hash.

That response doesn't indicate that the transaction is processed. Only that it was sent and assigned a UID. The transaction hash doesn't tell you anything about the address - just the transaction id you're interested in. It's up to the client to watch for the transaction to be mined and then observe what happened because that's where the address is.

There is a confusing array of approaches to your problem.

  1. The Truffle framework has a sophisticated Migrations system that tracks the addresses of contracts it deployed for you. Assuming you did truffle migrate and your migration script did deployer.deploy(DepositPaying), then you can get the instance conveniently with instance = DepositPaying.deployed() and the address with instance.address. This works great if you expect to deploy one instance of the contract.
  2. You can use the Web3 API to deploy and .on('receipt) to fire a callback (more like a promise) when the receipt is available (because it was mined) and something like getTransactionReceipt(txnHash) which includes the address of a deployed contract, if any, that came from your transaction.
  3. You can use a contract factory pattern to track it yourself. Using a contract factory has advantages. You can maintain an index of all the addresses created by the factory, make that easy to inspect, employ access control so your contracts only talk to other "trusted" contracts and emit events. If your factory emits events, then the front-end can just listen to contract creation events.

Consider:

contract DepositPaying {
 ...
}

contract Factory {

  address[] public deployed;

  event Deployed(address newContract);

  function deployDepositPaying() public returns(address addr) {
    addr = new DepositPaying(msg.sender);
    deployed.push(addr);
    emit Deployed(addr);
  }

Some elaboration on the factory pattern: https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html?highlight=contract%20address#gettransactionreceipt

REMIX

The reason it all seems to work on Remix is that it "helps" by abstracting away some of the gory details. Transactions mine more or less immediately, it gets the receipts for you and it gives the response as through the front-end had dutifully attended to all of that stuff. These features make it much more productive to iterate over contract code, see what it does and get it right but it can create a false sense of simplicity when you move to front-end work without the training wheels.

Hope it helps.

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.