0

Would this function cost gas in v0.4.25? When i try run the contract on ropsten and call this function it dosent trigger any gas cost in metamask. But from what i have understood writing to memory does cost some gas anyway.So im a bit confused regarding the same. ps: Users is a structure that contains an address and 6 usint8 values.

    function returnplayers() external view returns(address[],uint[],uint[],uint[],uint[],uint[],uint[]) {

    address[] memory addrs = new address[](count);
    uint[]   memory balls_a = new uint[](count);
    uint[]   memory balls_b = new uint[](count);
    uint[]   memory balls_c = new uint[](count);
    uint[]   memory balls_d = new uint[](count);
    uint[]   memory balls_e = new uint[](count);
    uint[]   memory balls_mega = new uint[](count);

    for (uint i = 0; i < count; i++) {
        Users memory user = users[i];
        addrs[i] = user.player;
        balls_a[i] = user.no1;
        balls_b[i] = user.no2;
        balls_c[i] = user.no3;
        balls_d[i] = user.no4;
        balls_e[i] = user.no5;
        balls_mega[i] = user.no6;
    }

    return (addrs, balls_a, balls_b, balls_c, balls_d, balls_e, balls_mega);
}
2

As per my understanding, all functions cost gas.

You do not have to pay for the gas of view functions if they are called externally. This is because, the contract state is not being changed.

However, if the read operation is carried out as part of a transaction that is changing some state in the contract storage, EVM machine has to be run and all the operations' gas is to be paid for as per the gas price.

In your case, the variables are memory variables and not storage variables that are part of the contract state. They do not have to be stored anywhere after the code is executed

0

An external view function would cost gas only if it was invoked by a function which is not constant, i.e., neither pure nor view (more precisely, invoked within the calling-tree of such function).

  • so if we called it manually from outside the contract it shouldnt cost gas rite? – Sundeep Kumar May 31 at 8:17
  • 1
    @SundeepKumar: Precisely! – goodvibration May 31 at 8:46

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.