[Ethereum] VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if

accesscontract-designcontract-developmentsoliditystruct

Hi Guys 👋

I have two contacts first one is SubToken and second one is MainContract.

First I deploy MainContract after that I use createNewContract function and I create new SubToken.

and then

I deploy SubToken with a address which is come from create contract function in my MainContract.

I want to access my name and I make a few functions in SubToken contract for that but I see revert error 🙁

VM error: revert.
revert The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

I dont know what am I supposed to do. Anyone can help me please 🙂

Here's the a few ss of my code:

https://eksiup.com/p/aj79538kzmgp

https://eksiup.com/p/w779539wzqmc

https://eksiup.com/p/9t79541aevh6

pragma solidity ^0.5.0; 
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol'; 

contract SubToken{ 
    MainContract mc;

    constructor(address _address) public{  
       mc=MainContract(_address);

    }  

    function getName1() view public returns(string memory){
        return mc.getNameMain(address(this));
    }



    //function transferFrom(address from, address to, uint tokens) public returns (bool success); 
    //function allowance(address tokenOwner, address spender) public view returns (uint remaining);  
    //function transfer(address to, uint tokens) public returns (bool success) ;  
    //function approve(address spender, uint tokens) public returns (bool success);  
    //function transferFrom(address from, address to, uint tokens) public returns (bool success);  

    event Transfer(address indexed from, address indexed to, uint tokens); 
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens); 

} 
contract MainContract {
    //mapping(address => mapping(address=>uint)) balances;

    //mapping(address => mapping(address => uint)) allowed;

    address[] public contracts;

    using SafeMath for uint;

    address contract_address;

    struct token{ 
        string  name; 
        string   symbol; 
        uint8  decimals; 
        uint  totalSupply; 
        uint balances;
    } 
    mapping(address => token) public tokens;

    address mycontraddress;

    function createNewContract(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply,uint _balance) public returns(address youraddress){ 
        SubToken st = new SubToken(address(this));//address(this)
        tokens[address(st)] = token(_name,_symbol,_decimals,_totalSupply,_balance);
        mycontraddress=address(st);
        return mycontraddress;
    } 
    function seeContracttAddress() public view returns(address){
        return mycontraddress;
    }

    function getNameMain(address _address) public view returns(string memory){
        return tokens[_address].name;
    }

    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);

}

Best Answer

The problem is not with your code. The problem is the order of issuing transactions. You should follow the following steps:

  1. Deploy MainContract
  2. Issue createNewContract and get the returned address of the deployed SubToken Contract.

    ** Note that this address is the address of SubToken contract which has been deployed in createNewContract method. You need not deploy SubToken again.

  3. Get instance of the SubToken Contract using the retrieved address in step 2.

  4. Call the method getName1() of the instance of the SubToken Contract of step 3.

The problem was you were deploying SubToken Contract again and calling getName1(). Inside getName1() you called mc.getNameMain(address(this)); The address you were passing as parameter had not been mapped in token of MainContract. So, as there was no entry with that address, it threw error.

Related Topic