1
0
Fork 0
forked from forks/go-multicall

chunk calls

This commit is contained in:
Caner Çıdam 2023-06-09 19:10:35 +03:00
parent 953d6c522a
commit 15e8bbc6b0
2 changed files with 23 additions and 3 deletions

View file

@ -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
}

View file

@ -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)
}