Solidity

時間不儲存在映射中

  • December 16, 2017

我寫了一個函式來交換一個乙太幣的報價。在檢查了發送的乙太幣的價值和提供的成本之後,我在一個映射中記錄了訂閱時間。我的問題是,當我使用命令發送交易時:

offer.subscribeOffer.sendTransaction(0,{from:eth.accounts[0], to:0x2333a7124d57795757239f0f86990ca9f429e032, value: 2,gas:4000000}) 控制台拍攝

映射中沒有保存任何內容,地址和時間都沒有。

這是我的契約程式碼:

pragma solidity ^0.4.9;

contract Offer {

address public owner;
struct Offer {
   string title;
   string description;
   uint  offerTime;
   mapping (address => uint) subscribers;
   mapping (uint => address) subscribersAddress;
   mapping (uint => uint) nbrSubscribersPerID;
   uint price;
}

Offer[] public  offers;


// add offer
function addOffer(string  description1, string title1, uint price1,uint offerTime1) 
 returns (uint, string, string, uint, uint) {
   uint offerID = offers.length++;
   Offer o = offres[offreID];

   o.description = description1;
   o.offerTime=offerTime1;
   o.title = title1;
   o.price = price1;
   return (offerID, offers[offerID].description, offers[offerID].title, offers[offerID].price, 
     offers[offerID].offerTime);
}

//return the  number of offers 
function returnNbroffer() constant returns(uint) {
   uint nbr_offer =offers.length;
   return(nbr_offer);
}

function subscribeOffer(uint offerID) public payable  returns(address,uint,address)   {

     Offer o = offers[offerID];
     uint i=0;
     uint count=0;
        if (msg.value>= o.price)
          { address addre=msg.sender;
            offres[offerID].subscribersAddress[count=++i]=msg.sender;
            offres[offerID].subscribers[msg.sender]=now;
            return(addre,offers[offerID].subscribers[msg.sender],offers[offerID].subscribersAddress[i]);


           }
   offers[offerID].nbreSubscribersParID[offerID]=count;



}
 function expirationOffer(uint offerID) constant  returns(bool,uint,uint)
 { 
    Offer o = offers[offerID];
    bool expire=false;
    uint timenow=now;
    uint timeSpent=offres[offerID].subscribers[msg.sender]+o.offerTime ;
    if (timenow > timeSpent )
      {expire=true;}
  return(expire,timenow,timeSpent);
  }
}

在糾正了一些錯別字後,我嘗試了您的契約:

pragma solidity ^0.4.9;

contract Offer {

address public owner;
struct Offer {
   string title;
   string description;
   uint  offerTime;
   mapping (address => uint) subscribers;
   mapping (uint => address) subscribersAddress;
   mapping (uint => uint) nbrSubscribersPerID;
   uint price;
}

Offer[] public  offers;


// add offer
function addOffer(string  description1, string title1, uint price1,uint offerTime1) 
 returns (uint, string, string, uint, uint) {
   uint offerID = offers.length++;
   Offer o = offers[offerID];

   o.description = description1;
   o.offerTime=offerTime1;
   o.title = title1;
   o.price = price1;
   return (offerID, offers[offerID].description, offers[offerID].title, offers[offerID].price, 
     offers[offerID].offerTime);
}

//return the  number of offers 
function returnNbroffer() constant returns(uint) {
   uint nbr_offer =offers.length;
   return(nbr_offer);
}

function subscribeOffer(uint offerID) public payable  returns(address,uint,address)   {

     Offer o = offers[offerID];
     uint i=0;
     uint count=0;
        if (msg.value>= o.price)
          { address addre=msg.sender;
            offers[offerID].subscribersAddress[count=++i]=msg.sender;
            offers[offerID].subscribers[msg.sender]=now;
            return(addre,offers[offerID].subscribers[msg.sender],offers[offerID].subscribersAddress[i]);


           }
   offers[offerID].nbrSubscribersPerID[offerID]=count;



}
 function expirationOffer(uint offerID) constant  returns(bool,uint,uint)
 { 
    Offer o = offers[offerID];
    bool expire=false;
    uint timenow=now;
    uint timeSpent=offers[offerID].subscribers[msg.sender]+o.offerTime ;
    if (timenow > timeSpent )
      {expire=true;}
  return(expire,timenow,timeSpent);
  }
}

我認為你得到 ao 是因為你沒有向你的合約發送太多乙太幣,因為在你的函式中subscribeOffer你設置了一個條件,如果(msg.value>= o.price)這樣,你的交易應該持有比報價更多的乙太幣,否則你得到 0。我試過使用solidity瀏覽器和它工作正常。 在此處輸入圖像描述

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