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,050 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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:

answered Jul 23, 2018 by Perry
• 16,450 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 ariaholic
• 7,320 points
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 2, 2018 in Blockchain by Shashank
• 10,330 points
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,040 points
+3 votes
4 answers

How do I add a new node on a Hyperledger network?

You can do this by generating the crypto ...READ MORE

answered Apr 6, 2018 in Blockchain by Christine
• 15,790 points
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
0 votes
1 answer
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
• 67,000 points
+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 7, 2018 in Blockchain by Perry
• 16,450 points
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
• 16,450 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.