diff --git a/caller.go b/caller.go index ed2c7ce..4267752 100644 --- a/caller.go +++ b/caller.go @@ -75,3 +75,24 @@ func (caller *Caller) Call(opts *bind.CallOpts, calls ...*Call) ([]*Call, error) return calls, nil } + +// CallChunked makes multiple multicalls by chunking given calls. +func (caller *Caller) CallChunked(opts *bind.CallOpts, chunkSize int, calls ...*Call) ([]*Call, error) { + if chunkSize <= 0 || len(calls) < 2 { + return caller.Call(opts, calls...) + } + callCount := len(calls) / chunkSize + + var allCalls []*Call + for i := 0; i < callCount; i++ { + start := i * chunkSize + end := start + chunkSize + chunk, err := caller.Call(opts, calls[start:end]...) + if err != nil { + return calls, fmt.Errorf("call chunk [%d] failed: %v", i, err) + } + allCalls = append(allCalls, chunk...) + } + + return allCalls, nil +} diff --git a/caller_test.go b/caller_test.go index 162270e..6144763 100644 --- a/caller_test.go +++ b/caller_test.go @@ -147,13 +147,12 @@ func TestCaller_TwoCalls(t *testing.T) { return [][]byte{ // return inputs as outputs by stripping the method prefix calls[0].CallData[4:], - calls[1].CallData[4:], } }, }, } - calls, err := caller.Call(nil, call1, call2) + calls, err := caller.CallChunked(nil, 1, call1, call2) r.NoError(err) call1Out := calls[0].Outputs.(*testType) @@ -207,7 +206,7 @@ func TestCaller_EmptyCall(t *testing.T) { }, } - calls, err := caller.Call(nil, call) + calls, err := caller.CallChunked(nil, 1, call) r.NoError(err) r.Len(calls, 1) }