Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions pkg/bee/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
Loading