-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathGateway.sol
337 lines (307 loc) · 12.4 KB
/
Gateway.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.18;
pragma abicoder v2;
import {Messages} from "./Messages.sol";
import "../util/Auth.sol";
interface InvestmentManagerLike {
function handleExecutedDecreaseInvestOrder(
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 currencyPayout
) external;
function handleExecutedDecreaseRedeemOrder(
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 trancheTokensPayout
) external;
function handleExecutedCollectInvest(
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 currencyPayout,
uint128 trancheTokensPayout
) external;
function handleExecutedCollectRedeem(
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 currencyPayout,
uint128 trancheTokensPayout
) external;
}
interface PoolManagerLike {
function addPool(uint64 poolId) external;
function allowPoolCurrency(uint64 poolId, uint128 currency) external;
function addTranche(
uint64 poolId,
bytes16 trancheId,
string memory tokenName,
string memory tokenSymbol,
uint8 decimals,
uint128 price
) external;
function updateMember(uint64 poolId, bytes16 trancheId, address user, uint64 validUntil) external;
function updateTrancheTokenPrice(uint64 poolId, bytes16 trancheId, uint128 price) external;
function updateTrancheTokenMetadata(
uint64 poolId,
bytes16 trancheId,
string memory tokenName,
string memory tokenSymbol
) external;
function addCurrency(uint128 currency, address currencyAddress) external;
function handleTransfer(uint128 currency, address recipient, uint128 amount) external;
function handleTransferTrancheTokens(uint64 poolId, bytes16 trancheId, address destinationAddress, uint128 amount)
external;
}
interface RouterLike {
function send(bytes memory message) external;
}
interface AuthLike {
function rely(address usr) external;
}
interface RootLike {
function paused() external returns (bool);
function scheduleRely(address target) external;
function cancelRely(address target) external;
}
contract Gateway is Auth {
RootLike public immutable root;
InvestmentManagerLike public immutable investmentManager;
PoolManagerLike public immutable poolManager;
mapping(address => bool) public incomingRouters;
RouterLike public outgoingRouter;
// --- Events ---
event AddIncomingRouter(address indexed router);
event RemoveIncomingRouter(address indexed router);
event UpdateOutgoingRouter(address indexed router);
constructor(address root_, address investmentManager_, address poolManager_, address router_) {
root = RootLike(root_);
investmentManager = InvestmentManagerLike(investmentManager_);
poolManager = PoolManagerLike(poolManager_);
incomingRouters[router_] = true;
outgoingRouter = RouterLike(router_);
wards[msg.sender] = 1;
emit Rely(msg.sender);
}
modifier onlyInvestmentManager() {
require(msg.sender == address(investmentManager), "Gateway/only-investment-manager-allowed-to-call");
_;
}
modifier onlyPoolManager() {
require(msg.sender == address(poolManager), "Gateway/only-token-manager-allowed-to-call");
_;
}
modifier onlyIncomingRouter() {
require(incomingRouters[msg.sender], "Gateway/only-router-allowed-to-call");
_;
}
modifier pauseable() {
require(!root.paused(), "Gateway/paused");
_;
}
// --- Administration ---
function addIncomingRouter(address router) public auth {
incomingRouters[router] = true;
emit AddIncomingRouter(router);
}
function removeIncomingRouter(address router) public auth {
incomingRouters[router] = false;
emit RemoveIncomingRouter(router);
}
function updateOutgoingRouter(address router) public auth {
outgoingRouter = RouterLike(router);
emit UpdateOutgoingRouter(router);
}
// --- Outgoing ---
function transferTrancheTokensToCentrifuge(
uint64 poolId,
bytes16 trancheId,
address sender,
bytes32 destinationAddress,
uint128 amount
) public onlyPoolManager pauseable {
outgoingRouter.send(
Messages.formatTransferTrancheTokens(
poolId,
trancheId,
addressToBytes32(sender),
Messages.formatDomain(Messages.Domain.Centrifuge),
destinationAddress,
amount
)
);
}
function transferTrancheTokensToEVM(
uint64 poolId,
bytes16 trancheId,
address sender,
uint64 destinationChainId,
address destinationAddress,
uint128 amount
) public onlyPoolManager pauseable {
outgoingRouter.send(
Messages.formatTransferTrancheTokens(
poolId,
trancheId,
addressToBytes32(sender),
Messages.formatDomain(Messages.Domain.EVM, destinationChainId),
destinationAddress,
amount
)
);
}
function transfer(uint128 token, address sender, bytes32 receiver, uint128 amount)
public
onlyPoolManager
pauseable
{
outgoingRouter.send(Messages.formatTransfer(token, addressToBytes32(sender), receiver, amount));
}
function increaseInvestOrder(uint64 poolId, bytes16 trancheId, address investor, uint128 currency, uint128 amount)
public
onlyInvestmentManager
pauseable
{
outgoingRouter.send(
Messages.formatIncreaseInvestOrder(poolId, trancheId, addressToBytes32(investor), currency, amount)
);
}
function decreaseInvestOrder(uint64 poolId, bytes16 trancheId, address investor, uint128 currency, uint128 amount)
public
onlyInvestmentManager
pauseable
{
outgoingRouter.send(
Messages.formatDecreaseInvestOrder(poolId, trancheId, addressToBytes32(investor), currency, amount)
);
}
function increaseRedeemOrder(uint64 poolId, bytes16 trancheId, address investor, uint128 currency, uint128 amount)
public
onlyInvestmentManager
pauseable
{
outgoingRouter.send(
Messages.formatDecreaseRedeemOrder(poolId, trancheId, addressToBytes32(investor), currency, amount)
);
}
function collectInvest(uint64 poolId, bytes16 trancheId, address investor, uint128 currency)
public
onlyInvestmentManager
pauseable
{
outgoingRouter.send(Messages.formatCollectInvest(poolId, trancheId, addressToBytes32(investor), currency));
}
function collectRedeem(uint64 poolId, bytes16 trancheId, address investor, uint128 currency)
public
onlyInvestmentManager
pauseable
{
outgoingRouter.send(Messages.formatCollectRedeem(poolId, trancheId, addressToBytes32(investor), currency));
}
function cancelInvestOrder(uint64 poolId, bytes16 trancheId, address investor, uint128 currency)
public
onlyInvestmentManager
pauseable
{
outgoingRouter.send(Messages.formatCancelInvestOrder(poolId, trancheId, addressToBytes32(investor), currency));
}
function cancelRedeemOrder(uint64 poolId, bytes16 trancheId, address investor, uint128 currency)
public
onlyInvestmentManager
pauseable
{
outgoingRouter.send(Messages.formatCancelRedeemOrder(poolId, trancheId, addressToBytes32(investor), currency));
}
// --- Incoming ---
function handle(bytes memory _msg) external onlyIncomingRouter pauseable {
if (Messages.isAddCurrency(_msg)) {
(uint128 currency, address currencyAddress) = Messages.parseAddCurrency(_msg);
poolManager.addCurrency(currency, currencyAddress);
} else if (Messages.isAddPool(_msg)) {
(uint64 poolId) = Messages.parseAddPool(_msg);
poolManager.addPool(poolId);
} else if (Messages.isAllowPoolCurrency(_msg)) {
(uint64 poolId, uint128 currency) = Messages.parseAllowPoolCurrency(_msg);
poolManager.allowPoolCurrency(poolId, currency);
} else if (Messages.isAddTranche(_msg)) {
(
uint64 poolId,
bytes16 trancheId,
string memory tokenName,
string memory tokenSymbol,
uint8 decimals,
uint128 price
) = Messages.parseAddTranche(_msg);
poolManager.addTranche(poolId, trancheId, tokenName, tokenSymbol, decimals, price);
} else if (Messages.isUpdateMember(_msg)) {
(uint64 poolId, bytes16 trancheId, address user, uint64 validUntil) = Messages.parseUpdateMember(_msg);
poolManager.updateMember(poolId, trancheId, user, validUntil);
} else if (Messages.isUpdateTrancheTokenPrice(_msg)) {
(uint64 poolId, bytes16 trancheId, uint128 price) = Messages.parseUpdateTrancheTokenPrice(_msg);
poolManager.updateTrancheTokenPrice(poolId, trancheId, price);
} else if (Messages.isTransfer(_msg)) {
(uint128 currency, address recipient, uint128 amount) = Messages.parseIncomingTransfer(_msg);
poolManager.handleTransfer(currency, recipient, amount);
} else if (Messages.isTransferTrancheTokens(_msg)) {
(uint64 poolId, bytes16 trancheId, address destinationAddress, uint128 amount) =
Messages.parseTransferTrancheTokens20(_msg);
poolManager.handleTransferTrancheTokens(poolId, trancheId, destinationAddress, amount);
} else if (Messages.isExecutedDecreaseInvestOrder(_msg)) {
(uint64 poolId, bytes16 trancheId, address investor, uint128 currency, uint128 currencyPayout) =
Messages.parseExecutedDecreaseInvestOrder(_msg);
investmentManager.handleExecutedDecreaseInvestOrder(poolId, trancheId, investor, currency, currencyPayout);
} else if (Messages.isExecutedDecreaseRedeemOrder(_msg)) {
(uint64 poolId, bytes16 trancheId, address investor, uint128 currency, uint128 trancheTokensPayout) =
Messages.parseExecutedDecreaseRedeemOrder(_msg);
investmentManager.handleExecutedDecreaseRedeemOrder(
poolId, trancheId, investor, currency, trancheTokensPayout
);
} else if (Messages.isExecutedCollectInvest(_msg)) {
(
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 currencyPayout,
uint128 trancheTokensPayout
) = Messages.parseExecutedCollectInvest(_msg);
investmentManager.handleExecutedCollectInvest(
poolId, trancheId, investor, currency, currencyPayout, trancheTokensPayout
);
} else if (Messages.isExecutedCollectRedeem(_msg)) {
(
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 currencyPayout,
uint128 trancheTokensPayout
) = Messages.parseExecutedCollectRedeem(_msg);
investmentManager.handleExecutedCollectRedeem(
poolId, trancheId, investor, currency, currencyPayout, trancheTokensPayout
);
} else if (Messages.isScheduleUpgrade(_msg)) {
address target = Messages.parseScheduleUpgrade(_msg);
root.scheduleRely(target);
} else if (Messages.isCancelUpgrade(_msg)) {
address target = Messages.parseCancelUpgrade(_msg);
root.cancelRely(target);
} else if (Messages.isUpdateTrancheTokenMetadata(_msg)) {
(uint64 poolId, bytes16 trancheId, string memory tokenName, string memory tokenSymbol) =
Messages.parseUpdateTrancheTokenMetadata(_msg);
poolManager.updateTrancheTokenMetadata(poolId, trancheId, tokenName, tokenSymbol);
} else {
revert("Gateway/invalid-message");
}
}
// Utils
function addressToBytes32(address x) private pure returns (bytes32) {
return bytes32(bytes20(x));
}
}