Do we require a specific condition when working with indices on solidity

0 votes

I know that an assert-style exception is generated in the situation if you access an array at a too large or negative index (i.e. x[i] where i >= x.length or i < 0).

But should I check the condition every time?

Following is my code:

pragma solidity ^0.4.0;

contract A{
    byte[10] arr;

    function setElement(uint index, byte value) public {
        require(index >= 0 && index < arr.length); //Should I leave it as is?
        arr[index] = value;
    }

    function getElement(uint index) view public returns (byte) {
        require(index >= 0 && index < arr.length); //Or not?
        return arr[index];
    }
}

Also, how do I refund the remaining gas to the executor?

Jul 23, 2018 in Blockchain by sabby
• 4,370 points
573 views

1 answer to this question.

0 votes

Your code is written correctly. require is intended to be used to check input parameters whereas assert is to verify the internals of your contract (mostly for testing purposes). If a require condition fails, the remaining gas will be refunded.

You can refer to the section in the solidity documentation here: http://solidity.readthedocs.io/en/develop/control-structures.html?highlight=require#error-handling-assert-require-revert-and-exceptions

answered Jul 23, 2018 by Perry
• 17,100 points

Related Questions In Blockchain

0 votes
1 answer

Should I require a specific condition when working with indexes in Solidity?

You are using it correctly. require is intended to ...READ MORE

answered Jul 17, 2018 in Blockchain by aryya
• 7,460 points
587 views
0 votes
1 answer

How can we decrypt a transaction payload when confidentiality is on?

You can try the following: you can only ...READ MORE

answered May 3, 2018 in Blockchain by Shashank
• 10,400 points
834 views
0 votes
2 answers

How do I interact with a smart contract on a private network through web3.js

I found a blog that explains how ...READ MORE

answered Aug 20, 2018 in Blockchain by slayer
• 29,370 points
3,951 views
0 votes
1 answer

Solidity: Code to hide message sender address (msg.sender) when writing on a contract

Hey @Wilson, You can use a smart contract ...READ MORE

answered Sep 14, 2020 in Blockchain by Rajiv
• 8,870 points
1,989 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,220 points
1,464 views
+1 vote
1 answer

Do we need metamask extension to make transactions on ether?

Actually, we don't need Metamask. To make ...READ MORE

answered Apr 8, 2018 in Blockchain by Perry
• 17,100 points
1,213 views
0 votes
1 answer

How do I develop a supply chain DApp on Blockchain?

Either of the ways is acceptable but ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
1,497 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP