Skip to content

Instantly share code, notes, and snippets.

@chebykin
Last active May 30, 2018 12:38
Show Gist options
  • Save chebykin/d37c187e4ac9488849736b42162ef54b to your computer and use it in GitHub Desktop.
Save chebykin/d37c187e4ac9488849736b42162ef54b to your computer and use it in GitHub Desktop.
Transfer asset
pragma solidity ^0.4.11;
contract Test {
mapping (address => address[]) owner2assets;
mapping (address => address) asset2owner;
event ConstructorCall(string);
function Test () public {
owner2assets[msg.sender] = [0x661d576d9486cb7699648518a364d95301ecea6d,
0x3e11f1942810f0f64325e29fba754c34cb80a5a5];
asset2owner[0x661d576d9486cb7699648518a364d95301ecea6d] = msg.sender;
asset2owner[0x3e11f1942810f0f64325e29fba754c34cb80a5a5] = msg.sender;
ConstructorCall("hey");
}
function addOneAsset(address _asset) public {
owner2assets[msg.sender].push(_asset);
asset2owner[_asset] = _asset;
}
function transferAsset(address _nextOwner, address _assetId) {
assert(asset2owner[_assetId] == msg.sender);
address[] storage myAssets = owner2assets[msg.sender];
// todo: iterate over owner2assets[msg.sender] and remove current id
for (uint i = 0; i < myAssets.length; i++) {
if (myAssets[i] == _assetId) {
myAssets[i] = myAssets[myAssets.length - 1];
break;
}
}
assert(myAssets.length > uint(0));
myAssets.length -= 1;
owner2assets[_nextOwner].push(_assetId);
asset2owner[_assetId] = _nextOwner;
}
function myAssets() public returns (address[]) {
return owner2assets[msg.sender];
}
function myAssetsLength() public returns (uint256) {
return owner2assets[msg.sender].length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment