-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathNTransferUtilV2.sol
More file actions
86 lines (74 loc) · 3.24 KB
/
NTransferUtilV2.sol
File metadata and controls
86 lines (74 loc) · 3.24 KB
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
/* solhint-disable */
// Neptune Mutual Protocol (https://neptunemutual.com)
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "openzeppelin-solidity/contracts/token/ERC20/utils/SafeERC20.sol";
library NTransferUtilV2 {
using SafeERC20 for IERC20;
/**
*
* @dev Ensures approval of ERC20-like token
* @custom:suppress-malicious-erc The address `malicious` can't be trusted and therefore we are ensuring that it does not act funny.
* @custom:suppress-address-trust-issue The address `malicious` can't be trusted and therefore we are ensuring that it does not act funny.
*
*/
function ensureApproval(
IERC20 malicious,
address spender,
uint256 amount
) external {
require(address(malicious) != address(0), "Invalid token address");
require(spender != address(0), "Invalid spender");
require(amount > 0, "Invalid transfer amount");
malicious.safeIncreaseAllowance(spender, amount);
}
/**
* @dev Ensures transfer of ERC20-like token
*
* @custom:suppress-malicious-erc The address `malicious` can't be trusted and therefore we are ensuring that it does not act funny.
* @custom:suppress-address-trust-issue The address `malicious` can't be trusted and therefore we are ensuring that it does not act funny.
* The address `recipient` can be trusted as we're not treating (or calling) it as a contract.
*
*/
function ensureTransfer(
IERC20 malicious,
address recipient,
uint256 amount
) external {
require(address(malicious) != address(0), "Invalid token address");
require(recipient != address(0), "Spender can't be zero");
require(amount > 0, "Invalid transfer amount");
uint256 balanceBeforeTransfer = malicious.balanceOf(recipient);
malicious.safeTransfer(recipient, amount);
uint256 balanceAfterTransfer = malicious.balanceOf(recipient);
// @suppress-subtraction
uint256 actualTransferAmount = balanceAfterTransfer - balanceBeforeTransfer;
require(actualTransferAmount == amount, "Invalid transfer");
}
/**
* @dev Ensures transferFrom of ERC20-like token
*
* @custom:suppress-malicious-erc The address `malicious` can't be trusted and therefore we are ensuring that it does not act funny.
* @custom:suppress-address-trust-issue The address `malicious` can't be trusted and therefore we are ensuring that it does not act funny.
* The address `recipient` can be trusted as we're not treating (or calling) it as a contract.
*
*/
function ensureTransferFrom(
IERC20 malicious,
address sender,
address recipient,
uint256 amount
) external {
require(address(malicious) != address(0), "Invalid token address");
require(sender != address(0), "Invalid sender");
require(recipient != address(0), "Invalid recipient");
require(amount > 0, "Invalid transfer amount");
uint256 balanceBeforeTransfer = malicious.balanceOf(recipient);
malicious.safeTransferFrom(sender, recipient, amount);
uint256 balanceAfterTransfer = malicious.balanceOf(recipient);
// @suppress-subtraction
uint256 actualTransferAmount = balanceAfterTransfer - balanceBeforeTransfer;
require(actualTransferAmount == amount, "Invalid transfer");
}
}