-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSavingsAccountUtil.sol
128 lines (119 loc) · 4.11 KB
/
SavingsAccountUtil.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
// SPDX-License-Identifier: MIT
pragma solidity 0.7.6;
import '../interfaces/ISavingsAccount.sol';
import '@openzeppelin/contracts/token/ERC20/SafeERC20.sol';
import '@openzeppelin/contracts/utils/ReentrancyGuard.sol';
library SavingsAccountUtil {
using SafeERC20 for IERC20;
function depositFromSavingsAccount(
ISavingsAccount _savingsAccount,
address _from,
address _to,
uint256 _amount,
address _token,
address _strategy,
bool _withdrawShares,
bool _toSavingsAccount
) internal returns (uint256) {
if (_toSavingsAccount) {
return savingsAccountTransfer(_savingsAccount, _from, _to, _amount, _token, _strategy);
} else {
return withdrawFromSavingsAccount(_savingsAccount, _from, _to, _amount, _token, _strategy, _withdrawShares);
}
}
function directDeposit(
ISavingsAccount _savingsAccount,
address _from,
address _to,
uint256 _amount,
address _token,
bool _toSavingsAccount,
address _strategy
) internal returns (uint256) {
if (_toSavingsAccount) {
return directSavingsAccountDeposit(_savingsAccount, _from, _to, _amount, _token, _strategy);
} else {
return transferTokens(_token, _amount, _from, _to);
}
}
function directSavingsAccountDeposit(
ISavingsAccount _savingsAccount,
address _from,
address _to,
uint256 _amount,
address _token,
address _strategy
) internal returns (uint256 _sharesReceived) {
transferTokens(_token, _amount, _from, address(this));
uint256 _ethValue;
if (_token == address(0)) {
_ethValue = _amount;
} else {
address _approveTo = _strategy;
if (_strategy == address(0)) {
_approveTo = address(_savingsAccount);
}
IERC20(_token).safeApprove(_approveTo, _amount);
}
_sharesReceived = _savingsAccount.deposit{value: _ethValue}(_amount, _token, _strategy, _to);
}
function savingsAccountTransfer(
ISavingsAccount _savingsAccount,
address _from,
address _to,
uint256 _amount,
address _token,
address _strategy
) internal returns (uint256) {
if (_from == address(this)) {
_savingsAccount.transfer(_amount, _token, _strategy, _to);
} else {
_savingsAccount.transferFrom(_amount, _token, _strategy, _from, _to);
}
return _amount;
}
function withdrawFromSavingsAccount(
ISavingsAccount _savingsAccount,
address _from,
address _to,
uint256 _amount,
address _token,
address _strategy,
bool _withdrawShares
) internal returns (uint256 _amountReceived) {
if (_from == address(this)) {
_amountReceived = _savingsAccount.withdraw(_amount, _token, _strategy, payable(_to), _withdrawShares);
} else {
_amountReceived = _savingsAccount.withdrawFrom(_amount, _token, _strategy, _from, payable(_to), _withdrawShares);
}
}
function transferTokens(
address _token,
uint256 _amount,
address _from,
address _to
) internal returns (uint256) {
if (_amount == 0) {
return 0;
}
if (_token == address(0)) {
require(msg.value >= _amount, 'ethers provided should be greater than _amount');
if (_to != address(this)) {
(bool success, ) = payable(_to).call{value: _amount}('');
require(success, 'Transfer failed');
}
if (msg.value > _amount) {
(bool success, ) = payable(address(msg.sender)).call{value: msg.value - _amount}('');
require(success, 'Transfer failed');
}
return _amount;
}
if (_from == address(this)) {
IERC20(_token).safeTransfer(_to, _amount);
} else {
//pool
IERC20(_token).safeTransferFrom(_from, _to, _amount);
}
return _amount;
}
}