diff --git a/pkg/bee/client.go b/pkg/bee/client.go index 711ffeb6..096c68da 100644 --- a/pkg/bee/client.go +++ b/pkg/bee/client.go @@ -516,28 +516,9 @@ func (c *Client) CreatePostageBatch(ctx context.Context, amount int64, depth uin return id, nil } +// GetOrCreateMutableBatch returns a usable mutable batch of the node with the +// given label. The chain is consulted only when a new batch has to be bought. func (c *Client) GetOrCreateMutableBatch(ctx context.Context, postageTTL time.Duration, depth uint64, label string) (string, error) { - csr, err := c.api.Postage.GetChainState(ctx) - if err != nil { - return "", fmt.Errorf("get chain state: %w", err) - } - - blockTime, err := c.swapClient.FetchBlockTime(ctx, swap.WithOffset(1000)) - if err != nil { - return "", fmt.Errorf("fetching block time: %w", err) - } - - amount := int64(1) // use 1 as the minimum amount when the chain price is 0, since any amount is valid - - price := csr.CurrentPrice.Int64() - if price > 0 { - amount = (int64(postageTTL.Seconds()) / blockTime) * price - } else { - c.log.Warningf("invalid chain price: %v", price) - } - - c.log.Debugf("calculated mutable batch: amount '%d', label '%s', price '%d', block time '%d', ttl '%s'", amount, label, price, blockTime, postageTTL) - batches, err := c.PostageBatches(ctx) if err != nil { return "", fmt.Errorf("get postage batches: %w", err) @@ -559,6 +540,27 @@ func (c *Client) GetOrCreateMutableBatch(ctx context.Context, postageTTL time.Du } } + csr, err := c.api.Postage.GetChainState(ctx) + if err != nil { + return "", fmt.Errorf("get chain state: %w", err) + } + + blockTime, err := c.swapClient.FetchBlockTime(ctx, swap.WithOffset(1000)) + if err != nil { + return "", fmt.Errorf("fetching block time: %w", err) + } + + amount := int64(1) // use 1 as the minimum amount when the chain price is 0, since any amount is valid + + price := csr.CurrentPrice.Int64() + if price > 0 { + amount = (int64(postageTTL.Seconds()) / blockTime) * price + } else { + c.log.Warningf("invalid chain price: %v", price) + } + + c.log.Debugf("calculated mutable batch: amount '%d', label '%s', price '%d', block time '%d', ttl '%s'", amount, label, price, blockTime, postageTTL) + return c.CreatePostageBatch(ctx, amount, depth, label, false) }