-
Notifications
You must be signed in to change notification settings - Fork 8
/
Reclaimer.sol
102 lines (90 loc) · 3.9 KB
/
Reclaimer.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.20;
import {IERC721} from "@openzeppelin-contracts/token/ERC721/IERC721.sol";
import {IERC1155} from "@openzeppelin-contracts/token/ERC1155/IERC1155.sol";
import {RentalOrder, Item, ItemType} from "@src/libraries/RentalStructs.sol";
import {Errors} from "@src/libraries/Errors.sol";
/**
* @title Reclaimer
* @notice Retrieves rented assets from a wallet contract once a rental has been
* stopped, and transfers them to the proper recipient.
*/
abstract contract Reclaimer {
// The original address that this contract was deployed with
address private immutable original;
/**
* @dev Use the constructor to store the original deployment address.
*/
constructor() {
original = address(this);
}
/**
* @dev Helper function to transfer an ERC721 token.
*
* @param item Item which will be transferred.
* @param recipient Address which will receive the token.
*/
function _transferERC721(Item memory item, address recipient) private {
IERC721(item.token).safeTransferFrom(address(this), recipient, item.identifier);
}
/**
* @dev Helper function to transfer an ERC1155 token.
*
* @param item Item which will be transferred.
* @param recipient Address which will receive the token.
*/
function _transferERC1155(Item memory item, address recipient) private {
IERC1155(item.token).safeTransferFrom(
address(this),
recipient,
item.identifier,
item.amount,
""
);
}
/**
* @notice Reclaims the assets that are currently owned by a rental safe.
* This function is intended to be delegate called by the safe.
*
* NOTE: At first, it may seem that this function can be used to exploit
* the safe, since this is an external function that can transfer tokens
* out of a rental safe, so long as the caller is:
* 1) Using delegate call to extract the assets
* 2) The rental safe that holds those assets
*
* This exploit is prevented because delegate calls from the rental safe can
* only be made to addresses which have been explicitly whitelisted by the
* Admin policy. Further, since the Stop policy is a whitelisted module on
* the safe, `reclaimRentalOrder()` can only be called via the context of the
* Stop policy contract, which prevents unauthorized reclaiming.
*
* @param rentalOrder Order which will have its contained items reclaimed by the
* recipient parties.
*/
function reclaimRentalOrder(RentalOrder calldata rentalOrder) external {
// This contract address must be in the context of another address.
if (address(this) == original) {
revert Errors.ReclaimerPackage_OnlyDelegateCallAllowed();
}
// Only the rental wallet specified in the order can be the address that
// initates the reclaim. In the context of a delegate call, address(this)
// will be the safe.
if (address(this) != rentalOrder.rentalWallet) {
revert Errors.ReclaimerPackage_OnlyRentalSafeAllowed(
rentalOrder.rentalWallet
);
}
// Get a count for the number of items.
uint256 itemCount = rentalOrder.items.length;
// Transfer each item if it is a rented asset.
for (uint256 i = 0; i < itemCount; ++i) {
Item memory item = rentalOrder.items[i];
// Check if the item is an ERC721.
if (item.itemType == ItemType.ERC721)
_transferERC721(item, rentalOrder.lender);
// check if the item is an ERC1155.
if (item.itemType == ItemType.ERC1155)
_transferERC1155(item, rentalOrder.lender);
}
}
}