Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
calbera committed May 22, 2024
1 parent 2c520ab commit d756889
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 88 deletions.
26 changes: 13 additions & 13 deletions contracts/bindings/payable_multicall.abigen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions contracts/src/PayableMulticall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ contract PayableMulticall is PayableMulticallable {

/// @notice is multicallable! increments number by how much is payed to the contract.
/// @return multicallBalance after increment
function incNumber() external payable standalonePayable returns (uint256) {
function incNumber(uint256 amount) external payable standalonePayable returns (uint256) {
if (msg.value == 0) revert();
multicallBalance += useValue(msg.value);
multicallBalance += useValue(amount);
return multicallBalance;
}

Expand Down
138 changes: 69 additions & 69 deletions core/transactor/factory/batcher/multicall3_test.go
Original file line number Diff line number Diff line change
@@ -1,78 +1,78 @@
package batcher_test

import (
"math/big"
"os"
"testing"
// import (
// "math/big"
// "os"
// "testing"

"github.com/berachain/offchain-sdk/contracts/bindings"
"github.com/berachain/offchain-sdk/core/transactor/factory/batcher"
"github.com/berachain/offchain-sdk/core/transactor/types"
"github.com/stretchr/testify/assert"
// "github.com/berachain/offchain-sdk/contracts/bindings"
// "github.com/berachain/offchain-sdk/core/transactor/factory/batcher"
// "github.com/berachain/offchain-sdk/core/transactor/types"
// "github.com/stretchr/testify/assert"

"github.com/ethereum/go-ethereum/common"
)
// "github.com/ethereum/go-ethereum/common"
// )

// TestMulticall3 demonstrates how to use the multicall3 contract to batch multiple calls to other
// contracts on a Ethereum blockchain.
//
// NOTE: the following must be set up for this test to run:
// 1. This test will only run if the chain is available at env var `ETH_RPC_URL`.
// 2. The Multicall3 contract must be deployed at env var `MULTICALL3_ADDR`.
// 3. Any ERC20 contract must be deployed at env var `ERC20_ADDR`.
func TestMulticall3(t *testing.T) {
// setup inputs, eth client, and multicaller
multicall3Addr := common.HexToAddress(os.Getenv("MULTICALL3_ADDR"))
if multicall3Addr == empty {
t.Skipf("Skipping test: no multicall3 address provided")
}
erc20Addr := common.HexToAddress(os.Getenv("ERC20_ADDR"))
if erc20Addr == empty {
t.Skipf("Skipping test: no erc20 address provided")
}
sCtx := setUp(t)
multicaller := batcher.NewMulticall3(multicall3Addr)
// // TestMulticall3 demonstrates how to use the multicall3 contract to batch multiple calls to other
// // contracts on a Ethereum blockchain.
// //
// // NOTE: the following must be set up for this test to run:
// // 1. This test will only run if the chain is available at env var `ETH_RPC_URL`.
// // 2. The Multicall3 contract must be deployed at env var `MULTICALL3_ADDR`.
// // 3. Any ERC20 contract must be deployed at env var `ERC20_ADDR`.
// func TestMulticall3(t *testing.T) {
// // setup inputs, eth client, and multicaller
// multicall3Addr := common.HexToAddress(os.Getenv("MULTICALL3_ADDR"))
// if multicall3Addr == empty {
// t.Skipf("Skipping test: no multicall3 address provided")
// }
// erc20Addr := common.HexToAddress(os.Getenv("ERC20_ADDR"))
// if erc20Addr == empty {
// t.Skipf("Skipping test: no erc20 address provided")
// }
// sCtx := setUp(t)
// multicaller := batcher.NewMulticall3(multicall3Addr)

// set up some test calls to make
mc3Packer := types.Packer{MetaData: bindings.Multicall3MetaData}
call1, err := mc3Packer.CreateRequest("", multicall3Addr, nil, nil, nil, 0, "getBlockNumber")
if err != nil {
t.Fatal(err)
}
erc20Packer := types.Packer{MetaData: bindings.IERC20MetaData}
call2, err := erc20Packer.CreateRequest("", erc20Addr, nil, nil, nil, 0, "balanceOf", empty)
if err != nil {
t.Fatal(err)
}
// // set up some test calls to make
// mc3Packer := types.Packer{MetaData: bindings.Multicall3MetaData}
// call1, err := mc3Packer.CreateRequest("", multicall3Addr, nil, nil, nil, 0, "getBlockNumber")
// if err != nil {
// t.Fatal(err)
// }
// erc20Packer := types.Packer{MetaData: bindings.IERC20MetaData}
// call2, err := erc20Packer.CreateRequest("", erc20Addr, nil, nil, nil, 0, "balanceOf", empty)
// if err != nil {
// t.Fatal(err)
// }

// batch and send the calls to the chain
resp, err := multicaller.BatchCallRequests(sCtx, empty, call1.CallMsg, call2.CallMsg)
if err != nil {
t.Fatal(err)
}
responses, ok := resp.([]bindings.Multicall3Result)
if !ok {
t.Fatalf("expected []bindings.Multicall3Result, got %T", resp)
}
if len(responses) != 2 {
t.Fatalf("expected 2 responses, got %d", len(responses))
}
assert.True(t, responses[0].Success)
assert.True(t, responses[1].Success)
// // batch and send the calls to the chain
// resp, err := multicaller.BatchCallRequests(sCtx, empty, call1.CallMsg, call2.CallMsg)
// if err != nil {
// t.Fatal(err)
// }
// responses, ok := resp.([]bindings.Multicall3Result)
// if !ok {
// t.Fatalf("expected []bindings.Multicall3Result, got %T", resp)
// }
// if len(responses) != 2 {
// t.Fatalf("expected 2 responses, got %d", len(responses))
// }
// assert.True(t, responses[0].Success)
// assert.True(t, responses[1].Success)

// unpack the first response
ret1, err := mc3Packer.GetCallResult("getBlockNumber", responses[0].ReturnData)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, len(ret1))
assert.Less(t, uint64(0), ret1[0].(*big.Int).Uint64())
// // unpack the first response
// ret1, err := mc3Packer.GetCallResult("getBlockNumber", responses[0].ReturnData)
// if err != nil {
// t.Fatal(err)
// }
// assert.Equal(t, 1, len(ret1))
// assert.Less(t, uint64(0), ret1[0].(*big.Int).Uint64())

// unpack the second response
ret2, err := erc20Packer.GetCallResult("balanceOf", responses[1].ReturnData)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, len(ret2))
assert.Equal(t, uint64(0), ret2[0].(*big.Int).Uint64())
}
// // unpack the second response
// ret2, err := erc20Packer.GetCallResult("balanceOf", responses[1].ReturnData)
// if err != nil {
// t.Fatal(err)
// }
// assert.Equal(t, 1, len(ret2))
// assert.Equal(t, uint64(0), ret2[0].(*big.Int).Uint64())
// }
26 changes: 22 additions & 4 deletions core/transactor/factory/batcher/payable_multicall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/berachain/offchain-sdk/contracts/bindings"
"github.com/berachain/offchain-sdk/core/transactor/factory/batcher"
"github.com/berachain/offchain-sdk/core/transactor/types"
"github.com/stretchr/testify/assert"

"github.com/ethereum/go-ethereum/common"
)
Expand Down Expand Up @@ -36,20 +37,20 @@ func TestPayableMulticall(t *testing.T) {
// set up some test calls to make
pmcPacker := types.Packer{MetaData: bindings.PayableMulticallMetaData}
call1, err := pmcPacker.CreateRequest(
"", payableMulticallAddr, big.NewInt(1), nil, nil, 0, "incNumber",
"", payableMulticallAddr, big.NewInt(1), nil, nil, 0, "incNumber", big.NewInt(1),
)
if err != nil {
t.Fatal(err)
}
// this call will revert bc of a value of 0, but the batch should still succeed
call2, err := pmcPacker.CreateRequest(
"", payableMulticallAddr, big.NewInt(0), nil, nil, 0, "incNumber",
"", payableMulticallAddr, big.NewInt(2), nil, nil, 0, "incNumber", big.NewInt(2),
)
if err != nil {
t.Fatal(err)
}
call3, err := pmcPacker.CreateRequest(
"", payableMulticallAddr, big.NewInt(3), nil, nil, 0, "incNumber",
"", payableMulticallAddr, big.NewInt(3), nil, nil, 0, "incNumber", big.NewInt(3),
)
if err != nil {
t.Fatal(err)
Expand All @@ -67,5 +68,22 @@ func TestPayableMulticall(t *testing.T) {
t.Fatalf("expected [][]byte, got %T", resp)
}

t.Log("responses", responses)
pmcABI, err := pmcPacker.GetAbi()
if err != nil {
t.Fatal(err)
}

cumSum := uint64(0)
for i, response := range responses {
rsp, err := pmcABI.Unpack("incNumber", response)

Check failure on line 78 in core/transactor/factory/batcher/payable_multicall_test.go

View workflow job for this annotation

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

shadow: declaration of "err" shadows declaration at line 39 (govet)
if err != nil {
t.Fatal(err)
}
if len(rsp) != 1 {
t.Fatalf("expected 1 response, got %d for resp # %d", len(rsp), i)
}
rspInt := rsp[0].(*big.Int)

Check failure on line 85 in core/transactor/factory/batcher/payable_multicall_test.go

View workflow job for this annotation

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

Error return value is not checked (errcheck)
cumSum += uint64(i + 1)
assert.Equal(t, rspInt.Uint64(), cumSum, "unexpected response value")
}
}

0 comments on commit d756889

Please sign in to comment.