From c59b51e247cc68e71983eb153cba7f86f1fca931 Mon Sep 17 00:00:00 2001 From: spongeboi Date: Thu, 21 Dec 2023 05:42:25 +0530 Subject: [PATCH 1/4] make a public contract query builder method --- ethrpc/ethrpc.go | 43 ++++++------------------------------------- ethrpc/methods.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 37 deletions(-) diff --git a/ethrpc/ethrpc.go b/ethrpc/ethrpc.go index 844506f5..2391304f 100644 --- a/ethrpc/ethrpc.go +++ b/ethrpc/ethrpc.go @@ -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" @@ -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 } diff --git a/ethrpc/methods.go b/ethrpc/methods.go index bb2246aa..82d2601d 100644 --- a/ethrpc/methods.go +++ b/ethrpc/methods.go @@ -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" @@ -282,6 +283,50 @@ 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(raw json.RawMessage, ret *[]string) error { + resp, err := ethcoder.AbiDecodeExprAndStringify(outputAbiExpr, raw) + 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", From 23db38068cf75c15193cf0da006b3c339bca0bfe Mon Sep 17 00:00:00 2001 From: spongeboi Date: Thu, 21 Dec 2023 05:55:34 +0530 Subject: [PATCH 2/4] fix intoFn --- ethrpc/methods.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ethrpc/methods.go b/ethrpc/methods.go index 82d2601d..12ff6cb1 100644 --- a/ethrpc/methods.go +++ b/ethrpc/methods.go @@ -316,8 +316,13 @@ func ContractQuery(contractAddress common.Address, inputAbiExpr, outputAbiExpr s return CallBuilder[[]string]{ method: "eth_call", params: []any{toCallArg(msg), toBlockNumArg(nil)}, - intoFn: func(raw json.RawMessage, ret *[]string) error { - resp, err := ethcoder.AbiDecodeExprAndStringify(outputAbiExpr, raw) + 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) } From 43856840fb1b51426801fd02c6114ddcfafcd0ee Mon Sep 17 00:00:00 2001 From: spongeboi Date: Sat, 6 Jan 2024 21:44:03 +0530 Subject: [PATCH 3/4] change method to contract query builder --- ethrpc/ethrpc.go | 2 +- ethrpc/methods.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ethrpc/ethrpc.go b/ethrpc/ethrpc.go index 2391304f..72eb6d31 100644 --- a/ethrpc/ethrpc.go +++ b/ethrpc/ethrpc.go @@ -434,7 +434,7 @@ 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) - contractQueryBuilder, err := ContractQuery(contract, inputAbiExpr, outputAbiExpr, args) + contractQueryBuilder, err := ContractQueryBuilder(contract, inputAbiExpr, outputAbiExpr, args) if err != nil { return nil, err } diff --git a/ethrpc/methods.go b/ethrpc/methods.go index 12ff6cb1..0ea02b24 100644 --- a/ethrpc/methods.go +++ b/ethrpc/methods.go @@ -283,7 +283,7 @@ func PendingTransactionCount() CallBuilder[uint] { } } -func ContractQuery(contractAddress common.Address, inputAbiExpr, outputAbiExpr string, args interface{}) (CallBuilder[[]string], error) { +func ContractQueryBuilder(contractAddress common.Address, inputAbiExpr, outputAbiExpr string, args interface{}) (CallBuilder[[]string], error) { var ( calldata []byte err error From b46de932e359a5f749607cacd2b74cdbd3b4e70f Mon Sep 17 00:00:00 2001 From: spongeboi Date: Sat, 6 Jan 2024 22:06:42 +0530 Subject: [PATCH 4/4] rename back to ContractQuery --- ethrpc/ethrpc.go | 2 +- ethrpc/methods.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ethrpc/ethrpc.go b/ethrpc/ethrpc.go index 72eb6d31..2391304f 100644 --- a/ethrpc/ethrpc.go +++ b/ethrpc/ethrpc.go @@ -434,7 +434,7 @@ 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) - contractQueryBuilder, err := ContractQueryBuilder(contract, inputAbiExpr, outputAbiExpr, args) + contractQueryBuilder, err := ContractQuery(contract, inputAbiExpr, outputAbiExpr, args) if err != nil { return nil, err } diff --git a/ethrpc/methods.go b/ethrpc/methods.go index 0ea02b24..12ff6cb1 100644 --- a/ethrpc/methods.go +++ b/ethrpc/methods.go @@ -283,7 +283,7 @@ func PendingTransactionCount() CallBuilder[uint] { } } -func ContractQueryBuilder(contractAddress common.Address, inputAbiExpr, outputAbiExpr string, args interface{}) (CallBuilder[[]string], error) { +func ContractQuery(contractAddress common.Address, inputAbiExpr, outputAbiExpr string, args interface{}) (CallBuilder[[]string], error) { var ( calldata []byte err error