From 88c76036bbd9efc4e1ae44e562c25d5ae1aa6eda Mon Sep 17 00:00:00 2001 From: TobyMeller <120049117+TobyMeller@users.noreply.github.com> Date: Mon, 19 Aug 2024 09:23:32 +0100 Subject: [PATCH] Update wlth-tranche-init.rain - added spread - integrated new io-ratio-base --- strategies/wlth-tranche-init.rain | 34 ++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/strategies/wlth-tranche-init.rain b/strategies/wlth-tranche-init.rain index dff33de..bc8d10e 100644 --- a/strategies/wlth-tranche-init.rain +++ b/strategies/wlth-tranche-init.rain @@ -408,12 +408,12 @@ deployments: #tranche-space-recharge-delay !The duration in seconds that no recharging occurs after a trade occurs. #tranche-size-expr !The binding to get the tranche size for the current tranche space. -#tranche-size-base !Base tranche size is the size of the smallest tranche, denominated in output token. -#io-ratio-spread !The distance the tranche starts from away from the cost basis. +#tranche-size-base !Base tranche size is the size of the smallest tranche, denominated in output token. #tranche-size-growth !The exponential growth factor of the size of each tranche. E.g. 0.01 is 1% output amount growth per tranche. #io-ratio-expr !The binding to get the IO ratio for the current tranche space. -#io-ratio-base !The base IO ratio, as a decimal 18 fixed point number. This is the IO ratio at tranche space 0 and grows according to the growth factor per tranche. +/* #io-ratio-base !The base IO ratio, as a decimal 18 fixed point number. This is the IO ratio at tranche space 0 and grows according to the growth factor per tranche. */ +#io-ratio-spread !The size of the spread in percentage terms which is the distance of the first tranche away from the midpoint. #io-ratio-growth !The exponential growth factor of the IO ratio. E.g. 0.01 is 1% io-ratio growth per tranche. #reference-token !Strategy token for which the budget is written. @@ -551,15 +551,25 @@ deployments: sale-cost-basis: div(cumulative-sale-costb cumulative-sale-volumeb), buy-cost-basis: div(cumulative-buy-costb cumulative-buy-volumeb), - /* ADD : if initized hasnt change then intiilised */ - + /* ADD : if initized hasnt change then use the initialised price*/ + /* the below should be in dollar terms */ current-cost-basis: div(add(mul(sale-cost-basis cumulative-sale-volumeb) mul(buy-cost-basis cumulative-buy-volumeb)) total-volume); +#apply-spread + amount-is-output :, + io-ratio-spread :, + cost-basis : call<'calculate-midpoint>(), + spread-basis : if(amount-is-output + mul(inv(cost-basis) sub(1 io-ratio-spread)) + mul(cost-basis sub(1 io-ratio-spread))); /*have to invert the cost basis as its in dollar terms for the buy side*/ + + #calculate-io using-words-from uniswap-words orderbook-subparser tranche-space-now tranche-space-available tranche-total-size: call<'calculate-tranche>(), + io-ratio-base: call<'apply-spread>(amount-is-output io-ratio-spread), tranche-io-ratio: call<'io-ratio-expr>(io-ratio-base io-ratio-growth floor(tranche-space-now)), final-io-ratio: mul(tranche-io-ratio call<'io-ratio-multiplier>()), amount-available: mul(tranche-total-size tranche-space-available), @@ -585,7 +595,8 @@ deployments: greater-than-or-equal-to(tranche-space-after-snapped add(tranche-space-before min-tranche-space-diff)) "Minimum trade size not met." ), - :call<'set-last-tranche>(tranche-space-after-snapped current-time); + :call<'set-last-tranche>(tranche-space-after-snapped current-time), + :call<'post-update-cost-basis>(amount-is-output); #post-action-buy _: uniswap-v3-twap-output-ratio( @@ -606,6 +617,7 @@ deployments: #post-action-tranche-linear current-ratio: , + io-ratio-base: , init-tranche: div( saturating-sub(current-ratio io-ratio-base) io-ratio-growth @@ -613,6 +625,7 @@ deployments: #post-action-tranche-exponential current-ratio: , + io-ratio-base: , init-tranche: div( saturating-sub(ln(current-ratio) ln(io-ratio-base)) ln(add(1 io-ratio-growth)) @@ -637,15 +650,16 @@ sudo code of what i want to achieve below, deonminated in USDC cumulative_buy_volume += volume cumulative_buy_cost += volume * price */ + amount-is-output:, :set(hash(order-hash() cumulative-buy-volume) if(amount-is-output - add(get(hash(order-hash()cumulative-buy-volume)) output-vault-increase()) + add(get(hash(order-hash()cumulative-buy-volume)) output-vault-decrease()) get(hash(order-hash()cumulative-buy-volume)))), :set(hash(order-hash() cumulative-buy-cost) if(is-zero(amount-is-output) - add(get(hash(order-hash() cumulative-buy-cost)) mul(output-vault-increase() inv(calculated-io-ratio()))) - get(hash(order-hash() cumulative-buy-cost))), + add(get(hash(order-hash() cumulative-buy-volume)) mul(output-vault-decrease() inv(calculated-io-ratio()))) + get(hash(order-hash() cumulative-buy-cost)))), :set(hash(order-hash()cumulative-sale-volume) if(is-zero(amount-is-output) @@ -654,5 +668,5 @@ sudo code of what i want to achieve below, deonminated in USDC :set(hash(order-hash()cumulative-sale-cost) if(is-zero(amount-is-output) - add(get(hash(order-hash() cumulative-sale-cost)) mul(input-vault-increase() calculated-io-ratio())) + add(get(hash(order-hash() cumulative-sale-volume)) mul(input-vault-increase() calculated-io-ratio())) get(hash(order-hash() cumulative-sale-cost))));