Skip to content

Commit 61f0075

Browse files
committed
fix: openzeppelin safetransfer for tokens
1 parent e07b112 commit 61f0075

File tree

8 files changed

+21
-162
lines changed

8 files changed

+21
-162
lines changed

‎nightfall-deployer/contracts/ERC1155_2Mock.sol

-20
This file was deleted.

‎nightfall-deployer/contracts/ERC721_2Mock.sol

-25
This file was deleted.

‎nightfall-deployer/contracts/ERCInterface.sol

-39
This file was deleted.

‎nightfall-deployer/contracts/ERCStub.sol

-58
This file was deleted.

‎nightfall-deployer/contracts/Shield.sol

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// SPDX-License-Identifier: CC0-1.0
22
import '@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol';
33
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';
47
/**
58
Contract to enable someone to submit a ZKP transaction for processing.
69
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.
1114
pragma solidity ^0.8.0;
1215

1316
import './Utils.sol';
14-
import './ERCInterface.sol';
1517
import './Key_Registry.sol';
1618
import './Structures.sol';
1719
import './Config.sol';
1820
import './Stateful.sol';
1921
import './Ownable.sol';
2022

2123
contract Shield is Stateful, Ownable, Structures, Config, Key_Registry, ReentrancyGuardUpgradeable {
24+
using SafeERC20Upgradeable for IERC20Upgradeable;
2225
mapping(bytes32 => bool) public withdrawn;
2326
mapping(bytes32 => uint256) public feeBook;
2427
mapping(bytes32 => address) public advancedWithdrawals;
@@ -33,12 +36,11 @@ contract Shield is Stateful, Ownable, Structures, Config, Key_Registry, Reentran
3336
}
3437

3538
function transferShieldBalance(address ercAddress, uint256 value) public onlyOwner {
36-
ERCInterface tokenContract = ERCInterface(ercAddress);
3739
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);
4042
} else {
41-
tokenContract.transfer(owner(), value);
43+
IERC20Upgradeable(ercAddress).safeTransfer(owner(), value);
4244
}
4345
}
4446

@@ -191,9 +193,7 @@ contract Shield is Stateful, Ownable, Structures, Config, Key_Registry, Reentran
191193
// TODO should we check if the withdrawal is not in a finalised block
192194
// this might incentives sniping freshly finalised blocks by liquidity providers
193195
// 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)));
197197
address originalRecipientAddress =
198198
address(uint160(uint256(withdrawTransaction.recipientAddress)));
199199
address currentOwner =
@@ -210,7 +210,7 @@ contract Shield is Stateful, Ownable, Structures, Config, Key_Registry, Reentran
210210
advancedFeeWithdrawals[withdrawTransactionHash] = 0;
211211
advancedWithdrawals[withdrawTransactionHash] = msg.sender;
212212
state.addPendingWithdrawal(msg.sender, advancedFee);
213-
tokenContract.transferFrom(
213+
IERC20Upgradeable(tokenAddress).safeTransferFrom(
214214
address(msg.sender),
215215
currentOwner,
216216
uint256(withdrawTransaction.value)
@@ -251,27 +251,25 @@ contract Shield is Stateful, Ownable, Structures, Config, Key_Registry, Reentran
251251
function payOut(Transaction memory t, address recipientAddress) internal {
252252
// Now pay out the value of the commitment
253253
address addr = address(uint160(uint256(t.ercAddress)));
254-
ERCInterface tokenContract = ERCInterface(addr);
255-
// address recipientAddress = address(uint160(uint256(t.recipientAddress)));
256254

257255
if (t.tokenType == TokenType.ERC20) {
258256
if (t.tokenId != ZERO) revert('ERC20 deposit should have tokenId equal to ZERO');
259257
if (t.value > super.getRestriction(addr, 1))
260258
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));
262260
} else if (t.tokenType == TokenType.ERC721) {
263261
if (t.value != 0)
264262
// value should always be equal to 0
265263
revert('Invalid inputs for ERC721 deposit');
266264
else
267-
tokenContract.safeTransferFrom(
265+
IERC721(addr).safeTransferFrom(
268266
address(this),
269267
recipientAddress,
270268
uint256(t.tokenId),
271269
''
272270
);
273271
} else if (t.tokenType == TokenType.ERC1155) {
274-
tokenContract.safeTransferFrom(
272+
IERC1155(addr).safeTransferFrom(
275273
address(this),
276274
recipientAddress,
277275
uint256(t.tokenId),
@@ -285,22 +283,25 @@ contract Shield is Stateful, Ownable, Structures, Config, Key_Registry, Reentran
285283

286284
function payIn(Transaction memory t) internal {
287285
address addr = address(uint160(uint256(t.ercAddress)));
288-
ERCInterface tokenContract = ERCInterface(addr);
289286

290287
if (t.tokenType == TokenType.ERC20) {
291288
if (t.tokenId != ZERO) revert('ERC20 deposit should have tokenId equal to ZERO');
292289
uint256 check = super.getRestriction(addr, 0);
293290
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+
);
297298
} else if (t.tokenType == TokenType.ERC721) {
298299
if (t.value != 0)
299300
// value should always be equal to 0
300301
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), '');
302303
} else if (t.tokenType == TokenType.ERC1155) {
303-
tokenContract.safeTransferFrom(
304+
IERC1155(addr).safeTransferFrom(
304305
msg.sender,
305306
address(this),
306307
uint256(t.tokenId),

0 commit comments

Comments
 (0)