3

I have written a code to add offer in my contract, however, when I compiled the code in the console and I tried to return the number of offer in this contract, I got 0. PS: I tried this code in the solidity compiler online and it works well !

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() public returns(uint) {
        uint nbr_offer =offers.length;
        return(nbr_offer);
    }
}

I still getting 0 in my console! I replaced public with constant but it doesn't work! I dont know why it works well when I tested it in the solidity compiler online .

this is my console shot

2

It is because your returnNbroffer() function was not marked constant. I have replaced public with constant and this code works:

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

Here is the modified version of your code that I flattened, pasted and deployed from the geth console:

> var offerSource='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;  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); }  function returnNbroffer() constant returns(uint) { uint nbr_offer =offers.length; return(nbr_offer); }}'
undefined
> var offerCompiled=web3.eth.compile.solidity(offerSource);
undefined
> var offerContract = web3.eth.contract(offerCompiled['<stdin>:Offer'].info.abiDefinition);
undefined
> var offer = offerContract.new({
    from: web3.eth.accounts[0],
    data: offerCompiled['<stdin>:Offer'].code, gas: 600000},
    function(e, contract) {
      if (!e) {
        if (!contract.address) {
          console.log("Contract transaction send: TransactionHash: " +
            contract.transactionHash + " waiting to be mined...");
        } else {
          console.log("Contract mined! Address: " + contract.address);
          console.log(contract);
        }
    }
});
Contract transaction send: TransactionHash: 0x68ee1322883fef739b82fea9c267d9941cfe19408a5c67b0f2d894d5c5d7fa39 waiting to be mined...
undefined
> Contract mined! Address: 0x31e137906cc44ff6e1a1a098303bd7573fe715cc

I checked the transaction receipt:

> eth.getTransactionReceipt("0x68ee1322883fef739b82fea9c267d9941cfe19408a5c67b0f2d894d5c5d7fa39")
{
  blockHash: "0xf3b2ba913e43ad8b7a1377ec3e066f6a1436605fbf2423a08bc189d04dae6557",
  blockNumber: 7932,
  contractAddress: "0x31e137906cc44ff6e1a1a098303bd7573fe715cc",
  cumulativeGasUsed: 577610,
  from: "0x000d1009bd8f0b1301cc5edc28ed1222a3ce671e",
  gasUsed: 577610,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  root: "0x4a336c0521b10b59bd335a2b1242ff9a79bb30be7f22e31c3d884a60c63030d7",
  to: null,
  transactionHash: "0x68ee1322883fef739b82fea9c267d9941cfe19408a5c67b0f2d894d5c5d7fa39",
  transactionIndex: 0
}

I sent an addOffer(...) transaction and waited for a short while:

> offer.addOffer("one", "one", 1, 1, {from: eth.accounts[0], gas: 400000});
"0x103f16250f3b5e71144bde66b61ddbe07a02b3dfc2ac670513dae77083cf525a"

I then checked the constant function:

> offer.returnNbroffer()
1

With the constant function modifier added, your contract seems to work as expected. Sometime you also have to increase the gas.

  • thanks for your help but it doesn't work ! – mypeeka Mar 19 '17 at 11:02
2

I found two issues in your code:

I. Typos

Offer o = offres[offreID];

should be

Offer o = offres[offerID];

II. There is a difference between a read and a write operation in the blockchain.

o Write means you send a transaction which will be mined. This operation cost gas and takes a few seconds.

To declare a write function in Solidity, just do what you wrote:

function functionName(args) returns (return_args) {
}

o Read doesn't cost any gas because your node contains the blockchain, no transaction are processed for that. To explicitly tag a function for read, use the keyword constant

function functionName(args) constant returns (return_args) {
    return a;
}

In your case

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

Documentation:


EDIT

truffle(development)> var c = web3.eth.contract(Offer.abi);
undefined
truffle(development)> Offer.address
'0x10fdfeecd15082519ab1b0648df07831c665d5d6'
truffle(development)> var i = c.at('0x10fdfeecd15082519ab1b0648df07831c665d5d6');
undefined
truffle(development)> i.addOffer("a", "b", 10, 5, {from: web3.eth.accounts[0]});
'0xbaaf59c7ce30a54a44a56553d58ecb2aa4e84b02e59508d97ad43419491733e7'
truffle(development)> i.returnNbroffer.call().toNumber()
1
  • Thanks for your help but it doesn't work! I still getting 0 when I call "mycontract.returnNbroffer.call()" – mypeeka Mar 19 '17 at 11:01
  • Just edited my answer. Works fine for me – Greg Jeanmart Mar 19 '17 at 11:18
  • There I feel lost! It's really weird! thanks a lot for your help – mypeeka Mar 19 '17 at 11:27
0

May help you. I tested with Mist.

pragma solidity ^0.4.9;

contract OfferContract {

    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) public
      returns (uint, string, string, uint, uint) {
        uint offerID = offers.length++;
        Offer storage 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 public returns(uint) {
        uint nbr_offer =offers.length;
        return(nbr_offer);
    }
}

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.