Liquidity Pool Farm Contracts
Fully Audited: All smart contracts used by Liquidus are fully audited by reputational auditing companies.
Farm | Blockchain | Contract | Lock Duration |
---|---|---|---|
LIQ-BNB LP PancakeSwap | BNB Smart Chain | 0x8B79d987363a3e1EDA2A2eEBb38C242311C6c008 | 1 Month |
LIQ-BUSD LP Apeswap | BNB Smart Chain | 0xB67640F6D742d35650F95E4C0294670E8824adaa | 1 Month |
LIQ-ETH LP Uniswap | Ethereum | 0xd27fAb2323eDB75AF6f4348E3745247E77690E53 | 1 Month |
LIQ-CRO LP Cronaswap | Cronos | 0x1c7fDE0a9619bC81b23cAEF6992288BA5547a34F | 0 Months |
1
// File: @openzeppelin/contracts/utils/Address.sol
2
3
pragma solidity ^0.8.0;
4
5
/**
6
* @dev Collection of functions related to the address type
7
*/
8
library Address {
9
/**
10
* @dev Returns true if `account` is a contract.
11
*
12
* [IMPORTANT]
13
* ====
14
* It is unsafe to assume that an address for which this function returns
15
* false is an externally-owned account (EOA) and not a contract.
16
*
17
* Among others, `isContract` will return false for the following
18
* types of addresses:
19
*
20
* - an externally-owned account
21
* - a contract in construction
22
* - an address where a contract will be created
23
* - an address where a contract lived, but was destroyed
24
* ====
25
*/
26
function isContract(address account) internal view returns (bool) {
27
// This method relies on extcodesize, which returns 0 for contracts in
28
// construction, since the code is only stored at the end of the
29
// constructor execution.
30
31
uint256 size;
32
assembly {
33
size := extcodesize(account)
34
}
35
return size > 0;
36
}
37
38
/**
39
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
40
* `recipient`, forwarding all available gas and reverting on errors.
41
*
42
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
43
* of certain opcodes, possibly making contracts go over the 2300 gas limit
44
* imposed by `transfer`, making them unable to receive funds via
45
* `transfer`. {sendValue} removes this limitation.
46
*
47
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
48
*
49
* IMPORTANT: because control is transferred to `recipient`, care must be
50
* taken to not create reentrancy vulnerabilities. Consider using
51
* {ReentrancyGuard} or the
52
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
53
*/
54
function sendValue(address payable recipient, uint256 amount) internal {
55
require(address(this).balance >= amount, "Address: insufficient balance");
56
57
(bool success, ) = recipient.call{value: amount}("");
58
require(success, "Address: unable to send value, recipient may have reverted");
59
}
60
61
/**
62
* @dev Performs a Solidity function call using a low level `call`. A
63
* plain `call` is an unsafe replacement for a function call: use this
64
* function instead.
65
*
66
* If `target` reverts with a revert reason, it is bubbled up by this
67
* function (like regular Solidity function calls).
68
*
69
* Returns the raw returned data. To convert to the expected return value,
70
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
71
*
72
* Requirements:
73
*
74
* - `target` must be a contract.
75
* - calling `target` with `data` must not revert.
76
*
77
* _Available since v3.1._
78
*/
79
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
80
return functionCall(target, data, "Address: low-level call failed");
81
}
82
83
/**
84
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
85
* `errorMessage` as a fallback revert reason when `target` reverts.
86
*
87
* _Available since v3.1._
88
*/
89
function functionCall(
90
address target,
91
bytes memory data,
92
string memory errorMessage
93
) internal returns (bytes memory) {
94
return functionCallWithValue(target, data, 0, errorMessage);
95
}
96
97
/**
98
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
99
* but also transferring `value` wei to `target`.
100
*
101
* Requirements:
102
*
103
* - the calling contract must have an ETH balance of at least `value`.
104
* - the called Solidity function must be `payable`.
105
*
106
* _Available since v3.1._
107
*/
108
function functionCallWithValue(
109
address target,
110
bytes memory data,
111
uint256 value
112
) internal returns (bytes memory) {
113
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
114
}
115
116
/**
117
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
118
* with `errorMessage` as a fallback revert reason when `target` reverts.
119
*
120
* _Available since v3.1._
121
*/
122
function functionCallWithValue(
123
address target,
124
bytes memory data,
125
uint256 value,
126
string memory errorMessage
127
) internal returns (bytes memory) {
128
require(address(this).balance >= value, "Address: insufficient balance for call");
129
require(isContract(target), "Address: call to non-contract");
130
131
(bool success, bytes memory returndata) = target.call{value: value}(data);
132
return verifyCallResult(success, returndata, errorMessage);
133
}
134
135
/**
136
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
137
* but performing a static call.
138
*
139
* _Available since v3.3._
140
*/
141
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
142
return functionStaticCall(target, data, "Address: low-level static call failed");
143
}
144
145
/**
146
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
147
* but performing a static call.
148
*
149
* _Available since v3.3._
150
*/
151
function functionStaticCall(
152
address target,
153
bytes memory data,
154
string memory errorMessage
155
) internal view returns (bytes memory) {
156
require(isContract(target), "Address: static call to non-contract");
157
158
(bool success, bytes memory returndata) = target.staticcall(data);
159
return verifyCallResult(success, returndata, errorMessage);
160
}
161
162
/**
163
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
164
* but performing a delegate call.
165
*
166
* _Available since v3.4._
167
*/
168
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
169
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
170
}
171
172
/**
173
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
174
* but performing a delegate call.
175
*
176
* _Available since v3.4._
177
*/
178
function functionDelegateCall(
179
address target,
180
bytes memory data,
181
string memory errorMessage
182
) internal returns (bytes memory) {
183
require(isContract(target), "Address: delegate call to non-contract");
184
185
(bool success, bytes memory returndata) = target.delegatecall(data);
186
return verifyCallResult(success, returndata, errorMessage);
187
}
188
189
/**
190
* @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
191
* revert reason using the provided one.
192
*
193
* _Available since v4.3._
194
*/
195
function verifyCallResult(
196
bool success,
197
bytes memory returndata,
198
string memory errorMessage
199
) internal pure returns (bytes memory) {
200
if (success) {
201
return returndata;
202
} else {
203
// Look for revert reason and bubble it up if present
204
if (returndata.length > 0) {
205
// The easiest way to bubble the revert reason is using memory via assembly
206
207
assembly {
208
let returndata_size := mload(returndata)
209
revert(add(32, returndata), returndata_size)
210
}
211
} else {
212
revert(errorMessage);
213
}
214
}
215
}
216
}
217
218
// File: @openzeppelin/contracts/token/ERC20/IERC20.sol
219
220
221
222
pragma solidity ^0.8.0;
223
224
/**
225
* @dev Interface of the ERC20 standard as defined in the EIP.
226
*/
227
interface IERC20 {
228
/**
229
* @dev Returns the amount of tokens in existence.
230
*/
231
function totalSupply() external view returns (uint256);
232
233
/**
234
* @dev Returns the amount of tokens owned by `account`.
235
*/
236
function balanceOf(address account) external view returns (uint256);
237
238
/**
239
* @dev Moves `amount` tokens from the caller's account to `recipient`.
240
*
241
* Returns a boolean value indicating whether the operation succeeded.
242
*
243
* Emits a {Transfer} event.
244
*/
245
function transfer(address recipient, uint256 amount) external returns (bool);
246
247
/**
248
* @dev Returns the remaining number of tokens that `spender` will be
249
* allowed to spend on behalf of `owner` through {transferFrom}. This is
250
* zero by default.
251
*
252
* This value changes when {approve} or {transferFrom} are called.
253
*/
254
function allowance(address owner, address spender) external view returns (uint256);
255
256
/**
257
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
258
*
259
* Returns a boolean value indicating whether the operation succeeded.
260
*
261
* IMPORTANT: Beware that changing an allowance with this method brings the risk
262
* that someone may use both the old and the new allowance by unfortunate
263
* transaction ordering. One possible solution to mitigate this race
264
* condition is to first reduce the spender's allowance to 0 and set the
265
* desired value afterwards:
266
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
267
*
268
* Emits an {Approval} event.
269
*/
270
function approve(address spender, uint256 amount) external returns (bool);
271
272
/**
273
* @dev Moves `amount` tokens from `sender` to `recipient` using the
274
* allowance mechanism. `amount` is then deducted from the caller's
275
* allowance.
276
*
277
* Returns a boolean value indicating whether the operation succeeded.
278
*
279
* Emits a {Transfer} event.
280
*/
281
function transferFrom(
282
address sender,
283
address recipient,
284
uint256 amount
285
) external returns (bool);
286
287
/**
288
* @dev Emitted when `value` tokens are moved from one account (`from`) to
289
* another (`to`).
290
*
291
* Note that `value` may be zero.
292
*/
293
event Transfer(address indexed from, address indexed to, uint256 value);
294
295
/**
296
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
297
* a call to {approve}. `value` is the new allowance.
298
*/
299
event Approval(address indexed owner, address indexed spender, uint256 value);
300
}
301
302
// File: @openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol
303
304
305
306
pragma solidity ^0.8.0;
307
308
309
310
/**
311
* @title SafeERC20
312
* @dev Wrappers around ERC20 operations that throw on failure (when the token
313
* contract returns false). Tokens that return no value (and instead revert or
314
* throw on failure) are also supported, non-reverting calls are assumed to be
315
* successful.
316
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
317
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
318
*/
319
library SafeERC20 {
320
using Address for address;
321
322
function safeTransfer(
323
IERC20 token,
324
address to,
325
uint256 value
326
) internal {
327
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
328
}
329
330
function safeTransferFrom(
331
IERC20 token,
332
address from,
333
address to,
334
uint256 value
335
) internal {
336
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
337
}
338
339
/**
340
* @dev Deprecated. This function has issues similar to the ones found in
341
* {IERC20-approve}, and its usage is discouraged.
342
*
343
* Whenever possible, use {safeIncreaseAllowance} and
344
* {safeDecreaseAllowance} instead.
345
*/
346
function safeApprove(
347
IERC20 token,
348
address spender,
349
uint256 value
350
) internal {
351
// safeApprove should only be called when setting an initial allowance,
352
// or when resetting it to zero. To increase and decrease it, use
353
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
354
require(
355
(value == 0) || (token.allowance(address(this), spender) == 0),
356
"SafeERC20: approve from non-zero to non-zero allowance"
357
);
358
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
359
}
360
361
function safeIncreaseAllowance(
362
IERC20 token,
363
address spender,
364
uint256 value
365
) internal {
366
uint256 newAllowance = token.allowance(address(this), spender) + value;
367
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
368
}
369
370
function safeDecreaseAllowance(
371
IERC20 token,
372
address spender,
373
uint256 value
374
) internal {
375
unchecked {
376
uint256 oldAllowance = token.allowance(address(this), spender);
377
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
378
uint256 newAllowance = oldAllowance - value;
379
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
380
}
381
}
382
383
/**
384
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
385
* on the return value: the return value is optional (but if data is returned, it must not be false).
386
* @param token The token targeted by the call.
387
* @param data The call data (encoded using abi.encode or one of its variants).
388
*/
389
function _callOptionalReturn(IERC20 token, bytes memory data) private {
390
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
391
// we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
392
// the target address contains contract code and also asserts for success in the low-level call.
393
394
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
395
if (returndata.length > 0) {
396
// Return data is optional
397
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
398
}
399
}
400
}
401
402
// File: @openzeppelin/contracts/utils/math/SafeCast.sol
403
404
405
406
pragma solidity ^0.8.0;
407
408
/**
409
* @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
410
* checks.
411
*
412
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
413
* easily result in undesired exploitation or bugs, since developers usually
414
* assume that overflows raise errors. `SafeCast` restores this intuition by
415
* reverting the transaction when such an operation overflows.
416
*
417
* Using this library instead of the unchecked operations eliminates an entire
418
* class of bugs, so it's recommended to use it always.
419
*
420
* Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
421
* all math on `uint256` and `int256` and then downcasting.
422
*/
423
library SafeCast {
424
/**
425
* @dev Returns the downcasted uint224 from uint256, reverting on
426
* overflow (when the input is greater than largest uint224).
427
*
428
* Counterpart to Solidity's `uint224` operator.
429
*
430
* Requirements:
431
*
432
* - input must fit into 224 bits
433
*/
434
function toUint224(uint256 value) internal pure returns (uint224) {
435
require(value <= type(uint224).max, "SafeCast: value doesn't fit in 224 bits");
436
return uint224(value);
437
}
438
439
/**
440
* @dev Returns the downcasted uint128 from uint256, reverting on
441
* overflow (when the input is greater than largest uint128).
442
*
443
* Counterpart to Solidity's `uint128` operator.
444
*
445
* Requirements:
446
*
447
* - input must fit into 128 bits
448
*/
449
function toUint128(uint256 value) internal pure returns (uint128) {
450
require(value <= type(uint128).max, "SafeCast: value doesn't fit in 128 bits");
451
return uint128(value);
452
}
453
454
/**
455
* @dev Returns the downcasted uint96 from uint256, reverting on
456
* overflow (when the input is greater than largest uint96).
457
*
458
* Counterpart to Solidity's `uint96` operator.
459
*
460
* Requirements:
461
*
462
* - input must fit into 96 bits
463
*/
464
function toUint96(uint256 value) internal pure returns (uint96) {
465
require(value <= type(uint96).max, "SafeCast: value doesn't fit in 96 bits");
466
return uint96(value);
467
}
468
469
/**
470
* @dev Returns the downcasted uint64 from uint256, reverting on
471
* overflow (when the input is greater than largest uint64).
472
*
473
* Counterpart to Solidity's `uint64` operator.
474
*
475
* Requirements:
476
*
477
* - input must fit into 64 bits
478
*/
479
function toUint64(uint256 value) internal pure returns (uint64) {
480
require(value <= type(uint64).max, "SafeCast: value doesn't fit in 64 bits");
481
return uint64(value);
482
}
483
484
/**
485
* @dev Returns the downcasted uint32 from uint256, reverting on
486
* overflow (when the input is greater than largest uint32).
487
*
488
* Counterpart to Solidity's `uint32` operator.
489
*
490
* Requirements:
491
*
492
* - input must fit into 32 bits
493
*/
494
function toUint32(uint256 value) internal pure returns (uint32) {
495
require(value <= type(uint32).max, "SafeCast: value doesn't fit in 32 bits");
496
return uint32(value);
497
}
498
499
/**
500
* @dev Returns the downcasted uint16 from uint256, reverting on
501
* overflow (when the input is greater than largest uint16).
502
*
503
* Counterpart to Solidity's `uint16` operator.
504
*
505
* Requirements:
506
*
507
* - input must fit into 16 bits
508
*/
509
function toUint16(uint256 value) internal pure returns (uint16) {
510
require(value <= type(uint16).max, "SafeCast: value doesn't fit in 16 bits");
511
return uint16(value);
512
}
513
514
/**
515
* @dev Returns the downcasted uint8 from uint256, reverting on
516
* overflow (when the input is greater than largest uint8).
517
*
518
* Counterpart to Solidity's `uint8` operator.
519
*
520
* Requirements:
521
*
522
* - input must fit into 8 bits.
523
*/
524
function toUint8(uint256 value) internal pure returns (uint8) {
525
require(value <= type(uint8).max, "SafeCast: value doesn't fit in 8 bits");
526
return uint8(value);
527
}
528
529
/**
530
* @dev Converts a signed int256 into an unsigned uint256.
531
*
532
* Requirements:
533
*
534
* - input must be greater than or equal to 0.
535
*/
536
function toUint256(int256 value) internal pure returns (uint256) {
537
require(value >= 0, "SafeCast: value must be positive");
538
return uint256(value);
539
}
540
541
/**
542
* @dev Returns the downcasted int128 from int256, reverting on
543
* overflow (when the input is less than smallest int128 or
544
* greater than largest int128).
545
*
546
* Counterpart to Solidity's `int128` operator.
547
*
548
* Requirements:
549
*
550
* - input must fit into 128 bits
551
*
552
* _Available since v3.1._
553
*/
554
function toInt128(int256 value) internal pure returns (int128) {
555
require(value >= type(int128).min && value <= type(int128).max, "SafeCast: value doesn't fit in 128 bits");
556
return int128(value);
557
}
558
559
/**
560
* @dev Returns the downcasted int64 from int256, reverting on
561
* overflow (when the input is less than smallest int64 or
562
* greater than largest int64).
563
*
564
* Counterpart to Solidity's `int64` operator.
565
*
566
* Requirements:
567
*
568
* - input must fit into 64 bits
569
*
570
* _Available since v3.1._
571
*/
572
function toInt64(int256 value) internal pure returns (int64) {
573
require(value >= type(int64).min && value <= type(int64).max, "SafeCast: value doesn't fit in 64 bits");
574
return int64(value);
575
}
576
577
/**
578
* @dev Returns the downcasted int32 from int256, reverting on
579
* overflow (when the input is less than smallest int32 or
580
* greater than largest int32).
581
*
582
* Counterpart to Solidity's `int32` operator.
583
*
584
* Requirements:
585
*
586
* - input must fit into 32 bits
587
*
588
* _Available since v3.1._
589
*/
590
function toInt32(int256 value) internal pure returns (int32) {
591
require(value >= type(int32).min && value <= type(int32).max, "SafeCast: value doesn't fit in 32 bits");
592
return int32(value);
593
}
594
595
/**
596
* @dev Returns the downcasted int16 from int256, reverting on
597
* overflow (when the input is less than smallest int16 or
598
* greater than largest int16).
599
*
600
* Counterpart to Solidity's `int16` operator.
601
*
602
* Requirements:
603
*
604
* - input must fit into 16 bits
605
*
606
* _Available since v3.1._
607
*/
608
function toInt16(int256 value) internal pure returns (int16) {
609
require(value >= type(int16).min && value <= type(int16).max, "SafeCast: value doesn't fit in 16 bits");
610
return int16(value);
611
}
612
613
/**
614
* @dev Returns the downcasted int8 from int256, reverting on
615
* overflow (when the input is less than smallest int8 or
616
* greater than largest int8).
617
*
618
* Counterpart to Solidity's `int8` operator.
619
*
620
* Requirements:
621
*
622
* - input must fit into 8 bits.
623
*
624
* _Available since v3.1._
625
*/
626
function toInt8(int256 value) internal pure returns (int8) {
627
require(value >= type(int8).min && value <= type(int8).max, "SafeCast: value doesn't fit in 8 bits");
628
return int8(value);
629
}
630
631
/**
632
* @dev Converts an unsigned uint256 into a signed int256.
633
*
634
* Requirements:
635
*
636
* - input must be less than or equal to maxInt256.
637
*/
638
function toInt256(uint256 value) internal pure returns (int256) {
639
// Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
640
require(value <= uint256(type(int256).max), "SafeCast: value doesn't fit in an int256");
641
return int256(value);
642
}
643
}
644
645
// File: @openzeppelin/contracts/utils/math/SignedSafeMath.sol
646
647
648
649
pragma solidity ^0.8.0;
650
651
/**
652
* @dev Wrappers over Solidity's arithmetic operations.
653
*
654
* NOTE: `SignedSafeMath` is no longer needed starting with Solidity 0.8. The compiler
655
* now has built in overflow checking.
656
*/
657
library SignedSafeMath {
658
/**
659
* @dev Returns the multiplication of two signed integers, reverting on
660
* overflow.
661
*
662
* Counterpart to Solidity's `*` operator.
663
*
664
* Requirements:
665
*
666
* - Multiplication cannot overflow.
667
*/
668
function mul(int256 a, int256 b) internal pure returns (int256) {
669
return a * b;
670
}
671
672
/**
673
* @dev Returns the integer division of two signed integers. Reverts on
674
* division by zero. The result is rounded towards zero.
675
*
676
* Counterpart to Solidity's `/` operator.
677
*
678
* Requirements:
679
*
680
* - The divisor cannot be zero.
681
*/
682
function div(int256 a, int256 b) internal pure returns (int256) {
683
return a / b;
684
}
685
686
/**
687
* @dev Returns the subtraction of two signed integers, reverting on
688
* overflow.
689
*
690
* Counterpart to Solidity's `-` operator.
691
*
692
* Requirements:
693
*
694
* - Subtraction cannot overflow.
695
*/
696
function sub(int256 a, int256 b) internal pure returns (int256) {
697
return a - b;
698
}
699
700
/**
701
* @dev Returns the addition of two signed integers, reverting on
702
* overflow.
703
*
704
* Counterpart to Solidity's `+` operator.
705
*
706
* Requirements:
707
*
708
* - Addition cannot overflow.
709
*/
710
function add(int256 a, int256 b) internal pure returns (int256) {
711
return a + b;
712
}
713
}
714
715
// File: @openzeppelin/contracts/utils/math/SafeMath.sol
716
717
718
719
pragma solidity ^0.8.0;
720
721
// CAUTION
722
// This version of SafeMath should only be used with Solidity 0.8 or later,
723
// because it relies on the compiler's built in overflow checks.
724
725
/**
726
* @dev Wrappers over Solidity's arithmetic operations.
727
*
728
* NOTE: `SafeMath` is no longer needed starting with Solidity 0.8. The compiler
729
* now has built in overflow checking.
730
*/
731
library SafeMath {
732
/**
733
* @dev Returns the addition of two unsigned integers, with an overflow flag.
734
*
735
* _Available since v3.4._
736
*/
737
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
738
unchecked {
739
uint256 c = a + b;
740
if (c < a) return (false, 0);
741
return (true, c);
742
}
743
}
744
745
/**
746
* @dev Returns the substraction of two unsigned integers, with an overflow flag.
747
*
748
* _Available since v3.4._
749
*/
750
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
751
unchecked {
752
if (b > a) return (false, 0);
753
return (true, a - b);
754
}
755
}
756
757
/**
758
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
759
*
760
* _Available since v3.4._
761
*/
762
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
763
unchecked {
764
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
765
// benefit is lost if 'b' is also tested.
766
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
767
if (a == 0) return (true, 0);
768
uint256 c = a * b;
769
if (c / a != b) return (false, 0);
770
return (true, c);
771
}
772
}
773
774
/**
775
* @dev Returns the division of two unsigned integers, with a division by zero flag.
776
*
777
* _Available since v3.4._
778
*/
779
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
780
unchecked {
781
if (b == 0) return (false, 0);
782
return (true, a / b);
783
}
784
}
785
786
/**
787
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
788
*
789
* _Available since v3.4._
790
*/
791
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
792
unchecked {
793
if (b == 0) return (false, 0);
794
return (true, a % b);
795
}
796
}
797
798
/**
799
* @dev Returns the addition of two unsigned integers, reverting on
800
* overflow.
801
*
802
* Counterpart to Solidity's `+` operator.
803
*
804
* Requirements:
805
*
806
* - Addition cannot overflow.
807
*/
808
function add(uint256 a, uint256 b) internal pure returns (uint256) {
809
return a + b;
810
}
811
812
/**
813
* @dev Returns the subtraction of two unsigned integers, reverting on
814
* overflow (when the result is negative).
815
*
816
* Counterpart to Solidity's `-` operator.
817
*
818
* Requirements:
819
*
820
* - Subtraction cannot overflow.
821
*/
822
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
823
return a - b;
824
}
825
826
/**
827
* @dev Returns the multiplication of two unsigned integers, reverting on
828
* overflow.
829
*
830
* Counterpart to Solidity's `*` operator.
831
*
832
* Requirements:
833
*
834
* - Multiplication cannot overflow.
835
*/
836
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
837
return a * b;
838
}
839
840
/**
841
* @dev Returns the integer division of two unsigned integers, reverting on
842
* division by zero. The result is rounded towards zero.
843
*
844
* Counterpart to Solidity's `/` operator.
845
*
846
* Requirements:
847
*
848
* - The divisor cannot be zero.
849
*/
850
function div(uint256 a, uint256 b) internal pure returns (uint256) {
851
return a / b;
852
}
853
854
/**
855
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
856
* reverting when dividing by zero.
857
*
858
* Counterpart to Solidity's `%` operator. This function uses a `revert`
859
* opcode (which leaves remaining gas untouched) while Solidity uses an
860
* invalid opcode to revert (consuming all remaining gas).
861
*
862
* Requirements:
863
*
864
* - The divisor cannot be zero.
865
*/
866
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
867
return a % b;
868
}
869
870
/**
871
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
872
* overflo