Commit 658af64e by Leo Arias Committed by Francisco Giordano

Rename AutoIncrementing to Counter (#1307)


(cherry picked from commit b4f87bb8)
parent ed84f3fc
pragma solidity ^0.4.24; pragma solidity ^0.4.24;
import "../utils/AutoIncrementing.sol"; import "../utils/Counter.sol";
contract AutoIncrementingImpl { contract CounterImpl {
using AutoIncrementing for AutoIncrementing.Counter; using Counter for Counter.Index;
uint256 public theId; uint256 public theId;
// use whatever key you want to track your counters // use whatever key you want to track your counters
mapping(string => AutoIncrementing.Counter) private _counters; mapping(string => Counter.Index) private _counters;
function doThing(string key) function doThing(string key)
public public
returns (uint256) returns (uint256)
{ {
theId = _counters[key].nextId(); theId = _counters[key].next();
return theId; return theId;
} }
} }
...@@ -2,28 +2,28 @@ pragma solidity ^0.4.24; ...@@ -2,28 +2,28 @@ pragma solidity ^0.4.24;
/** /**
* @title AutoIncrementing * @title Counter
* @author Matt Condon (@shrugs) * @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. * 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. * @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. * 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 * (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.) * so it's not something you have to worry about.)
*/ */
library AutoIncrementing { library Counter {
struct Counter { struct Index {
uint256 prevId; // default: 0 uint256 currentId; // default: 0
} }
function nextId(Counter storage counter) function next(Index storage index)
internal internal
returns (uint256) returns (uint256)
{ {
counter.prevId = counter.prevId + 1; index.currentId = index.currentId + 1;
return counter.prevId; return index.currentId;
} }
} }
const AutoIncrementing = artifacts.require('AutoIncrementingImpl');
const Counter = artifacts.require('CounterImpl');
require('chai') require('chai')
.use(require('chai-bignumber')(web3.BigNumber)) .use(require('chai-bignumber')(web3.BigNumber))
...@@ -8,9 +9,9 @@ const EXPECTED = [1, 2, 3, 4]; ...@@ -8,9 +9,9 @@ const EXPECTED = [1, 2, 3, 4];
const KEY1 = web3.sha3('key1'); const KEY1 = web3.sha3('key1');
const KEY2 = web3.sha3('key2'); const KEY2 = web3.sha3('key2');
contract('AutoIncrementing', function ([_, owner]) { contract('Counter', function ([_, owner]) {
beforeEach(async function () { beforeEach(async function () {
this.mock = await AutoIncrementing.new({ from: owner }); this.mock = await Counter.new({ from: owner });
}); });
context('custom key', async function () { context('custom key', async function () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment