@@ -103,26 +103,27 @@ export const getStartRebalance = (
103103 // D27{tok/share}{wholeShare/wholeTok} = D27 * {tok/wholeTok} / {share/wholeShare}
104104 const limitMultiplier = D27d . mul ( new Decimal ( `1e${ decimals [ i ] } ` ) ) . div ( D18d ) ;
105105
106- if ( spotWeight . eq ( ZERO ) ) {
107- newWeights . push ( {
108- low : 0n ,
109- spot : 0n ,
110- high : 0n ,
111- } ) ;
112- } else if ( weightControl ) {
106+ if ( weightControl ) {
113107 // NATIVE case
114108
115- // {wholeTok/wholeShare} = {wholeTok/wholeShare} / {1}
116- const lowWeight = spotWeight . mul ( ONE . sub ( priceError [ i ] ) ) ;
117- const highWeight = spotWeight . div ( ONE . sub ( priceError [ i ] ) ) ;
118-
119109 // D27{tok/share} = {wholeTok/wholeShare} * D27{tok/share}{wholeShare/wholeTok} / {BU/share}
120110 newWeights . push ( {
121- low : deferWeights ? 0n : bn ( lowWeight . mul ( limitMultiplier ) ) ,
111+ low : bn ( spotWeight . mul ( ONE . sub ( priceError [ i ] ) ) . mul ( limitMultiplier ) ) ,
122112 spot : bn ( spotWeight . mul ( limitMultiplier ) ) ,
123- high : deferWeights ? D27n * D27n : bn ( highWeight . mul ( limitMultiplier ) ) ,
113+ high : bn ( spotWeight . div ( ONE . sub ( priceError [ i ] ) ) . mul ( limitMultiplier ) ) ,
124114 } ) ;
125- // 1e54 MAX_WEIGHT
115+
116+ // deferWeights case (ONLY for NATIVE)
117+ if ( deferWeights ) {
118+ newWeights [ i ] . low = 0n ;
119+
120+ // prevent removeFromBasket() griefing for removals
121+ if ( newWeights [ i ] . spot == 0n ) {
122+ newWeights [ i ] . spot = 1n ;
123+ }
124+
125+ newWeights [ i ] . high = D27n * D27n ;
126+ }
126127 } else {
127128 // TRACKING case
128129
@@ -134,6 +135,16 @@ export const getStartRebalance = (
134135 } ) ;
135136 }
136137
138+ // check if weights are valid
139+ if (
140+ newWeights [ i ] . low < 0n ||
141+ newWeights [ i ] . low > newWeights [ i ] . spot ||
142+ newWeights [ i ] . spot > newWeights [ i ] . high ||
143+ newWeights [ i ] . high > D27n * D27n
144+ ) {
145+ throw new Error ( `invalid weights for token ${ tokens [ i ] } ` ) ;
146+ }
147+
137148 // === newPrices ===
138149
139150 // D27{wholeTok/tok} = D27 / {tok/wholeTok}
0 commit comments