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

make a public contract query builder method #110

Merged
merged 4 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
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
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) {
pkieltyka marked this conversation as resolved.
Show resolved Hide resolved
pkieltyka marked this conversation as resolved.
Show resolved Hide resolved
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] {
pkieltyka marked this conversation as resolved.
Show resolved Hide resolved
return CallBuilder[[]byte]{
method: "eth_call",
Expand Down
Loading