* first commit * feat: implemented get tokens and improved modifier * feat: added missing functions (to implement) * feat: implemented getPoolIds * feat: implemented getCapabilities * feat: implemented getLimits function * fix: fixed constructor * feat and fix: implemented testGetLimitsFraxEthV3 and fixed getLimits * feat: implementing getPriceAt function * feat and fix: finished implementing getPriceAt, fixed modifier * feat: price function and tests implemented * fix: removed onlySupportedTokens modifier from getPriceAt and applied it in price and swap function * feat: implemented sell, buy, and swap functions * implementing final tests * aligned with main branch * fixes * fix: Review Fixes * feat: 🎨 sfraxeth substream initial scaffolding * fix: 🎨 protocol component creation at deployment block * build: 💚 cargo build * feat: 🎨 map proper event to balance changes * fix: 🚚 remove unnecessary files * fix: 💚 ci checks Due to the CI checks requiring the latest rust nightly, the rust-lang organisation introduced new doc related rules. This commit fixes the CI errors by making the necessary changes to the comments in substreams-balancer comments * fix: 🐛 wasm output name * fix: 🐛 update starting block = deployment block -1 * feat: 🎨 add store for reward cycles and update balances accounting after after deposit before Withdraw * feat: 🎨 finish setting up block reward logic * docs: 📝 add comments on extra module * build: 📌 adapt dependencies to workspace dependencies setting prost-types to workspace version causes build errors * feat: 🎨 add support for several EVM compatible networks * fix: 🐛 update balance delta accounting logic following the `NextRewardCycle` event only * fix: 🐛 hex address string param encoding * fix: 🐛 deployment transaction check * ci: 💚 ci check passing * fix: 🐛 issues with hex-binary encoding * refactor: ♻️ address mappings for various networks * fix: 💚 formatting * feat: Implemented testPoolBehaviour * chore: Removed unused comments * feat: ⬆️ update to recent sdk * feat: 🎨 testing setup * test: ✅ setup test environminte for sfraxeth * fix: 🐛 unwrap error in map_protocol_changes * build: ⬆️ update rust version * build: ➖ remove unnecessary deps * build: 🚚 remove unnecessary pb/tycho * fix: 🐛 remove balance owner attribute * fix: 🐛 remove unnecessary static attributes * fix: 🐛 remove manual updates * fix: 🔥 remove unused data model from contract.proto * fix: 🐛 filter by known components * feat: ⚡ use store delta for reward change accounting * refactor: ♻️ remove shallow create vault component * feat: ⚡ replace is_deployment_tx logic with simpler txn match * test: ✅ manual testing with inspection against etherscan https://etherscan.io/address/0xac3E018457B222d93114458476f3E3416Abbe38F#events * ci: 💚 ci checks * fix: 🐛 map_protocol_components output data * fix: 🐛 output type on map_protocol_changes * test: 🧪 skip balance checks * fixed FraxV3FrxEthAdapter arguments for constructor in manifest.yaml * fix: 🐛 adapter error with overflow/underflow and addresses * restore: restored previous adater version * fix: set minimum swap amount to prevent overflow/underflow * fix: set minimum swap amount only for sfrxETH -> frxETH * improve: added print block_number to runner.py when get_amout_out fails * removed console.log * alignment with propeller main * Update forge-std submodule reference to include ds-test * installed protosim_py 0.21.0 * commented out minimum swap amount for sfrxEth -> frxEth pair * updated adapter limits * working on fixes * fix: Adjust getLimits according to protocol limitation. Previously limits were estimated with token supplies, this commit simplifies limits and adjusts them so they correspond closely with what is supported by the sfrxETH contract. * chore: fmt * wip: Changed ubuntu to 20.04, fmt adapters * wip: Updated python tests * wip: Trying with ubuntu: latest * chore: fmt adapters * wip: Using ubuntu 20.04 * chore: Switched back to ubuntu-latest --------- Co-authored-by: Ignazio Bovo <ignazio@jsgenesis.com> Co-authored-by: domenicodev <domenico.romeo3919@gmail.com> Co-authored-by: kayibal <alan@datarevenue.com> Co-authored-by: domenicodev <domenico.rom3@gmail.com>
274 lines
9.2 KiB
Solidity
274 lines
9.2 KiB
Solidity
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
pragma solidity ^0.8.13;
|
|
|
|
import "./AdapterTest.sol";
|
|
import "forge-std/Test.sol";
|
|
import "forge-std/console.sol";
|
|
import "src/interfaces/ISwapAdapterTypes.sol";
|
|
import "src/libraries/FractionMath.sol";
|
|
import "src/sfraxeth/FraxV3FrxEthAdapter.sol";
|
|
|
|
contract FraxV3FrxEthAdapterTest is Test, ISwapAdapterTypes, AdapterTest {
|
|
using FractionMath for Fraction;
|
|
|
|
FraxV3FrxEthAdapter adapter;
|
|
|
|
address FRAXETH_ADDRESS;
|
|
address constant SFRAXETH_ADDRESS =
|
|
0xac3E018457B222d93114458476f3E3416Abbe38F;
|
|
address constant FRAXETHMINTER_ADDRESS =
|
|
0xbAFA44EFE7901E04E39Dad13167D089C559c1138;
|
|
address constant ETH_ADDRESS = address(0);
|
|
|
|
IERC20 FRAXETH;
|
|
IERC20 constant SFRAXETH =
|
|
IERC20(0xac3E018457B222d93114458476f3E3416Abbe38F);
|
|
IERC20 constant WBTC = IERC20(0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599);
|
|
|
|
uint256 constant TEST_ITERATIONS = 10;
|
|
uint256 constant AMOUNT0 = 1000000000000000000;
|
|
bytes32 constant PAIR = bytes32(0);
|
|
|
|
function setUp() public {
|
|
uint256 forkBlock = 19341682;
|
|
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
|
|
|
|
adapter =
|
|
new FraxV3FrxEthAdapter(FRAXETHMINTER_ADDRESS, SFRAXETH_ADDRESS);
|
|
FRAXETH = IERC20(address(ISfrxEth(address(SFRAXETH)).asset()));
|
|
FRAXETH_ADDRESS = address(FRAXETH);
|
|
}
|
|
|
|
/////////////////////////////////////// PRICE
|
|
// ////////////////////////////////////
|
|
|
|
/// @dev set lower limit to greater than 1, because previewDeposit returns 0
|
|
/// with an amountIn == 1
|
|
function testPriceFuzzFraxEthV3FraxEth(uint256 amount0, uint256 amount1)
|
|
public
|
|
{
|
|
uint256[] memory limits =
|
|
adapter.getLimits(PAIR, FRAXETH_ADDRESS, SFRAXETH_ADDRESS);
|
|
vm.assume(amount0 < limits[0]);
|
|
vm.assume(amount0 > 1);
|
|
vm.assume(amount1 < limits[0]);
|
|
vm.assume(amount1 > 1);
|
|
|
|
uint256[] memory amounts = new uint256[](2);
|
|
amounts[0] = amount0;
|
|
amounts[1] = amount1;
|
|
|
|
Fraction[] memory prices =
|
|
adapter.price(PAIR, FRAXETH_ADDRESS, SFRAXETH_ADDRESS, amounts);
|
|
|
|
for (uint256 i = 0; i < prices.length; i++) {
|
|
assertGt(prices[i].numerator, 0);
|
|
assertGt(prices[i].denominator, 0);
|
|
}
|
|
}
|
|
|
|
/// @dev The price is kept among swaps if no FRAX rewards are distributed in
|
|
/// the contract during time
|
|
function testPriceKeepingSellFraxEthFraxEthV3() public {
|
|
uint256[] memory amounts = new uint256[](TEST_ITERATIONS);
|
|
|
|
for (uint256 i = 0; i < TEST_ITERATIONS; i++) {
|
|
amounts[i] = 1000 * (i + 1) * 10 ** 18;
|
|
}
|
|
|
|
Fraction[] memory prices =
|
|
adapter.price(PAIR, FRAXETH_ADDRESS, SFRAXETH_ADDRESS, amounts);
|
|
|
|
for (uint256 i = 0; i < TEST_ITERATIONS - 1; i++) {
|
|
assertEq(prices[i].compareFractions(prices[i + 1]), 0);
|
|
assertGt(prices[i].denominator, 0);
|
|
assertGt(prices[i + 1].denominator, 0);
|
|
}
|
|
}
|
|
|
|
function testPriceKeepingSellSFraxEthFraxEthV3() public {
|
|
uint256[] memory amounts = new uint256[](TEST_ITERATIONS);
|
|
|
|
for (uint256 i = 0; i < TEST_ITERATIONS; i++) {
|
|
amounts[i] = 1000 * (i + 1) * 10 ** 18;
|
|
}
|
|
|
|
Fraction[] memory prices =
|
|
adapter.price(PAIR, SFRAXETH_ADDRESS, FRAXETH_ADDRESS, amounts);
|
|
|
|
for (uint256 i = 0; i < TEST_ITERATIONS - 1; i++) {
|
|
assertEq(prices[i].compareFractions(prices[i + 1]), 0);
|
|
assertGt(prices[i].denominator, 0);
|
|
assertGt(prices[i + 1].denominator, 0);
|
|
}
|
|
}
|
|
|
|
function testPriceKeepingSellEthFraxEthV3() public {
|
|
uint256[] memory amounts = new uint256[](TEST_ITERATIONS);
|
|
|
|
for (uint256 i = 0; i < TEST_ITERATIONS; i++) {
|
|
amounts[i] = 1000 * (i + 1) * 10 ** 18;
|
|
}
|
|
|
|
Fraction[] memory prices =
|
|
adapter.price(PAIR, ETH_ADDRESS, SFRAXETH_ADDRESS, amounts);
|
|
|
|
for (uint256 i = 0; i < TEST_ITERATIONS - 1; i++) {
|
|
assertEq(prices[i].compareFractions(prices[i + 1]), 0);
|
|
assertGt(prices[i].denominator, 0);
|
|
assertGt(prices[i + 1].denominator, 0);
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////// SWAP
|
|
// ///////////////////////////////////
|
|
|
|
function testSwapFuzzsFraxEthV3WithFraxEth(
|
|
uint256 specifiedAmount,
|
|
bool isBuy
|
|
) public {
|
|
vm.assume(specifiedAmount > 1);
|
|
|
|
OrderSide side = isBuy ? OrderSide.Buy : OrderSide.Sell;
|
|
|
|
uint256[] memory limits =
|
|
adapter.getLimits(PAIR, FRAXETH_ADDRESS, SFRAXETH_ADDRESS);
|
|
|
|
if (side == OrderSide.Buy) {
|
|
vm.assume(specifiedAmount < limits[1]);
|
|
|
|
deal(address(FRAXETH), address(this), type(uint256).max);
|
|
FRAXETH.approve(address(adapter), type(uint256).max);
|
|
} else {
|
|
vm.assume(specifiedAmount < limits[0]);
|
|
|
|
deal(address(FRAXETH), address(this), specifiedAmount);
|
|
FRAXETH.approve(address(adapter), specifiedAmount);
|
|
}
|
|
|
|
uint256 frxEth_balance_before = FRAXETH.balanceOf(address(this));
|
|
uint256 sfrxEth_balance_before = SFRAXETH.balanceOf(address(this));
|
|
|
|
Trade memory trade = adapter.swap(
|
|
PAIR, FRAXETH_ADDRESS, SFRAXETH_ADDRESS, side, specifiedAmount
|
|
);
|
|
|
|
uint256 frxEth_balance_after = FRAXETH.balanceOf(address(this));
|
|
uint256 sfrxEth_balance_after = SFRAXETH.balanceOf(address(this));
|
|
|
|
if (trade.calculatedAmount > 0) {
|
|
if (side == OrderSide.Buy) {
|
|
assertEq(
|
|
specifiedAmount,
|
|
sfrxEth_balance_after - sfrxEth_balance_before
|
|
);
|
|
assertEq(
|
|
trade.calculatedAmount,
|
|
frxEth_balance_before - frxEth_balance_after
|
|
);
|
|
} else {
|
|
assertEq(
|
|
specifiedAmount,
|
|
frxEth_balance_before - frxEth_balance_after
|
|
);
|
|
assertEq(
|
|
trade.calculatedAmount,
|
|
sfrxEth_balance_after - sfrxEth_balance_before
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
function testSwapFuzzFraxEthV3WithSFraxEth(
|
|
uint256 specifiedAmount,
|
|
bool isBuy
|
|
) public {
|
|
vm.assume(specifiedAmount > 1);
|
|
|
|
OrderSide side = isBuy ? OrderSide.Buy : OrderSide.Sell;
|
|
|
|
uint256[] memory limits =
|
|
adapter.getLimits(PAIR, SFRAXETH_ADDRESS, FRAXETH_ADDRESS);
|
|
|
|
if (side == OrderSide.Buy) {
|
|
vm.assume(specifiedAmount < limits[1]);
|
|
|
|
deal(address(SFRAXETH), address(this), type(uint256).max);
|
|
SFRAXETH.approve(address(adapter), type(uint256).max);
|
|
} else {
|
|
vm.assume(specifiedAmount < limits[0]);
|
|
|
|
deal(address(SFRAXETH), address(this), specifiedAmount);
|
|
SFRAXETH.approve(address(adapter), specifiedAmount);
|
|
}
|
|
|
|
uint256 frxEth_balance_before = FRAXETH.balanceOf(address(this));
|
|
uint256 sfrxEth_balance_before = SFRAXETH.balanceOf(address(this));
|
|
|
|
ISfrxEth(SFRAXETH_ADDRESS).totalAssets();
|
|
|
|
Trade memory trade = adapter.swap(
|
|
PAIR, SFRAXETH_ADDRESS, FRAXETH_ADDRESS, side, specifiedAmount
|
|
);
|
|
|
|
uint256 frxEth_balance_after = FRAXETH.balanceOf(address(this));
|
|
uint256 sfrxEth_balance_after = SFRAXETH.balanceOf(address(this));
|
|
|
|
if (trade.calculatedAmount > 0) {
|
|
if (side == OrderSide.Buy) {
|
|
assertEq(
|
|
specifiedAmount,
|
|
frxEth_balance_after - frxEth_balance_before
|
|
);
|
|
assertEq(
|
|
trade.calculatedAmount,
|
|
sfrxEth_balance_before - sfrxEth_balance_after
|
|
);
|
|
} else {
|
|
assertEq(
|
|
specifiedAmount,
|
|
sfrxEth_balance_before - sfrxEth_balance_after
|
|
);
|
|
assertEq(
|
|
trade.calculatedAmount,
|
|
frxEth_balance_after - frxEth_balance_before
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
function testGetTokensFraxEthV3() public {
|
|
address[] memory tokens = adapter.getTokens(bytes32(0));
|
|
|
|
assertEq(tokens[0], FRAXETH_ADDRESS);
|
|
assertEq(tokens[1], SFRAXETH_ADDRESS);
|
|
}
|
|
|
|
function testGetLimitsFraxEthV3() public {
|
|
uint256[] memory limits =
|
|
adapter.getLimits(bytes32(0), FRAXETH_ADDRESS, SFRAXETH_ADDRESS);
|
|
assertEq(limits.length, 2);
|
|
|
|
adapter.getLimits(bytes32(0), ETH_ADDRESS, SFRAXETH_ADDRESS);
|
|
assertEq(limits.length, 2);
|
|
|
|
adapter.getLimits(bytes32(0), SFRAXETH_ADDRESS, FRAXETH_ADDRESS);
|
|
assertEq(limits.length, 2);
|
|
}
|
|
|
|
function testGetCapabilitiesFraxEthV3() public {
|
|
Capability[] memory res =
|
|
adapter.getCapabilities(bytes32(0), ETH_ADDRESS, FRAXETH_ADDRESS);
|
|
|
|
assertEq(res.length, 4);
|
|
}
|
|
// This test is currently broken due to a bug in runPoolBehaviour
|
|
// with constant price pools.
|
|
//
|
|
// function testPoolBehaviourFraxV3Sfrax() public {
|
|
// bytes32[] memory poolIds = new bytes32[](1);
|
|
// poolIds[0] = bytes32(0);
|
|
// runPoolBehaviourTest(adapter, poolIds);
|
|
// }
|
|
}
|