Solidity

基於資產的轉賬 Solidity 合約

  • December 17, 2018

我正在嘗試通過solidity語言創建基於資產的轉移智能合約。我正在使用乙太坊。

功能是

  1. 創建具有契約所有者所有權的資產(land_id、land_size、land_price)。
  2. 將資產轉讓給他人。
  3. 獲取目前所有權
  4. 以前的所有者列表
  5. 獲取資產詳細資訊。

我的契約:

pragma solidity >=0.4.22 <0.6.0; 

contract Asset {

uint256 public owners_count;
address public contract_owner;              // Manufacturer/owner
bytes32 public land_id;                   // land_id
bytes32 public land_sqrfeet;              // land_sqrfeet
bytes32 public land_created_date;         // Created date
mapping(uint => address) public owners;     // list of owners

function createland(bytes32 _land_id, bytes32 _land_sqrfeet, bytes32 _land_created_date) public returns (bool){
   setOwner(msg.sender);
   land_id = _land_id;
   land_sqrfeet = _land_sqrfeet;
   land_created_date = _land_created_date;
   return true;
}

//Modifier that only allows owner of the bag to Smart Contract AKA Good to use the function
modifier onlyOwner(){
   require(msg.sender == contract_owner);
   _;
}

//This function transfer ownership of contract from one entity to another
function transferOwnership(address _newOwner) public onlyOwner(){
   require(_newOwner != address(0));
   contract_owner = _newOwner;
}

//Get the previous owner in the mappings
function previousOwner() view public returns(address){
   if(owners_count != 0){
       uint256 previous_owner = owners_count - 1;
       return owners[previous_owner];
   }
}

function setOwner(address owner)public{
   owners_count += 1 ;
   owners[owners_count] = owner;
}

function getCurrentOwner() view public returns(address){
   return owners[owners_count] ;
}

function getOwnerCount() view public returns(uint256){
   return owners_count;
}}

但有了這份契約,我可以獲得最後創建的資產詳細資訊。我無法獲得以前的創建資產詳細資訊。

問題是:

  1. 每個資產應該是不同的合約地址還是一個合約地址?
  2. 如果為所有資產使用一個合約地址,我如何通過合約功能獲取舊交易?
  1. 每個資產應該是不同的合約地址還是一個合約地址?

Ans : 每個資產只能在一個合約地址上。

  1. 如果為所有資產使用一個合約地址,我如何通過合約功能獲取舊交易?

**Ans : 您可以使用結構(資料結構)來儲存您的所有資產詳細資訊,也可以用於獲取詳細資訊。**您可以查看此連結以了解如何創建結構

引用自:https://ethereum.stackexchange.com/questions/64168