diff --git a/README.md b/README.md index 11cc535..79d3271 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,6 @@ │ │ └── ERC20.sol │ └── utils │ ├── Counters.sol -│ ├── FixedPointMathLib.sol │ ├── MerkleProofLib.sol │ ├── ReentrancyGuard.sol │ └── SafeTransferLib.sol diff --git a/artifacts/contracts/DataTypes.sol/DataTypes.dbg.json b/artifacts/contracts/DataTypes.sol/DataTypes.dbg.json index 490b60b..3dcab43 100644 --- a/artifacts/contracts/DataTypes.sol/DataTypes.dbg.json +++ b/artifacts/contracts/DataTypes.sol/DataTypes.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../build-info/727fbe6b5a53dc5c0af688ab7bc65edd.json" + "buildInfo": "../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/artifacts/contracts/EventAndErrors.sol/EventAndErrors.dbg.json b/artifacts/contracts/EventAndErrors.sol/EventAndErrors.dbg.json index 490b60b..3dcab43 100644 --- a/artifacts/contracts/EventAndErrors.sol/EventAndErrors.dbg.json +++ b/artifacts/contracts/EventAndErrors.sol/EventAndErrors.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../build-info/727fbe6b5a53dc5c0af688ab7bc65edd.json" + "buildInfo": "../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/artifacts/contracts/Reputation.sol/Reputation.dbg.json b/artifacts/contracts/Reputation.sol/Reputation.dbg.json index 490b60b..3dcab43 100644 --- a/artifacts/contracts/Reputation.sol/Reputation.dbg.json +++ b/artifacts/contracts/Reputation.sol/Reputation.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../build-info/727fbe6b5a53dc5c0af688ab7bc65edd.json" + "buildInfo": "../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/artifacts/contracts/Reputation.sol/Reputation.json b/artifacts/contracts/Reputation.sol/Reputation.json index ef9cd41..0aa879f 100644 --- a/artifacts/contracts/Reputation.sol/Reputation.json +++ b/artifacts/contracts/Reputation.sol/Reputation.json @@ -99,8 +99,8 @@ "type": "function" } ], - "bytecode": "0x608060405234801561001057600080fd5b50600080546001600160a01b031916339081178255604051909182917f8292fce18fa69edf4db7b94ea2e58241df0ae57f97e0a6c9b29067028bf92d76908290a350610347806100616000396000f3fe608060405234801561001057600080fd5b50600436106100675760003560e01c80634d2b1791116100505780634d2b17911461009e5780638da5cb5b146100b1578063a4b34557146100dc57600080fd5b806313af40351461006c5780631a861d2614610081575b600080fd5b61007f61007a3660046102f8565b6100e8565b005b61008b620f424081565b6040519081526020015b60405180910390f35b61008b6100ac366004610321565b6101a9565b6000546100c4906001600160a01b031681565b6040516001600160a01b039091168152602001610095565b61008b643a3529440081565b6000546001600160a01b031633146101465760405162461bcd60e51b815260206004820152600c60248201527f554e415554484f52495a45440000000000000000000000000000000000000000604482015260640160405180910390fd5b600080547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081178255604051909133917f8292fce18fa69edf4db7b94ea2e58241df0ae57f97e0a6c9b29067028bf92d769190a350565b6000806101b9620f4240846101fa565b905060006101c784806101fa565b905060006101e26101dd83643a352944006101fa565b610216565b90506101ee83826102c8565b60010195945050505050565b600061020f8383670de0b6b3a76400006102d9565b9392505050565b60b58171010000000000000000000000000000000000811061023d5760409190911b9060801c5b690100000000000000000081106102595760209190911b9060401c5b6501000000000081106102715760109190911b9060201c5b630100000081106102875760089190911b9060101c5b62010000010260121c80820401600190811c80830401811c80830401811c80830401811c80830401811c80830401811c80830401901c908190048111900390565b600061020f83670de0b6b3a7640000845b8282028115158415858304851417166102f157600080fd5b0492915050565b60006020828403121561030a57600080fd5b81356001600160a01b038116811461020f57600080fd5b60006020828403121561033357600080fd5b503591905056fea164736f6c6343000809000a", - "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100675760003560e01c80634d2b1791116100505780634d2b17911461009e5780638da5cb5b146100b1578063a4b34557146100dc57600080fd5b806313af40351461006c5780631a861d2614610081575b600080fd5b61007f61007a3660046102f8565b6100e8565b005b61008b620f424081565b6040519081526020015b60405180910390f35b61008b6100ac366004610321565b6101a9565b6000546100c4906001600160a01b031681565b6040516001600160a01b039091168152602001610095565b61008b643a3529440081565b6000546001600160a01b031633146101465760405162461bcd60e51b815260206004820152600c60248201527f554e415554484f52495a45440000000000000000000000000000000000000000604482015260640160405180910390fd5b600080547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081178255604051909133917f8292fce18fa69edf4db7b94ea2e58241df0ae57f97e0a6c9b29067028bf92d769190a350565b6000806101b9620f4240846101fa565b905060006101c784806101fa565b905060006101e26101dd83643a352944006101fa565b610216565b90506101ee83826102c8565b60010195945050505050565b600061020f8383670de0b6b3a76400006102d9565b9392505050565b60b58171010000000000000000000000000000000000811061023d5760409190911b9060801c5b690100000000000000000081106102595760209190911b9060401c5b6501000000000081106102715760109190911b9060201c5b630100000081106102875760089190911b9060101c5b62010000010260121c80820401600190811c80830401811c80830401811c80830401811c80830401811c80830401811c80830401901c908190048111900390565b600061020f83670de0b6b3a7640000845b8282028115158415858304851417166102f157600080fd5b0492915050565b60006020828403121561030a57600080fd5b81356001600160a01b038116811461020f57600080fd5b60006020828403121561033357600080fd5b503591905056fea164736f6c6343000809000a", + "bytecode": "0x608060405234801561001057600080fd5b50600080546001600160a01b031916339081178255604051909182917f8292fce18fa69edf4db7b94ea2e58241df0ae57f97e0a6c9b29067028bf92d76908290a35061036b806100616000396000f3fe608060405234801561001057600080fd5b50600436106100675760003560e01c80634d2b1791116100505780634d2b17911461009e5780638da5cb5b146100b1578063a4b34557146100dc57600080fd5b806313af40351461006c5780631a861d2614610081575b600080fd5b61007f61007a3660046102a6565b6100e8565b005b61008b620f424081565b6040519081526020015b60405180910390f35b61008b6100ac3660046102d6565b6101a9565b6000546100c4906001600160a01b031681565b6040516001600160a01b039091168152602001610095565b61008b643a3529440081565b6000546001600160a01b031633146101465760405162461bcd60e51b815260206004820152600c60248201527f554e415554484f52495a45440000000000000000000000000000000000000000604482015260640160405180910390fd5b600080547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081178255604051909133917f8292fce18fa69edf4db7b94ea2e58241df0ae57f97e0a6c9b29067028bf92d769190a350565b60006101cc6101b88380610305565b6101c790643a35294400610324565b6101f4565b6101d983620f4240610305565b6101e3919061033c565b6101ee906001610324565b92915050565b60b58171010000000000000000000000000000000000811061021b5760409190911b9060801c5b690100000000000000000081106102375760209190911b9060401c5b65010000000000811061024f5760109190911b9060201c5b630100000081106102655760089190911b9060101c5b62010000010260121c80820401600190811c80830401811c80830401811c80830401811c80830401811c80830401811c80830401901c908190048111900390565b6000602082840312156102b857600080fd5b81356001600160a01b03811681146102cf57600080fd5b9392505050565b6000602082840312156102e857600080fd5b5035919050565b634e487b7160e01b600052601160045260246000fd5b600081600019048311821515161561031f5761031f6102ef565b500290565b60008219821115610337576103376102ef565b500190565b60008261035957634e487b7160e01b600052601260045260246000fd5b50049056fea164736f6c6343000809000a", + "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100675760003560e01c80634d2b1791116100505780634d2b17911461009e5780638da5cb5b146100b1578063a4b34557146100dc57600080fd5b806313af40351461006c5780631a861d2614610081575b600080fd5b61007f61007a3660046102a6565b6100e8565b005b61008b620f424081565b6040519081526020015b60405180910390f35b61008b6100ac3660046102d6565b6101a9565b6000546100c4906001600160a01b031681565b6040516001600160a01b039091168152602001610095565b61008b643a3529440081565b6000546001600160a01b031633146101465760405162461bcd60e51b815260206004820152600c60248201527f554e415554484f52495a45440000000000000000000000000000000000000000604482015260640160405180910390fd5b600080547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081178255604051909133917f8292fce18fa69edf4db7b94ea2e58241df0ae57f97e0a6c9b29067028bf92d769190a350565b60006101cc6101b88380610305565b6101c790643a35294400610324565b6101f4565b6101d983620f4240610305565b6101e3919061033c565b6101ee906001610324565b92915050565b60b58171010000000000000000000000000000000000811061021b5760409190911b9060801c5b690100000000000000000081106102375760209190911b9060401c5b65010000000000811061024f5760109190911b9060201c5b630100000081106102655760089190911b9060101c5b62010000010260121c80820401600190811c80830401811c80830401811c80830401811c80830401811c80830401811c80830401901c908190048111900390565b6000602082840312156102b857600080fd5b81356001600160a01b03811681146102cf57600080fd5b9392505050565b6000602082840312156102e857600080fd5b5035919050565b634e487b7160e01b600052601160045260246000fd5b600081600019048311821515161561031f5761031f6102ef565b500290565b60008219821115610337576103376102ef565b500190565b60008261035957634e487b7160e01b600052601260045260246000fd5b50049056fea164736f6c6343000809000a", "linkReferences": {}, "deployedLinkReferences": {} } diff --git a/artifacts/contracts/lib/auth/Owned.sol/Owned.dbg.json b/artifacts/contracts/lib/auth/Owned.sol/Owned.dbg.json index ab09155..b46c4cd 100644 --- a/artifacts/contracts/lib/auth/Owned.sol/Owned.dbg.json +++ b/artifacts/contracts/lib/auth/Owned.sol/Owned.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../../../build-info/da574cda4da83daffd2579005f189352.json" + "buildInfo": "../../../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/artifacts/contracts/lib/interfaces/IReputation.sol/IReputation.dbg.json b/artifacts/contracts/lib/interfaces/IReputation.sol/IReputation.dbg.json index 9a2dc31..b46c4cd 100644 --- a/artifacts/contracts/lib/interfaces/IReputation.sol/IReputation.dbg.json +++ b/artifacts/contracts/lib/interfaces/IReputation.sol/IReputation.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../../../build-info/727fbe6b5a53dc5c0af688ab7bc65edd.json" + "buildInfo": "../../../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/artifacts/contracts/lib/mock/mockToken.sol/MockToken.dbg.json b/artifacts/contracts/lib/mock/mockToken.sol/MockToken.dbg.json index ab09155..b46c4cd 100644 --- a/artifacts/contracts/lib/mock/mockToken.sol/MockToken.dbg.json +++ b/artifacts/contracts/lib/mock/mockToken.sol/MockToken.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../../../build-info/da574cda4da83daffd2579005f189352.json" + "buildInfo": "../../../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/artifacts/contracts/lib/tokens/ERC20.sol/ERC20.dbg.json b/artifacts/contracts/lib/tokens/ERC20.sol/ERC20.dbg.json index ab09155..b46c4cd 100644 --- a/artifacts/contracts/lib/tokens/ERC20.sol/ERC20.dbg.json +++ b/artifacts/contracts/lib/tokens/ERC20.sol/ERC20.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../../../build-info/da574cda4da83daffd2579005f189352.json" + "buildInfo": "../../../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/artifacts/contracts/lib/utils/Counters.sol/Counters.dbg.json b/artifacts/contracts/lib/utils/Counters.sol/Counters.dbg.json index ab09155..b46c4cd 100644 --- a/artifacts/contracts/lib/utils/Counters.sol/Counters.dbg.json +++ b/artifacts/contracts/lib/utils/Counters.sol/Counters.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../../../build-info/da574cda4da83daffd2579005f189352.json" + "buildInfo": "../../../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/artifacts/contracts/lib/utils/FixedPointMathLib.sol/FixedPointMathLib.dbg.json b/artifacts/contracts/lib/utils/FixedPointMathLib.sol/FixedPointMathLib.dbg.json deleted file mode 100644 index 9a2dc31..0000000 --- a/artifacts/contracts/lib/utils/FixedPointMathLib.sol/FixedPointMathLib.dbg.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "_format": "hh-sol-dbg-1", - "buildInfo": "../../../../build-info/727fbe6b5a53dc5c0af688ab7bc65edd.json" -} diff --git a/artifacts/contracts/lib/utils/FixedPointMathLib.sol/FixedPointMathLib.json b/artifacts/contracts/lib/utils/FixedPointMathLib.sol/FixedPointMathLib.json deleted file mode 100644 index c86207f..0000000 --- a/artifacts/contracts/lib/utils/FixedPointMathLib.sol/FixedPointMathLib.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "_format": "hh-sol-artifact-1", - "contractName": "FixedPointMathLib", - "sourceName": "contracts/lib/utils/FixedPointMathLib.sol", - "abi": [], - "bytecode": "0x602d6037600b82828239805160001a607314602a57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea164736f6c6343000809000a", - "deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea164736f6c6343000809000a", - "linkReferences": {}, - "deployedLinkReferences": {} -} diff --git a/artifacts/contracts/lib/utils/MerkleProofLib.sol/MerkleProofLib.dbg.json b/artifacts/contracts/lib/utils/MerkleProofLib.sol/MerkleProofLib.dbg.json index 9a2dc31..b46c4cd 100644 --- a/artifacts/contracts/lib/utils/MerkleProofLib.sol/MerkleProofLib.dbg.json +++ b/artifacts/contracts/lib/utils/MerkleProofLib.sol/MerkleProofLib.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../../../build-info/727fbe6b5a53dc5c0af688ab7bc65edd.json" + "buildInfo": "../../../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/artifacts/contracts/lib/utils/ReentrancyGuard.sol/ReentrancyGuard.dbg.json b/artifacts/contracts/lib/utils/ReentrancyGuard.sol/ReentrancyGuard.dbg.json index 9a2dc31..b46c4cd 100644 --- a/artifacts/contracts/lib/utils/ReentrancyGuard.sol/ReentrancyGuard.dbg.json +++ b/artifacts/contracts/lib/utils/ReentrancyGuard.sol/ReentrancyGuard.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../../../build-info/727fbe6b5a53dc5c0af688ab7bc65edd.json" + "buildInfo": "../../../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/artifacts/contracts/lib/utils/SafeTransferLib.sol/SafeTransferLib.dbg.json b/artifacts/contracts/lib/utils/SafeTransferLib.sol/SafeTransferLib.dbg.json index ab09155..b46c4cd 100644 --- a/artifacts/contracts/lib/utils/SafeTransferLib.sol/SafeTransferLib.dbg.json +++ b/artifacts/contracts/lib/utils/SafeTransferLib.sol/SafeTransferLib.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../../../build-info/da574cda4da83daffd2579005f189352.json" + "buildInfo": "../../../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/artifacts/contracts/p2pix.sol/P2PIX.dbg.json b/artifacts/contracts/p2pix.sol/P2PIX.dbg.json index 0580507..3dcab43 100644 --- a/artifacts/contracts/p2pix.sol/P2PIX.dbg.json +++ b/artifacts/contracts/p2pix.sol/P2PIX.dbg.json @@ -1,4 +1,4 @@ { "_format": "hh-sol-dbg-1", - "buildInfo": "../../build-info/17edf92346d029fe0a2c191f0008ac90.json" + "buildInfo": "../../build-info/59c1a703d41bd7d2c615a37cdb738573.json" } diff --git a/contracts/Reputation.sol b/contracts/Reputation.sol index 4f15579..751e813 100644 --- a/contracts/Reputation.sol +++ b/contracts/Reputation.sol @@ -3,19 +3,16 @@ pragma solidity 0.8.9; import { IReputation } from "./lib/interfaces/IReputation.sol"; import { Owned } from "./lib/auth/Owned.sol"; -import { FixedPointMathLib as WADMath } from "./lib/utils/FixedPointMathLib.sol"; contract Reputation is IReputation, Owned(msg.sender) { - using WADMath for uint256; - /// @dev Asymptote numerator constant value for the `limiter` fx. uint256 public constant maxLimit = 1e6; /// @dev Denominator's constant operand for the `limiter` fx. uint256 public constant magicValue = 2.5e11; - constructor() /* */ { - /* */ - } + // prettier-ignore + // solhint-disable-next-line no-empty-blocks + constructor(/* */) {/* */} function limiter( uint256 _userCredit @@ -25,23 +22,58 @@ contract Reputation is IReputation, Owned(msg.sender) { override(IReputation) returns (uint256 _spendLimit) { - // _spendLimit = 1 + ( ( maxLimit * _userCredit ) / sqrt( magicValue * ( _userCredit * _userCredit ) ) ); - // return _spendLimit; + _spendLimit = (1 + + ((maxLimit * _userCredit) / + sqrt( + magicValue + (_userCredit * _userCredit) + ))); + } - unchecked { - uint256 numeratorWad = maxLimit.mulWadDown( - _userCredit - ); - uint256 userCreditSquaredWad = _userCredit - .mulWadDown(_userCredit); - uint256 denominatorSqrtWad = ( - userCreditSquaredWad.mulWadDown(magicValue) - ).sqrt(); + /// @notice Taken from Solmate's FixedPointMathLib. + /// (https://github.com/transmissions11/solmate/blob/main/src/utils/FixedPointMathLib.sol) + function sqrt( + uint256 x + ) internal pure returns (uint256 z) { + /// @solidity memory-safe-assembly + assembly { + let y := x // We start y at x, which will help us make our initial estimate. - _spendLimit = (1 + - (numeratorWad).divWadDown( - denominatorSqrtWad - )); + z := 181 // The "correct" value is 1, but this saves a multiplication later. + + // We check y >= 2^(k + 8) but shift right by k bits + // each branch to ensure that if x >= 256, then y >= 256. + if iszero( + lt(y, 0x10000000000000000000000000000000000) + ) { + y := shr(128, y) + z := shl(64, z) + } + if iszero(lt(y, 0x1000000000000000000)) { + y := shr(64, y) + z := shl(32, z) + } + if iszero(lt(y, 0x10000000000)) { + y := shr(32, y) + z := shl(16, z) + } + if iszero(lt(y, 0x1000000)) { + y := shr(16, y) + z := shl(8, z) + } + + // There is no overflow risk here since y < 2^136 after the first branch above. + z := shr(18, mul(z, add(y, 65536))) // A mul() is saved from starting z at 181. + + // Given the worst case multiplicative error of 2.84 above, 7 iterations should be enough. + z := shr(1, add(z, div(x, z))) + z := shr(1, add(z, div(x, z))) + z := shr(1, add(z, div(x, z))) + z := shr(1, add(z, div(x, z))) + z := shr(1, add(z, div(x, z))) + z := shr(1, add(z, div(x, z))) + z := shr(1, add(z, div(x, z))) + + z := sub(z, lt(div(x, z), z)) } } } diff --git a/contracts/lib/utils/FixedPointMathLib.sol b/contracts/lib/utils/FixedPointMathLib.sol deleted file mode 100644 index 64b50c9..0000000 --- a/contracts/lib/utils/FixedPointMathLib.sol +++ /dev/null @@ -1,129 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity >=0.8.4; - -/// @notice Arithmetic library with operations for fixed-point numbers. -/// @author Solmate (https://github.com/Rari-Capital/solmate/blob/main/src/utils/FixedPointMathLib.sol) -library FixedPointMathLib { - /*////////////////////////////////////////////////////////////// - SIMPLIFIED FIXED POINT OPERATIONS - //////////////////////////////////////////////////////////////*/ - - /// @dev The scalar of ETH and most ERC20s. - uint256 internal constant WAD = 1e18; - - function mulWadDown( - uint256 x, - uint256 y - ) internal pure returns (uint256) { - // Equivalent to (x * y) / WAD rounded down. - return mulDivDown(x, y, WAD); - } - - function divWadDown( - uint256 x, - uint256 y - ) internal pure returns (uint256) { - // Equivalent to (x * WAD) / y rounded down. - return mulDivDown(x, WAD, y); - } - - /*////////////////////////////////////////////////////////////// - LOW LEVEL FIXED POINT OPERATIONS - //////////////////////////////////////////////////////////////*/ - - function mulDivDown( - uint256 x, - uint256 y, - uint256 denominator - ) internal pure returns (uint256 z) { - assembly { - // Store x * y in z for now. - z := mul(x, y) - - // Equivalent to require(denominator != 0 && (x == 0 || (x * y) / x == y)) - if iszero( - and( - iszero(iszero(denominator)), - or(iszero(x), eq(div(z, x), y)) - ) - ) { - revert(0, 0) - } - - // Divide z by the denominator. - z := div(z, denominator) - } - } - - /*////////////////////////////////////////////////////////////// - GENERAL NUMBER UTILITIES - //////////////////////////////////////////////////////////////*/ - - function sqrt( - uint256 x - ) internal pure returns (uint256 z) { - assembly { - let y := x // We start y at x, which will help us make our initial estimate. - - z := 181 // The "correct" value is 1, but this saves a multiplication later. - - // This segment is to get a reasonable initial estimate for the Babylonian method. With a bad - // start, the correct # of bits increases ~linearly each iteration instead of ~quadratically. - - // We check y >= 2^(k + 8) but shift right by k bits - // each branch to ensure that if x >= 256, then y >= 256. - if iszero( - lt(y, 0x10000000000000000000000000000000000) - ) { - y := shr(128, y) - z := shl(64, z) - } - if iszero(lt(y, 0x1000000000000000000)) { - y := shr(64, y) - z := shl(32, z) - } - if iszero(lt(y, 0x10000000000)) { - y := shr(32, y) - z := shl(16, z) - } - if iszero(lt(y, 0x1000000)) { - y := shr(16, y) - z := shl(8, z) - } - - // Goal was to get z*z*y within a small factor of x. More iterations could - // get y in a tighter range. Currently, we will have y in [256, 256*2^16). - // We ensured y >= 256 so that the relative difference between y and y+1 is small. - // That's not possible if x < 256 but we can just verify those cases exhaustively. - - // Now, z*z*y <= x < z*z*(y+1), and y <= 2^(16+8), and either y >= 256, or x < 256. - // Correctness can be checked exhaustively for x < 256, so we assume y >= 256. - // Then z*sqrt(y) is within sqrt(257)/sqrt(256) of sqrt(x), or about 20bps. - - // For s in the range [1/256, 256], the estimate f(s) = (181/1024) * (s+1) is in the range - // (1/2.84 * sqrt(s), 2.84 * sqrt(s)), with largest error when s = 1 and when s = 256 or 1/256. - - // Since y is in [256, 256*2^16), let a = y/65536, so that a is in [1/256, 256). Then we can estimate - // sqrt(y) using sqrt(65536) * 181/1024 * (a + 1) = 181/4 * (y + 65536)/65536 = 181 * (y + 65536)/2^18. - - // There is no overflow risk here since y < 2^136 after the first branch above. - z := shr(18, mul(z, add(y, 65536))) // A mul() is saved from starting z at 181. - - // Given the worst case multiplicative error of 2.84 above, 7 iterations should be enough. - z := shr(1, add(z, div(x, z))) - z := shr(1, add(z, div(x, z))) - z := shr(1, add(z, div(x, z))) - z := shr(1, add(z, div(x, z))) - z := shr(1, add(z, div(x, z))) - z := shr(1, add(z, div(x, z))) - z := shr(1, add(z, div(x, z))) - - // If x+1 is a perfect square, the Babylonian method cycles between - // floor(sqrt(x)) and ceil(sqrt(x)). This statement ensures we return floor. - // See: https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division - // Since the ceil is rare, we save gas on the assignment and repeat division in the rare case. - // If you don't care whether the floor or ceil square root is returned, you can remove this statement. - z := sub(z, lt(div(x, z), z)) - } - } -} diff --git a/src/types/factories/Reputation__factory.ts b/src/types/factories/Reputation__factory.ts index f0d5e05..cffb401 100644 --- a/src/types/factories/Reputation__factory.ts +++ b/src/types/factories/Reputation__factory.ts @@ -105,7 +105,7 @@ const _abi = [ ]; const _bytecode = - "0x608060405234801561001057600080fd5b50600080546001600160a01b031916339081178255604051909182917f8292fce18fa69edf4db7b94ea2e58241df0ae57f97e0a6c9b29067028bf92d76908290a350610347806100616000396000f3fe608060405234801561001057600080fd5b50600436106100675760003560e01c80634d2b1791116100505780634d2b17911461009e5780638da5cb5b146100b1578063a4b34557146100dc57600080fd5b806313af40351461006c5780631a861d2614610081575b600080fd5b61007f61007a3660046102f8565b6100e8565b005b61008b620f424081565b6040519081526020015b60405180910390f35b61008b6100ac366004610321565b6101a9565b6000546100c4906001600160a01b031681565b6040516001600160a01b039091168152602001610095565b61008b643a3529440081565b6000546001600160a01b031633146101465760405162461bcd60e51b815260206004820152600c60248201527f554e415554484f52495a45440000000000000000000000000000000000000000604482015260640160405180910390fd5b600080547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081178255604051909133917f8292fce18fa69edf4db7b94ea2e58241df0ae57f97e0a6c9b29067028bf92d769190a350565b6000806101b9620f4240846101fa565b905060006101c784806101fa565b905060006101e26101dd83643a352944006101fa565b610216565b90506101ee83826102c8565b60010195945050505050565b600061020f8383670de0b6b3a76400006102d9565b9392505050565b60b58171010000000000000000000000000000000000811061023d5760409190911b9060801c5b690100000000000000000081106102595760209190911b9060401c5b6501000000000081106102715760109190911b9060201c5b630100000081106102875760089190911b9060101c5b62010000010260121c80820401600190811c80830401811c80830401811c80830401811c80830401811c80830401811c80830401901c908190048111900390565b600061020f83670de0b6b3a7640000845b8282028115158415858304851417166102f157600080fd5b0492915050565b60006020828403121561030a57600080fd5b81356001600160a01b038116811461020f57600080fd5b60006020828403121561033357600080fd5b503591905056fea164736f6c6343000809000a"; + "0x608060405234801561001057600080fd5b50600080546001600160a01b031916339081178255604051909182917f8292fce18fa69edf4db7b94ea2e58241df0ae57f97e0a6c9b29067028bf92d76908290a35061036b806100616000396000f3fe608060405234801561001057600080fd5b50600436106100675760003560e01c80634d2b1791116100505780634d2b17911461009e5780638da5cb5b146100b1578063a4b34557146100dc57600080fd5b806313af40351461006c5780631a861d2614610081575b600080fd5b61007f61007a3660046102a6565b6100e8565b005b61008b620f424081565b6040519081526020015b60405180910390f35b61008b6100ac3660046102d6565b6101a9565b6000546100c4906001600160a01b031681565b6040516001600160a01b039091168152602001610095565b61008b643a3529440081565b6000546001600160a01b031633146101465760405162461bcd60e51b815260206004820152600c60248201527f554e415554484f52495a45440000000000000000000000000000000000000000604482015260640160405180910390fd5b600080547fffffffffffffffffffffffff0000000000000000000000000000000000000000166001600160a01b0383169081178255604051909133917f8292fce18fa69edf4db7b94ea2e58241df0ae57f97e0a6c9b29067028bf92d769190a350565b60006101cc6101b88380610305565b6101c790643a35294400610324565b6101f4565b6101d983620f4240610305565b6101e3919061033c565b6101ee906001610324565b92915050565b60b58171010000000000000000000000000000000000811061021b5760409190911b9060801c5b690100000000000000000081106102375760209190911b9060401c5b65010000000000811061024f5760109190911b9060201c5b630100000081106102655760089190911b9060101c5b62010000010260121c80820401600190811c80830401811c80830401811c80830401811c80830401811c80830401811c80830401901c908190048111900390565b6000602082840312156102b857600080fd5b81356001600160a01b03811681146102cf57600080fd5b9392505050565b6000602082840312156102e857600080fd5b5035919050565b634e487b7160e01b600052601160045260246000fd5b600081600019048311821515161561031f5761031f6102ef565b500290565b60008219821115610337576103376102ef565b500190565b60008261035957634e487b7160e01b600052601260045260246000fd5b50049056fea164736f6c6343000809000a"; type ReputationConstructorParams = | [signer?: Signer] diff --git a/test/Reputation.test.ts b/test/Reputation.test.ts index e69de29..395ca5c 100644 --- a/test/Reputation.test.ts +++ b/test/Reputation.test.ts @@ -0,0 +1,41 @@ +import "@nomicfoundation/hardhat-chai-matchers"; +import { loadFixture } from "@nomicfoundation/hardhat-network-helpers"; +import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; +import { expect } from "chai"; +import { ethers, network } from "hardhat"; + +import { Reputation } from "../src/types"; +import { curve, repFixture } from "./utils/fixtures"; + +describe("Reputation", () => { + // contract deployer/admin + let owner: SignerWithAddress; + // Reputation Interface instance; + let reputation: Reputation; + + before("Set signers and reset network", async () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + [owner] = await (ethers as any).getSigners(); + + await network.provider.send("hardhat_reset"); + }); + beforeEach("Load deployment fixtures", async () => { + ({ reputation } = await loadFixture(repFixture)); + }); + + describe("Limiter", async () => { + it("Curve reliability", async () => { + const tx1 = await reputation.connect(owner).limiter(0); + const tx2 = await reputation.limiter(500); + const tx3 = await reputation + .connect(owner) + .limiter(444444); + const tx4 = await reputation.limiter(988700); + + expect(tx1).to.eq(curve(0)); + expect(tx2).to.eq(curve(500)); + expect(tx3).to.eq(curve(444444)); + expect(tx4).to.eq(curve(988700)); + }); + }); +}); diff --git a/test/p2pix.test.ts b/test/p2pix.test.ts index 074d4cf..5f89d84 100644 --- a/test/p2pix.test.ts +++ b/test/p2pix.test.ts @@ -235,7 +235,7 @@ describe("P2PIX", () => { }); }); describe("Deposit", async () => { - // it ("should revert if ERC20 is not alloed") + // it("should revert if ERC20 is not allowed", async () => { // it ("should revert if deposit already exists") // it ("should create deposit, update storage and emit event") // it ("should create multiple deposits") - EDGE CASE TEST diff --git a/test/utils/fixtures.ts b/test/utils/fixtures.ts index 55835c2..6d8e0e0 100644 --- a/test/utils/fixtures.ts +++ b/test/utils/fixtures.ts @@ -13,13 +13,18 @@ import { // exported interfaces export interface P2pixFixture { p2pix: P2PIX; - reputation: Reputation; erc20: MockToken; // proof: string[]; // wrongProof: string[]; // merkleRoot: string; } +export interface RepFixture { + reputation: Reputation; +} + +type P2PixAndReputation = P2pixFixture & RepFixture; + // exported constants export const getSignerAddrs = ( amount: number, @@ -32,6 +37,7 @@ export const getSignerAddrs = ( } return signers; }; + export const randomSigners = (amount: number): Signer[] => { const signers: Signer[] = []; for (let i = 0; i < amount; i++) { @@ -39,10 +45,12 @@ export const randomSigners = (amount: number): Signer[] => { } return signers; }; + export const getError = (Error: string) => ethers.utils .keccak256(ethers.utils.toUtf8Bytes(Error)) .slice(0, 10); + // eslint-disable-next-line @typescript-eslint/no-explicit-any export const padBuffer = (addr: any) => { return Buffer.from( @@ -51,8 +59,24 @@ export const padBuffer = (addr: any) => { ); }; +export const curve = (x: number): number => { + return Math.round( + 1 + (10 ** 6 * x) / Math.sqrt(2.5 * 10 ** 11 + x * x), + ); +}; + // exported async functions -export async function p2pixFixture(): Promise { +export async function repFixture(): Promise { + const Reputation = await ethers.getContractFactory( + "Reputation", + ); + const reputation = + (await Reputation.deploy()) as Reputation; + + return { reputation }; +} + +export async function p2pixFixture(): Promise { const validSigners = getSignerAddrs( 2, await ethers.getSigners(),