Skip to content

Commit

Permalink
Add ExtendedBatchGetLatestValues
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Jan 29, 2025
1 parent ac32780 commit 7cd7574
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions pkg/contractreader/extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ type Extended interface {
confidenceLevel primitives.ConfidenceLevel,
params, returnVal any,
) error
ExtendedBatchGetLatestValues(
ctx context.Context,
contractName string,
methodName string,
params []any,
retValType any,
) (types.ContractBatchResults, error)
}

type ExtendedBoundContract struct {
Expand Down Expand Up @@ -122,6 +129,54 @@ func (e *extendedContractReader) ExtendedGetLatestValue(
)
}

// ExtendedBatchGetLatestValue calls a single method of a contract once for
// each set of parameters passed in params slice. The requests will be processed in
// parallel, and all results returned as types.ContractBatchResults aka []BatchReadResult
// Nothing is returned in retValType, it will determine the type of each
// BatchReadResult.returnValue
func (e *extendedContractReader) ExtendedBatchGetLatestValues(
ctx context.Context,
contractName string,
methodName string,
params []any,
retValType any,
) (types.ContractBatchResults, error) {
binding, err := e.getOneBinding(contractName)
if err != nil {
return nil, fmt.Errorf("ExtendedBatchGetLatestValues: %w", err)
}

readIdentifier := binding.Binding.ReadIdentifier(methodName)

reqs := make(types.ContractBatch, 0, len(params))
for p := range params {
req := types.BatchRead{
ReadName: readIdentifier,
Params: p,
ReturnVal: retValType,
}
reqs = append(reqs, req)
}

batchReq := types.BatchGetLatestValuesRequest{
binding.Binding: reqs,
}

res, err := e.BatchGetLatestValues(
ctx,
batchReq,
)
if err != nil {
return nil, err
}

extRes, ok := res[binding.Binding]
if !ok {
return nil, fmt.Errorf("unexpected result from BatchGetLatestValues: %v", res)
}
return extRes, nil
}

func (e *extendedContractReader) Bind(ctx context.Context, allBindings []types.BoundContract) error {
validBindings := slicelib.Filter(allBindings, func(b types.BoundContract) bool { return !e.bindingExists(b) })
if len(validBindings) == 0 {
Expand Down

0 comments on commit 7cd7574

Please sign in to comment.