1

I have a special needs, store the mapping with assembly code. thank you very for everyone who can response this question.

for example:

contract assemblyMappingTest{
  mapping(address => uint) public assets;

  function save(address _user, uint _amount) public
  {
     assets[_user] = _amount;
  }

  function saveWithAssembly(address _user, uint _amount) public
  {
     assembly {
        // how to write these codes, so that it can store assets like the function "save"
     }     
  }
}

1 Answer 1

3

Ok, i reslove by myself.

contract assemblyMappingTest{
  mapping(address => uint) public assets;

  function save(address _user, uint _amount) public
  {
     assets[_user] = _amount;
  }

  function saveWithAssembly(address _user, uint _amount) public
  {
     assembly {
        mstore(0, _user)
        mstore(32, assets.slot)
        let hash := keccak256(0, 64)
        sstore(hash, _amount)
     }     
  }
}
1
  • How about if assets was a mapping of structs, how would you then write to it? Apr 6, 2022 at 21:05

Your Answer

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

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