mirror of
https://github.com/forta-network/go-multicall.git
synced 2026-02-26 15:47:23 +00:00
chunk calls
This commit is contained in:
parent
953d6c522a
commit
15e8bbc6b0
2 changed files with 23 additions and 3 deletions
21
caller.go
21
caller.go
|
|
@ -75,3 +75,24 @@ func (caller *Caller) Call(opts *bind.CallOpts, calls ...*Call) ([]*Call, error)
|
||||||
|
|
||||||
return calls, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -147,13 +147,12 @@ func TestCaller_TwoCalls(t *testing.T) {
|
||||||
return [][]byte{
|
return [][]byte{
|
||||||
// return inputs as outputs by stripping the method prefix
|
// return inputs as outputs by stripping the method prefix
|
||||||
calls[0].CallData[4:],
|
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)
|
r.NoError(err)
|
||||||
|
|
||||||
call1Out := calls[0].Outputs.(*testType)
|
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.NoError(err)
|
||||||
r.Len(calls, 1)
|
r.Len(calls, 1)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue