Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
60 changes: 34 additions & 26 deletions cmd/ethkit/abigen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,37 @@ import (
"strings"

"github.com/0xsequence/ethkit/ethartifact"
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi/bind"
geth_abigen "github.com/0xsequence/ethkit/go-ethereum/accounts/abi/abigen"
"github.com/spf13/cobra"
)

func init() {
abigen := &abigen{}
cmd := &cobra.Command{
Use: "abigen",
Short: "Generate contract Go client code from an abi or truffle artifacts file",
Short: "Generate contract Go client code from an abi or contract artifact file",
Run: abigen.Run,
}

cmd.Flags().String("artifactsFile", "", "path to truffle contract artifacts file")
cmd.Flags().String("abiFile", "", "path to abi json file")
cmd.Flags().String("lang", "", "target language, supported: [go], default=go")
cmd.Flags().String("pkg", "", "pkg (optional)")
cmd.Flags().String("type", "", "type (optional)")
cmd.Flags().String("outFile", "", "outFile (optional), default=stdout")
cmd.Flags().Bool("includeDeployed", false, "include deployed bytecode on the generated file")
cmd.Flags().String("artifactsFile", "", "path to compiled contract artifact file")
cmd.Flags().String("abiFile", "", "path to abi json file (optional)")
cmd.Flags().String("pkg", "", "go package name")
cmd.Flags().String("type", "", "type name used in generated output")
cmd.Flags().String("outFile", "", "outFile (optional, default=stdout)")
cmd.Flags().Bool("includeDeployedBin", false, "include deployed bytecode in the generated file (default=false)")
cmd.Flags().Bool("v2", false, "use go-ethereum abigen v2 (default=false)")

rootCmd.AddCommand(cmd)
}

type abigen struct {
fArtifactsFile string
fAbiFile string
fPkg string
fType string
fOutFile string
fIncludeDeployed bool
fArtifactsFile string
fAbiFile string
fPkg string
fType string
fOutFile string
fIncludeDeployedBin bool
fUseV2 bool
}

func (c *abigen) Run(cmd *cobra.Command, args []string) {
Expand All @@ -45,7 +46,8 @@ func (c *abigen) Run(cmd *cobra.Command, args []string) {
c.fPkg, _ = cmd.Flags().GetString("pkg")
c.fType, _ = cmd.Flags().GetString("type")
c.fOutFile, _ = cmd.Flags().GetString("outFile")
c.fIncludeDeployed, _ = cmd.Flags().GetBool("includeDeployed")
c.fIncludeDeployedBin, _ = cmd.Flags().GetBool("includeDeployedBin")
c.fUseV2, _ = cmd.Flags().GetBool("v2")

if c.fArtifactsFile == "" && c.fAbiFile == "" {
fmt.Println("error: please pass one of --artifactsFile or --abiFile")
Expand Down Expand Up @@ -92,11 +94,9 @@ func (c *abigen) generateGo(artifact ethartifact.RawArtifact) error {
var (
abis []string
bins []string
dbins []string
types []string
sigs []map[string]string
libs = make(map[string]string)
lang = bind.LangGo
)

if strings.Contains(string(artifact.Bytecode), "//") {
Expand All @@ -122,21 +122,29 @@ func (c *abigen) generateGo(artifact ethartifact.RawArtifact) error {
bins = append(bins, artifact.Bytecode)
aliases := map[string]string{}

if c.fIncludeDeployed {
dbins = append(dbins, artifact.DeployedBytecode)
var code string
var err error

if strings.Contains(string(artifact.DeployedBytecode), "//") {
log.Fatal("Contract has additional library references, which is unsupported at this time.")
}
// NOTE: "bytecode" in an artifact contains both the constructor + the runtime code of the contract,
// the "bytecode" value is what we use to deploy a new contract.
//
// Whereas the "deployedBytecode" is the runtime code of the contract, and can be used to verify
// the contract bytecode once its been deployed. For our purposes of generating a client, we only
// need the constructor code, so we use the "bytecode" value.

if c.fUseV2 {
code, err = geth_abigen.BindV2(types, abis, bins, pkgName, libs, aliases)
} else {
dbins = append(dbins, "")
code, err = geth_abigen.Bind(types, abis, bins, sigs, pkgName, libs, aliases)
}

code, err := bind.Bind(types, abis, bins, dbins, sigs, pkgName, lang, libs, aliases)
if err != nil {
return err
}

if c.fIncludeDeployedBin {
code = fmt.Sprintf("%s\n// %sDeployedBin is the resulting bytecode of the created contract\nconst %sDeployedBin = %q\n", code, typeName, typeName, artifact.DeployedBytecode)
}

if c.fOutFile == "" {
fmt.Println(code)
} else {
Expand Down
12 changes: 5 additions & 7 deletions cmd/ethkit/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package main

import (
"bytes"
"fmt"
"strings"
"testing"

"github.com/0xsequence/ethkit/go-ethereum/common/math"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -36,11 +34,11 @@ func Test_BlockCmd_InvalidRpcUrl(t *testing.T) {
}

// Note: this test will eventually fail
func Test_BlockCmd_NotFound(t *testing.T) {
res, err := execBlockCmd(fmt.Sprint(math.MaxInt64) + " --rpc-url https://nodes.sequence.app/mainnet")
assert.Contains(t, err.Error(), "not found")
assert.Empty(t, res)
}
// func Test_BlockCmd_NotFound(t *testing.T) {
// res, err := execBlockCmd(fmt.Sprint(math.MaxInt64) + " --rpc-url https://nodes.sequence.app/mainnet")
// assert.Contains(t, err.Error(), "not found")
// assert.Empty(t, res)
// }

func Test_BlockCmd_InvalidBlockHeight(t *testing.T) {
res, err := execBlockCmd("invalid --rpc-url https://nodes.sequence.app/mainnet")
Expand Down
18 changes: 12 additions & 6 deletions ethreceipts/ethreceipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type ReceiptsListener struct {
alert util.Alerter
provider ethrpc.Interface
monitor *ethmonitor.Monitor
chainID *big.Int
br *breaker.Breaker

// fetchSem is used to limit amount of concurrenct fetch requests
Expand Down Expand Up @@ -156,12 +157,14 @@ func (l *ReceiptsListener) lazyInit(ctx context.Context) error {
l.mu.Lock()
defer l.mu.Unlock()

var err error
l.chainID, err = getChainID(ctx, l.provider)
if err != nil {
l.chainID = big.NewInt(1) // assume mainnet in case of unlikely error
}

if l.options.NumBlocksToFinality <= 0 {
chainID, err := getChainID(ctx, l.provider)
if err != nil {
chainID = big.NewInt(1) // assume mainnet in case of unlikely error
}
network, ok := ethrpc.Networks[chainID.Uint64()]
network, ok := ethrpc.Networks[l.chainID.Uint64()]
if ok {
l.options.NumBlocksToFinality = network.NumBlocksToFinality
} else {
Expand Down Expand Up @@ -748,7 +751,10 @@ func (l *ReceiptsListener) processBlocks(blocks ethmonitor.Blocks, subscribers [
logs: txnLog,
transaction: txn,
}
txnMsg, err := ethtxn.AsMessage(txn)

// TODOXXX: avoid using AsMessage as its fairly expensive operation, especially
// to do it for every txn for every filter.
txnMsg, err := ethtxn.AsMessage(txn, l.chainID)
if err != nil {
// NOTE: this should never happen, but lets log in case it does. In the
// future, we should just not use go-ethereum for these types.
Expand Down
2 changes: 1 addition & 1 deletion ethreceipts/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Receipt struct {
Reorged bool // chain reorged / removed the txn

transaction *types.Transaction
message *core.Message // TODO: this intermediate type is lame.. with new ethrpc we can remove
message *core.Message // TODOXXX: this intermediate type is lame.. with new ethrpc we can remove
receipt *types.Receipt
logs []*types.Log
}
Expand Down
5 changes: 5 additions & 0 deletions ethtest/testchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ func (c *Testchain) Deploy(t *testing.T, contractName string, contractConstructo
wallet := c.GetDeployWallet()
signedTxn, err := wallet.NewTransaction(context.Background(), &ethtxn.TransactionRequest{
Data: data,

// NOTE: below are needed for geth. hardhat and anvil don't need them.
// Recommendation is to use anvil for testing, or hardhat.
GasLimit: 1_000_000,
GasPrice: big.NewInt(1e9), // 1 gwei
})
if err != nil {
t.Fatal(err)
Expand Down
11 changes: 6 additions & 5 deletions ethtest/testchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
"scripts": {
"start:hardhat": "hardhat node --hostname 0.0.0.0",
"start:hardhat:verbose": "hardhat --verbose node --hostname 0.0.0.0",
"start:geth": "docker run -p 8545:8545 --log-driver none --rm ethereum/client-go:v1.15.2 --dev --dev.period 2 --networkid ${npm_package_config_testchainChainID} --miner.gaslimit ${npm_package_config_testchainGasLimit} --miner.gasprice 1 --http --http.addr 0.0.0.0 --rpc.allow-unprotected-txs --verbosity 1",
"start:geth:verbose": "docker run -p 8545:8545 --rm ethereum/client-go:v1.15.2 --dev --dev.period 2 --networkid ${npm_package_config_testchainChainID} --miner.gaslimit ${npm_package_config_testchainGasLimit} --miner.gasprice 1 --http --http.addr 0.0.0.0 --rpc.allow-unprotected-txs",
"start:geth": "docker run -p 8545:8545 --rm ethereum/client-go:v1.15.11 --networkid ${npm_package_config_testchainChainID} --dev --dev.period 1 --dev.gaslimit ${npm_package_config_testchainGasLimit} --miner.gaslimit ${npm_package_config_testchainGasLimit} --miner.gasprice 1 --http --http.addr 0.0.0.0 --rpc.allow-unprotected-txs --verbosity 1",
"start:geth:verbose": "docker run -p 8545:8545 --rm ethereum/client-go:v1.15.11 --networkid ${npm_package_config_testchainChainID} --dev --dev.period 1 --dev.gaslimit ${npm_package_config_testchainGasLimit} --miner.gaslimit ${npm_package_config_testchainGasLimit} --miner.gasprice 1 --http --http.addr 0.0.0.0 --rpc.allow-unprotected-txs",
"start:anvil": "anvil --mnemonic \"${npm_package_config_mnemonic}\" --block-time 1 --balance ${npm_package_config_etherBalance} --host 0.0.0.0 --chain-id ${npm_package_config_testchainChainID} --gas-limit ${npm_package_config_testchainGasLimit} --gas-price ${npm_package_config_testchainGasPrice}",
"start:anvil:verbose": "anvil --mnemonic \"${npm_package_config_mnemonic}\" --block-time 1 --balance ${npm_package_config_etherBalance} --host 0.0.0.0 --chain-id ${npm_package_config_testchainChainID} --gas-limit ${npm_package_config_testchainGasLimit} --gas-price ${npm_package_config_testchainGasPrice} --verbose",
"start:anvil:verbose": "anvil --mnemonic \"${npm_package_config_mnemonic}\" --block-time 1 --balance ${npm_package_config_etherBalance} --host 0.0.0.0 --chain-id ${npm_package_config_testchainChainID} --gas-limit ${npm_package_config_testchainGasLimit} --gas-price ${npm_package_config_testchainGasPrice} -vvv",
"install:anvil": "curl -L https://foundry.paradigm.xyz | bash; foundryup",
"wait:server": "wait-on -t 120000 tcp:127.0.0.1:8545"
},
"devDependencies": {
"concurrently": "^9.1.2",
"hardhat": "^2.22.0",
"wait-on": "^8.0.2"
"hardhat": "^2.24.1",
"wait-on": "^8.0.3"
},
"config": {
"mnemonic": "major danger this key only test please avoid main net use okay",
Expand Down
Loading
Loading