I am trying to create a smart contract that will allow the creator of the contract to add asset properties and allocate it to an address. Once that's done, the receiving address can now send it to another address, only if it is the current owner.

I tried to allocate a struct 'Asset' by mapping an address to the assetId which is the count of assets being created.

I am not really sure if its the right approach to do it. Also kinda confused on how I can check the current owner based on the AssetId (since there is no where clause like in c#/LINQ.) And what would be the right way to assign an asset from current owner to next owner.

pragma solidity ^0.4.2;

contract AssetDistribution { 
address public issuer;
uint public assetCount;

event AllocationDetails(address from, address to, string description);

function AssetDistribution() {
    issuer = msg.sender;

struct Asset {
    uint assetId;
    address currentOwner;
    string description;
    uint cost;
mapping (address=> mapping(uint=>Asset)) private owners;
//mapping(address=> mapping(uint=>bool)) private ownerPropertyCheck;

//creates an asset and allocates it to an address at the same time. 

function setCreateAsset(address newOwner, string description, uint cost) 
    returns(string) {
    if (msg.sender == issuer) {
    Asset memory myAsset = Asset(assetCount,newOwner,description,cost);      
    owners[newOwner][assetCount] = myAsset;
    return "created By: government";
    //return strConcat("created By: ","test");
    } else { 
        return "This is not the creator";

function getassetCount() constant returns(uint) {
    return assetCount;

function getOwner(uint id) returns(address) {


In C# I would have mostly created a class with asset properties and would have changed the currentOwner when a transfer is done. Here I am having trouble approaching in a similar way.Should I even use struct for this?

Edit One - Solution for me

mapping the assetId to ownerAddress & mapping addresses + assetId to structs solved the dilemma I was having

mapping(uint=>address) assetAddress;
mapping(address=>mapping(uint=>Asset)) ownerAssets ;

returns the current owner address of asset

function getCurrentOwner(uint assetId) constant public returns (address) {
    return assetAddress[assetId];

checks if the assetId (Id of struct) belongs to address

function isOwnerOfAsset(address userId, uint assetId) private constant returns(bool) {
    return assetAddress[assetId] == userId? true:false;

P.S I have mostly coded in C#/Backend, jumping to solidity is confusing me a bit. If you know any good resources to refer, feel free to share.

1 Answer 1


Since mappings are not iterable , I suggest you to keep a mapping to identify owner of an asset as follow,

mapping (uint => address) private assetOwners;

when creating an asset inside setCreateAsset set the value of account number with the key of assetId,

assetOwners[assetCount] = newOwner;

To get the accountNo of the asset Owner of given assetId,

address assetOwner = assetOwners[assetId];

To reassign an asset you can do,

 Asset newAsset = owners[currentOwner];
 newAsset.currentOwner = newOwner;
 owners[newOwner] = newAsset;
 delete owners[currentOwner];
  • 1
    Thank you! Your answer gave me a good idea on how to approach what I was trying to do :)
    – vishnulphb
    Aug 30, 2017 at 20:00

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.