package batching
import (
"context"
"fmt"
"math/big"
"github.com/ethereum-optimism/optimism/op-service/sources/batching/rpcblock"
)
// ReadArray uses batch calls to load all entries from an array.
// countCall is used to retrieve the current array length, then getCall is used to create calls for each element
// which are sent in a batch call.
// The returned *CallResult slice, contains a result for each entry in the array, in the same order as in the contract.
func ReadArray(ctx context.Context, caller *MultiCaller, block rpcblock.Block, countCall *ContractCall, getCall func(i *big.Int) *ContractCall) ([]*CallResult, error) {
result, err := caller.SingleCall(ctx, block, countCall)
if err != nil {
return nil, fmt.Errorf("failed to load array length: %w", err)
}
count := result.GetBigInt(0).Uint64()
calls := make([]Call, count)
for i := uint64(0); i < count; i++ {
calls[i] = getCall(new(big.Int).SetUint64(i))
}
results, err := caller.Call(ctx, block, calls...)
if err != nil {
return nil, fmt.Errorf("failed to fetch array data: %w", err)
}
return results, nil
}
-
Adrian Sutton authored
* multicaller: Support generic calls, not just eth_call * multicaller: Support generic calls, not just eth_call * multicaller: Implement balance_call * multicaller: Split out a generic rpc stub for testing RPC requests other than eth_call Test balance call * multicaller: Move Block type to separate package to avoid dependency cycles for multicaller tests. * fix(op-service): remove old balance call test --------- Co-authored-by:
refcell <abigger87@gmail.com>
70c0778d