You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/modules/ROOT/pages/utilities.adoc
+31-15
Original file line number
Diff line number
Diff line change
@@ -71,29 +71,45 @@ contract MyContract {
71
71
[[math]]
72
72
== Math
73
73
74
-
The most popular math related library OpenZeppelin Contracts provides is xref:api:utils.adoc#SafeMath[`SafeMath`], which provides mathematical functions that protect your contract from overflows and underflows.
74
+
Although Solidity already provides math operators (i.e. `+`, `-`, etc.), Contracts includes xref:api:utils.adoc#Math[`Math`]; a set of utilities for dealing with mathematical operators, with support for extra operations (eg. xref:api:utils.adoc#Math-average-uint256-uint256-[`average`]) and xref:api:utils.adoc#SignedMath[`SignedMath`]; a library specialized in signed math operations.
75
75
76
-
Include the contract with `using SafeMath for uint256;` and then call the functions:
76
+
Include these contracts with `using Math for uint256` or `using SignedMath for int256` and then use their functions in your code:
77
77
78
-
* `myNumber.add(otherNumber)`
79
-
* `myNumber.sub(otherNumber)`
80
-
* `myNumber.div(otherNumber)`
81
-
* `myNumber.mul(otherNumber)`
82
-
* `myNumber.mod(otherNumber)`
78
+
[source,solidity]
79
+
----
80
+
contract MyContract {
81
+
using Math for uint256;
82
+
using SignedMath for int256;
83
+
84
+
function tryOperations(uint256 a, uint256 b) internal pure {
Want to split some payments between multiple people? Maybe you have an app that sends 30% of art purchases to the original creator and 70% of the profits to the current owner; you can build that with xref:api:finance.adoc#PaymentSplitter[`PaymentSplitter`]!
101
+
[[structures]]
102
+
== Structures
90
103
91
-
In Solidity, there are some security concerns with blindly sending money to accounts, since it allows them to execute arbitrary code. You can read up on these security concerns in the https://consensys.github.io/smart-contract-best-practices/[Ethereum Smart Contract Best Practices] website.
104
+
Some use cases require more powerful data structures than arrays and mappings offered natively in Solidity. Contracts provides these libraries for enhanced data structure management:
92
105
93
-
[[collections]]
94
-
== Collections
106
+
- xref:api:utils.adoc#BitMaps[`BitMaps`]: Store packed booleans in storage.
107
+
- xref:api:utils.adoc#Checkpoints[`Checkpoints`]: Checkpoint values with built-in lookups.
108
+
- xref:api:utils.adoc#DoubleEndedQueue[`DoubleEndedQueue`]: Store items in a queue with `pop()` and `queue()` constant time operations.
109
+
- xref:api:utils.adoc#EnumerableSet[`EnumerableSet`]: A https://en.wikipedia.org/wiki/Set_(abstract_data_type)[set] with enumeration capabilities.
110
+
- xref:api:utils.adoc#EnumerableMap[`EnumerableMap`]: A `mapping` variant with enumeration capabilities.
95
111
96
-
If you need support for more powerful collections than Solidity's native arrays and mappings, take a look at xref:api:utils.adoc#EnumerableSet[`EnumerableSet`] and xref:api:utils.adoc#EnumerableMap[`EnumerableMap`]. They are similar to mappings in that they store and remove elements in constant time and don't allow for repeated entries, but they also support _enumeration_, which means you can easily query all stored entries both on and off-chain.
112
+
The `Enumerable*` structures are similar to mappings in that they store and remove elements in constant time and don't allow for repeated entries, but they also support _enumeration_, which means you can easily query all stored entries both on and off-chain.
0 commit comments