Skip to content

Commit

Permalink
add cooldown to chunked calls
Browse files Browse the repository at this point in the history
  • Loading branch information
canercidam committed Jul 1, 2023
1 parent 0abb968 commit 44b77b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package multicall
import (
"context"
"fmt"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -77,9 +78,14 @@ func (caller *Caller) Call(opts *bind.CallOpts, calls ...*Call) ([]*Call, error)
}

// CallChunked makes multiple multicalls by chunking given calls.
func (caller *Caller) CallChunked(opts *bind.CallOpts, chunkSize int, calls ...*Call) ([]*Call, error) {
// Cooldown is helpful for sleeping between chunks and avoiding rate limits.
func (caller *Caller) CallChunked(opts *bind.CallOpts, chunkSize int, cooldown time.Duration, calls ...*Call) ([]*Call, error) {
var allCalls []*Call
for i, chunk := range chunkInputs(chunkSize, calls) {
if i > 0 && cooldown > 0 {
time.Sleep(cooldown)
}

chunk, err := caller.Call(opts, chunk...)
if err != nil {
return calls, fmt.Errorf("call chunk [%d] failed: %v", i, err)
Expand Down
4 changes: 2 additions & 2 deletions caller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestCaller_TwoCalls(t *testing.T) {
},
}

calls, err := caller.CallChunked(nil, 1, call1, call2)
calls, err := caller.CallChunked(nil, 1, 0, call1, call2)
r.NoError(err)

call1Out := calls[0].Outputs.(*testType)
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestCaller_EmptyCall(t *testing.T) {
},
}

calls, err := caller.CallChunked(nil, 1, call)
calls, err := caller.CallChunked(nil, 1, 0, call)
r.NoError(err)
r.Len(calls, 1)
}
Expand Down

0 comments on commit 44b77b5

Please sign in to comment.