Skip to content

Commit

Permalink
make a public contract query builder method (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubhaankar-Sharma authored Jan 6, 2024
1 parent 8330d0b commit 04e7f83
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
43 changes: 6 additions & 37 deletions ethrpc/ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net/http"
"sync/atomic"

"github.com/0xsequence/ethkit/ethcoder"
"github.com/0xsequence/ethkit/go-ethereum"
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi/bind"
"github.com/0xsequence/ethkit/go-ethereum/common"
Expand Down Expand Up @@ -435,42 +434,12 @@ func (p *Provider) ContractQuery(ctx context.Context, contractAddress string, in
func (p *Provider) contractQuery(ctx context.Context, contractAddress string, inputAbiExpr, outputAbiExpr string, args interface{}) ([]string, error) {
contract := common.HexToAddress(contractAddress)

var (
calldata []byte
err error
)

switch args := args.(type) {
case []string:
calldata, err = ethcoder.AbiEncodeMethodCalldataFromStringValues(inputAbiExpr, args)
if err != nil {
return nil, fmt.Errorf("abi encode failed: %w", err)
}

case []interface{}:
calldata, err = ethcoder.AbiEncodeMethodCalldata(inputAbiExpr, args)
if err != nil {
return nil, fmt.Errorf("abi encode failed: %w", err)
}
case nil:
calldata, err = ethcoder.AbiEncodeMethodCalldata(inputAbiExpr, nil)
if err != nil {
return nil, fmt.Errorf("abi encode failed: %w", err)
}
}

msg := ethereum.CallMsg{
To: &contract,
Data: calldata,
}

output, err := p.CallContract(ctx, msg, nil)
if err != nil {
return nil, fmt.Errorf("contract call failed: %w", err)
}
resp, err := ethcoder.AbiDecodeExprAndStringify(outputAbiExpr, output)
contractQueryBuilder, err := ContractQuery(contract, inputAbiExpr, outputAbiExpr, args)
if err != nil {
return nil, fmt.Errorf("abi decode of response failed: %w", err)
return nil, err
}
return resp, nil

var result []string
_, err = p.Do(ctx, contractQueryBuilder.Into(&result))
return result, err
}
50 changes: 50 additions & 0 deletions ethrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/big"

"github.com/0xsequence/ethkit/ethcoder"
"github.com/0xsequence/ethkit/ethrpc/jsonrpc"
"github.com/0xsequence/ethkit/go-ethereum"
"github.com/0xsequence/ethkit/go-ethereum/common"
Expand Down Expand Up @@ -282,6 +283,55 @@ func PendingTransactionCount() CallBuilder[uint] {
}
}

func ContractQuery(contractAddress common.Address, inputAbiExpr, outputAbiExpr string, args interface{}) (CallBuilder[[]string], error) {
var (
calldata []byte
err error
)

switch args := args.(type) {
case []string:
calldata, err = ethcoder.AbiEncodeMethodCalldataFromStringValues(inputAbiExpr, args)
if err != nil {
return CallBuilder[[]string]{}, fmt.Errorf("abi encode failed: %w", err)
}

case []interface{}:
calldata, err = ethcoder.AbiEncodeMethodCalldata(inputAbiExpr, args)
if err != nil {
return CallBuilder[[]string]{}, fmt.Errorf("abi encode failed: %w", err)
}
case nil:
calldata, err = ethcoder.AbiEncodeMethodCalldata(inputAbiExpr, nil)
if err != nil {
return CallBuilder[[]string]{}, fmt.Errorf("abi encode failed: %w", err)
}
}

msg := ethereum.CallMsg{
To: &contractAddress,
Data: calldata,
}

return CallBuilder[[]string]{
method: "eth_call",
params: []any{toCallArg(msg), toBlockNumArg(nil)},
intoFn: func(message json.RawMessage, ret *[]string) error {
var result hexutil.Bytes
if err := json.Unmarshal(message, &result); err != nil {
return err
}

resp, err := ethcoder.AbiDecodeExprAndStringify(outputAbiExpr, result)
if err != nil {
return fmt.Errorf("abi decode of response failed: %w", err)
}
*ret = resp
return nil
},
}, nil
}

func CallContract(msg ethereum.CallMsg, blockNum *big.Int) CallBuilder[[]byte] {
return CallBuilder[[]byte]{
method: "eth_call",
Expand Down

0 comments on commit 04e7f83

Please sign in to comment.