forked from forks/go-multicall
add cooldown to chunked calls
This commit is contained in:
parent
0abb968c07
commit
44b77b522a
2 changed files with 9 additions and 3 deletions
|
|
@ -3,6 +3,7 @@ package multicall
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
|
@ -77,9 +78,14 @@ func (caller *Caller) Call(opts *bind.CallOpts, calls ...*Call) ([]*Call, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CallChunked makes multiple multicalls by chunking given calls.
|
// 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
|
var allCalls []*Call
|
||||||
for i, chunk := range chunkInputs(chunkSize, calls) {
|
for i, chunk := range chunkInputs(chunkSize, calls) {
|
||||||
|
if i > 0 && cooldown > 0 {
|
||||||
|
time.Sleep(cooldown)
|
||||||
|
}
|
||||||
|
|
||||||
chunk, err := caller.Call(opts, chunk...)
|
chunk, err := caller.Call(opts, chunk...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return calls, fmt.Errorf("call chunk [%d] failed: %v", i, err)
|
return calls, fmt.Errorf("call chunk [%d] failed: %v", i, err)
|
||||||
|
|
|
||||||
|
|
@ -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)
|
r.NoError(err)
|
||||||
|
|
||||||
call1Out := calls[0].Outputs.(*testType)
|
call1Out := calls[0].Outputs.(*testType)
|
||||||
|
|
@ -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.NoError(err)
|
||||||
r.Len(calls, 1)
|
r.Len(calls, 1)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue