I have followed following answer for: Having trouble storing multiple values in a uint256.
For example, I want to pass four uint64
variables merged together as a single argument into smart contract and extract those values.
#python-script:
variable1 = 11;
variable2 = 12;
variable3 = 13;
variable4 = 14;
bits1='{0:064b}'.format(v1)
bits2='{0:064b}'.format(v2)
bits3='{0:064b}'.format(v3)
bits4='{0:064b}'.format(v4)
mergedVariable = int(bits1 + bits2 + bits3 + bits4, 2)
storagetest8.setCharacter(mergedVariable)
print(storagetest8.getVariables())
contract storagetest8 {
uint256 variable;
function setCharacter(uint variable_)
external
{
variable = variable_;
}
function getVariables()
external view
returns(uint64 variable1, uint64 variable2, uint64 variable3, uint64 variable4) {
variable1 = uint64(variable);
variable = uint64(variable>>64);
variable2 = uint64(variable);
variable = uint64(variable>>64);
variable3 = uint64(variable);
variable4 = uint64(variable>>64) // simplified
}
}
[Q] Is the approach I implemented in the smart contract correct?