1
1
// SPDX-License-Identifier: CC0-1.0
2
2
import '@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol ' ;
3
3
import '@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol ' ;
4
+ import '@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol ' ;
5
+ import '@openzeppelin/contracts/token/ERC721/IERC721.sol ' ;
6
+ import '@openzeppelin/contracts/token/ERC1155/IERC1155.sol ' ;
4
7
/**
5
8
Contract to enable someone to submit a ZKP transaction for processing.
6
9
It is possible we will move this off-chain in the future as blockchain
@@ -11,14 +14,14 @@ functionality is not really required - it's just a data availability aid.
11
14
pragma solidity ^ 0.8.0 ;
12
15
13
16
import './Utils.sol ' ;
14
- import './ERCInterface.sol ' ;
15
17
import './Key_Registry.sol ' ;
16
18
import './Structures.sol ' ;
17
19
import './Config.sol ' ;
18
20
import './Stateful.sol ' ;
19
21
import './Ownable.sol ' ;
20
22
21
23
contract Shield is Stateful , Ownable , Structures , Config , Key_Registry , ReentrancyGuardUpgradeable {
24
+ using SafeERC20Upgradeable for IERC20Upgradeable ;
22
25
mapping (bytes32 => bool ) public withdrawn;
23
26
mapping (bytes32 => uint256 ) public feeBook;
24
27
mapping (bytes32 => address ) public advancedWithdrawals;
@@ -33,12 +36,11 @@ contract Shield is Stateful, Ownable, Structures, Config, Key_Registry, Reentran
33
36
}
34
37
35
38
function transferShieldBalance (address ercAddress , uint256 value ) public onlyOwner {
36
- ERCInterface tokenContract = ERCInterface (ercAddress);
37
39
if (value == uint256 (0 )) {
38
- uint256 balance = tokenContract .balanceOf (address (this ));
39
- tokenContract. transfer (owner (), balance);
40
+ uint256 balance = IERC20Upgradeable (ercAddress) .balanceOf (address (this ));
41
+ IERC20Upgradeable (ercAddress). safeTransfer (owner (), balance);
40
42
} else {
41
- tokenContract. transfer (owner (), value);
43
+ IERC20Upgradeable (ercAddress). safeTransfer (owner (), value);
42
44
}
43
45
}
44
46
@@ -191,9 +193,7 @@ contract Shield is Stateful, Ownable, Structures, Config, Key_Registry, Reentran
191
193
// TODO should we check if the withdrawal is not in a finalised block
192
194
// this might incentives sniping freshly finalised blocks by liquidity providers
193
195
// this is risk-free as the block is finalised, the advancedFee should reflect a risk premium.
194
-
195
- ERCInterface tokenContract =
196
- ERCInterface (address (uint160 (uint256 (withdrawTransaction.ercAddress))));
196
+ address tokenAddress = address (uint160 (uint256 (withdrawTransaction.ercAddress)));
197
197
address originalRecipientAddress =
198
198
address (uint160 (uint256 (withdrawTransaction.recipientAddress)));
199
199
address currentOwner =
@@ -210,7 +210,7 @@ contract Shield is Stateful, Ownable, Structures, Config, Key_Registry, Reentran
210
210
advancedFeeWithdrawals[withdrawTransactionHash] = 0 ;
211
211
advancedWithdrawals[withdrawTransactionHash] = msg .sender ;
212
212
state.addPendingWithdrawal (msg .sender , advancedFee);
213
- tokenContract. transferFrom (
213
+ IERC20Upgradeable (tokenAddress). safeTransferFrom (
214
214
address (msg .sender ),
215
215
currentOwner,
216
216
uint256 (withdrawTransaction.value)
@@ -251,27 +251,25 @@ contract Shield is Stateful, Ownable, Structures, Config, Key_Registry, Reentran
251
251
function payOut (Transaction memory t , address recipientAddress ) internal {
252
252
// Now pay out the value of the commitment
253
253
address addr = address (uint160 (uint256 (t.ercAddress)));
254
- ERCInterface tokenContract = ERCInterface (addr);
255
- // address recipientAddress = address(uint160(uint256(t.recipientAddress)));
256
254
257
255
if (t.tokenType == TokenType.ERC20 ) {
258
256
if (t.tokenId != ZERO) revert ('ERC20 deposit should have tokenId equal to ZERO ' );
259
257
if (t.value > super .getRestriction (addr, 1 ))
260
258
revert ('Value is above current restrictions for withdrawals ' );
261
- else tokenContract. transfer (recipientAddress, uint256 (t.value));
259
+ else IERC20Upgradeable (addr). safeTransfer (recipientAddress, uint256 (t.value));
262
260
} else if (t.tokenType == TokenType.ERC721 ) {
263
261
if (t.value != 0 )
264
262
// value should always be equal to 0
265
263
revert ('Invalid inputs for ERC721 deposit ' );
266
264
else
267
- tokenContract .safeTransferFrom (
265
+ IERC721 (addr) .safeTransferFrom (
268
266
address (this ),
269
267
recipientAddress,
270
268
uint256 (t.tokenId),
271
269
''
272
270
);
273
271
} else if (t.tokenType == TokenType.ERC1155 ) {
274
- tokenContract .safeTransferFrom (
272
+ IERC1155 (addr) .safeTransferFrom (
275
273
address (this ),
276
274
recipientAddress,
277
275
uint256 (t.tokenId),
@@ -285,22 +283,25 @@ contract Shield is Stateful, Ownable, Structures, Config, Key_Registry, Reentran
285
283
286
284
function payIn (Transaction memory t ) internal {
287
285
address addr = address (uint160 (uint256 (t.ercAddress)));
288
- ERCInterface tokenContract = ERCInterface (addr);
289
286
290
287
if (t.tokenType == TokenType.ERC20 ) {
291
288
if (t.tokenId != ZERO) revert ('ERC20 deposit should have tokenId equal to ZERO ' );
292
289
uint256 check = super .getRestriction (addr, 0 );
293
290
require (check > 0 , 'Cannot have restrictions of zero value ' );
294
- if (t.value > check)
295
- revert ('Value is above current restrictions for deposits ' );
296
- else require (tokenContract.transferFrom (msg .sender , address (this ), uint256 (t.value)), 'transferFrom returned false ' );
291
+ if (t.value > check) revert ('Value is above current restrictions for deposits ' );
292
+ else
293
+ IERC20Upgradeable (addr).safeTransferFrom (
294
+ msg .sender ,
295
+ address (this ),
296
+ uint256 (t.value)
297
+ );
297
298
} else if (t.tokenType == TokenType.ERC721 ) {
298
299
if (t.value != 0 )
299
300
// value should always be equal to 0
300
301
revert ('Invalid inputs for ERC721 deposit ' );
301
- else tokenContract .safeTransferFrom (msg .sender , address (this ), uint256 (t.tokenId), '' );
302
+ else IERC721 (addr) .safeTransferFrom (msg .sender , address (this ), uint256 (t.tokenId), '' );
302
303
} else if (t.tokenType == TokenType.ERC1155 ) {
303
- tokenContract .safeTransferFrom (
304
+ IERC1155 (addr) .safeTransferFrom (
304
305
msg .sender ,
305
306
address (this ),
306
307
uint256 (t.tokenId),
0 commit comments