Skip to content

Commit

Permalink
update init tranche
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth2207 committed Aug 13, 2024
1 parent ebfc580 commit 2cf6845
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 19 deletions.
4 changes: 4 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 18 additions & 16 deletions strategies/wlth-tranche-init.rain
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
82 changes: 79 additions & 3 deletions test/WlthTrancheInitTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand Down

0 comments on commit 2cf6845

Please sign in to comment.