2

When I run the below solidity function:

function startAuction(string name, uint timeLimit) public returns (uint[]) {
    uint auctionID = totalAuctions++;
    AuctionStruct a = Auctions[auctionID];
    a.recipient = msg.sender; // the person who starts the auction is the recipient
    a.name = name;
    uint array = auctionList.length; 
    auctionList.push(13);
    Update(a.name, a.highestBid, a.highestBidder, a.recipient, auctionID, array);   
    return auctionList;    
}

Using the below request from javascript:

contractInstance.startAuction(auctionname, duration, { from: buyerAddress }).then(function(result) {...

I receive a response with an empty logs array.

But when I remove the below line from the above solidity function:

auctionList.push(13);

And add the below Solidity function I receive a filled logs array from the first function and an auctionList from the below function including the pushed 12:

function listAuctions() public returns (uint[]) {
    auctionList.push(12);
    return auctionList;

}

Which is called using the below request in javacsript (next to the prior script to request the solidity function):

contractInstance.listAuctions.call().then(function(v) {...

I am using truffle-contract to access the contracts, which is activated using the below code:

var Auction = contract(auction_artifacts);
console.log(Auction);
Auction.setProvider(web3.currentProvider);

The contract is included using browserify:

global.contract = require('truffle-contract');
global.auction_artifacts = require('../build/contracts/Auction.json');

When I run the above functions from the truffle console all seems to be working fine.

Could anyone explain this?

  • Is Update(a.name, a.highestBid, a.highestBidder, a.recipient, auctionID, array); an Event? – Luiz Soares Dec 15 '17 at 19:19
  • Yes that is correct – The Code Buccaneer Dec 16 '17 at 6:06
  • Is the transaction successful when the auctionList.push(13); is included? As in can you check the status field (inside the transaction receipt object returned in the .then()) is equal to 1? If it isn't then the transaction isn't successfully being executed and auctionList.push(13); is causing that function/transaction to fail for some reason. – willjgriff Dec 20 '17 at 23:16
  • That startAuction is returning no events means the transaction has failed, and the cause seems to be it is running out of gas. – Ismael Mar 27 '18 at 14:27
3

You are confusing "call" and "sendTransaction". When you use "call", you get the return value and you did when executing contractInstance.listAuctions.call().then(function(v) {...

And your code here is not using "call" contractInstance.startAuction(auctionname, duration, { from: buyerAddress }).then(function(result) {...

It is a common question as you see here: What is the difference between a transaction and a call?

call - It is synchronous and the return value of the contract function is returned immediately.

transaction - it is asynchronous, the immediate return value of a transaction is always the transaction's hash

  • It does return values using the "sendTransaction" option when the code ""auctionList.push(13)" is not included so I don't think that is the issue. Also when I use the truffle console I do get the right result using "sendTransaction" even when the "auctionList.push(13)" is included – The Code Buccaneer Dec 16 '17 at 6:09
  • 1
    It doesn´t return a transaction hash when you use sendTransaction? That´s weird. I couldn´t reproduce here. I always get a transaction hash. And when I use Promise I get a complex object after it is mined. – Luiz Soares Dec 18 '17 at 11:13
2

The function you describe modifies state variables, so you can’t call them from outside solidity with call()

When you remove the line that pushes the item into the array, the function does not modify the state anymore.

If the function is intended to modify an state variable you don’t need to use .call(). Just do contract.myfunc(var);

  • Thanks, but the call function is working correctly. The function that uses the contract.myfunc(var) does not work correctly in this case – The Code Buccaneer Dec 10 '17 at 16:27
  • 4
    @pabloruiz55 You can call a function that modifies the contract storage, they will not permanently modify the storage (only within the call). They will not cause any error ethereum.stackexchange.com/questions/28040/… – Ismael Dec 11 '17 at 21:48

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.