Skip to content

Commit

Permalink
Update wlth-tranche-init.rain
Browse files Browse the repository at this point in the history
- added spread 
- integrated new io-ratio-base
  • Loading branch information
TobyMeller authored Aug 19, 2024
1 parent 168c8d1 commit 88c7603
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions strategies/wlth-tranche-init.rain
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Expand All @@ -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(
Expand All @@ -606,13 +617,15 @@ deployments:

#post-action-tranche-linear
current-ratio: ,
io-ratio-base: ,
init-tranche: div(
saturating-sub(current-ratio io-ratio-base)
io-ratio-growth
);

#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))
Expand All @@ -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)
Expand All @@ -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))));

0 comments on commit 88c7603

Please sign in to comment.