Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Solidity contract functions not returning correctly with Truffle, but working on Remix #506

Closed
christian-valadez opened this issue Jul 21, 2017 · 1 comment

Comments

@christian-valadez
Copy link

I'm testing my contracts using Truffle, but my functions are not returning the correct uint, despite doing so when testing in Remix. This is my Test.js code:

    var EventContract = artifacts.require("./EventContract.sol");
    contract ('EventContract', function (accounts){
    it("account1 purchases 4 tickets from ticketContract, which had 10 tickets to start with at a cost of 2", function(){
        var event;
        var account1 = accounts[0];
    
        return EventContract.deployed()
        .then(function(instance){
          event = instance;
            return event.buyTicketsFromContract.call(account1, 2, {from: account1, value: 4});
        })
        .then (function (success){
          console.log("purchased from contract with success: " + success);
          assert.equal(true, success, "bought 2 tickets for 4 ether");
          return event.getContractTicketBalance.call();
        })
        .then (function(contractBalance){
          console.log(contractBalance.toNumber());
    // Fails here
          assert.equal(8, contractBalance.toNumber(), "contract ticket balance is not 8");
          return event.getTicketBalance.call(account1);
        })
        .then (function(accountBalance){
          assert.equal(2, accountBalance.toNumber(), "account balance is not 2");
        })
        ;
      })
      ;

It fails because the contract ticket balance is 10 (initial amount), not 8. When using the same functions on Remix, the values returned are correct.

Someone else on Github also had this issue a couple days ago: https://ethereum.stackexchange.com/questions/21483/why-contract-is-not-working-on-test-net-but-work-in-remix

Environment

  • Operating System: Windows 10
  • Truffle version: 3.4.5
  • Ethereum client: testRPC
  • node version: 8.1.4
  • npm version: 5.3.0
@christian-valadez
Copy link
Author

I was incorrectly using .call(), which is for read-only.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant