Skip to content

Commit

Permalink
GetContractCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed Jun 23, 2024
1 parent cf42878 commit 20ba836
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 70 deletions.
7 changes: 3 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"github.com/enviodev/hypersync-client-go/options"
"github.com/enviodev/hypersync-client-go/types"
"github.com/enviodev/hypersync-client-go/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"math/big"
"testing"
)

func TestClients(t *testing.T) {
//toBlock := uint64(10000001)

testCases := []struct {
name string
opts options.Options
Expand All @@ -34,8 +33,8 @@ func TestClients(t *testing.T) {
FromBlock: big.NewInt(10000000),
Transactions: []types.TransactionSelection{
{
ContractAddress: []types.Address{
"0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE",
ContractAddress: []common.Address{
common.HexToAddress("0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE"),
},
},
},
Expand Down
36 changes: 36 additions & 0 deletions handlers_contract_creator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package hypersyncgo

import (
"context"
"github.com/enviodev/hypersync-client-go/types"
"github.com/ethereum/go-ethereum/common"
"math/big"
)

// GetContractCreator fetches the transaction details of the creator of a specified contract address.
//
// This function sends a POST request to the Envio API to retrieve information about the contract creator.
// It takes the context, network ID, and contract address as parameters and returns the transaction details.
//
// Parameters:
// - ctx: The context for managing the request lifecycle.
// - networkId: The ID of the network where the contract is deployed.
// - addr: The contract address for which the creator's transaction details are to be fetched.
//
// Returns:
// - *Transaction: The transaction details of the contract creator, if found.
// - error: An error if the request fails or the contract is not found.
func (c *Client) GetContractCreator(ctx context.Context, addr common.Address) (*types.QueryResponse, error) {
query := types.Query{
FromBlock: big.NewInt(0),
Transactions: []types.TransactionSelection{
{
ContractAddress: []common.Address{addr},
},
},
FieldSelection: types.FieldSelection{
Block: []string{"block", "hash"},
},
}
return c.GetArrow(ctx, &query)
}
54 changes: 54 additions & 0 deletions handlers_contract_creator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package hypersyncgo

import (
"context"
"github.com/enviodev/hypersync-client-go/options"
"github.com/enviodev/hypersync-client-go/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"testing"
)

func TestGetContractCreatorByNumber(t *testing.T) {
testCases := []struct {
name string
opts options.Options
addrs []common.Address
}{{
name: "Test Ethereum Client",
opts: options.Options{
Blockchains: []options.Node{
{
Type: utils.EthereumNetwork,
NetworkId: utils.EthereumNetworkID,
Endpoint: "https://eth.hypersync.xyz",
RpcEndpoint: "https://eth.rpc.hypersync.xyz",
},
},
},
addrs: []common.Address{
common.HexToAddress("0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE"),
},
}}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Fetch the first node out of the blockchain definitions
nodeOpts := testCase.opts.Blockchains[0]
client, err := NewClient(ctx, nodeOpts)
require.NoError(t, err)
require.NotNil(t, client)

for _, q := range testCase.addrs {
resp, rErr := client.GetContractCreator(ctx, q)
require.NoError(t, rErr)
require.NotNil(t, resp)
//require.Equal(t, resp., q)
}

})
}
}
60 changes: 0 additions & 60 deletions handlers_creator.go

This file was deleted.

12 changes: 6 additions & 6 deletions types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
)

type TransactionSelection struct {
From []Address `json:"from,omitempty"`
To []Address `json:"to,omitempty"`
SigHash []SigHash `json:"sighash,omitempty"`
Status *uint8 `json:"status,omitempty"`
Kind []uint8 `json:"type,omitempty"`
ContractAddress []Address `json:"contract_address,omitempty"`
From []common.Address `json:"from,omitempty"`
To []common.Address `json:"to,omitempty"`
SigHash []SigHash `json:"sighash,omitempty"`
Status *uint8 `json:"status,omitempty"`
Kind []uint8 `json:"type,omitempty"`
ContractAddress []common.Address `json:"contract_address,omitempty"`
}

// Transaction represents an Ethereum transaction object.
Expand Down

0 comments on commit 20ba836

Please sign in to comment.