Skip to content

Commit 606ffc8

Browse files
committed
fix: clean up
1 parent 4e823dd commit 606ffc8

4 files changed

Lines changed: 87 additions & 67 deletions

File tree

pkg/config/chain.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ type ChainConfig struct {
2121
SwarmTokenSymbol string
2222

2323
// Addresses.
24-
StakingAddress common.Address
25-
PostageStampAddress common.Address
26-
RedistributionAddress common.Address
27-
SwapPriceOracleAddress common.Address // Swap swear and swindle (S3) Contracts
28-
CurrentFactoryAddress common.Address
29-
TokenContractAddress common.Address
24+
StakingAddress common.Address
25+
PostageStampAddress common.Address
26+
RedistributionAddress common.Address
27+
IncentivesPriceOracleAddress common.Address // storage-incentives PriceOracle (postage / stake commitment)
28+
SwapPriceOracleAddress common.Address // Swap swear and swindle (S3) Contracts
29+
CurrentFactoryAddress common.Address
30+
TokenContractAddress common.Address
3031

3132
// ABIs.
3233
StakingABI string
@@ -59,12 +60,13 @@ var (
5960
NativeTokenSymbol: "ETH",
6061
SwarmTokenSymbol: "sBZZ",
6162

62-
StakingAddress: common.HexToAddress(abi.TestnetStakingAddress),
63-
PostageStampAddress: common.HexToAddress(abi.TestnetPostageStampAddress),
64-
RedistributionAddress: common.HexToAddress(abi.TestnetRedistributionAddress),
65-
SwapPriceOracleAddress: common.HexToAddress("0x1814e9b3951Df0CB8e12b2bB99c5594514588936"),
66-
CurrentFactoryAddress: common.HexToAddress("0x0fF044F6bB4F684a5A149B46D7eC03ea659F98A1"),
67-
TokenContractAddress: common.HexToAddress(abi.TestnetBzzTokenAddress),
63+
StakingAddress: common.HexToAddress(abi.TestnetStakingAddress),
64+
PostageStampAddress: common.HexToAddress(abi.TestnetPostageStampAddress),
65+
RedistributionAddress: common.HexToAddress(abi.TestnetRedistributionAddress),
66+
IncentivesPriceOracleAddress: common.HexToAddress(abi.TestnetPriceOracleAddress),
67+
SwapPriceOracleAddress: common.HexToAddress("0x1814e9b3951Df0CB8e12b2bB99c5594514588936"),
68+
CurrentFactoryAddress: common.HexToAddress("0x0fF044F6bB4F684a5A149B46D7eC03ea659F98A1"),
69+
TokenContractAddress: common.HexToAddress(abi.TestnetBzzTokenAddress),
6870

6971
StakingABI: abi.TestnetStakingABI,
7072
PostageStampABI: abi.TestnetPostageStampABI,
@@ -82,12 +84,13 @@ var (
8284
NativeTokenSymbol: "xDAI",
8385
SwarmTokenSymbol: "xBZZ",
8486

85-
StakingAddress: common.HexToAddress(abi.MainnetStakingAddress),
86-
PostageStampAddress: common.HexToAddress(abi.MainnetPostageStampAddress),
87-
RedistributionAddress: common.HexToAddress(abi.MainnetRedistributionAddress),
88-
SwapPriceOracleAddress: common.HexToAddress("0xA57A50a831B31c904A770edBCb706E03afCdbd94"),
89-
CurrentFactoryAddress: common.HexToAddress("0xc2d5a532cf69aa9a1378737d8ccdef884b6e7420"),
90-
TokenContractAddress: common.HexToAddress(abi.MainnetBzzTokenAddress),
87+
StakingAddress: common.HexToAddress(abi.MainnetStakingAddress),
88+
PostageStampAddress: common.HexToAddress(abi.MainnetPostageStampAddress),
89+
RedistributionAddress: common.HexToAddress(abi.MainnetRedistributionAddress),
90+
IncentivesPriceOracleAddress: common.HexToAddress(abi.MainnetPriceOracleAddress),
91+
SwapPriceOracleAddress: common.HexToAddress("0xA57A50a831B31c904A770edBCb706E03afCdbd94"),
92+
CurrentFactoryAddress: common.HexToAddress("0xc2d5a532cf69aa9a1378737d8ccdef884b6e7420"),
93+
TokenContractAddress: common.HexToAddress(abi.MainnetBzzTokenAddress),
9194

9295
StakingABI: abi.MainnetStakingABI,
9396
PostageStampABI: abi.MainnetPostageStampABI,

pkg/node/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ func NewBee(
12241224
stakingContractAddress = common.HexToAddress(o.StakingContractAddress)
12251225
}
12261226

1227-
stakingContract := staking.New(overlayEthAddress, stakingContractAddress, abiutil.MustParseABI(chainCfg.StakingABI), bzzTokenAddress, transactionService, common.BytesToHash(nonce), contractGasLimit, uint8(o.ReserveCapacityDoubling))
1227+
stakingContract := staking.New(overlayEthAddress, stakingContractAddress, abiutil.MustParseABI(chainCfg.StakingABI), bzzTokenAddress, transactionService, common.BytesToHash(nonce), contractGasLimit, uint8(o.ReserveCapacityDoubling), chainCfg.IncentivesPriceOracleAddress)
12281228

12291229
if chainEnabled {
12301230

pkg/storageincentives/staking/contract.go

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
196196
func 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

420422
func (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

Comments
 (0)