0

I have two contracts, Factory working as a factory for Business:

// Factory.sol
pragma solidity ^0.8.7;

import "./Business.sol";

contract Factory { // Public only for debugging purposes, it must be private address public businessContract;

function create() external returns(address) {
    businessContract = address(new Business());
    return businessContract ;
}

function validate(address businessContractAddress) external returns(bool) {
    require(msg.sender == businessContract);
    return true;
}

}

Now I want to write a test using truffle suite to check correctness:

const Factory = artifacts.require("Factory");

contract("Factory - JS", async accounts => { let factory;

beforeEach(async () => {
    factory = await Factory.new();
});

it("Test Factory", async () => {
    const result = await factory.create();
    const address = await factory.businessContract(); // 0xAc5BFb2B621AAcDcEA78eDa76e47449a4a6904e1
    const validation = await factory.validate(result); // Fails
    const validation2 = await factory.validate(address); //Wors
});

});

When I check the result, I see this:

{
"tx": "0xe73a67d2e6e3bc0dd026ae40e5cce4cbccf0007ec848e0e99e88ca464338599f",
"receipt": {
    "transactionHash": "0xe73a67d2e6e3bc0dd026ae40e5cce4cbccf0007ec848e0e99e88ca464338599f",
    "transactionIndex": 0,
    "blockHash": "0x7ec2d0a484e0b74884bc4b302ae08c1f93c6f09fd8a585e9e586875f77ff9913",
    "blockNumber": 9,
    "from": "0xf17f52151ebef6c7334fad080c5704d77216b732",
    "to": "0x9fbda871d559710256a2502a2517b794b482db40",
    "gasUsed": 679669,
    "cumulativeGasUsed": 679669,
    "contractAddress": null,
    "logs": [],
    "status": true,
    "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    "rawLogs": []
},
"logs": []
}

I understand that await factory.validate(result) fails cause it is actually NOT AN ADDRESS, however, I don't see any address in that response.

I reduced the whole scenario to the simplest possible example. This example still fails.

What am I missing?

  • It fails because you are verifying agains msg.sender require(msg.sender == businessContract) and never use the param businessContractAddress; – Ismael Sep 07 '21 at 02:08
  • Thanks a lot! That's just a typo when I wrote the question (I'll fix it). The issue is that create does not return an address but something I cant understand. – Facundo La Rocca Sep 07 '21 at 03:32
  • 1
    The problem there is that create modifies the contract so it is a transaction and transactions doesn't return a value but the transaction hash. In this question it is explained the problem and possible solutions like using events. https://ethereum.stackexchange.com/questions/765/what-is-the-difference-between-a-transaction-and-a-call. – Ismael Sep 08 '21 at 04:04

0 Answers0