1

Similar to this question How to get access to the storage array through the solidity assembler?

But instead of accessing a storage variable, I want to create a new storage variable via assembly. That means instead of declaring a variable, I use a function to generate the variable.

Instead of:

contract MetaCoin{
 uint A;
}

Do this:

contract MetaCoin{
 function createVariableA(){
  //assembler code here
 }
}

After generating the variable, other functions can somehow use it like a normal declared variable.

Is this possible?

1 Answer 1

2

I assume that you need to pass the value of the variable as an attribute of the function:

Remember that sstore saves the variable value in slots. I created two functions to see how it works.

pragma solidity ^0.5.8;

contract MetaCoin {
    function createVariable(uint _x) public  {
        assembly {
            let v := _x
            sstore(0, v)
            let w := add(_x,1)
            sstore(1, w)
        }
    }

    function getVariable() public view returns (uint r, uint s) {
        assembly{
            r := sload(0)
            s := sload(1)
        }
    }

    function addValue(uint _y) public returns (uint t){
        assembly{
            t := add(_y,sload(0))
            sstore(0,t)
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.