Skip to content

Commit

Permalink
Merge pull request #69 from valory-xyz/testdepository
Browse files Browse the repository at this point in the history
test: deep investigate ProductSupplyLow vs ds-math-sub-underflow
  • Loading branch information
DavidMinarsch authored Apr 19, 2022
2 parents 393cbe0 + ffae35e commit 7043777
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 87 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ jobs:

# Compile the code
- name: Compile, adjust uniswap library hash and recompile again
run: |
./scripts/uni-adjust/adjust-point.sh
./node_modules/.bin/hardhat compile
./scripts/uni-adjust/adjust-pair.sh
./node_modules/.bin/hardhat compile
run: npm run compile

# Run tests and deploy script(s)
- name: Run tests
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ ADD . /code

ENV NODE_OPTIONS=--openssl-legacy-provider
RUN yarn
RUN npx hardhat compile
RUN npm run compile

CMD [ "npx", "hardhat", "node", "--hostname", "0.0.0.0" ]
12 changes: 8 additions & 4 deletions contracts/Depository.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import "./interfaces/IErrors.sol";
import "./interfaces/ITreasury.sol";
import "./interfaces/ITokenomics.sol";


/// @title Bond Depository - Smart contract for OLA Bond Depository
/// @author AL
/// @author Aleksandr Kuperman - <[email protected]>
contract Depository is IErrors, Ownable {
using SafeERC20 for IERC20;

Expand Down Expand Up @@ -100,10 +100,10 @@ contract Depository is IErrors, Ownable {
}

// Calculate the payout in OLA tokens based on the LP pair with the discount factor (DF) calculation
uint256 _epoch = block.number / ITokenomics(tokenomics).epochLen();
uint256 epoch = block.number / ITokenomics(tokenomics).epochLen();
// df uint with defined decimals
payout = ITokenomics(tokenomics).calculatePayoutFromLP(token, tokenAmount, _epoch);

payout = ITokenomics(tokenomics).calculatePayoutFromLP(token, tokenAmount, epoch);
// Check for the sufficient supply
if (payout > product.supply) {
revert ProductSupplyLow(token, productId, payout, product.supply);
Expand All @@ -124,6 +124,10 @@ contract Depository is IErrors, Ownable {
// Take into account this bond in current epoch
ITokenomics(tokenomics).usedBond(payout);

// Uniswap allowance implementation does not revert with the accurate message, check before SafeMath is engaged
if (product.token.allowance(msg.sender, address(this)) < tokenAmount) {
revert InsufficientAllowance(product.token.allowance((msg.sender), address(this)), tokenAmount);
}
// Transfer tokens to the depository
product.token.safeTransferFrom(msg.sender, address(this), tokenAmount);
// Approve treasury for the specified token amount
Expand Down
1 change: 1 addition & 0 deletions contracts/Dispenser.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import "./interfaces/ITokenomics.sol";

/// @title Dispenser - Smart contract for rewards
/// @author AL
/// @author Aleksandr Kuperman - <[email protected]>
contract Dispenser is IErrors, IStructs, Ownable, Pausable, ReentrancyGuard {
using SafeERC20 for IERC20;

Expand Down
23 changes: 13 additions & 10 deletions contracts/Tokenomics.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import "./interfaces/IStructs.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";



/// @title Tokenomics - Smart contract for store/interface for key tokenomics params
/// @author AL
/// @author Aleksandr Kuperman - <[email protected]>
contract Tokenomics is IErrors, IStructs, Ownable {
using FixedPoint for *;

Expand Down Expand Up @@ -488,16 +490,16 @@ contract Tokenomics is IErrors, IStructs, Ownable {
// @dev Calculates the amount of OLA tokens based on LP (see the doc for explanation of price computation). Any can do it
/// @param token Token address.
/// @param tokenAmount Token amount.
/// @param _epoch epoch number
/// @param epoch epoch number
/// @return resAmount Resulting amount of OLA tokens.
function calculatePayoutFromLP(address token, uint256 tokenAmount, uint _epoch) external view
function calculatePayoutFromLP(address token, uint256 tokenAmount, uint256 epoch) external view
returns (uint256 resAmount)
{
uint256 df;
PointEcomonics memory _PE;
// avoid start checkpoint from calculatePayoutFromLP
uint256 _epochC = _epoch + 1;
for (uint256 i = _epochC; i > 0; i--) {
uint256 epochC = epoch + 1;
for (uint256 i = epochC; i > 0; i--) {
_PE = mapEpochEconomics[i-1];
// if current point undefined, so calculatePayoutFromLP called before mined tx(checkpoint)
if(_PE.exists) {
Expand Down Expand Up @@ -545,6 +547,7 @@ contract Tokenomics is IErrors, IStructs, Ownable {
balance1 -= amount1;
uint256 reserveIn = (token0 == ola) ? balance1 : balance0;
uint256 reserveOut = (token0 == ola) ? balance0 : balance1;

amountOLA = amountOLA + getAmountOut(amountPairForOLA, reserveIn, reserveOut);

// The resulting DF amount cannot be bigger than the maximum possible one
Expand Down Expand Up @@ -584,10 +587,10 @@ contract Tokenomics is IErrors, IStructs, Ownable {
}

/// @dev get Point by epoch
/// @param _epoch number of a epoch
/// @param epoch number of a epoch
/// @return _PE raw point
function getPoint(uint256 _epoch) public view returns (PointEcomonics memory _PE) {
_PE = mapEpochEconomics[_epoch];
function getPoint(uint256 epoch) public view returns (PointEcomonics memory _PE) {
_PE = mapEpochEconomics[epoch];
}

/// @dev Get last epoch Point.
Expand All @@ -597,10 +600,10 @@ contract Tokenomics is IErrors, IStructs, Ownable {
}

// decode a uq112x112 into a uint with 18 decimals of precision (cycle into the past), INITIAL_DF if not exist
function getDF(uint256 _epoch) public view returns (uint256 df) {
function getDF(uint256 epoch) public view returns (uint256 df) {
PointEcomonics memory _PE;
uint256 _epochC = _epoch + 1;
for (uint256 i = _epochC; i > 0; i--) {
uint256 epochC = epoch + 1;
for (uint256 i = epochC; i > 0; i--) {
_PE = mapEpochEconomics[i-1];
// if current point undefined, so getDF called before mined tx(checkpoint)
if(_PE.exists) {
Expand Down
1 change: 1 addition & 0 deletions contracts/Treasury.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "./interfaces/IStructs.sol";

/// @title Treasury - Smart contract for managing OLA Treasury
/// @author AL
/// @author Aleksandr Kuperman - <[email protected]>
contract Treasury is IErrors, IStructs, Ownable, ReentrancyGuard {
using SafeERC20 for IERC20;

Expand Down
25 changes: 9 additions & 16 deletions contracts/governance/VotingEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ more than `MAXTIME` (4 years).
//# When new types are added - the whole contract is changed
//# The check() method is modifying to be able to use caching
//# for individual wallet addresses
interface SmartWalletChecker {
interface IChecker {
function check(address account) external returns (bool);
}

Expand Down Expand Up @@ -116,9 +116,8 @@ contract VotingEscrow is Ownable, ReentrancyGuard, ERC20VotesCustom {
string public symbol;
string public version;

// Checker for whitelisted (smart contract) wallets which are allowed to deposit
// Smart wallet contract checker address for whitelisted (smart contract) wallets which are allowed to deposit
// The goal is to prevent tokenizing the escrow
address public futureSmartWalletChecker;
address public smartWalletChecker;

/// @dev Contract constructor
Expand Down Expand Up @@ -151,25 +150,19 @@ contract VotingEscrow is Ownable, ReentrancyGuard, ERC20VotesCustom {
}

/// @dev Set an external contract to check for approved smart contract wallets
/// @param account Address of Smart contract checker
function commitSmartWalletChecker(address account) external onlyOwner {
futureSmartWalletChecker = account;
/// @param checker Address of Smart contract checker
function changeSmartWalletChecker(address checker) external onlyOwner {
smartWalletChecker = checker;
}


/// @dev Apply setting external contract to check approved smart contract wallets
function applySmartWalletChecker() external onlyOwner {
smartWalletChecker = futureSmartWalletChecker;
}


/// @dev Check if the call is from a whitelisted smart contract, revert if not
/// @param account Address to be checked
function assertNotContract(address account) internal {
if (account != tx.origin) {
address checker = smartWalletChecker;
require(checker != address(0) && SmartWalletChecker(checker).check(account),
"SC depositors not allowed");
// TODO Implement own smart contract checker or use one from oracle-dev
if (smartWalletChecker != address(0)) {
require(IChecker(smartWalletChecker).check(account), "SC depositors not allowed");
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions contracts/interfaces/IErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ interface IErrors {
/// @param expected Expected amount.
error WrongAmount(uint256 provided, uint256 expected);

/// @dev Insufficient token allowance.
/// @param provided Provided amount.
/// @param expected Minimum expected amount.
error InsufficientAllowance(uint256 provided, uint256 expected);

/// @dev Incorrect deposit provided for the registration activation.
/// @param sent Sent amount.
/// @param expected Expected amount.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"compile": "./scripts/uni-adjust/adjust-point.sh && ./node_modules/.bin/hardhat compile && ./scripts/uni-adjust/adjust-pair.sh && ./node_modules/.bin/hardhat compile"
},
"repository": {
"type": "git",
Expand Down
2 changes: 0 additions & 2 deletions scripts/uni-adjust/adjust-pair.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ FILE="node_modules/@uniswap/v2-periphery/contracts/libraries/UniswapV2Library.so
x=$(npx hardhat run scripts/uni-adjust/adjust.js)
case "$(uname -s)" in
Darwin)
echo 'Mac OS X'
sed -i.bu "s/96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f/$x/g" $FILE
;;

Linux)
echo 'Linux'
sed -i "s/96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f/$x/g" $FILE
;;

Expand Down
6 changes: 0 additions & 6 deletions scripts/uni-adjust/adjust-point.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
FILE="./node_modules/@uniswap/lib/contracts/libraries/BitMath.sol"
case "$(uname -s)" in
Darwin)
echo 'Mac OS X'
sed -i.bu "s/uint128(-1)/type(uint128).max/g" $FILE
sed -i.bu "s/uint64(-1)/type(uint64).max/g" $FILE
sed -i.bu "s/uint32(-1)/type(uint32).max/g" $FILE
Expand All @@ -11,7 +10,6 @@ case "$(uname -s)" in
;;

Linux)
echo 'Linux'
sed -i "s/uint128(-1)/type(uint128).max/g" $FILE
sed -i "s/uint64(-1)/type(uint64).max/g" $FILE
sed -i "s/uint32(-1)/type(uint32).max/g" $FILE
Expand All @@ -28,7 +26,6 @@ FILE="./node_modules/@uniswap/lib/contracts/libraries/FixedPoint.sol"
rm -rf $FILE
case "$(uname -s)" in
Darwin)
echo 'Mac OS X'
cat << 'EOF' > ./node_modules/@uniswap/lib/contracts/libraries/FixedPoint.sol
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.4.0;
Expand Down Expand Up @@ -185,7 +182,6 @@ library FixedPoint {
EOF
;;
Linux)
echo 'Linux'
cat << EOF > ./node_modules/@uniswap/lib/contracts/libraries/FixedPoint.sol
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.4.0;
Expand Down Expand Up @@ -351,7 +347,6 @@ FILE="./node_modules/@uniswap/lib/contracts/libraries/FullMath.sol"
rm -rf $FILE
case "$(uname -s)" in
Darwin)
echo 'Mac OS X'
cat << 'EOF' >> ./node_modules/@uniswap/lib/contracts/libraries/FullMath.sol
// SPDX-License-Identifier: CC-BY-4.0
pragma solidity >=0.4.0;
Expand Down Expand Up @@ -413,7 +408,6 @@ EOF
;;

Linux)
echo 'Linux'
cat << 'EOF' >> ./node_modules/@uniswap/lib/contracts/libraries/FullMath.sol
// SPDX-License-Identifier: CC-BY-4.0
pragma solidity >=0.4.0;
Expand Down
4 changes: 2 additions & 2 deletions test/integration/TokenomicsLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ describe("Tokenomics integration", async () => {
const maxThreshold = 1;
const name = "service name";
const description = "service description";
const milETHBalance = ethers.utils.parseEther("1000000");
const regServiceRevenue = milETHBalance;
const hundredETHBalance = ethers.utils.parseEther("100");
const regServiceRevenue = hundredETHBalance;
const agentId = 1;
const agentParams = [1, regBond];
const serviceId = 1;
Expand Down
Loading

0 comments on commit 7043777

Please sign in to comment.