0

I am trying to get contract instance payload created via instance but don't seem to know how. I don't want to save just the contract address. Kindly help. See my code below.

contract LandRegistry {
//Land Details 
struct landDetails{ 
string state; 
string district; 
string village; 
uint256 surveyNumber; 
address payable CurrentOwner; 
uint marketValue; 
bool isAvailable; 
address requester;        
reqStatus requestStatus;
    }

   landDetails public land;

constructor( 
string memory _state, 
string memory _district, 
string memory _village, 
uint256 _surveyNumber, 
address payable _OwnerAddress, 
uint _marketValue, 
uint id) public {         
land.state = _state;        
land.district = _district;         
land.village = _village;         
land.surveyNumber = _surveyNumber;         
land.CurrentOwner = _OwnerAddress;         
land.marketValue = _marketValue;
profile[_OwnerAddress].assetList.push(id);
} }

Contract to call the landregistry

Contract Factory {
//Registration of land details.
function Registration( 
string memory _state, 
string memory _district, 
string memory _village, 
uint256 _surveyNumber, 
address payable _OwnerAddress, 
uint _marketValue, uint id) public returns(LandRegistry) { 
require(superAdmin[_village] == _msgSender() || isOwner(),''); 
LandRegistry registry = new LandRegistry(_state, _district, _village, _surveyNumber, _OwnerAddress, _marketValue, id);
_LandRegistry[keccak256(abi.encodePacked(_msgSender()))] = registry; return registry;     } }
  • Do you want to get the address of a LandRegistry created by the Registration function in Factory? In that case the easier way is to trigger an event emit ContractCreated(registry); when a new instance is created. – Ismael Jan 14 at 19:58
  • In the future, posting formatted code that compiles makes it much easier for people to help / answer your question – Steven V Jan 14 at 21:55
  • This is sort of round-about. I'm not sure what you mean or why you want to, given that the bytecode is precisely what you put there and the address, plus code and/or ABI is all anyone needs to interact with it. – Rob Hitchens Jan 14 at 22:38
0

You can use assembly language to access the byte code of the smart contract that you've just deployed within your smart contract. See the simplified example below for details. Using remix, you can deploy the factory, and then call register and it will emit an event with the byte code of the smart contract you just deployed. Based on your example - you could then store a hash of this byte code.

Assembly Code Source

Factory

pragma solidity ^0.5.0;

import "./LandRegistry.sol";

contract Factory {
    //Registration of land details.
    event DeployedBytes(bytes ContractBytes);

    function Registration( 
        string memory _state
    ) public returns( LandRegistry ) { 
        LandRegistry registry = new LandRegistry(_state);
        bytes memory b = at(address(registry));
        emit DeployedBytes(b);
    }

    function at(address _addr) private view returns (bytes memory o_code) {
        assembly {
            // retrieve the size of the code, this needs assembly
            let size := extcodesize(_addr)
            // allocate output byte array - this could also be done without assembly
            // by using o_code = new bytes(size)
            o_code := mload(0x40)
            // new "memory end" including padding
            mstore(0x40, add(o_code, and(add(add(size, 0x20), 0x1f), not(0x1f))))
            // store length in memory
            mstore(o_code, size)
            // actually retrieve the code, this needs assembly
            extcodecopy(_addr, add(o_code, 0x20), 0, size)
        }
    }
}

Land Registry

pragma solidity ^0.5.0;

contract LandRegistry {
    //Land Details 
    struct landDetails{ 
        string state; 
    }

    landDetails public land;

    constructor( 
        string memory _state 
    )public {         
        land.state = _state;
    }
}

Remix

Here you can see the byte code of the contract from the emitted event. If you go to the compile page in remix and select copy byte code - you can confirm this matches.

enter image description here

| improve this answer | |
  • Hi Steven. I appreciate your feedback. How can I read the bytecode and return the variable(_state) stored during creation? – Charles Okaformbah Jan 17 at 12:14
  • The variable is not included in the byte code because that is put in the smart contract storage. It is not a part of the smart contract code. – Steven V Jan 17 at 14:58
  • alright. Thank you for your help. – Charles Okaformbah Jan 20 at 6:43
  • One more question @Steven V, the smart contract storage where the variable is stored is in the Factory contract, right? – Charles Okaformbah Jan 20 at 15:17
  • you can access the address of the smart contract that you deployed with address(registry) after the contract is created in the Factory smart contract function. You could change the factory contract to save the address of the contracts. The variable is stored in the variable land.state in the registration contract. – Steven V Jan 20 at 20:50

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.