Skip to content

Commit

Permalink
better interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
calbera committed Feb 15, 2024
1 parent f5dae2b commit af3712d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
6 changes: 3 additions & 3 deletions core/transactor/factory/multicall.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
tryAggregate = `tryAggregate`
method = `tryAggregate`
executionReverted = `execution reverted: `
)

Expand Down Expand Up @@ -71,7 +71,7 @@ func (mc *Multicall3Batcher) BatchTxRequests(
}

txRequest, _ := mc.packer.CreateTxRequest(
mc.contractAddress, totalValue, gasTipCap, gasFeeCap, gasLimit, tryAggregate, false, calls,
"", mc.contractAddress, totalValue, gasTipCap, gasFeeCap, gasLimit, method, false, calls,
)
return txRequest
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (mc *Multicall3Batcher) BatchCallRequests(
}

// unpack the return data into call responses
callResult, err := mc.packer.GetCallResponse(tryAggregate, ret)
callResult, err := mc.packer.GetCallResponse(method, ret)
if err != nil {
sCtx.Logger().Error("failed to unpack call response", "err", err)
return nil, err
Expand Down
14 changes: 7 additions & 7 deletions core/transactor/factory/multicall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
)

var (
multicallAddress = common.HexToAddress("0x9d1dB8253105b007DDDE65Ce262f701814B91125")
erc20Address = common.HexToAddress("0x7EeCA4205fF31f947EdBd49195a7A88E6A91161B")
from = common.Address{}
ethHTTPURL = "http://localhost:8545" // configure this
multicallAddr = common.HexToAddress("0x9d1dB8253105b007DDDE65Ce262f701814B91125")
erc20Addr = common.HexToAddress("0x7EeCA4205fF31f947EdBd49195a7A88E6A91161B")
from = common.Address{}
ethHTTPURL = "http://localhost:8545" // configure this
)

// TestMulticall demonstrates how to use the multicall contract to batch multiple calls to other
Expand All @@ -43,16 +43,16 @@ func TestMulticall(t *testing.T) {
sCtx := sdk.NewContext(
ctx, &eth.ExtendedEthClient{Client: chain}, log.NewLogger(os.Stdout, "test-runner"), nil,
)
multicaller := factory.NewMulticall3Batcher(multicallAddress)
multicaller := factory.NewMulticall3Batcher(multicallAddr)

// set up some test calls to make
mcPacker := types.Packer{MetaData: bindings.Multicall3MetaData}
call1, err := mcPacker.CreateTxRequest(multicallAddress, nil, nil, nil, 0, "getBlockNumber")
call1, err := mcPacker.CreateTxRequest("", multicallAddr, nil, nil, nil, 0, "getBlockNumber")
if err != nil {
t.Fatal(err)
}
erc20Packer := types.Packer{MetaData: bindings.IERC20MetaData}
call2, err := erc20Packer.CreateTxRequest(erc20Address, nil, nil, nil, 0, "balanceOf", from)
call2, err := erc20Packer.CreateTxRequest("", erc20Addr, nil, nil, nil, 0, "balanceOf", from)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion core/transactor/types/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Packer struct {

// CreateTxRequest function for creating transaction request.
func (p *Packer) CreateTxRequest(
msgID string, // optional, user-provided string id for this tx request
to common.Address, // address to send transaction to
value *big.Int, // value to be sent in the transaction (optional)
gasTipCap *big.Int, // gas tip cap for the transaction (optional)
Expand All @@ -32,7 +33,7 @@ func (p *Packer) CreateTxRequest(
return nil, err
}

return NewTxRequest(to, gasLimit, gasFeeCap, gasTipCap, value, bz), nil
return NewTxRequest(to, gasLimit, gasFeeCap, gasTipCap, value, bz, msgID), nil
}

// GetCallResponse function for unpacking the return data from a call response.
Expand Down
17 changes: 9 additions & 8 deletions core/transactor/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (
"github.com/ethereum/go-ethereum/common"
)

// txReqDivider the string divider used in the unique id of a batched tx request.
const txReqDivider = `---`

// TxResultType represents the type of error that occurred when sending a tx.
// There are two different classes of errors:
// Errors where we fail to send it to the chain:
Expand All @@ -40,7 +37,7 @@ type (
// TxRequest is a transaction request, using the go-ethereum call msg.
TxRequest struct {
*ethereum.CallMsg
ID string // ID is the optional, user-provided string id for this tx request
MsgID string // MsgID is the optional, user-provided string id for this tx request
}

// TxResult represents the error that occurred when sending a tx.
Expand All @@ -53,11 +50,15 @@ type (
}
)

// NewTxRequest returns a new transaction request with the given input data.
// NewTxRequest returns a new transaction request with the given input data. The ID is optional,
// but at most 1 is allowed per tx request.
func NewTxRequest(
to common.Address, gasLimit uint64, gasFeeCap, gasTipCap, value *big.Int, data []byte,
id ...string,
msgID ...string,
) *TxRequest {
if len(msgID) > 1 {
panic("must only pass in 1 id for a new tx request")
}
return &TxRequest{
CallMsg: &ethereum.CallMsg{
To: &to,
Expand All @@ -67,7 +68,7 @@ func NewTxRequest(
Value: value,
Data: data,
},
ID: strings.Join(id, txReqDivider),
MsgID: strings.Join(msgID, ""),
}
}

Expand All @@ -78,7 +79,7 @@ func (TxRequest) New() types.Marshallable {

// String() implements fmt.Stringer

Check failure on line 80 in core/transactor/types/types.go

View workflow job for this annotation

GitHub Actions / ci (lint, ubuntu-latest, 1.21.0)

Comment should end in a period (godot)
func (tx *TxRequest) String() string {
return tx.ID
return tx.MsgID
}

// NewTxRequest returns a new TxRequest with the given type and error.
Expand Down

0 comments on commit af3712d

Please sign in to comment.