Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multicaller change to closure #16

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions utils/multicall/multicaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@ import (
)

type Call struct {
Method string `json:"method"`
Target common.Address `json:"target"`
CallData []byte `json:"call_data"`
Contract *rocketpool.Contract
output interface{}
Target common.Address `json:"target"`
CallData []byte `json:"call_data"`
UnpackFunc func([]byte) error
}

type CallResponse struct {
Method string
Status bool
ReturnDataRaw []byte `json:"returnData"`
}

type Result struct {
Success bool `json:"success"`
Output interface{}
}

func (call Call) GetMultiCall() MultiCall {
Expand All @@ -43,7 +39,7 @@ type MultiCaller struct {
Client rocketpool.ExecutionClient
ABI abi.ABI
ContractAddress common.Address
calls []Call
Calls []Call
}

func NewMultiCaller(client rocketpool.ExecutionClient, multicallerAddress common.Address) (*MultiCaller, error) {
Expand All @@ -56,7 +52,7 @@ func NewMultiCaller(client rocketpool.ExecutionClient, multicallerAddress common
Client: client,
ABI: mcAbi,
ContractAddress: multicallerAddress,
calls: []Call{},
Calls: []Call{},
}, nil
}

Expand All @@ -66,19 +62,19 @@ func (caller *MultiCaller) AddCall(contract *rocketpool.Contract, output interfa
return fmt.Errorf("error adding call [%s]: %w", method, err)
}
call := Call{
Method: method,
Target: *contract.Address,
CallData: callData,
Contract: contract,
output: output,
UnpackFunc: func(rawData []byte) error {
return contract.ABI.UnpackIntoInterface(output, method, rawData)
},
}
caller.calls = append(caller.calls, call)
caller.Calls = append(caller.Calls, call)
return nil
}

func (caller *MultiCaller) Execute(requireSuccess bool, opts *bind.CallOpts) ([]CallResponse, error) {
var multiCalls = make([]MultiCall, 0, len(caller.calls))
for _, call := range caller.calls {
var multiCalls = make([]MultiCall, 0, len(caller.Calls))
for _, call := range caller.Calls {
multiCalls = append(multiCalls, call.GetMultiCall())
}
callData, err := caller.ABI.Pack("tryAggregate", requireSuccess, multiCalls)
Expand All @@ -97,37 +93,35 @@ func (caller *MultiCaller) Execute(requireSuccess bool, opts *bind.CallOpts) ([]
return nil, err
}

results := make([]CallResponse, len(caller.calls))
results := make([]CallResponse, len(caller.Calls))
for i, response := range responses[0].([]struct {
Success bool `json:"success"`
ReturnData []byte `json:"returnData"`
}) {
results[i].Method = caller.calls[i].Method
results[i].ReturnDataRaw = response.ReturnData
results[i].Status = response.Success
}
return results, nil
}

func (caller *MultiCaller) FlexibleCall(requireSuccess bool, opts *bind.CallOpts) ([]Result, error) {
res := make([]Result, len(caller.calls))
res := make([]Result, len(caller.Calls))
results, err := caller.Execute(requireSuccess, opts)
if err != nil {
caller.calls = []Call{}
caller.Calls = []Call{}
return nil, err
}
for i, call := range caller.calls {
for i, call := range caller.Calls {
callSuccess := results[i].Status
if callSuccess {
err := call.Contract.ABI.UnpackIntoInterface(call.output, call.Method, results[i].ReturnDataRaw)
err := call.UnpackFunc(results[i].ReturnDataRaw)
if err != nil {
caller.calls = []Call{}
caller.Calls = []Call{}
return nil, err
}
}
res[i].Success = callSuccess
res[i].Output = call.output
}
caller.calls = []Call{}
caller.Calls = []Call{}
return res, err
}