Inheritance refactoring

This commit is contained in:
PedroCailleret
2023-05-19 05:14:11 -03:00
parent 440048453b
commit ce5f3e4265
44 changed files with 4314 additions and 306 deletions

View File

@@ -0,0 +1,110 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import { OwnerSettings } from "./OwnerSettings.sol";
import { ECDSA } from "../lib/utils/ECDSA.sol";
import { MerkleProofLib as Merkle } from "../lib/utils/MerkleProofLib.sol";
abstract contract BaseUtils is OwnerSettings {
/// ███ Storage ████████████████████████████████████████████████████████████
/// @dev List of Pix transactions already signed.
/// mapping(bytes32 => bool) public usedTransactions;
/// @dev Value in custom storage slot given by:
/// let value := sload(bytes32).
/// ███ Helper FX ██████████████████████████████████████████████████████████
function _setUsedTransactions(bytes32 message) internal {
assembly {
sstore(message, true)
}
}
function usedTransactions(
bytes32 message
) public view returns (bool used) {
assembly {
used := sload(message)
}
}
function _signerCheck(
bytes32 _message,
bytes32 _r,
bytes32 _s,
uint8 _v
) internal view {
if (usedTransactions(_message))
revert TxAlreadyUsed();
if (
!validBacenSigners(
_castAddrToKey(
ECDSA.recover(
ECDSA.toEthSignedMessageHash(
_message
),
_v,
_r,
_s
)
)
)
) revert InvalidSigner();
}
function _merkleVerify(
bytes32[] calldata _merkleProof,
bytes32 _root,
address _addr
) internal pure {
if (
!Merkle.verify(
_merkleProof,
_root,
bytes32(uint256(uint160(_addr)))
)
) revert AddressDenied();
}
function _castToUint(
uint96 _amount,
uint160 _pixTarget,
bool _valid
)
internal
pure
returns (
uint256 _amountCasted,
uint256 _pixTargetCasted,
uint256 _validCasted
)
{
assembly {
_amountCasted := _amount
_pixTargetCasted := _pixTarget
_validCasted := _valid
}
}
/// @notice Public method that handles `address`
/// to `uint256` safe type casting.
/// @dev Function sighash: 0x4b2ae980.
function _castAddrToKey(
address _addr
) public pure returns (uint256 _key) {
// _key = uint256(uint160(address(_addr))) << 12;
assembly {
_key := shl(12, _addr)
}
}
function _castKeyToAddr(
uint256 _key
) public pure returns (address _addr) {
// _addr = address(uint160(uint256(_key >> 12)));
assembly {
_addr := shr(12, _key)
}
}
}

View File

@@ -0,0 +1,49 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
abstract contract Constants {
/// ███ Constants ██████████████████████████████████████████████████████████
uint256 constant _ROOT_UPDATED_EVENT_SIGNATURE =
0x0b294da292f26e55fd442b5c0164fbb9013036ff00c5cfdde0efd01c1baaf632;
uint256 constant _ALLOWED_ERC20_UPDATED_EVENT_SIGNATURE =
0x5d6e86e5341d57a92c49934296c51542a25015c9b1782a1c2722a940131c3d9a;
/// @dev Seller casted to key => Seller's allowlist merkleroot.
/// mapping(uint256 => bytes32) public sellerAllowList;
uint256 constant _SELLER_ALLOWLIST_SLOT_SEED = 0x74dfee70;
/// @dev Tokens allowed to serve as the underlying amount of a deposit.
/// mapping(ERC20 => bool) public allowedERC20s;
uint256 constant _ALLOWED_ERC20_SLOT_SEED = 0xcbc9d1c4;
/// @dev `balance` max. value = 10**26.
/// @dev `pixTarget` keys are restricted to 160 bits.
/// mapping(uint256 => mapping(ERC20 => uint256)) public sellerBalance;
/// @dev Bits layout:
/// `uint96` [0...94] := balance
/// `uint160` [95...254] := pixTarget
/// `bool` [255] := valid
/// @dev Value in custom storage slot given by:
/// mstore(0x20, token)
/// mstore(0x0c, _SELLER_BALANCE_SLOT_SEED)
/// mstore(0x00, seller)
/// let value := sload(keccak256(0x0c, 0x34)).
uint256 constant _SELLER_BALANCE_SLOT_SEED = 0x739094b1;
/// @dev The bitmask of `sellerBalance` entry.
uint256 constant BITMASK_SB_ENTRY = (1 << 94) - 1;
/// @dev The bit position of `pixTarget` in `sellerBalance`.
uint256 constant BITPOS_PIXTARGET = 95;
/// @dev The bit position of `valid` in `sellerBalance`.
uint256 constant BITPOS_VALID = 255;
/// @dev The bitmask of all 256 bits of `sellerBalance` except for the last one.
uint256 constant BITMASK_VALID = (1 << 255) - 1;
/// @dev The scalar of BRZ token.
uint256 constant WAD = 1e18;
uint256 constant MAXBALANCE_UPPERBOUND = 1e8 ether;
uint256 constant REPUTATION_LOWERBOUND = 1e2 ether;
uint256 constant LOCKAMOUNT_UPPERBOUND = 1e6 ether;
}

View File

@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
library DataTypes {
struct Lock {
uint256 sellerKey;
uint256 counter;
/// @dev If not paid at this block will be expired.
uint256 expirationBlock;
uint160 pixTarget;
/// @dev Amount to be paid for relayer.
uint80 relayerPremium;
/// @dev Where the tokens are sent the when order gets validated.
/// @dev Amount to be tranfered via PIX.
uint80 amount;
address buyerAddress;
/// @dev Relayer address (msg.sender) that facilitated this transaction.
/// @dev Relayer's target address that receives `relayerPremium` funds.
/// @dev Reputation points accruer.
address relayerAddress;
address token;
}
// prettier-ignore
enum LockStatus {
Inexistent, // 0 := Uninitialized Lock.
Active, // 1 := Valid Lock.
Expired, // 2 := Expired Lock.
Released // 3 := Already released Lock.
}
}

View File

@@ -0,0 +1,125 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
// prettier-ignore
interface EventAndErrors {
/// ███ Events ████████████████████████████████████████████████████████████
/// @dev 0x63d8d7d5e63e9840ec91a12a160d27b7cfab294f6ba070b7359692acfe6b03bf
event DepositAdded(
address indexed seller,
// uint256 depositID,
address token,
uint256 amount
);
/// @dev 0xca585721b6b442dc9183932f7c84dc2880efb67c4da52cc06873e78971105d49
event ValidSet(
address indexed seller,
address token,
bool state
);
/// @dev 0x2cd6435b1b961c13f55202979edd0765a809f69a539d8a477436c94c1211e43e
event DepositWithdrawn(
address indexed seller,
address token,
uint256 amount
);
/// @dev 0x8fb3989f70bd172a37d15b41b015e48ea09d59329638377304a4198cd0c4ea65
event LockAdded(
address indexed buyer,
uint256 indexed lockID,
uint256 seller,
uint256 amount
);
/// @dev 0x364537f14276f2a0ce9905588413f96454cbb8fb2e4f5308389307c1098bede8
event LockReleased(
address indexed buyer,
uint256 lockId,
uint256 amount
);
/// @dev 0x830501e61b8b075e170b22a430e39454bdb12ed3e9620e586430b6ac00079da5
event LockReturned(
address indexed buyer,
uint256 lockId
);
/// @dev 0xeaff4b37086828766ad3268786972c0cd24259d4c87a80f9d3963a3c3d999b0d
event FundsWithdrawn(
address owner,
uint256 amount
);
/// @dev 0x0b294da292f26e55fd442b5c0164fbb9013036ff00c5cfdde0efd01c1baaf632
event RootUpdated(
address indexed seller,
bytes32 indexed merkleRoot
);
/// @dev 0x5d6e86e5341d57a92c49934296c51542a25015c9b1782a1c2722a940131c3d9a
event AllowedERC20Updated(
address indexed token,
bool indexed state
);
/// @dev 0xe127cf589a3879da0156d4a24f43b44f65cfa3570de594806b0bfa2fcf06884f
event ReputationUpdated(address reputation);
/// @dev 0x70fa43ca70216ad905ade86b9e650a691b2ce5a01980d0a81bdd8324141b8511
event LockBlocksUpdated(uint256 blocks);
/// @dev 0x14a422d2412784a5749d03da98921fe468c98577b767851389a9f58ea5a363d7
event ValidSignersUpdated(address[] signers);
/// ███ Errors ████████████████████████████████████████████████████████████
/// @dev Only seller could call this function.
/// @dev `msg.sender` and the seller differ.
/// @dev 0x85d1f726
error OnlySeller();
/// @dev Lock not expired or already released.
/// @dev Another lock with same ID is not expired yet.
/// @dev 0xd0404f85
error NotExpired();
/// @dev Loop bounds have overflowed.
/// @dev 0xdfb035c9
error LoopOverflow();
/// @dev Deposit not valid anymore.
/// @dev 0xb2e532de
error InvalidDeposit();
/// @dev Not enough token remaining on deposit.
/// @dev 0x22bbb43c
error NotEnoughTokens();
/// @dev Lock already released or returned.
/// @dev 0x63b4904e
error AlreadyReleased();
/// @dev Transaction already used to unlock payment.
/// @dev 0xf490a6ea
error TxAlreadyUsed();
/// @dev Signer is not a valid signer.
/// @dev 0x815e1d64
error InvalidSigner();
/// @dev Address doesn't exist in a MerkleTree.
/// @dev Address not allowed as relayer.
/// @dev 0x3b8474be
error AddressDenied();
/// @dev Arrays' length don't match.
/// @dev 0xff633a38
error LengthMismatch();
/// @dev No tokens array provided as argument.
/// @dev 0xdf957883
error NoTokens();
/// @dev Token address not allowed to be deposited.
/// @dev 0x1578328e
error TokenDenied();
/// @dev Wished amount to be locked exceeds the limit allowed.
/// @dev 0x1c18f846
error AmountNotAllowed();
/// @dev Reverts when success return value returns false.
/// @dev 0xe10bf1cc
error StaticCallFailed();
/// @dev Reverts on an expired lock.
/// @dev 0xf6fafba0
error LockExpired();
/// @dev 0xce3a3d37
error DecOverflow();
/// @dev 0xf3fb0eb9
error MaxBalExceeded();
/// @dev 0x6a3bc53e
error EmptyPixTarget();
/// @dev 0x87138d5c
error NotInitialized();
}

View File

@@ -0,0 +1,197 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import { ERC20, SafeTransferLib } from "../lib/utils/SafeTransferLib.sol";
import { IReputation } from "../lib/interfaces/IReputation.sol";
import { EventAndErrors } from "./EventAndErrors.sol";
import { Constants } from "./Constants.sol";
import { Owned } from "../lib/auth/Owned.sol";
abstract contract OwnerSettings is
Constants,
EventAndErrors,
Owned(msg.sender)
{
/// ███ Storage ████████████████████████████████████████████████████████████
/// @dev List of valid Bacen signature addresses
/// mapping(uint256 => bool) public validBacenSigners;
/// @dev Value in custom storage slot given by:
/// let value := sload(shl(12, address)).
IReputation public reputation;
/// @dev Default blocks that lock will hold tokens.
uint256 public defaultLockBlocks;
/// ███ Constructor ████████████████████████████████████████████████████████
constructor(
uint256 defaultBlocks,
address[] memory validSigners,
address _reputation,
address[] memory tokens,
bool[] memory tokenStates
) {
setDefaultLockBlocks(defaultBlocks);
setValidSigners(validSigners);
setReputation(IReputation(_reputation));
tokenSettings(tokens, tokenStates);
}
/// ███ Owner Only █████████████████████████████████████████████████████████
/// @dev Contract's underlying balance withdraw method.
/// @dev Function sighash: 0x5fd8c710.
function withdrawBalance() external onlyOwner {
uint256 balance = address(this).balance;
SafeTransferLib.safeTransferETH(msg.sender, balance);
emit FundsWithdrawn(msg.sender, balance);
}
function setReputation(
IReputation _reputation
) public onlyOwner {
assembly {
sstore(reputation.slot, _reputation)
}
emit ReputationUpdated(address(_reputation));
}
function setDefaultLockBlocks(
uint256 _blocks
) public onlyOwner {
assembly {
sstore(defaultLockBlocks.slot, _blocks)
}
emit LockBlocksUpdated(_blocks);
}
function setValidSigners(
address[] memory _validSigners
) public onlyOwner {
assembly {
let i := add(_validSigners, 0x20)
let end := add(i, shl(0x05, mload(_validSigners)))
for {
/* */
} iszero(returndatasize()) {
/* */
} {
sstore(shl(12, mload(i)), true)
i := add(i, 0x20)
if iszero(lt(i, end)) {
break
}
}
}
emit ValidSignersUpdated(_validSigners);
}
function tokenSettings(
address[] memory _tokens,
bool[] memory _states
) public onlyOwner {
/* Yul Impl */
assembly {
// first 32 bytes eq to array's length
let tLen := mload(_tokens)
// NoTokens()
if iszero(tLen) {
mstore(0x00, 0xdf957883)
revert(0x1c, 0x04)
}
// LengthMismatch()
if iszero(eq(tLen, mload(_states))) {
mstore(0x00, 0xff633a38)
revert(0x1c, 0x04)
}
let tLoc := add(_tokens, 0x20)
let sLoc := add(_states, 0x20)
for {
let end := add(tLoc, shl(5, tLen))
} iszero(eq(tLoc, end)) {
tLoc := add(tLoc, 0x20)
sLoc := add(sLoc, 0x20)
} {
// cache hashmap entry in scratch space
mstore(0x0c, _ALLOWED_ERC20_SLOT_SEED)
mstore(0x00, mload(tLoc))
// let mapSlot := keccak256(0x0c, 0x20)
sstore(keccak256(0x0c, 0x20), mload(sLoc))
// emit AllowedERC20Updated(address, bool)
log3(
0,
0,
_ALLOWED_ERC20_UPDATED_EVENT_SIGNATURE,
mload(tLoc),
mload(sLoc)
)
}
}
}
/// ███ View FX ████████████████████████████████████████████████████████████
function validBacenSigners(
uint256 signer
) public view returns (bool valid) {
assembly {
valid := sload(signer)
}
}
function sellerAllowList(
uint256 sellerKey
) public view returns (bytes32 root) {
assembly {
mstore(0x0c, _SELLER_ALLOWLIST_SLOT_SEED)
mstore(0x00, shr(12, sellerKey))
root := sload(keccak256(0x00, 0x20))
}
}
function allowedERC20s(
ERC20 erc20
) public view returns (bool state) {
assembly {
mstore(0x0c, _ALLOWED_ERC20_SLOT_SEED)
mstore(0x00, erc20)
state := sload(keccak256(0x0c, 0x20))
}
}
function _limiter(
uint256 _userCredit
) internal view returns (uint256 _spendLimit) {
bytes memory encodedParams = abi.encodeWithSelector(
// IReputation.limiter.selector,
0x4d2b1791,
_userCredit
);
bool success;
assembly {
success := staticcall(
// gas
0x7530,
// address
sload(reputation.slot),
// argsOffset
add(encodedParams, 0x20),
// argsSize
mload(encodedParams),
// retOffset
0x00,
// retSize
0x20
)
_spendLimit := mload(0x00)
if iszero(success) {
// StaticCallFailed()
mstore(0x00, 0xe10bf1cc)
revert(0x1c, 0x04)
}
}
}
}