Skip to content

Commit ab967b8

Browse files
Amxxernestognw
andcommitted
Update the "utilities" documentation page (#4678)
Co-authored-by: ernestognw <ernestognw@gmail.com>
1 parent a34d986 commit ab967b8

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

Diff for: docs/modules/ROOT/pages/utilities.adoc

+31-15
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,45 @@ contract MyContract {
7171
[[math]]
7272
== Math
7373

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.
7575

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:
7777

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 {
85+
(bool overflowsAdd, uint256 resultAdd) = x.tryAdd(y);
86+
(bool overflowsSub, uint256 resultSub) = x.trySub(y);
87+
(bool overflowsMul, uint256 resultMul) = x.tryMul(y);
88+
(bool overflowsDiv, uint256 resultDiv) = x.tryDiv(y);
89+
// ...
90+
}
8391
84-
Easy!
92+
function unsignedAverage(int256 a, int256 b) {
93+
int256 avg = a.average(b);
94+
// ...
95+
}
96+
}
97+
----
8598

86-
[[payment]]
87-
== Payment
99+
Easy!
88100

89-
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
90103

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:
92105

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.
95111

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.
97113

98114
[[misc]]
99115
== Misc

0 commit comments

Comments
 (0)