@@ -74,6 +74,7 @@ type contract struct {
7474 stakingContractAddress common.Address
7575 stakingContractABI abi.ABI
7676 bzzTokenAddress common.Address
77+ priceOracleAddress common.Address
7778 transactionService transaction.Service
7879 overlayNonce common.Hash
7980 gasLimit uint64
@@ -89,12 +90,14 @@ func New(
8990 nonce common.Hash ,
9091 gasLimit uint64 ,
9192 height uint8 ,
93+ priceOracleAddress common.Address ,
9294) Contract {
9395 return & contract {
9496 owner : owner ,
9597 stakingContractAddress : stakingContractAddress ,
9698 stakingContractABI : stakingContractABI ,
9799 bzzTokenAddress : bzzTokenAddress ,
100+ priceOracleAddress : priceOracleAddress ,
98101 transactionService : transactionService ,
99102 overlayNonce : nonce ,
100103 gasLimit : gasLimit ,
@@ -189,34 +192,33 @@ func (c *contract) GetMinDeposit(ctx context.Context) (*big.Int, error) {
189192 return calculateMinDeposit (potential , committed , price , c .height ), nil
190193}
191194
192- // calculateMinDeposit returns the minimum additional deposit in PLUR that
193- // manageStake will accept. The first deposit must cover 2^height * MIN_STAKE.
194- // Later deposits must keep committed stake from decreasing after a price
195- // increase; if that constraint is already satisfied the minimum is 1 PLUR.
195+ // calculateMinDeposit returns the minimum additional deposit in PLUR that manageStake will accept according to contract.
196196func calculateMinDeposit (potential , committed * big.Int , price uint32 , height uint8 ) * big.Int {
197197 minAdd := new (big.Int )
198198
199+ // potential stake should be at least MIN_STAKE * 2^height.
200+ // minAdd = max(0, minTotal - potential).
199201 minTotal := new (big.Int ).Lsh (new (big.Int ).Set (MinimumStakeAmount ), uint (height ))
200202 if gap := new (big.Int ).Sub (minTotal , potential ); gap .Sign () > 0 {
201203 minAdd .Set (gap )
202204 }
203205
204206 if price != 0 && committed .Sign () > 0 {
207+ // User already has committed stake.
208+ // Commitment protection: required = committed * price * 2^height
205209 required := new (big.Int ).SetUint64 (uint64 (price ))
206- required .Lsh (required , uint (height ))
207- required .Mul (required , committed )
210+ required .Lsh (required , uint (height )) // * 2^height
211+ required .Mul (required , committed ) // * committed
208212 if gap := new (big.Int ).Sub (required , potential ); gap .Cmp (minAdd ) > 0 {
209213 minAdd .Set (gap )
210214 }
211215 }
212216
217+ // floors and commitment already satisfied.
218+ // DepositStake still needs a positive addAmount; 1 PLUR is enough.
213219 if minAdd .Sign () == 0 {
214- if potential .Sign () > 0 {
215- return big .NewInt (1 )
216- }
217- return new (big.Int ).Set (MinimumStakeAmount )
220+ return big .NewInt (1 )
218221 }
219-
220222 return minAdd
221223}
222224
@@ -396,7 +398,7 @@ func (c *contract) getStake(ctx context.Context) (committed, potential *big.Int,
396398 Data : callData ,
397399 })
398400 if err != nil {
399- return nil , nil , fmt .Errorf ("get potential stake : %w" , err )
401+ return nil , nil , fmt .Errorf ("get stakes : %w" , err )
400402 }
401403
402404 // overlay bytes32,
@@ -418,44 +420,20 @@ func (c *contract) getStake(ctx context.Context) (committed, potential *big.Int,
418420}
419421
420422func (c * contract ) getCurrentPrice (ctx context.Context ) (uint32 , error ) {
421- callData , err := c . stakingContractABI . Pack ("OracleContract " )
423+ callData , err := priceOracleABI . Pack ("currentPrice " )
422424 if err != nil {
423425 return 0 , err
424426 }
425427
426428 result , err := c .transactionService .Call (ctx , & transaction.TxRequest {
427- To : & c .stakingContractAddress ,
428- Data : callData ,
429- })
430- if err != nil {
431- return 0 , fmt .Errorf ("get oracle address: %w" , err )
432- }
433-
434- results , err := c .stakingContractABI .Unpack ("OracleContract" , result )
435- if err != nil {
436- return 0 , err
437- }
438-
439- if len (results ) == 0 {
440- return 0 , errors .New ("unexpected empty results" )
441- }
442-
443- oracleAddr := * abi .ConvertType (results [0 ], new (common.Address )).(* common.Address )
444-
445- callData , err = priceOracleABI .Pack ("currentPrice" )
446- if err != nil {
447- return 0 , err
448- }
449-
450- result , err = c .transactionService .Call (ctx , & transaction.TxRequest {
451- To : & oracleAddr ,
429+ To : & c .priceOracleAddress ,
452430 Data : callData ,
453431 })
454432 if err != nil {
455433 return 0 , fmt .Errorf ("get current price: %w" , err )
456434 }
457435
458- results , err = priceOracleABI .Unpack ("currentPrice" , result )
436+ results , err : = priceOracleABI .Unpack ("currentPrice" , result )
459437 if err != nil {
460438 return 0 , err
461439 }
0 commit comments