-1

while testing I got a strange behavior of truffle, please see the subject smart contract code first

    uint256 public burningRatePercent = 50;
function onlyOwnerSetBurningRate(uint256 _burningRatePercent) onlyOwner public returns (uint)
{
    burningRatePercent = _burningRatePercent;
    return (burningRatePercent);
}

and this is the test script

it("should return set value from onlyOwnerSetBurningRate", function() {
    var token;
    return Token.deployed().then(function(instance){
        token = instance;
        const tst = token.onlyOwnerSetBurningRate.call(1234);
        return tst;
    }).then(function(result){
        assert.equal(result.toNumber(), 1234, 'onlyOwnerSetBurningRate failed');
        var ret =  token.burningRatePercent.call();
        return ret;
    }).then(function(result){
        assert.equal(result.toNumber(), 1234, 'Reading BurningRate set value failed');
    }); 
}); 

here value of 'tst' clearly says that the variable value changed thus passes OK for "onlyOwnerSetBurningRate failed" , but in next line reading attempt of the same variable in 'var ret' returns '50' which is initial value, thus "Reading BurningRate set value failed" does not passes in green tick. so what's wrong here, my code , or truffle bug, extremely strange please help me

enter image description here

This code worked correctly and the subject function transfer is not pure or view

    it("should transfer token to correct address, include burning, and balance test", function() {
    var token;
    return Token.deployed().then(function(instance){
        token = instance;
        return token.transfer(accounts[1], "500000000000000000000000000");
    }).then(function(){
        return token.balanceOf.call(accounts[1]);
    }).then(function(result){
        assert.equal(result, CP(RP(500000000)), 'accounts[1] balance is wrong');
        return token.balanceOf.call(accounts[0]);
    }).then(function(result){
        assert.equal(result, RP(500000000), 'accounts[0] balance is wrong');
    })
}); 

and same for transferfrom also worked correctly

    it("should pass test of approve transferfrom and balance check of 0,1,3 after transaction", function() {
    var token;
    return Token.deployed().then(function(instance){
        token = instance;
        return token.approve(accounts[1], "200000000000000000000000000");
    }).then(function(){
        return token.allowance.call(accounts[0], accounts[1]);
    }).then(function(result){
        assert.equal(result, RP(200000000), 'allowance is wrong');
        return token.transferFrom(accounts[0], accounts[2], "200000000000000000000000000", {from: accounts[1]});
    }).then(function(){
        return token.balanceOf.call(accounts[0]);
    }).then(function(result){
        assert.equal(result, RP(300000000), 'accounts[0] balance is wrong');
        return token.balanceOf.call(accounts[1]);
    }).then(function(result){
        assert.equal(result, CP(RP(500000000)), 'accounts[1] balance is wrong');
        return token.balanceOf.call(accounts[2]);
    }).then(function(result){
        assert.equal(result, CP(RP(200000000)), 'accounts[2] balance is wrong');
    })
});

here CP is to reduce some percent and RP is to rise power ( 10**18), this code also works correctly

1

Function onlyOwnerSetBurningRate is not constant (neither pure nor view).

As such, you cannot fetch its return-value from an off-chain call (i.e., the return-value is useful only when you call this function inside a contract).

So your usage of .call() here is wrong to begin with.

After you remove it, token.onlyOwnerSetBurningRate(1234) will return the transaction object.

You will be able to fetch the return-value from that object, if you emit an event which contains it (in the function itself).

However, since you are fetching burningRatePercent directly, there doesn't seem to be any need to do that.

So in short, just remove the part with const tst = token.onlyOwnerSetBurningRate.call(1234).

Also, you can change your function to:

function onlyOwnerSetBurningRate(uint256 _burningRatePercent) onlyOwner public
{
    burningRatePercent = _burningRatePercent;
}
  • I tried , it says "TypeError: result1.toNumber is not a function", but in previous the same toNumber() works, so please show me whats wrong here ? Thanks . – FLAME'S KING Jul 28 at 3:43
  • @FLAME'SKING: Sorry, your notion is wrong. Function onlyOwnerSetBurningRate is not constant (pure or view). You cannot fetch its return-value from an off-chain call. – goodvibration Jul 28 at 3:49
  • "You cannot fetch its return-value from an off-chain call" so please show me the way to fetch it in test script , but the same code for balanceOf check after transaction worked well, which is also not constant ( pure or view ) – FLAME'S KING Jul 28 at 3:57
  • @FLAME'SKING: What do you mean "the same code for BalanceOf check"??? I don't see this code in your question, how exactly am I supposed to refer to that??? And like I said, you can either emit an event containing the return-value in your Solidity function and then fetch it from the transaction object, or you can simply read the state-variable after you call that function, which you are already doing! – goodvibration Jul 28 at 3:59
  • I mean to say that the same approach worked with transfer and transfer from function , is it because of emitted event ? as seems in your detail reply , Thank You – FLAME'S KING Jul 28 at 4:14
0

some changes of mine worked, like if need to change storage variable, need to call function without call() and if with call() I felt that perhaps truffle runs the function as just local and temporary only and hence doesn't affects storage variable, that's why returning value was different in subject question.

and whenever you felt that the returning value is not correct even without call() ( I also felt at some point ), then just start a new contract('xxxxxxxx2', function(accounts); in next line and get a fresh start of contract for next part of test.

I don't know the deep inside of truffle , but I can imagine and this works for me

answer of @goodvibration was also correct up to some extent, but expression was not straight forward as I think ( knowing too lot is another skill and expression in simplified way and straight forward is another skill ), Thank You All.

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.