From 2cf684563e08d3037f63e49013f9ffe3fb89f483 Mon Sep 17 00:00:00 2001 From: Siddharth2207 Date: Tue, 13 Aug 2024 15:53:45 +0530 Subject: [PATCH] update init tranche --- foundry.toml | 4 ++ strategies/wlth-tranche-init.rain | 34 +++++++------ test/WlthTrancheInitTest.t.sol | 82 +++++++++++++++++++++++++++++-- 3 files changed, 101 insertions(+), 19 deletions(-) diff --git a/foundry.toml b/foundry.toml index 8c71083..a2aec50 100644 --- a/foundry.toml +++ b/foundry.toml @@ -6,7 +6,11 @@ libs = ["lib"] solc = "0.8.25" # See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options +optimizer = true +optimizer_runs = 1000000 evm_version = "cancun" +bytecode_hash = "none" +cbor_metadata = false # Needed for dotrain compose ffi = true diff --git a/strategies/wlth-tranche-init.rain b/strategies/wlth-tranche-init.rain index 73a610f..3937c8b 100644 --- a/strategies/wlth-tranche-init.rain +++ b/strategies/wlth-tranche-init.rain @@ -176,6 +176,7 @@ scenarios: # If the market is outside the chart then set this to 0. initial-tranche-space: 0 post-action: '''post-action-buy' + post-action-tranche: '''post-action-tranche-linear' scenarios: prod: bindings: @@ -227,6 +228,7 @@ scenarios: # If the market is outside the chart then set this to 0. initial-tranche-space: 0 post-action: '''post-action-sell' + post-action-tranche: '''post-action-tranche-linear' scenarios: prod: bindings: @@ -439,6 +441,7 @@ deployments: #plottables !The binding for additional things we want to plot during testing. #post-action !Post action source to get current ratio +#post-action-tranche !Binding to calculate tranche space for post action. #io-ratio-current !Current io-ratio to set the #uniswap-words !The subparser for the Uniswap words @@ -577,25 +580,24 @@ deployments: #post-action-constant _: io-ratio-current; +#post-action-tranche-linear + current-ratio: , + init-tranche: div( + saturating-sub(current-ratio io-ratio-base) + io-ratio-growth + ); + +#post-action-tranche-exponential + current-ratio: , + init-tranche: div( + saturating-sub(ln(current-ratio) ln(io-ratio-base)) + ln(add(1 io-ratio-growth)) + ); + #post-add-order using-words-from uniswap-words orderbook-subparser current-ratio: call<'post-action>(), - tranche-0-ratio: call<'io-ratio-expr>(io-ratio-base io-ratio-growth 0), - tranche-1-ratio: call<'io-ratio-expr>(io-ratio-base io-ratio-growth 1), - tranche-2-ratio: call<'io-ratio-expr>(io-ratio-base io-ratio-growth 2), - tranche-3-ratio: call<'io-ratio-expr>(io-ratio-base io-ratio-growth 3), - tranche-4-ratio: call<'io-ratio-expr>(io-ratio-base io-ratio-growth 4), - tranche-5-ratio: call<'io-ratio-expr>(io-ratio-base io-ratio-growth 5), - - init-tranche: conditions( - greater-than(tranche-0-ratio current-ratio) 0 - greater-than(tranche-1-ratio current-ratio) 1 - greater-than(tranche-2-ratio current-ratio) 2 - greater-than(tranche-3-ratio current-ratio) 3 - greater-than(tranche-4-ratio current-ratio) 4 - greater-than(tranche-5-ratio current-ratio) 5 - "default" initial-tranche-space - ), + init-tranche: call<'post-action-tranche>(current-ratio), :set(hash(order-hash() init-key) 1), :set(hash(order-hash() tranche-space-key) init-tranche), :set(hash(order-hash() update-time-key) now()); \ No newline at end of file diff --git a/test/WlthTrancheInitTest.t.sol b/test/WlthTrancheInitTest.t.sol index df4d987..752da2c 100644 --- a/test/WlthTrancheInitTest.t.sol +++ b/test/WlthTrancheInitTest.t.sol @@ -48,7 +48,7 @@ contract StopLimitTest is StrategyTests { using SafeERC20 for IERC20; using Strings for address; - uint256 constant FORK_BLOCK_NUMBER = 18339410; + uint256 constant FORK_BLOCK_NUMBER = 18376923; function selectFork() internal { uint256 fork = vm.createFork(vm.envString("RPC_URL_BASE")); @@ -79,8 +79,8 @@ contract StopLimitTest is StrategyTests { IO[] memory outputVaults = new IO[](1); outputVaults[0] = baseUsdcIo(); - uint256 expectedRatio = 20.5e18; - uint256 expectedOrderAmount = 5.3e18; + uint256 expectedRatio = 22.5e18; + uint256 expectedOrderAmount = 1.676309165235004558e18; LibStrategyDeployment.StrategyDeploymentV4 memory strategy = LibStrategyDeployment.StrategyDeploymentV4( @@ -103,6 +103,82 @@ contract StopLimitTest is StrategyTests { checkStrategyCalculationsArbOrder(strategy); + } + + function testSuccessiveTranches() public { + + IO[] memory inputVaults = new IO[](1); + inputVaults[0] = baseWlthIo(); + + IO[] memory outputVaults = new IO[](1); + outputVaults[0] = baseUsdcIo(); + + LibStrategyDeployment.StrategyDeploymentV4 memory strategy = LibStrategyDeployment.StrategyDeploymentV4( + getEncodedSellWlthRoute(), + getEncodedBuyWlthRoute(), + 0, + 0, + 100000e18, + 10000e6, + 0, + 0, + "strategies/wlth-tranche-init.rain", + "wlth-tranches.buy.initialized.prod", + "./lib/h20.test-std/lib/rain.orderbook", + "./lib/h20.test-std/lib/rain.orderbook/Cargo.toml", + inputVaults, + outputVaults, + new SignedContextV1[](0) + ); + + OrderV3 memory order = addOrderDepositOutputTokens(strategy); + + + // Tranche 0 + { + vm.recordLogs(); + takeExternalOrder(order, strategy.inputTokenIndex, strategy.outputTokenIndex); + + Vm.Log[] memory entries = vm.getRecordedLogs(); + (uint256 strategyAmount, uint256 strategyRatio) = getCalculationContext(entries); + + uint256 expectedTrancheAmount = 1.676309165235004558e18; + uint256 expectedTrancheRatio = 22.5e18; + + assertEq(strategyAmount, expectedTrancheAmount); + assertEq(strategyRatio, expectedTrancheRatio); + } + + // Tranche 1 + { + vm.recordLogs(); + takeExternalOrder(order, strategy.inputTokenIndex, strategy.outputTokenIndex); + + Vm.Log[] memory entries = vm.getRecordedLogs(); + (uint256 strategyAmount, uint256 strategyRatio) = getCalculationContext(entries); + + uint256 expectedTrancheAmount = 5.8e18; + uint256 expectedTrancheRatio = 23e18; + + assertEq(strategyAmount, expectedTrancheAmount); + assertEq(strategyRatio, expectedTrancheRatio); + } + + // Tranche 2 + { + vm.recordLogs(); + takeExternalOrder(order, strategy.inputTokenIndex, strategy.outputTokenIndex); + + Vm.Log[] memory entries = vm.getRecordedLogs(); + (uint256 strategyAmount, uint256 strategyRatio) = getCalculationContext(entries); + + uint256 expectedTrancheAmount = 5.9e18; + uint256 expectedTrancheRatio = 23.5e18; + + assertEq(strategyAmount, expectedTrancheAmount); + assertEq(strategyRatio, expectedTrancheRatio); + } + } function getEncodedBuyWlthRoute() internal pure returns (bytes memory) {