Compare commits

..

No commits in common. "7c9c9f90def71e0854444a43af31e9d35d8f04ca" and "fe9cf0a00c30b81077144888a4b264d5fcaa2b87" have entirely different histories.

5 changed files with 36 additions and 289 deletions

View File

@ -4,14 +4,10 @@ pragma solidity ^0.8.20;
import "@openzeppelin-contracts/token/ERC20/IERC20.sol"; import "@openzeppelin-contracts/token/ERC20/IERC20.sol";
import "@openzeppelin-contracts/token/ERC20/extensions/IERC20Metadata.sol"; import "@openzeppelin-contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "@openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol"; import "@openzeppelin-contracts/token/ERC20/utils/SafeERC20.sol";
import "@uniswap-v2-core-1.0.1/interfaces/IUniswapV2Factory.sol";
import "@uniswap-v2-core-1.0.1/interfaces/IUniswapV2Pair.sol";
import "@uniswap-v2-periphery-1.1.0-beta.0/interfaces/IUniswapV2Router02.sol"; import "@uniswap-v2-periphery-1.1.0-beta.0/interfaces/IUniswapV2Router02.sol";
import "@uniswap-v2-periphery-1.1.0-beta.0/interfaces/IUniswapV2Router01.sol"; import "@uniswap-v2-periphery-1.1.0-beta.0/interfaces/IUniswapV2Router01.sol";
import "./types/GhostAccessControlled.sol"; import "./types/GhostAccessControlled.sol";
import "./libraries/FixedPoint.sol";
import "./interfaces/IFTSO.sol"; import "./interfaces/IFTSO.sol";
import "./interfaces/ISTNK.sol"; import "./interfaces/ISTNK.sol";
@ -215,58 +211,58 @@ contract GhostTreasury is GhostAccessControlled, ITreasury {
emit Permissioned(toDisable, status, false); emit Permissioned(toDisable, status, false);
} }
function _quantityToBeSwapped(uint256 xa, uint256 x1) internal pure returns (uint256) {
uint256 y1 = x1 * 1994 / 1000;
uint256 y2 = Babylonian.sqrt(y1**2 + 4 * xa * x1 * 997 / 1000);
return (y2 - y1) * 1000 / 1994;
}
function redeemReserve( function redeemReserve(
address router, // could be an issue address router, // could be an issue
uint256 amount, address token,
uint256 slippage uint256 swapAmount,
uint256 liquidityAmount
) external onlyGovernor { ) external onlyGovernor {
uint256 deadline = block.timestamp; // No delays in timestamp
address weth = IUniswapV2Router01(router).WETH(); address weth = IUniswapV2Router01(router).WETH();
address pair = IUniswapV2Factory(IUniswapV2Router01(router).factory()).getPair(ftso, weth);
IERC20(weth).approve(router, amount + 1); // Reconstruct path based on token address.
// Avoid user's input error.
(uint256 reserve0, uint256 reserve1,) = IUniswapV2Pair(pair).getReserves();
address[] memory path; address[] memory path;
if (ftso < weth) { if (token < weth) {
path[0] = ftso; path[0] = token;
path[1] = weth; path[1] = weth;
reserve0 = reserve1 ^ reserve0;
reserve1 = reserve1 ^ reserve0;
reserve0 = reserve1 ^ reserve0;
} else { } else {
path[0] = weth; path[0] = weth;
path[1] = ftso; path[1] = token;
}
uint256 totalAmount = swapAmount + liquidityAmount;
if (IERC20(token).allowance(address(this), router) <= totalAmount) {
IERC20(token).approve(router, totalAmount + 1);
}
uint256[] memory amounts = IUniswapV2Router02(router).getAmountsOut(swapAmount, path);
uint256 amountOut = amounts[0];
if (token < weth) {
amountOut = amounts[1];
} }
uint256 amountIn = _quantityToBeSwapped(amount, reserve0);
uint256[] memory amounts = IUniswapV2Router02(router).getAmountsOut(amountIn, path);
IUniswapV2Router02(router).swapExactTokensForTokens( IUniswapV2Router02(router).swapExactTokensForTokens(
amountIn, swapAmount,
amounts[0], amountOut,
path, path,
address(this), address(this),
block.timestamp deadline
); );
amountIn = amount - amountIn; if (IERC20(weth).allowance(address(this), router) <= amountOut) {
IERC20(ftso).approve(router, amountIn + 1); IERC20(token).approve(router, amountOut + 1);
}
IUniswapV2Router02(router).addLiquidity( IUniswapV2Router02(router).addLiquidity(
path[0], path[0],
path[1], path[1],
amountIn, liquidityAmount,
amounts[0], amountOut,
amountIn * (1e7 - slippage) / 1e7, liquidityAmount,
amounts[0] * (1e7 - slippage) / 1e7, amountOut,
address(this), address(this),
block.timestamp deadline
); );
} }
@ -349,11 +345,6 @@ contract GhostTreasury is GhostAccessControlled, ITreasury {
} }
} }
function originalCoefficient() external view returns (uint256) {
address[] memory reserveTokens = registry[STATUS.RESERVETOKEN];
return IBondingCalculator(bondCalculator[reserveTokens[0]]).fraction();
}
function excessReserves() public view override returns (uint256) { function excessReserves() public view override returns (uint256) {
return totalReserves - (IFTSO(ftso).totalSupply() - totalDebt); return totalReserves - (IFTSO(ftso).totalSupply() - totalDebt);
} }
@ -364,7 +355,7 @@ contract GhostTreasury is GhostAccessControlled, ITreasury {
) public view override returns (uint256 value) { ) public view override returns (uint256 value) {
if (permissions[STATUS.LIQUIDITYTOKEN][token]) { if (permissions[STATUS.LIQUIDITYTOKEN][token]) {
value = IBondingCalculator(bondCalculator[token]).valuation(token, amount); value = IBondingCalculator(bondCalculator[token]).valuation(token, amount);
} else if (permissions[STATUS.RESERVETOKEN][token]) { } else {
value = amount * 1e9 / (10**IERC20Metadata(token).decimals()); value = amount * 1e9 / (10**IERC20Metadata(token).decimals());
value = value * IBondingCalculator(bondCalculator[token]).fraction() / 1e18; value = value * IBondingCalculator(bondCalculator[token]).fraction() / 1e18;
} }

View File

@ -239,7 +239,7 @@ contract GhostTreasuryTest is Test {
assertEq(treasury.permissions(ITreasury.STATUS.RESERVEDEPOSITOR, msg.sender), false); assertEq(treasury.permissions(ITreasury.STATUS.RESERVEDEPOSITOR, msg.sender), false);
} }
function test_mainnet_disableReserveAndLiquidity() public { function test_tokenValueIsCorret() public {
address realDexPair = 0xB20bd5D04BE54f870D5C0d3cA85d82b34B836405; address realDexPair = 0xB20bd5D04BE54f870D5C0d3cA85d82b34B836405;
vm.startPrank(governor); vm.startPrank(governor);
@ -247,40 +247,12 @@ contract GhostTreasuryTest is Test {
treasury.enable(ITreasury.STATUS.LIQUIDITYTOKEN, realDexPair, address(calculator)); treasury.enable(ITreasury.STATUS.LIQUIDITYTOKEN, realDexPair, address(calculator));
vm.stopPrank(); vm.stopPrank();
uint256 totalSupply = IUniswapV2Pair(realDexPair).totalSupply();
assertEq(treasury.tokenValue(address(reserve), 1e18), 1e9); assertEq(treasury.tokenValue(address(reserve), 1e18), 1e9);
(uint256 reserve0, uint256 reserve1,) = IUniswapV2Pair(realDexPair).getReserves(); (uint256 reserve0, uint256 reserve1,) = IUniswapV2Pair(realDexPair).getReserves();
uint256 reserves = reserve0 * reserve1 / 1e6; uint256 reserves = reserve0 * reserve1 / 1e6;
uint256 reserveEps = reserves * 1e5 / 1e7; // 1% uint256 reserveEps = reserves * 1e5 / 1e7; // 1%
uint256 totalSupply = IUniswapV2Pair(realDexPair).totalSupply();
uint256 liquidityValue = (treasury.tokenValue(realDexPair, totalSupply) / 2)**2;
assertEq(liquidityValue + reserveEps >= reserves, true);
assertEq(liquidityValue - reserveEps <= reserves, true);
vm.startPrank(governor);
treasury.disable(ITreasury.STATUS.RESERVETOKEN, address(reserve));
treasury.disable(ITreasury.STATUS.LIQUIDITYTOKEN, realDexPair);
vm.stopPrank();
assertEq(treasury.tokenValue(address(reserve), 1e18), 0);
assertEq(treasury.tokenValue(realDexPair, 1e18), 0);
}
function test_mainnet_tokenValueIsCorret() public {
address realDexPair = 0xB20bd5D04BE54f870D5C0d3cA85d82b34B836405;
vm.startPrank(governor);
treasury.enable(ITreasury.STATUS.RESERVETOKEN, address(reserve), address(calculator));
treasury.enable(ITreasury.STATUS.LIQUIDITYTOKEN, realDexPair, address(calculator));
vm.stopPrank();
assertEq(treasury.tokenValue(address(reserve), 1e18), 1e9);
(uint256 reserve0, uint256 reserve1,) = IUniswapV2Pair(realDexPair).getReserves();
uint256 reserves = reserve0 * reserve1 / 1e6;
uint256 reserveEps = reserves * 1e5 / 1e7; // 1%
uint256 totalSupply = IUniswapV2Pair(realDexPair).totalSupply();
uint256 liquidityValue = (treasury.tokenValue(realDexPair, totalSupply) / 2)**2; uint256 liquidityValue = (treasury.tokenValue(realDexPair, totalSupply) / 2)**2;
assertEq(liquidityValue + reserveEps >= reserves, true); assertEq(liquidityValue + reserveEps >= reserves, true);

View File

@ -1,95 +0,0 @@
pragma solidity 0.8.20;
import {Test} from "forge-std/Test.sol";
import {StdChains} from "forge-std/StdChains.sol";
import "../../src/FatsoERC20.sol";
import "../../src/GhostAuthority.sol";
import "../../src/Treasury.sol";
import "../../src/StandardBondingCalculator.sol";
import "../../src/mocks/ERC20Mock.sol";
contract GhostTreasuryCoefficienBiggerTest is Test {
address public constant owner = 0x0000000000000000000000000000000000000001;
address public constant governor = 0x0000000000000000000000000000000000000002;
address public constant guardian = 0x0000000000000000000000000000000000000003;
address public constant other = 0x0000000000000000000000000000000000000004;
address public constant alice = 0x0000000000000000000000000000000000000005;
address public constant bob = 0x0000000000000000000000000000000000000006;
uint256 public constant amount = 69 * 1e18;
Fatso ftso;
ERC20Mock reserve;
ERC20Mock liquidity;
GhostTreasury treasury;
GhostAuthority authority;
GhostBondingCalculator calculator;
function setUp() public {
vm.startPrank(owner);
authority = new GhostAuthority(
governor,
guardian,
owner,
owner
);
reserve = new ERC20Mock("Reserve Token", "RET");
liquidity = new ERC20Mock("Liquidity Token", "LDT");
ftso = new Fatso(address(authority), "Fatso", "FTSO");
treasury = new GhostTreasury(address(ftso), 69, address(authority));
calculator = new GhostBondingCalculator(address(ftso), 20, 1);
vm.stopPrank();
vm.prank(governor);
authority.pushVault(address(treasury));
vm.startPrank(alice);
reserve.mint(alice, amount);
reserve.approve(address(treasury), type(uint256).max);
ftso.approve(address(treasury), type(uint256).max);
vm.stopPrank();
}
function test_auditTreasuryReservesWithCoefficient() public {
vm.startPrank(governor);
treasury.enable(ITreasury.STATUS.RESERVEDEPOSITOR, alice, address(0));
treasury.enable(ITreasury.STATUS.RESERVETOKEN, address(reserve), address(calculator));
vm.stopPrank();
assertEq(treasury.totalReserves(), 0);
uint256 tokenValue = treasury.tokenValue(address(reserve), amount);
vm.prank(alice);
IERC20(reserve).transfer(address(treasury), amount);
assertEq(treasury.totalReserves(), 0);
vm.prank(governor);
treasury.auditReserves();
assertEq(treasury.totalReserves(), tokenValue);
uint256 coefficient = treasury.originalCoefficient();
uint256 decimals = IERC20Metadata(address(reserve)).decimals() + 9;
uint256 convertedReserves = treasury.totalReserves() * 10**decimals / coefficient;
uint256 convertedEps = convertedReserves * 1e5 / 1e12; // .000001%
assertEq(convertedReserves + convertedEps >= convertedReserves, true);
assertEq(convertedReserves - convertedEps <= convertedReserves, true);
assertEq(treasury.excessReserves(), tokenValue);
}
function test_originalCoefficientCorrect() public {
vm.startPrank(governor);
treasury.enable(ITreasury.STATUS.RESERVEDEPOSITOR, alice, address(0));
treasury.enable(ITreasury.STATUS.RESERVETOKEN, address(reserve), address(calculator));
vm.stopPrank();
uint256 estimation = 20 * 1e18;
uint256 estimationEps = estimation * 1e5 / 1e12; // .000001%
assertEq(estimation + estimationEps >= treasury.originalCoefficient(), true);
assertEq(estimation - estimationEps <= treasury.originalCoefficient(), true);
}
}

View File

@ -1,121 +0,0 @@
pragma solidity 0.8.20;
import {Test} from "forge-std/Test.sol";
import {StdChains} from "forge-std/StdChains.sol";
import "../../src/FatsoERC20.sol";
import "../../src/GhostAuthority.sol";
import "../../src/Treasury.sol";
import "../../src/StandardBondingCalculator.sol";
import "../../src/mocks/ERC20Mock.sol";
contract GhostTreasuryCoefficientLesserTest is Test {
address public constant owner = 0x0000000000000000000000000000000000000001;
address public constant governor = 0x0000000000000000000000000000000000000002;
address public constant guardian = 0x0000000000000000000000000000000000000003;
address public constant other = 0x0000000000000000000000000000000000000004;
address public constant alice = 0x0000000000000000000000000000000000000005;
address public constant bob = 0x0000000000000000000000000000000000000006;
uint256 public constant amount = 69 * 1e18;
Fatso ftso;
ERC20Mock reserve;
ERC20Mock liquidity;
GhostTreasury treasury;
GhostAuthority authority;
GhostBondingCalculator calculator;
function setUp() public {
vm.startPrank(owner);
authority = new GhostAuthority(
governor,
guardian,
owner,
owner
);
reserve = new ERC20Mock("Reserve Token", "RET");
liquidity = new ERC20Mock("Liquidity Token", "LDT");
ftso = new Fatso(address(authority), "Fatso", "FTSO");
treasury = new GhostTreasury(address(ftso), 69, address(authority));
calculator = new GhostBondingCalculator(address(ftso), 1, 20);
vm.stopPrank();
vm.prank(governor);
authority.pushVault(address(treasury));
vm.startPrank(alice);
reserve.mint(alice, amount);
reserve.approve(address(treasury), type(uint256).max);
ftso.approve(address(treasury), type(uint256).max);
vm.stopPrank();
}
function test_deposit_onlyIfApprovedTokenAndApprovedAddress() public {
vm.expectRevert();
vm.prank(alice);
treasury.deposit(address(reserve), amount, 0);
vm.prank(governor);
treasury.enable(ITreasury.STATUS.RESERVEDEPOSITOR, alice, address(0));
vm.expectRevert();
vm.prank(alice);
treasury.deposit(address(reserve), amount, 0);
vm.prank(governor);
treasury.enable(ITreasury.STATUS.RESERVETOKEN, address(reserve), address(calculator));
vm.prank(alice);
uint256 send = treasury.deposit(address(reserve), amount, 0);
assertEq(ftso.balanceOf(alice), send);
assertEq(reserve.balanceOf(address(treasury)), amount);
vm.startPrank(governor);
treasury.enable(ITreasury.STATUS.RESERVEDEPOSITOR, alice, address(0));
treasury.enable(ITreasury.STATUS.RESERVETOKEN, address(reserve), address(calculator));
vm.stopPrank();
}
function test_auditTreasuryReservesWithCoefficient() public {
vm.startPrank(governor);
treasury.enable(ITreasury.STATUS.RESERVEDEPOSITOR, alice, address(0));
treasury.enable(ITreasury.STATUS.RESERVETOKEN, address(reserve), address(calculator));
vm.stopPrank();
assertEq(treasury.totalReserves(), 0);
uint256 tokenValue = treasury.tokenValue(address(reserve), amount);
vm.prank(alice);
IERC20(reserve).transfer(address(treasury), amount);
assertEq(treasury.totalReserves(), 0);
vm.prank(governor);
treasury.auditReserves();
assertEq(treasury.totalReserves(), tokenValue);
uint256 coefficient = treasury.originalCoefficient();
uint256 decimals = IERC20Metadata(address(reserve)).decimals() + 9;
uint256 convertedReserves = treasury.totalReserves() * 10**decimals / coefficient;
uint256 convertedEps = convertedReserves * 1e5 / 1e12; // .000001%
assertEq(convertedReserves + convertedEps >= convertedReserves, true);
assertEq(convertedReserves - convertedEps <= convertedReserves, true);
assertEq(treasury.excessReserves(), tokenValue);
}
function test_originalCoefficientCorrect() public {
vm.startPrank(governor);
treasury.enable(ITreasury.STATUS.RESERVEDEPOSITOR, alice, address(0));
treasury.enable(ITreasury.STATUS.RESERVETOKEN, address(reserve), address(calculator));
vm.stopPrank();
uint256 estimation = 1e18 / 20;
uint256 estimationEps = estimation * 1e5 / 1e12; // .000001%
assertEq(estimation + estimationEps >= treasury.originalCoefficient(), true);
assertEq(estimation - estimationEps <= treasury.originalCoefficient(), true);
}
}

View File

@ -1,5 +1,5 @@
# exclude all tests dependant on rpc # exclude all tests dependant on rpc
forge test --no-match-test test_mainnet_ forge test --no-match-test test_tokenValueIsCorret
echo "" echo ""
echo "#####################################" echo "#####################################"
@ -11,4 +11,4 @@ echo ""
FORK_URL="${1:-https://eth-mainnet.alchemyapi.io/v2/pwc5rmJhrdoaSEfimoKEmsvOjKSmPDrP}" FORK_URL="${1:-https://eth-mainnet.alchemyapi.io/v2/pwc5rmJhrdoaSEfimoKEmsvOjKSmPDrP}"
# run tests where rpc to the mainnet is needed # run tests where rpc to the mainnet is needed
forge test --match-test test_mainnet_ --fork-url $FORK_URL forge test --match-test test_tokenValueIsCorret --fork-url $FORK_URL