0

I created asset transaction smart contract.

Functions are

  1. Create Assets (asset id, asset details, with owner) and pushing into array list.

  2. Get Assets by owner address: Looping assets array and get count

  3. Transfer asset: Looping assets array and check old owner and replace owner to new owner.

My code:

pragma solidity >=0.4.22 <0.6.0;

contract Asset {
struct asset {
    bytes32 asset_id;
    bytes32 asset_name;
    bytes32 asset_sqfeet;
    address owner;
}

asset[] items;

function createasset(bytes32 asset_id, bytes32 asset_name, bytes32 asset_sqfeet) public {
    address userwallet = msg.sender;
    asset memory m;
    m.asset_id = asset_id;
    m.asset_name = asset_name;
    m.asset_sqfeet = asset_sqfeet;
    m.owner = userwallet;
    items.push(m);
}

function getassetCount(address owner) public view returns (uint){
    uint count = 0;
    for(uint i=0; i < items.length; i++){
        if(items[i].owner == owner){
            count = count +1;
        }
    }
    return count;
}   

function getassetOwnership(address owner, bytes32 asset_id) public view returns (bool){
    for(uint i=0; i < items.length; i++){
        if(items[i].asset_id == asset_id){
            if(items[i].owner == owner){
                return true;    
            }
        }
    }
    return false;
}

function tranferasset(address new_owner, bytes32 asset_id) public payable {
    require(new_owner != msg.sender);
    address old_owner = msg.sender;
    for(uint i=0; i < items.length; i++){
        if(items[i].asset_id == asset_id){
            if(items[i].owner == old_owner){
                 items[i].owner = new_owner;
            }
        }
    }
}

}

My questions are:

  1. How about performance of this contract, if assets will more then one lakhs? (because i am using for loop.)

  2. Is there other way to do it?

  3. how about security of this contract?

Thanks in advance

1

You can use mappings which can replace your for loop code and will execute more efficiently than this code because more for loops will take more processing power to execute functions. In your code you can use mapping.for example

mapping (address => asset) asset_map;

You can use this mapping in your getassetOwnership function like this

function getassetOwnership(address owner, bytes32 asset_id) public view returns (bool){
   if(asset_map[owner].id == asset_id)
     {
       return true;
     }
   else
     {
       return false;
     }
}
  • can you please give small example "how to replace for loop by mapping"? i have lot confusion in mapping – Balakrishnan Dec 18 '18 at 6:58
  • Edited my answer have a look at it – Mr. Y Dec 18 '18 at 7:08
  • should i change createReingo function as well? – Balakrishnan Dec 18 '18 at 8:16
  • I can't see that function in your code – Mr. Y Dec 18 '18 at 8:30
  • i created createAsset function like "function createAsset(bytes32 reingo_id) public { address userwallet = msg.sender; asset_map[userwallet] = asset(reingo_id, reingo_name, reingo_sqfeet); } with this code i can not get previous asset – Balakrishnan Dec 18 '18 at 8:59
3
  1. How about performance of this contract, if assets will more then one lakhs? (because i am using for loop.)
  2. Is there other way to do it?

First I will try to explain you something. Smart Contracts are made for write your logic or terms you want to secure on blockchain that cannot be modified by others and also by you after it deployed on the network. Generally, smartcontracts are not used for store your large amount of data and all for that I will recommend you to use ipfs(distributed file storage protocol).

  1. how about security of this contract?

For security, It does not tamper or modifies (logic, not data that you store) by others after it is deployed on the network.

I hope it will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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