I wrote a function to exchange an offer by an amount of ether. After checking the value of ether sent and the cost of the offering, I record the subscription time in a mapping. My problem is that when I send a transaction with the command:

offer.subscribeOffer.sendTransaction(0,{from:eth.accounts[0], to:0x2333a7124d57795757239f0f86990ca9f429e032, value: 2,gas:4000000}) the console shot

nothing is saved in the mapping, neither the address nor the time.

This is my contract code:

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);
   }
 }
  • Doesn't compile. offer or offres? Where does o.cout come from? Suggest working it out in ethereum.github.io/browser-solidity – Rob Hitchens B9lab Mar 19 '17 at 19:28
  • These are typos, I have edited them! Yes it compile – mypeeka Mar 19 '17 at 19:37
  • try to add an event to check, besides Rob is right your code is erroneous doesn't compile. – Badr Bellaj Mar 19 '17 at 22:40
up vote 0 down vote accepted

I've tried your contract after correcting some typos :

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);
   }
 }

I think you get a o because you don't send much ether to your contract because in your function subscribeOffer you set a condition if (msg.value>= o.price) so your transaction should hold more ethers thant the offer price otherwise you get 0. I've tried using solidity browser and it works fine. enter image description here

  • thanks for your help! when I add an offer with price=0, it works well but when I change the offer price to 1 for example it doesn't work ,while I send ether enough in value. offer.subscribeOffer.sendTransaction(2,{from:"0x9f6b29ef03bd9134481d5ebf70b702b6cb36224e",to:0x6a029d9382b175b3ca7594c7aa2d10a95907b450,gas:40000000,value:2452225}) – mypeeka Mar 20 '17 at 10:24
  • 1
    It works when I hold more ether in value! I am so grateful :) – mypeeka Mar 20 '17 at 10:41

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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