Skip to content

Commit 36cacf7

Browse files
committed
fix: read price oracle address from contract
1 parent b974e20 commit 36cacf7

4 files changed

Lines changed: 70 additions & 33 deletions

File tree

pkg/config/chain.go

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

2323
// Addresses.
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
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
3130

3231
// ABIs.
3332
StakingABI string
@@ -60,13 +59,12 @@ var (
6059
NativeTokenSymbol: "ETH",
6160
SwarmTokenSymbol: "sBZZ",
6261

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),
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),
7068

7169
StakingABI: abi.TestnetStakingABI,
7270
PostageStampABI: abi.TestnetPostageStampABI,
@@ -84,13 +82,12 @@ var (
8482
NativeTokenSymbol: "xDAI",
8583
SwarmTokenSymbol: "xBZZ",
8684

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),
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),
9491

9592
StakingABI: abi.MainnetStakingABI,
9693
PostageStampABI: abi.MainnetPostageStampABI,

pkg/node/node.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,16 @@ 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), chainCfg.IncentivesPriceOracleAddress)
1227+
stakingContractABI := abiutil.MustParseABI(chainCfg.StakingABI)
1228+
var stakingPriceOracleAddress common.Address
1229+
if chainEnabled {
1230+
stakingPriceOracleAddress, err = staking.GetPriceOracleAddress(ctx, stakingContractAddress, stakingContractABI, transactionService)
1231+
if err != nil {
1232+
return nil, fmt.Errorf("get staking price oracle address: %w", err)
1233+
}
1234+
}
1235+
1236+
stakingContract := staking.New(overlayEthAddress, stakingContractAddress, stakingContractABI, bzzTokenAddress, transactionService, common.BytesToHash(nonce), contractGasLimit, uint8(o.ReserveCapacityDoubling), stakingPriceOracleAddress)
12281237

12291238
if chainEnabled {
12301239

pkg/storageincentives/staking/contract.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,36 @@ func New(
105105
}
106106
}
107107

108+
// GetPriceOracleAddress returns the price oracle configured by the staking contract.
109+
func GetPriceOracleAddress(ctx context.Context, stakingAddress common.Address, stakingABI abi.ABI, transactionService transaction.Service) (common.Address, error) {
110+
callData, err := stakingABI.Pack("OracleContract")
111+
if err != nil {
112+
return common.Address{}, err
113+
}
114+
115+
result, err := transactionService.Call(ctx, &transaction.TxRequest{
116+
To: &stakingAddress,
117+
Data: callData,
118+
})
119+
if err != nil {
120+
return common.Address{}, err
121+
}
122+
123+
results, err := stakingABI.Unpack("OracleContract", result)
124+
if err != nil {
125+
return common.Address{}, err
126+
}
127+
if len(results) == 0 {
128+
return common.Address{}, ErrUnexpectedLength
129+
}
130+
131+
oracleAddress, ok := results[0].(common.Address)
132+
if !ok {
133+
return common.Address{}, fmt.Errorf("unexpected oracle address type %T", results[0])
134+
}
135+
return oracleAddress, nil
136+
}
137+
108138
func (c *contract) DepositStake(ctx context.Context, stakedAmount *big.Int) (common.Hash, error) {
109139
minDeposit, err := c.GetMinDeposit(ctx)
110140
if err != nil {
@@ -194,13 +224,11 @@ func (c *contract) GetMinDeposit(ctx context.Context) (*big.Int, error) {
194224

195225
// calculateMinDeposit returns the minimum additional deposit in PLUR that manageStake will accept according to contract.
196226
func calculateMinDeposit(potential, committed *big.Int, price uint32, height uint8) *big.Int {
197-
minAdd := new(big.Int)
227+
minAdd := big.NewInt(1)
198228

199-
// potential stake should be at least MIN_STAKE * 2^height.
200-
// minAdd = max(0, minTotal - potential).
201-
minTotal := new(big.Int).Lsh(new(big.Int).Set(MinimumStakeAmount), uint(height))
202-
if gap := new(big.Int).Sub(minTotal, potential); gap.Sign() > 0 {
203-
minAdd.Set(gap)
229+
// The contract applies the minimum stake floor only when creating a stake.
230+
if potential.Sign() == 0 {
231+
minAdd.Lsh(new(big.Int).Set(MinimumStakeAmount), uint(height))
204232
}
205233

206234
if price != 0 && committed.Sign() > 0 {
@@ -214,11 +242,6 @@ func calculateMinDeposit(potential, committed *big.Int, price uint32, height uin
214242
}
215243
}
216244

217-
// floors and commitment already satisfied.
218-
// DepositStake still needs a positive addAmount; 1 PLUR is enough.
219-
if minAdd.Sign() == 0 {
220-
return big.NewInt(1)
221-
}
222245
return minAdd
223246
}
224247

pkg/storageincentives/staking/contract_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ func TestCalculateMinDeposit(t *testing.T) {
9393
height: 1,
9494
want: new(big.Int).Mul(committedAtMin, big.NewInt(2)),
9595
},
96+
{
97+
name: "existing slashed stake does not restore initial floor",
98+
potential: new(big.Int).Div(minStake, big.NewInt(10)),
99+
committed: committedAtMin,
100+
price: 100,
101+
height: 0,
102+
want: big.NewInt(1),
103+
},
96104
}
97105

98106
for _, tc := range tests {

0 commit comments

Comments
 (0)