-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoliditySmartContractTemplate.sol
142 lines (117 loc) · 3.68 KB
/
SoliditySmartContractTemplate.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
├── Pragma (version)
├── Imports
├── Events
├── Errors
├── Interfaces
├── Libraries
└── Contracts
├── Type declarations
├── State variables
├── Events
├── Errors
├── Modifiers
└── Functions
├── Constructor
├── Receive function (if exists)
├── Fallback function (if exists)
├── External
├── Public
├── Internal
├── Private
├── View
└── Pure
*/
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.16 <0.9.0; // ^0.8.16;
// Import other contracts
import "./Ownable.sol"; // Import from local
import "@openzeppelin/contracts/access/Ownable.sol"; // Import from npm
import "github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol"; // Import from GitHub
// Events
event Change(string message, uint newVal);
// Errors
error NotEnoughBalance(uint requested, uint available);
// Interfaces
interface IERC20 {
function transfer(address recipient, uint256 amount) external returns (bool);
}
// Libraries
library SquareLib {
function square(uint a) internal pure returns (uint) {
return a * a;
}
}
// Contract
/// @title Solidity contract template
/// @author Radek Sienkiewicz
/// @notice You can use this contract for quickly and easily building your layout and structure
/// @dev Explain to a developer any extra details here (e.g. "The functions are for illustration purposes only")
contract SimpleContract is Ownable {
// Type declarations
struct Person {
string name;
uint age;
}
// State variables
uint storedData;
address owner;
mapping(address => uint) balances;
uint startingBalance = 1_000_000_000_000_000_000; // Underscores are commonly used in Solidity to improve readability of large numbers. Much easier to read than 1000000000000000000.
// Events
event Change(string message, uint newVal);
// Errors
error NotEnoughBalance(uint requested, uint available);
// Modifiers
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function.");
_;
}
/* *** Functions *** */
// Constructor
constructor(uint initVal) {
storedData = initVal;
}
// Receive function
receive() external payable {
// ...
}
// Fallback function
fallback() external payable {
// ...
}
// External
function externalFunc() external {
// ...
}
function externalViewFunc() external view { // Within a grouping of functions (public, external, internal, private), put view and pure functions last
// ...
}
// A function with a full NatSpec comment
// It is recommended that Solidity contracts are fully annotated using NatSpec for all public interfaces (everything in the ABI).
// All tags in NatSpec are optional. Use what makes sense for your contract.
/// @notice Describe what the function does
/// @dev Add any extra details for developers here
/// @param a Describe a parameter
/// @param b Describe a parameter
/// @return What does it return? (e.g. "The sum of a and b")
function publicFuncWithNatSpec(uint a, uint b) external pure returns (uint) {
return a + b;
}
function externalPureFunc() external pure {
// ...
}
// Public
function publicFunc() public {
// ...
emit Change("Changed!", value); // Triggering event
}
// Internal
function internalFunc() internal {
// ...
}
// Private
function privateFunc() private {
// ...
}
}