From ad40ee5b714c4c704cdbc668bec890a5e665481e Mon Sep 17 00:00:00 2001 From: Mike Ng Date: Tue, 28 Nov 2023 19:08:48 +0800 Subject: [PATCH] Added access control when doing deposit - When deposit, will check is the `_msgSender()` has sufficient allowance of the `spender` - When do withdraw, will check is the `_msgSender()` is domain owner --- .openzeppelin/goerli.json | 460 ++++++++++++++++++++++++++++++++ contracts/mortgage/Mortgage.sol | 13 +- 2 files changed, 467 insertions(+), 6 deletions(-) diff --git a/.openzeppelin/goerli.json b/.openzeppelin/goerli.json index fb93821..f9946ff 100644 --- a/.openzeppelin/goerli.json +++ b/.openzeppelin/goerli.json @@ -14667,6 +14667,466 @@ } } } + }, + "69953c869773d29ef628426723d82bc7f186df37530db4975cf630341ba6b193": { + "address": "0x59609765cF63905aEf2Fa639eD462D1ea2F36f53", + "txHash": "0x53716d9c871e705ef3253a96c936022fb4efe09da4ea196e6b7991492f251ddf", + "layout": { + "solcVersion": "0.8.17", + "storage": [ + { + "label": "_initialized", + "offset": 0, + "slot": "0", + "type": "t_uint8", + "contract": "Initializable", + "src": "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol:63", + "retypedFrom": "bool" + }, + { + "label": "_initializing", + "offset": 1, + "slot": "0", + "type": "t_bool", + "contract": "Initializable", + "src": "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol:68" + }, + { + "label": "_status", + "offset": 0, + "slot": "1", + "type": "t_uint256", + "contract": "ReentrancyGuardUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol:38" + }, + { + "label": "__gap", + "offset": 0, + "slot": "2", + "type": "t_array(t_uint256)49_storage", + "contract": "ReentrancyGuardUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol:88" + }, + { + "label": "__gap", + "offset": 0, + "slot": "51", + "type": "t_array(t_uint256)50_storage", + "contract": "ContextUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol:36" + }, + { + "label": "__gap", + "offset": 0, + "slot": "101", + "type": "t_array(t_uint256)50_storage", + "contract": "ERC165Upgradeable", + "src": "@openzeppelin/contracts-upgradeable/utils/introspection/ERC165Upgradeable.sol:41" + }, + { + "label": "_roles", + "offset": 0, + "slot": "151", + "type": "t_mapping(t_bytes32,t_struct(RoleData)34_storage)", + "contract": "AccessControlUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol:62" + }, + { + "label": "__gap", + "offset": 0, + "slot": "152", + "type": "t_array(t_uint256)49_storage", + "contract": "AccessControlUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol:260" + }, + { + "label": "__gap", + "offset": 0, + "slot": "201", + "type": "t_array(t_uint256)50_storage", + "contract": "ERC1967UpgradeUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/proxy/ERC1967/ERC1967UpgradeUpgradeable.sol:169" + }, + { + "label": "__gap", + "offset": 0, + "slot": "251", + "type": "t_array(t_uint256)50_storage", + "contract": "UUPSUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol:111" + }, + { + "label": "_paused", + "offset": 0, + "slot": "301", + "type": "t_bool", + "contract": "PausableUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol:29" + }, + { + "label": "__gap", + "offset": 0, + "slot": "302", + "type": "t_array(t_uint256)49_storage", + "contract": "PausableUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol:116" + }, + { + "label": "_token", + "offset": 0, + "slot": "351", + "type": "t_contract(IERC20Upgradeable)1367", + "contract": "Mortgage", + "src": "contracts/mortgage/Mortgage.sol:14" + }, + { + "label": "_registry", + "offset": 0, + "slot": "352", + "type": "t_contract(IRegistry)10737", + "contract": "Mortgage", + "src": "contracts/mortgage/Mortgage.sol:15" + }, + { + "label": "_funds", + "offset": 0, + "slot": "353", + "type": "t_mapping(t_bytes32,t_mapping(t_bytes32,t_mapping(t_address,t_uint256)))", + "contract": "Mortgage", + "src": "contracts/mortgage/Mortgage.sol:17" + }, + { + "label": "_requirements", + "offset": 0, + "slot": "354", + "type": "t_mapping(t_bytes32,t_mapping(t_bytes32,t_uint256))", + "contract": "Mortgage", + "src": "contracts/mortgage/Mortgage.sol:18" + }, + { + "label": "__gap", + "offset": 0, + "slot": "355", + "type": "t_array(t_uint256)50_storage", + "contract": "Mortgage", + "src": "contracts/mortgage/Mortgage.sol:87" + } + ], + "types": { + "t_address": { + "label": "address", + "numberOfBytes": "20" + }, + "t_array(t_uint256)49_storage": { + "label": "uint256[49]", + "numberOfBytes": "1568" + }, + "t_array(t_uint256)50_storage": { + "label": "uint256[50]", + "numberOfBytes": "1600" + }, + "t_bool": { + "label": "bool", + "numberOfBytes": "1" + }, + "t_bytes32": { + "label": "bytes32", + "numberOfBytes": "32" + }, + "t_contract(IERC20Upgradeable)1367": { + "label": "contract IERC20Upgradeable", + "numberOfBytes": "20" + }, + "t_contract(IRegistry)10737": { + "label": "contract IRegistry", + "numberOfBytes": "20" + }, + "t_mapping(t_address,t_bool)": { + "label": "mapping(address => bool)", + "numberOfBytes": "32" + }, + "t_mapping(t_address,t_uint256)": { + "label": "mapping(address => uint256)", + "numberOfBytes": "32" + }, + "t_mapping(t_bytes32,t_mapping(t_address,t_uint256))": { + "label": "mapping(bytes32 => mapping(address => uint256))", + "numberOfBytes": "32" + }, + "t_mapping(t_bytes32,t_mapping(t_bytes32,t_mapping(t_address,t_uint256)))": { + "label": "mapping(bytes32 => mapping(bytes32 => mapping(address => uint256)))", + "numberOfBytes": "32" + }, + "t_mapping(t_bytes32,t_mapping(t_bytes32,t_uint256))": { + "label": "mapping(bytes32 => mapping(bytes32 => uint256))", + "numberOfBytes": "32" + }, + "t_mapping(t_bytes32,t_struct(RoleData)34_storage)": { + "label": "mapping(bytes32 => struct AccessControlUpgradeable.RoleData)", + "numberOfBytes": "32" + }, + "t_mapping(t_bytes32,t_uint256)": { + "label": "mapping(bytes32 => uint256)", + "numberOfBytes": "32" + }, + "t_struct(RoleData)34_storage": { + "label": "struct AccessControlUpgradeable.RoleData", + "members": [ + { + "label": "members", + "type": "t_mapping(t_address,t_bool)", + "offset": 0, + "slot": "0" + }, + { + "label": "adminRole", + "type": "t_bytes32", + "offset": 0, + "slot": "1" + } + ], + "numberOfBytes": "64" + }, + "t_uint256": { + "label": "uint256", + "numberOfBytes": "32" + }, + "t_uint8": { + "label": "uint8", + "numberOfBytes": "1" + } + } + } + }, + "ce86f6fd77f7e0439eb7a3a852f577798bc01b5465c68edb0f0b1739efbf0cf7": { + "address": "0x2d16dd46d49A165Fa025A0939F4f5bd27d101475", + "txHash": "0xdb44cb7f1c3783e154cb646b811483ea455d8981f96254edc5edb96241d3ec1b", + "layout": { + "solcVersion": "0.8.17", + "storage": [ + { + "label": "_initialized", + "offset": 0, + "slot": "0", + "type": "t_uint8", + "contract": "Initializable", + "src": "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol:63", + "retypedFrom": "bool" + }, + { + "label": "_initializing", + "offset": 1, + "slot": "0", + "type": "t_bool", + "contract": "Initializable", + "src": "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol:68" + }, + { + "label": "_status", + "offset": 0, + "slot": "1", + "type": "t_uint256", + "contract": "ReentrancyGuardUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol:38" + }, + { + "label": "__gap", + "offset": 0, + "slot": "2", + "type": "t_array(t_uint256)49_storage", + "contract": "ReentrancyGuardUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol:88" + }, + { + "label": "__gap", + "offset": 0, + "slot": "51", + "type": "t_array(t_uint256)50_storage", + "contract": "ContextUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol:36" + }, + { + "label": "__gap", + "offset": 0, + "slot": "101", + "type": "t_array(t_uint256)50_storage", + "contract": "ERC165Upgradeable", + "src": "@openzeppelin/contracts-upgradeable/utils/introspection/ERC165Upgradeable.sol:41" + }, + { + "label": "_roles", + "offset": 0, + "slot": "151", + "type": "t_mapping(t_bytes32,t_struct(RoleData)34_storage)", + "contract": "AccessControlUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol:62" + }, + { + "label": "__gap", + "offset": 0, + "slot": "152", + "type": "t_array(t_uint256)49_storage", + "contract": "AccessControlUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol:260" + }, + { + "label": "__gap", + "offset": 0, + "slot": "201", + "type": "t_array(t_uint256)50_storage", + "contract": "ERC1967UpgradeUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/proxy/ERC1967/ERC1967UpgradeUpgradeable.sol:169" + }, + { + "label": "__gap", + "offset": 0, + "slot": "251", + "type": "t_array(t_uint256)50_storage", + "contract": "UUPSUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol:111" + }, + { + "label": "_paused", + "offset": 0, + "slot": "301", + "type": "t_bool", + "contract": "PausableUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol:29" + }, + { + "label": "__gap", + "offset": 0, + "slot": "302", + "type": "t_array(t_uint256)49_storage", + "contract": "PausableUpgradeable", + "src": "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol:116" + }, + { + "label": "_token", + "offset": 0, + "slot": "351", + "type": "t_contract(IERC20Upgradeable)1367", + "contract": "Mortgage", + "src": "contracts/mortgage/Mortgage.sol:12" + }, + { + "label": "_registry", + "offset": 0, + "slot": "352", + "type": "t_contract(IRegistry)7108", + "contract": "Mortgage", + "src": "contracts/mortgage/Mortgage.sol:13" + }, + { + "label": "_funds", + "offset": 0, + "slot": "353", + "type": "t_mapping(t_bytes32,t_mapping(t_bytes32,t_mapping(t_address,t_uint256)))", + "contract": "Mortgage", + "src": "contracts/mortgage/Mortgage.sol:15" + }, + { + "label": "_requirements", + "offset": 0, + "slot": "354", + "type": "t_mapping(t_bytes32,t_mapping(t_bytes32,t_uint256))", + "contract": "Mortgage", + "src": "contracts/mortgage/Mortgage.sol:16" + }, + { + "label": "__gap", + "offset": 0, + "slot": "355", + "type": "t_array(t_uint256)50_storage", + "contract": "Mortgage", + "src": "contracts/mortgage/Mortgage.sol:88" + } + ], + "types": { + "t_address": { + "label": "address", + "numberOfBytes": "20" + }, + "t_array(t_uint256)49_storage": { + "label": "uint256[49]", + "numberOfBytes": "1568" + }, + "t_array(t_uint256)50_storage": { + "label": "uint256[50]", + "numberOfBytes": "1600" + }, + "t_bool": { + "label": "bool", + "numberOfBytes": "1" + }, + "t_bytes32": { + "label": "bytes32", + "numberOfBytes": "32" + }, + "t_contract(IERC20Upgradeable)1367": { + "label": "contract IERC20Upgradeable", + "numberOfBytes": "20" + }, + "t_contract(IRegistry)7108": { + "label": "contract IRegistry", + "numberOfBytes": "20" + }, + "t_mapping(t_address,t_bool)": { + "label": "mapping(address => bool)", + "numberOfBytes": "32" + }, + "t_mapping(t_address,t_uint256)": { + "label": "mapping(address => uint256)", + "numberOfBytes": "32" + }, + "t_mapping(t_bytes32,t_mapping(t_address,t_uint256))": { + "label": "mapping(bytes32 => mapping(address => uint256))", + "numberOfBytes": "32" + }, + "t_mapping(t_bytes32,t_mapping(t_bytes32,t_mapping(t_address,t_uint256)))": { + "label": "mapping(bytes32 => mapping(bytes32 => mapping(address => uint256)))", + "numberOfBytes": "32" + }, + "t_mapping(t_bytes32,t_mapping(t_bytes32,t_uint256))": { + "label": "mapping(bytes32 => mapping(bytes32 => uint256))", + "numberOfBytes": "32" + }, + "t_mapping(t_bytes32,t_struct(RoleData)34_storage)": { + "label": "mapping(bytes32 => struct AccessControlUpgradeable.RoleData)", + "numberOfBytes": "32" + }, + "t_mapping(t_bytes32,t_uint256)": { + "label": "mapping(bytes32 => uint256)", + "numberOfBytes": "32" + }, + "t_struct(RoleData)34_storage": { + "label": "struct AccessControlUpgradeable.RoleData", + "members": [ + { + "label": "members", + "type": "t_mapping(t_address,t_bool)", + "offset": 0, + "slot": "0" + }, + { + "label": "adminRole", + "type": "t_bytes32", + "offset": 0, + "slot": "1" + } + ], + "numberOfBytes": "64" + }, + "t_uint256": { + "label": "uint256", + "numberOfBytes": "32" + }, + "t_uint8": { + "label": "uint8", + "numberOfBytes": "1" + } + } + } } } } diff --git a/contracts/mortgage/Mortgage.sol b/contracts/mortgage/Mortgage.sol index a00243d..9ca2475 100644 --- a/contracts/mortgage/Mortgage.sol +++ b/contracts/mortgage/Mortgage.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.13; import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol"; -import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol"; +import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol"; import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol"; import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol"; @@ -9,8 +9,6 @@ import "./interfaces/IMortgage.sol"; import "../registry/interfaces/IRegistry.sol"; contract Mortgage is IMortgage, ReentrancyGuardUpgradeable, AccessControlUpgradeable, UUPSUpgradeable, PausableUpgradeable { - using SafeERC20Upgradeable for IERC20Upgradeable; - IERC20Upgradeable internal _token; IRegistry internal _registry; @@ -39,14 +37,17 @@ contract Mortgage is IMortgage, ReentrancyGuardUpgradeable, AccessControlUpgrade function deposit(bytes32 name, bytes32 tld, address owner, address spender, uint256 amount) public whenNotPaused nonReentrant { require(isExists(name, tld), "DOMAIN_NOT_EXISTS"); require(_registry.isLive(name, tld), "DOMAIN_EXPIRED"); - _token.safeTransferFrom(spender, address(this), getRequirement(name, tld)); + require(_token.allowance(spender, _msgSender()) >= amount, "INSUFFICIENT_TOKEN_ALLOWANCE"); + require(_token.balanceOf(spender) >= amount, "INSUFFICIENT_TOKEN_BALANCE"); + _token.transferFrom(spender, address(this), getRequirement(name, tld)); _funds[tld][name][owner] += amount; emit Deposit(name, tld, owner, amount); } function withdraw(bytes32 name, bytes32 tld, address recipient, uint256 amount) public whenNotPaused nonReentrant { - require(_funds[tld][name][_msgSender()] >= amount, "FUND_AMOUNT_EXCEEDED"); - _token.safeTransferFrom(address(this), recipient, amount); + require(_registry.getOwner(name, tld) == _msgSender(), "ONLY_OWNER"); + require(_funds[tld][name][_msgSender()] >= amount, "AMOUNT_EXCEEDED"); + _token.transferFrom(address(this), recipient, amount); emit Withdraw(name, tld, _msgSender(), amount); }