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.