NFT contract analysis (4) - Counters.sol
1. Configuration requirements:
1. Environmental requirements: WeBASE-Front
2. Contract language: Solidity >=0.6.0 <0.8.0
2. Counters.sol
pragma solidity >=0.6.0 <0.8.0;
import "./SafeMath.sol";
library Counters {
using SafeMath for uint256;
struct Counter {
uint256 _value;
}
function current(Counter storage counter) internal view returns (uint256) {
return counter._value;
}
function increment(Counter storage counter) internal {
counter._value += 1;
}
function decrement(Counter storage counter) internal {
counter._value = counter._value.sub(1);
}
}
3. Analyze the contract
pragma solidity >=0.6.0 <0.8.0; //1
import "./SafeMath.sol"; //2
library Counters {
//3
using SafeMath for uint256; //4
struct Counter {
//5
uint256 _value; //6
} //7
- Line 1: Declaration version
- Chapter 2: Import SafeMath.SOL Contract
- Line 3: Create library contract Counters
- Line 4: Reference all library functions of SafeMath to the uint256 type of the Counters library contract
- Lines 5-7: Create a Counter structure and declare a state variable _value of type uint256
function current(Counter storage counter) internal view returns (uint256) {
//1
return counter._value; //2
} //3
function increment(Counter storage counter) internal {
//4
counter._value += 1; //5
} //6
function decrement(Counter storage counter) internal {
//7
counter._value = counter._value.sub(1); //8
} //9
- Line 1: create method: current; parameter: structure counter of storage type; internal: built-in function, view: do not change the state variable; returns (uint256): return value type: uint256
- Lines 2-3: return the counter._value value
- Line 4: Create method: increment; parameter: structure counter of storage type; internal: built-in function
- Lines 5-6: The value of counter._value is incremented by 1
- Line 7: Create method decrement; parameter: structure counter of storage type; internal: built-in function
- Lines 8-9: counter._value.sub(1): call SafeMath's sub function, counter._value as the first parameter and 1 as the second parameter; the result is currently accepted by counter._value
Fourth, the last article: NFT contract analysis (3) - SafeMath.sol
NFT contract analysis (3) - SafeMath.sol
5. The next article: To be continued
6. Reference related articles
solidity notes (1) - first
solidity notes (2) - second
solidity notes (3) - abstract usage
solidity notes (4) - freezing and transaction properties
solidity notes (5) - event usage
solidity Notes (6) - modifier usage
solidity notes (7) - storage area memory storage stack
solidity notes (8) - pure usage
solidity notes (9) - library usage
solidity notes (10) - using for usage
solidity notes (11) - struct usage