From 15e8bbc6b05d720937cbf3a0c464ea2e1fe21a61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Caner=20=C3=87=C4=B1dam?= Date: Fri, 9 Jun 2023 19:10:35 +0300 Subject: [PATCH] chunk calls --- caller.go | 21 +++++++++++++++++++++ caller_test.go | 5 ++--- 2 files changed, 23 insertions(+), 3 deletions(-) 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) }