Skip to content

Commit

Permalink
Rename AutoIncrementing to Counter (#1307)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo Arias authored and nventuro committed Sep 13, 2018
1 parent 225b492 commit b4f87bb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
21 changes: 0 additions & 21 deletions contracts/mocks/AutoIncrementingImpl.sol

This file was deleted.

21 changes: 21 additions & 0 deletions contracts/mocks/CounterImpl.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pragma solidity ^0.4.24;

import "../utils/Counter.sol";


contract CounterImpl {
using Counter for Counter.Index;

uint256 public theId;

// use whatever key you want to track your counters
mapping(string => Counter.Index) private _counters;

function doThing(string key)
public
returns (uint256)
{
theId = _counters[key].next();
return theId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ pragma solidity ^0.4.24;


/**
* @title AutoIncrementing
* @title Counter
* @author Matt Condon (@shrugs)
* @dev Provides an auto-incrementing uint256 id acquired by the `Counter#nextId` getter.
* @dev Provides an incrementing uint256 id acquired by the `Index#next` getter.
* Use this for issuing ERC721 ids or keeping track of request ids, anything you want, really.
*
* Include with `using AutoIncrementing for AutoIncrementing.Counter;`
* Include with `using Counter for Counter.Index;`
* @notice Does not allow an Id of 0, which is popularly used to signify a null state in solidity.
* Does not protect from overflows, but if you have 2^256 ids, you have other problems.
* (But actually, it's generally impossible to increment a counter this many times, energy wise
* so it's not something you have to worry about.)
*/
library AutoIncrementing {
library Counter {

struct Counter {
uint256 prevId; // default: 0
struct Index {
uint256 currentId; // default: 0
}

function nextId(Counter storage counter)
function next(Index storage index)
internal
returns (uint256)
{
counter.prevId = counter.prevId + 1;
return counter.prevId;
index.currentId = index.currentId + 1;
return index.currentId;
}
}
7 changes: 4 additions & 3 deletions test/AutoIncrementing.test.js → test/Counter.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const AutoIncrementing = artifacts.require('AutoIncrementingImpl');

const Counter = artifacts.require('CounterImpl');

require('chai')
.use(require('chai-bignumber')(web3.BigNumber))
Expand All @@ -8,9 +9,9 @@ const EXPECTED = [1, 2, 3, 4];
const KEY1 = web3.sha3('key1');
const KEY2 = web3.sha3('key2');

contract('AutoIncrementing', function ([_, owner]) {
contract('Counter', function ([_, owner]) {
beforeEach(async function () {
this.mock = await AutoIncrementing.new({ from: owner });
this.mock = await Counter.new({ from: owner });
});

context('custom key', async function () {
Expand Down

0 comments on commit b4f87bb

Please sign in to comment.