Compare commits
2 Commits
fe9cf0a00c
...
7c9c9f90de
| Author | SHA1 | Date | |
|---|---|---|---|
| 7c9c9f90de | |||
| 02c0d51335 |
@ -4,10 +4,14 @@ pragma solidity ^0.8.20;
|
||||
import "@openzeppelin-contracts/token/ERC20/IERC20.sol";
|
||||
import "@openzeppelin-contracts/token/ERC20/extensions/IERC20Metadata.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/IUniswapV2Router01.sol";
|
||||
|
||||
import "./types/GhostAccessControlled.sol";
|
||||
import "./libraries/FixedPoint.sol";
|
||||
|
||||
import "./interfaces/IFTSO.sol";
|
||||
import "./interfaces/ISTNK.sol";
|
||||
@ -211,58 +215,58 @@ contract GhostTreasury is GhostAccessControlled, ITreasury {
|
||||
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(
|
||||
address router, // could be an issue
|
||||
address token,
|
||||
uint256 swapAmount,
|
||||
uint256 liquidityAmount
|
||||
uint256 amount,
|
||||
uint256 slippage
|
||||
) external onlyGovernor {
|
||||
uint256 deadline = block.timestamp; // No delays in timestamp
|
||||
address weth = IUniswapV2Router01(router).WETH();
|
||||
address pair = IUniswapV2Factory(IUniswapV2Router01(router).factory()).getPair(ftso, weth);
|
||||
|
||||
// Reconstruct path based on token address.
|
||||
// Avoid user's input error.
|
||||
IERC20(weth).approve(router, amount + 1);
|
||||
|
||||
(uint256 reserve0, uint256 reserve1,) = IUniswapV2Pair(pair).getReserves();
|
||||
address[] memory path;
|
||||
if (token < weth) {
|
||||
path[0] = token;
|
||||
if (ftso < weth) {
|
||||
path[0] = ftso;
|
||||
path[1] = weth;
|
||||
|
||||
reserve0 = reserve1 ^ reserve0;
|
||||
reserve1 = reserve1 ^ reserve0;
|
||||
reserve0 = reserve1 ^ reserve0;
|
||||
} else {
|
||||
path[0] = weth;
|
||||
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];
|
||||
path[1] = ftso;
|
||||
}
|
||||
|
||||
uint256 amountIn = _quantityToBeSwapped(amount, reserve0);
|
||||
uint256[] memory amounts = IUniswapV2Router02(router).getAmountsOut(amountIn, path);
|
||||
IUniswapV2Router02(router).swapExactTokensForTokens(
|
||||
swapAmount,
|
||||
amountOut,
|
||||
amountIn,
|
||||
amounts[0],
|
||||
path,
|
||||
address(this),
|
||||
deadline
|
||||
block.timestamp
|
||||
);
|
||||
|
||||
if (IERC20(weth).allowance(address(this), router) <= amountOut) {
|
||||
IERC20(token).approve(router, amountOut + 1);
|
||||
}
|
||||
amountIn = amount - amountIn;
|
||||
IERC20(ftso).approve(router, amountIn + 1);
|
||||
|
||||
IUniswapV2Router02(router).addLiquidity(
|
||||
path[0],
|
||||
path[1],
|
||||
liquidityAmount,
|
||||
amountOut,
|
||||
liquidityAmount,
|
||||
amountOut,
|
||||
amountIn,
|
||||
amounts[0],
|
||||
amountIn * (1e7 - slippage) / 1e7,
|
||||
amounts[0] * (1e7 - slippage) / 1e7,
|
||||
address(this),
|
||||
deadline
|
||||
block.timestamp
|
||||
);
|
||||
}
|
||||
|
||||
@ -345,6 +349,11 @@ 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) {
|
||||
return totalReserves - (IFTSO(ftso).totalSupply() - totalDebt);
|
||||
}
|
||||
@ -355,7 +364,7 @@ contract GhostTreasury is GhostAccessControlled, ITreasury {
|
||||
) public view override returns (uint256 value) {
|
||||
if (permissions[STATUS.LIQUIDITYTOKEN][token]) {
|
||||
value = IBondingCalculator(bondCalculator[token]).valuation(token, amount);
|
||||
} else {
|
||||
} else if (permissions[STATUS.RESERVETOKEN][token]) {
|
||||
value = amount * 1e9 / (10**IERC20Metadata(token).decimals());
|
||||
value = value * IBondingCalculator(bondCalculator[token]).fraction() / 1e18;
|
||||
}
|
||||
|
||||
@ -239,7 +239,7 @@ contract GhostTreasuryTest is Test {
|
||||
assertEq(treasury.permissions(ITreasury.STATUS.RESERVEDEPOSITOR, msg.sender), false);
|
||||
}
|
||||
|
||||
function test_tokenValueIsCorret() public {
|
||||
function test_mainnet_disableReserveAndLiquidity() public {
|
||||
address realDexPair = 0xB20bd5D04BE54f870D5C0d3cA85d82b34B836405;
|
||||
|
||||
vm.startPrank(governor);
|
||||
@ -247,12 +247,40 @@ contract GhostTreasuryTest is Test {
|
||||
treasury.enable(ITreasury.STATUS.LIQUIDITYTOKEN, realDexPair, address(calculator));
|
||||
vm.stopPrank();
|
||||
|
||||
uint256 totalSupply = IUniswapV2Pair(realDexPair).totalSupply();
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
assertEq(liquidityValue + reserveEps >= reserves, true);
|
||||
|
||||
95
test/treasury/TreasuryCoefficienBigger.sol
Normal file
95
test/treasury/TreasuryCoefficienBigger.sol
Normal file
@ -0,0 +1,95 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
121
test/treasury/TreasuryCoefficientLesser.sol
Normal file
121
test/treasury/TreasuryCoefficientLesser.sol
Normal file
@ -0,0 +1,121 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
# exclude all tests dependant on rpc
|
||||
forge test --no-match-test test_tokenValueIsCorret
|
||||
forge test --no-match-test test_mainnet_
|
||||
|
||||
echo ""
|
||||
echo "#####################################"
|
||||
@ -11,4 +11,4 @@ echo ""
|
||||
FORK_URL="${1:-https://eth-mainnet.alchemyapi.io/v2/pwc5rmJhrdoaSEfimoKEmsvOjKSmPDrP}"
|
||||
|
||||
# run tests where rpc to the mainnet is needed
|
||||
forge test --match-test test_tokenValueIsCorret --fork-url $FORK_URL
|
||||
forge test --match-test test_mainnet_ --fork-url $FORK_URL
|
||||
|
||||
Loading…
Reference in New Issue
Block a user