Skip to content

Commit

Permalink
update strat
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth2207 committed Feb 27, 2024
1 parent efb3dc9 commit ad6eab9
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/h20.pubstrats
12 changes: 7 additions & 5 deletions src/TrancheMirror.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ address constant POLYGON_USDT_HOLDER = 0xF977814e90dA44bFA03b6295A0616a897441ace
// Wrapped native token holder.
address constant POLYGON_WETH_HOLDER = 0x8C81A2c64Bf001b03AfB3c513a7be223Ba23de1B;

address constant POLYGON_IEON_ADMIN = 0x3a7bD65AB95678eB2A3a8d37962E89f42a6968c7;


/// @dev https://polygonscan.com/address/0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32
address constant UNI_V2_FACTORY = 0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32;
Expand Down Expand Up @@ -164,7 +166,7 @@ library LibTrancheSpreadOrders {
ffi[27] = "--bind";
ffi[28] = string.concat("spread-ratio=", uint2str(spreadRatio));
ffi[29] = "--bind";
ffi[30] = string.concat("tranche-space-edge-guard-threshold=", uint2str(1e16));
ffi[30] = string.concat("tranche-space-edge-guard-threshold=", uint2str(1e10));


trancheRefill = bytes.concat(getSubparserPrelude(orderBookSubparser), vm.ffi(ffi));
Expand Down Expand Up @@ -198,15 +200,15 @@ library LibTrancheSpreadOrders {
ffi[17] = "--bind";
ffi[18] = "tranche-reserve-amount-growth='tranche-reserve-amount-growth-constant";
ffi[19] = "--bind";
ffi[20] = string.concat("tranche-reserve-amount-base=", uint2str(100e18));
ffi[20] = string.concat("tranche-reserve-amount-base=", uint2str(1000e18));
ffi[21] = "--bind";
ffi[22] = "tranche-reserve-io-ratio-growth='tranche-reserve-io-ratio-linear";
ffi[23] = "--bind";
ffi[24] = string.concat("tranche-reserve-io-ratio-base=", uint2str(111e16));
ffi[24] = string.concat("tranche-reserve-io-ratio-base=", uint2str(326e18));
ffi[25] = "--bind";
ffi[26] = string.concat("spread-ratio=", uint2str(101e16));
ffi[26] = string.concat("spread-ratio=", uint2str(99999999999999999));
ffi[27] = "--bind";
ffi[28] = string.concat("tranche-space-edge-guard-threshold=", uint2str(1e16));
ffi[28] = string.concat("tranche-space-edge-guard-threshold=", uint2str(2e17));


trancheRefill = bytes.concat(getSubparserPrelude(orderBookSubparser), vm.ffi(ffi));
Expand Down
122 changes: 83 additions & 39 deletions test/TrancheMirrorTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,77 @@ import {console2, Test} from "forge-std/Test.sol";
import "test/util/TrancheMirrorUtils.sol";
import "src/TrancheMirror.sol";

contract TrancheMirrorTest is TrancheMirrorUtils {
interface IEONStatsTracker{
function setStatsTracker(address statsTracker) external;
}

using SafeERC20 for IERC20;
contract TrancheMirrorTest is TrancheMirrorUtils {

function testBuyOrderHappyFork() public {
{
uint256 depositAmount = 1e10;
giveTestAccountsTokens(WETH_TOKEN, POLYGON_WETH_HOLDER, ORDER_OWNER, depositAmount);
depositTokens(ORDER_OWNER, WETH_TOKEN, VAULT_ID, depositAmount);
}
OrderV2 memory buyOrder;
{
(bytes memory bytecode, uint256[] memory constants) = PARSER.parse(
LibTrancheSpreadOrders.getTrancheSpreadOrder(
vm,
address(ORDERBOOK_SUPARSER)
)
);
buyOrder = placeOrder(ORDER_OWNER, bytecode, constants, polygonIeonIo(), polygonWethIo());
}
takeOrder(buyOrder, BUY_ROUTE);
}
using SafeERC20 for IERC20;

function testSellOrderHappyFork() public {
function testSellBuyOrderHappyFork() public {
vm.startPrank(POLYGON_IEON_ADMIN);
IEONStatsTracker(address(IEON_TOKEN)).setStatsTracker(address(0));
vm.stopPrank();
{
uint256 depositAmount = 100e18;
uint256 depositAmount = 1000e18;
giveTestAccountsTokens(IEON_TOKEN, POLYGON_IEON_HOLDER, ORDER_OWNER, depositAmount);
depositTokens(ORDER_OWNER, IEON_TOKEN, VAULT_ID, depositAmount);
}
OrderV2 memory sellOrder;
{
OrderV2 memory trancheOrder;
{

IO[] memory tokenVaults = new IO[](2);
tokenVaults[0] = polygonIeonIo();
tokenVaults[1] = polygonWethIo();

(bytes memory bytecode, uint256[] memory constants) = PARSER.parse(
LibTrancheSpreadOrders.getTrancheSpreadOrder(
vm,
address(ORDERBOOK_SUPARSER)
)
);
sellOrder = placeOrder(ORDER_OWNER, bytecode, constants, polygonWethIo(), polygonIeonIo());
trancheOrder = placeOrder(ORDER_OWNER, bytecode, constants, tokenVaults, tokenVaults);
}
takeOrder(sellOrder, SELL_ROUTE);
}
{
vm.recordLogs();
takeOrder(trancheOrder, SELL_ROUTE,1,0);

Vm.Log[] memory entries = vm.getRecordedLogs();
uint256 ratio;
uint256 output;
uint256 input;
for (uint256 j = 0; j < entries.length; j++) {
if (entries[j].topics[0] == keccak256("Context(address,uint256[][])")) {
(, uint256[][] memory context) = abi.decode(entries[j].data, (address, uint256[][]));
ratio = context[2][1];
input = context[3][4];
output = context[4][4];
}
}
console2.log("%s",output);
}
console2.log("Sell clear");
// vm.warp(block.timestamp + 100);
// {
// vm.recordLogs();
// takeOrder(trancheOrder, BUY_ROUTE,0,1);

// Vm.Log[] memory entries = vm.getRecordedLogs();
// uint256 ratio;
// uint256 output;
// uint256 input;
// for (uint256 j = 0; j < entries.length; j++) {
// if (entries[j].topics[0] == keccak256("Context(address,uint256[][])")) {
// (, uint256[][] memory context) = abi.decode(entries[j].data, (address, uint256[][]));
// ratio = context[2][1];
// input = context[3][4];
// output = context[4][4];
// }
// }
// console2.log("%s",input);
// }
}

function giveTestAccountsTokens(IERC20 token, address from, address to, uint256 amount) internal {
vm.startPrank(from);
Expand All @@ -66,15 +96,10 @@ contract TrancheMirrorTest is TrancheMirrorUtils {
address orderOwner,
bytes memory bytecode,
uint256[] memory constants,
IO memory input,
IO memory output
IO[] memory inputs,
IO[] memory outputs
) internal returns (OrderV2 memory order) {
IO[] memory inputs = new IO[](1);
inputs[0] = input;

IO[] memory outputs = new IO[](1);
outputs[0] = output;


EvaluableConfigV3 memory evaluableConfig = EvaluableConfigV3(EXPRESSION_DEPLOYER, bytecode, constants);

OrderConfigV2 memory orderConfig = OrderConfigV2(inputs, outputs, evaluableConfig, "");
Expand All @@ -95,12 +120,9 @@ contract TrancheMirrorTest is TrancheMirrorUtils {
assertEq(stateChanged, true);
}

function takeOrder(OrderV2 memory order, bytes memory route) internal {
function takeOrder(OrderV2 memory order, bytes memory route, uint256 inputIOIndex, uint256 outputIOIndex) internal {
vm.startPrank(APPROVED_EOA);

uint256 inputIOIndex = 0;
uint256 outputIOIndex = 0;

TakeOrderConfigV2[] memory innerConfigs = new TakeOrderConfigV2[](1);

innerConfigs[0] = TakeOrderConfigV2(order, inputIOIndex, outputIOIndex, new SignedContextV1[](0));
Expand All @@ -109,5 +131,27 @@ contract TrancheMirrorTest is TrancheMirrorUtils {
ARB_INSTANCE.arb(takeOrdersConfig, 0);
vm.stopPrank();
}

function moveExternalPrice(
address inputToken,
address outputToken,
address tokenHolder,
uint256 amountIn,
bytes memory encodedRoute
) public {
// An External Account
address EXTERNAL_EOA = address(0x654FEf5Fb8A1C91ad47Ba192F7AA81dd3C821427);
{
giveTestAccountsTokens(IERC20(inputToken), tokenHolder, EXTERNAL_EOA, amountIn);
}
vm.startPrank(EXTERNAL_EOA);

IERC20(inputToken).approve(address(ROUTE_PROCESSOR), amountIn);

bytes memory decodedRoute = abi.decode(encodedRoute, (bytes));

ROUTE_PROCESSOR.processRoute(inputToken, amountIn, outputToken, 0, EXTERNAL_EOA, decodedRoute);
vm.stopPrank();
}
}

4 changes: 2 additions & 2 deletions test/util/TrancheMirrorUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ contract TrancheMirrorUtils is RainContracts, Test {
using LibFixedPointDecimalArithmeticOpenZeppelin for uint256;
using LibFixedPointDecimalScale for uint256;

uint256 constant FORK_BLOCK_NUMBER = 54005438;
uint256 constant FORK_BLOCK_NUMBER = 54013718;
uint256 constant CONTEXT_VAULT_IO_ROWS = 5;

function selectPolygonFork() internal {
uint256 fork = vm.createFork("https://1rpc.io/matic");
uint256 fork = vm.createFork(vm.envString("RPC_URL_POLYGON"));
vm.selectFork(fork);
vm.rollFork(FORK_BLOCK_NUMBER);
}
Expand Down

0 comments on commit ad6eab9

Please sign in to comment.