unshETH - Dependencies

Counters.sol

The file Counters.sol is a Solidity library that provides a counter structure that can only be incremented, decremented, or reset. The counters can be used to track the number of elements in a mapping, issue ERC721 ids, or count request ids. The library provides methods to get the current value, increment, decrement, and reset the counter.

Counter

A counter structure which holds a value that can only be incremented, decremented, or reset. The value should never be directly accessed by users of the library.

_value

The current value of the counter. This should not be accessed directly.

current

function current(Counter storage counter) internal view returns (uint256)

Get the current value of the counter.

Parameters

  • counter Counter storage: The counter to retrieve the current value from.

Return Values

  • uint256: The current value of the counter.

increment

function increment(Counter storage counter) internal

Increment the value of the counter.

Parameters

  • counter Counter storage: The counter to increment.

decrement

function decrement(Counter storage counter) internal

Decrement the value of the counter.

Parameters

  • counter Counter storage: The counter to decrement.

Errors

  • Counter: decrement overflow: Thrown if an attempt is made to decrement a counter that is already at zero.

reset

function reset(Counter storage counter) internal

Reset the counter to zero.

Parameters

  • counter Counter storage: The counter to reset.

Examples

Usage of Counters library

This example shows how to use the Counters library to create, increment, decrement, and reset a counter.

// Contract that uses the Counters library
contract Example {
    using Counters for Counters.Counter;
    Counters.Counter private _counter;

    function incrementCounter() public {
        Counters.increment(_counter);
    }

    function decrementCounter() public {
        Counters.decrement(_counter);
    }

    function resetCounter() public {
        Counters.reset(_counter);
    }

    function getCounter() public view returns (uint256) {
        return Counters.current(_counter);
    }
}
Previous
Context.sol