-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathIssuance.sol
93 lines (72 loc) · 2.75 KB
/
Issuance.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity =0.8.18;
import {VerifiableAddressArray} from "src/lib/VArray.sol";
import {IIndexToken} from "src/interfaces/IIndexToken.sol";
import {TokenInfo} from "src/Common.sol";
import {IVault} from "src/interfaces/IVault.sol";
import {SCALAR, fmul, fdiv} from "src/lib/FixedPoint.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IIssuance} from "src/interfaces/IIssuance.sol";
contract Issuance is IIssuance {
using VerifiableAddressArray for VerifiableAddressArray.VerifiableArray;
using SafeERC20 for IERC20;
IVault public immutable vault;
uint256 public reentrancyLock = 1;
modifier invariantCheck() {
_;
vault.invariantCheck();
}
modifier reentrancyGuard() {
if (reentrancyLock > 1) revert IssuanceReentrant();
reentrancyLock = 2;
_;
reentrancyLock = 1;
}
constructor(address _vault) {
vault = IVault(_vault);
}
/// @notice Issue index tokens
/// @param amount The amount of index tokens to issue
/// @dev requires approval of underlying tokens
/// @dev reentrancy guard in case callback in tokens
function issue(uint256 amount) external invariantCheck reentrancyGuard {
TokenInfo[] memory tokens = vault.virtualUnits();
if (tokens.length == 0) revert IssuanceNoTokens();
for (uint256 i; i < tokens.length; ) {
uint256 underlyingAmount = fmul(tokens[i].units + 1, amount) + 1;
IERC20(tokens[i].token).safeTransferFrom(
msg.sender,
address(vault),
underlyingAmount
);
unchecked {
++i;
}
}
vault.invokeMint(msg.sender, amount);
}
/// @notice Redeem index tokens
/// @param amount The amount of index tokens to redeem
/// @dev reentrancy guard in case callback in tokens
function redeem(uint256 amount) external invariantCheck reentrancyGuard {
TokenInfo[] memory tokens = vault.virtualUnits();
if (tokens.length == 0) revert IssuanceNoTokens();
IVault.InvokeERC20Args[] memory args = new IVault.InvokeERC20Args[](
tokens.length
);
for (uint256 i; i < tokens.length; ) {
uint256 underlyingAmount = fmul(tokens[i].units, amount);
args[i] = IVault.InvokeERC20Args({
token: tokens[i].token,
to: msg.sender,
amount: underlyingAmount
});
unchecked {
++i;
}
}
vault.invokeBurn(msg.sender, amount);
vault.invokeERC20s(args);
}
}