Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
openzeppelin-contracts-upgradeable
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
俞永鹏
openzeppelin-contracts-upgradeable
Commits
658af64e
Commit
658af64e
authored
Sep 13, 2018
by
Leo Arias
Committed by
Francisco Giordano
Sep 18, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename AutoIncrementing to Counter (#1307)
(cherry picked from commit
b4f87bb8
)
parent
ed84f3fc
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
17 deletions
+18
-17
CounterImpl.sol
contracts/mocks/CounterImpl.sol
+5
-5
Counter.sol
contracts/utils/Counter.sol
+9
-9
Counter.test.js
test/Counter.test.js
+4
-3
No files found.
contracts/mocks/
AutoIncrementing
Impl.sol
→
contracts/mocks/
Counter
Impl.sol
View file @
658af64e
pragma solidity ^0.4.24;
import "../utils/
AutoIncrementing
.sol";
import "../utils/
Counter
.sol";
contract
AutoIncrementing
Impl {
using
AutoIncrementing for AutoIncrementing.Counter
;
contract
Counter
Impl {
using
Counter for Counter.Index
;
uint256 public theId;
// 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)
public
returns (uint256)
{
theId = _counters[key].next
Id
();
theId = _counters[key].next();
return theId;
}
}
contracts/utils/
AutoIncrementing
.sol
→
contracts/utils/
Counter
.sol
View file @
658af64e
...
...
@@ -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
prev
Id; // default: 0
struct
Index
{
uint256
current
Id; // default: 0
}
function next
Id(Counter storage counter
)
function next
(Index storage index
)
internal
returns (uint256)
{
counter.prevId = counter.prev
Id + 1;
return
counter.prev
Id;
index.currentId = index.current
Id + 1;
return
index.current
Id;
}
}
test/
AutoIncrementing
.test.js
→
test/
Counter
.test.js
View file @
658af64e
const
AutoIncrementing
=
artifacts
.
require
(
'AutoIncrementingImpl'
);
const
Counter
=
artifacts
.
require
(
'CounterImpl'
);
require
(
'chai'
)
.
use
(
require
(
'chai-bignumber'
)(
web3
.
BigNumber
))
...
...
@@ -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
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment