From e7ee43c4e8bbe99c6b8ea9dbdf4fd07f7a644aed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Madis=20N=C3=B5mme?= Date: Fri, 15 Jul 2022 08:59:14 +0000 Subject: [PATCH] Update contracts to Solidity 0.8 --- browser-test/all.cljs | 2 +- contracts/Migrations.sol | 5 +- contracts/MyContract.sol | 7 +- contracts/auth/DSAuth.sol | 96 ++++++++++++---------------- contracts/auth/DSGuard.sol | 37 ++++------- contracts/proxy/DelegateProxy.sol | 11 ++-- contracts/proxy/Forwarder.sol | 10 ++- contracts/proxy/MutableForwarder.sol | 10 ++- 8 files changed, 83 insertions(+), 95 deletions(-) diff --git a/browser-test/all.cljs b/browser-test/all.cljs index cfe297d..d02c414 100644 --- a/browser-test/all.cljs +++ b/browser-test/all.cljs @@ -20,7 +20,7 @@ (def gas-limit 4500000) (def contract-source " - pragma solidity ^0.4.6; + pragma solidity ^0.8.0; contract test { function multiply(uint a) returns(uint d) { diff --git a/contracts/Migrations.sol b/contracts/Migrations.sol index c4efb65..912139e 100644 --- a/contracts/Migrations.sol +++ b/contracts/Migrations.sol @@ -1,10 +1,11 @@ -pragma solidity ^0.4.23; +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity ^0.8.0; contract Migrations { address public owner; uint public last_completed_migration; - constructor() public { + constructor() { owner = msg.sender; } diff --git a/contracts/MyContract.sol b/contracts/MyContract.sol index 7296db5..3f97bf4 100644 --- a/contracts/MyContract.sol +++ b/contracts/MyContract.sol @@ -1,4 +1,5 @@ -pragma solidity ^0.4.18; +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity ^0.8.0; contract MyContract { @@ -9,11 +10,11 @@ contract MyContract { event SetCounterEvent(uint previousValue, uint newValue); /* event SpecialEvent(uint someParam); */ - constructor(uint _counter) public { + constructor(uint _counter) { counter = _counter; } - function myPlus(uint a, uint b) public constant returns (uint) { + function myPlus(uint a, uint b) public pure returns (uint) { return a + b; } diff --git a/contracts/auth/DSAuth.sol b/contracts/auth/DSAuth.sol index e532996..13b082e 100644 --- a/contracts/auth/DSAuth.sol +++ b/contracts/auth/DSAuth.sol @@ -1,68 +1,56 @@ -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. +// SPDX-License-Identifier: GNU-3 +pragma solidity >=0.4.23; -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -pragma solidity ^0.4.13; - -contract DSAuthority { - function canCall( - address src, address dst, bytes4 sig - ) public view returns (bool); +interface DSAuthority { + function canCall( + address src, address dst, bytes4 sig + ) external view returns (bool); } contract DSAuthEvents { - event LogSetAuthority (address indexed authority); - event LogSetOwner (address indexed owner); + event LogSetAuthority (address indexed authority); + event LogSetOwner (address indexed owner); } contract DSAuth is DSAuthEvents { - DSAuthority public authority; - address public owner; + DSAuthority public authority; + address public owner; - function DSAuth() public { - owner = msg.sender; - LogSetOwner(msg.sender); - } + constructor() { + owner = msg.sender; + emit LogSetOwner(msg.sender); + } - function setOwner(address owner_) - public - auth - { - owner = owner_; - LogSetOwner(owner); - } + function setOwner(address owner_) + public + auth + { + owner = owner_; + emit LogSetOwner(owner); + } - function setAuthority(DSAuthority authority_) - public - auth - { - authority = authority_; - LogSetAuthority(authority); - } + function setAuthority(DSAuthority authority_) + public + auth + { + authority = authority_; + emit LogSetAuthority(address(authority)); + } - modifier auth { - require(isAuthorized(msg.sender, msg.sig)); - _; - } + modifier auth { + require(isAuthorized(msg.sender, msg.sig), "ds-auth-unauthorized"); + _; + } - function isAuthorized(address src, bytes4 sig) internal view returns (bool) { - if (src == address(this)) { - return true; - } else if (src == owner) { - return true; - } else if (authority == DSAuthority(0)) { - return false; - } else { - return authority.canCall(src, this, sig); + function isAuthorized(address src, bytes4 sig) internal view returns (bool) { + if (src == address(this)) { + return true; + } else if (src == owner) { + return true; + } else if (authority == DSAuthority(address(0))) { + return false; + } else { + return authority.canCall(src, address(this), sig); + } } - } } diff --git a/contracts/auth/DSGuard.sol b/contracts/auth/DSGuard.sol index 92c55e5..23ebe02 100644 --- a/contracts/auth/DSGuard.sol +++ b/contracts/auth/DSGuard.sol @@ -1,21 +1,6 @@ // guard.sol -- simple whitelist implementation of DSAuthority - -// Copyright (C) 2017 DappHub, LLC - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -pragma solidity ^0.4.13; +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity ^0.8.0; import "../auth/DSAuth.sol"; @@ -33,14 +18,18 @@ contract DSGuardEvents { } contract DSGuard is DSAuth, DSAuthority, DSGuardEvents { - bytes32 constant public ANY = bytes32(uint(- 1)); + bytes32 constant public ANY = bytes32(uint256(0x01)); mapping(bytes32 => mapping(bytes32 => mapping(bytes32 => bool))) acl; + function toBytes(address addr) public pure returns (bytes32) { + return bytes32(uint256(uint160(addr)) << 96); + } + function canCall( address src_, address dst_, bytes4 sig - ) public view returns (bool) { - bytes32 src = bytes32(src_); - bytes32 dst = bytes32(dst_); + ) public view override returns (bool) { + bytes32 src = toBytes(src_); + bytes32 dst = toBytes(dst_); return acl[src][dst][sig] || acl[src][dst][ANY] @@ -63,11 +52,11 @@ contract DSGuard is DSAuth, DSAuthority, DSGuardEvents { } function permit(address src, address dst, bytes32 sig) public { - permit(bytes32(src), bytes32(dst), sig); + permit(toBytes(src), toBytes(dst), sig); } function forbid(address src, address dst, bytes32 sig) public { - forbid(bytes32(src), bytes32(dst), sig); + forbid(toBytes(src), toBytes(dst), sig); } } @@ -77,6 +66,6 @@ contract DSGuardFactory { function newGuard() public returns (DSGuard guard) { guard = new DSGuard(); guard.setOwner(msg.sender); - isGuard[guard] = true; + isGuard[address(guard)] = true; } } diff --git a/contracts/proxy/DelegateProxy.sol b/contracts/proxy/DelegateProxy.sol index 92e0e0b..8bc38cb 100644 --- a/contracts/proxy/DelegateProxy.sol +++ b/contracts/proxy/DelegateProxy.sol @@ -1,4 +1,5 @@ -pragma solidity ^0.4.18; +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity ^0.8.0; contract DelegateProxy { /** @@ -6,11 +7,11 @@ contract DelegateProxy { * @param _dst Destination address to perform the delegatecall * @param _calldata Calldata for the delegatecall */ - function delegatedFwd(address _dst, bytes _calldata) internal { + function delegatedFwd(address _dst, bytes memory _calldata) internal { require(isContract(_dst)); assembly { - let result := delegatecall(sub(gas, 10000), _dst, add(_calldata, 0x20), mload(_calldata), 0, 0) - let size := returndatasize + let result := delegatecall(sub(gas(), 10000), _dst, add(_calldata, 0x20), mload(_calldata), 0, 0) + let size := returndatasize() let ptr := mload(0x40) returndatacopy(ptr, 0, size) @@ -27,4 +28,4 @@ contract DelegateProxy { assembly {size := extcodesize(_target)} return size > 0; } -} \ No newline at end of file +} diff --git a/contracts/proxy/Forwarder.sol b/contracts/proxy/Forwarder.sol index 0ca7a3d..396caa5 100644 --- a/contracts/proxy/Forwarder.sol +++ b/contracts/proxy/Forwarder.sol @@ -1,4 +1,5 @@ -pragma solidity ^0.4.18; +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity ^0.8.0; import "../proxy/DelegateProxy.sol"; @@ -9,7 +10,10 @@ contract Forwarder is DelegateProxy { /* * @dev Forwards all calls to target */ - function() payable { + fallback() external payable { delegatedFwd(target, msg.data); } -} \ No newline at end of file + + receive() external payable { + } +} diff --git a/contracts/proxy/MutableForwarder.sol b/contracts/proxy/MutableForwarder.sol index 8e19942..f2b935c 100644 --- a/contracts/proxy/MutableForwarder.sol +++ b/contracts/proxy/MutableForwarder.sol @@ -1,4 +1,5 @@ -pragma solidity ^0.4.18; +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity ^0.8.0; import "../proxy/DelegateProxy.sol"; import "../auth/DSAuth.sol"; @@ -27,8 +28,11 @@ contract MutableForwarder is DelegateProxy, DSAuth { target = _target; } - function() payable { + fallback() external payable { delegatedFwd(target, msg.data); } -} \ No newline at end of file + receive() external payable { + } + +}