Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tokens-received-whitelisting): <- adds logic for that #68

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions contracts/ERC777Upgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ contract ERC777Upgradeable is Initializable, ContextUpgradeable, IERC777Upgradea
// ERC20-allowances
mapping (address => mapping (address => uint256)) private _allowances;

mapping (address => bool) public TOKENS_RECEIVED_HOOK_WHITELIST;

/**
* @dev `defaultOperators` may be an empty array.
*/
Expand Down Expand Up @@ -168,8 +170,7 @@ contract ERC777Upgradeable is Initializable, ContextUpgradeable, IERC777Upgradea

_move(from, from, recipient, amount, "", "");

// NOTE: Disabling ALL hooks.
//_callTokensReceived(from, from, recipient, amount, "", "", false);
_callTokensReceived(from, from, recipient, amount, "", "", false);

return true;
}
Expand Down Expand Up @@ -302,8 +303,7 @@ contract ERC777Upgradeable is Initializable, ContextUpgradeable, IERC777Upgradea
_move(spender, holder, recipient, amount, "", "");
_approve(holder, spender, _allowances[holder][spender].sub(amount, "ERC777: transfer amount exceeds allowance"));

// NOTE:
//_callTokensReceived(spender, holder, recipient, amount, "", "", false);
_callTokensReceived(spender, holder, recipient, amount, "", "", false);

return true;
}
Expand Down Expand Up @@ -338,15 +338,14 @@ contract ERC777Upgradeable is Initializable, ContextUpgradeable, IERC777Upgradea

address operator = _msgSender();

// NOTE: Disabling ALL hooks.
// NOTE: Disabling hooks.
//_beforeTokenTransfer(operator, address(0), account, amount);

// Update state variables
_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);

// NOTE: Disabling ALL hooks.
//_callTokensReceived(operator, address(0), account, amount, userData, operatorData, true);
_callTokensReceived(operator, address(0), account, amount, userData, operatorData, true);

emit Minted(operator, account, amount, userData, operatorData);
emit Transfer(address(0), account, amount);
Expand All @@ -366,7 +365,7 @@ contract ERC777Upgradeable is Initializable, ContextUpgradeable, IERC777Upgradea
uint256 amount,
bytes memory userData,
bytes memory operatorData,
bool /* requireReceptionAck */
bool requireReceptionAck
)
internal
virtual
Expand All @@ -381,8 +380,7 @@ contract ERC777Upgradeable is Initializable, ContextUpgradeable, IERC777Upgradea

_move(operator, from, to, amount, userData, operatorData);

// NOTE: Disabling ALL hooks.
//_callTokensReceived(operator, from, to, amount, userData, operatorData, requireReceptionAck);
_callTokensReceived(operator, from, to, amount, userData, operatorData, requireReceptionAck);
}

/**
Expand Down Expand Up @@ -499,6 +497,11 @@ contract ERC777Upgradeable is Initializable, ContextUpgradeable, IERC777Upgradea
)
private
{
if (!TOKENS_RECEIVED_HOOK_WHITELIST[to]) {
// NOTE: Only allow `tokensReceived` hook to be called in whitelisted addresses!
return;
}

address implementer = _ERC1820_REGISTRY.getInterfaceImplementer(to, _TOKENS_RECIPIENT_INTERFACE_HASH);
if (implementer != address(0)) {
IERC777RecipientUpgradeable(implementer).tokensReceived(operator, from, to, amount, userData, operatorData);
Expand All @@ -522,5 +525,5 @@ contract ERC777Upgradeable is Initializable, ContextUpgradeable, IERC777Upgradea
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(address operator, address from, address to, uint256 amount) internal virtual { }
uint256[41] private __gap;
uint256[40] private __gap;
}
17 changes: 17 additions & 0 deletions contracts/pToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "./ERC777WithAdminOperatorUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";


contract PToken is
Initializable,
AccessControlUpgradeable,
Expand All @@ -23,6 +24,8 @@ contract PToken is
bytes4 destinationChainId
);

event TokensReceivedHookWhitelistChange(address indexed theAddress, bool wasAdded);

function initialize(
string memory tokenName,
string memory tokenSymbol,
Expand Down Expand Up @@ -168,4 +171,18 @@ contract PToken is
ORIGIN_CHAIN_ID = _newOriginChainId;
return true;
}

function addToTokensReceivedWhitelist(address _address) external onlyAdmin {
if (!TOKENS_RECEIVED_HOOK_WHITELIST[_address]) {
TOKENS_RECEIVED_HOOK_WHITELIST[_address] = true;
emit TokensReceivedHookWhitelistChange(_address, true);
}
}

function removeFromTokensReceivedWhitelist(address _address) external onlyAdmin {
if (TOKENS_RECEIVED_HOOK_WHITELIST[_address]) {
delete TOKENS_RECEIVED_HOOK_WHITELIST[_address];
emit TokensReceivedHookWhitelistChange(_address, false);
}
}
}
3 changes: 3 additions & 0 deletions contracts/test-contracts/MockNonERC777Recipient.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pragma solidity ^0.6.2;

contract MockNonErc777Recipient {}
Loading