Skip to content

Commit

Permalink
EVM-709 Log contract deployment transaction details (#1627)
Browse files Browse the repository at this point in the history
* Log contract deployment transaction details

* Changed log message

* Changed log message

* Log transaction used gas
  • Loading branch information
jelacamarko committed Jun 20, 2023
1 parent d69fff4 commit 573dcb7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
5 changes: 3 additions & 2 deletions command/rootchain/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
// deployContracts deploys and initializes rootchain smart contracts
func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client, chainID int64,
initialValidators []*validator.GenesisValidator, cmdCtx context.Context) (*polybft.RootchainConfig, int64, error) {
txRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithClient(client))
txRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithClient(client), txrelayer.WithWriter(outputter))
if err != nil {
return nil, 0, fmt.Errorf("failed to initialize tx relayer: %w", err)
}
Expand Down Expand Up @@ -573,7 +573,8 @@ func deployContracts(outputter command.OutputFormatter, client *jsonrpc.Client,

results[i] = newDeployContractsResult(contract.name,
types.Address(receipt.ContractAddress),
receipt.TransactionHash)
receipt.TransactionHash,
receipt.GasUsed)

return nil
}
Expand Down
9 changes: 7 additions & 2 deletions command/rootchain/deploy/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ type deployContractResult struct {
Name string `json:"name"`
Address types.Address `json:"address"`
Hash types.Hash `json:"hash"`
GasUsed uint64 `json:"gasUsed"`
}

func newDeployContractsResult(name string, address types.Address, hash ethgo.Hash) *deployContractResult {
func newDeployContractsResult(name string,
address types.Address,
hash ethgo.Hash, gasUsed uint64) *deployContractResult {
return &deployContractResult{
Name: name,
Address: address,
Hash: types.BytesToHash(hash.Bytes()),
GasUsed: gasUsed,
}
}

Expand All @@ -28,10 +32,11 @@ func (r deployContractResult) GetOutput() string {

buffer.WriteString("\n[ROOTCHAIN - DEPLOY CONTRACT]\n")

vals := make([]string, 0, 3)
vals := make([]string, 0, 4)
vals = append(vals, fmt.Sprintf("Name|%s", r.Name))
vals = append(vals, fmt.Sprintf("Contract (address)|%s", r.Address))
vals = append(vals, fmt.Sprintf("Transaction (hash)|%s", r.Hash))
vals = append(vals, fmt.Sprintf("Transaction (gas used)|%d", r.GasUsed))

buffer.WriteString(helper.FormatKV(vals))
buffer.WriteString("\n")
Expand Down
17 changes: 17 additions & 0 deletions txrelayer/txrelayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package txrelayer
import (
"errors"
"fmt"
"io"
"sync"
"time"

Expand Down Expand Up @@ -42,6 +43,8 @@ type TxRelayerImpl struct {
receiptTimeout time.Duration

lock sync.Mutex

writer io.Writer
}

func NewTxRelayer(opts ...TxRelayerOption) (TxRelayer, error) {
Expand Down Expand Up @@ -102,6 +105,8 @@ func (t *TxRelayerImpl) sendTransactionLocked(txn *ethgo.Transaction, key ethgo.

txn.Nonce = nonce

txn.From = key.Address()

if txn.GasPrice == 0 {
gasPrice, err := t.Client().Eth().GasPrice()
if err != nil {
Expand Down Expand Up @@ -130,6 +135,12 @@ func (t *TxRelayerImpl) sendTransactionLocked(txn *ethgo.Transaction, key ethgo.
return ethgo.ZeroHash, err
}

if t.writer != nil {
_, _ = t.writer.Write([]byte(
fmt.Sprintf("[TxRelayer.SendTransaction]\nFrom = %s \nGas = %d \nGas Price = %d\n",
txn.From, txn.Gas, txn.GasPrice)))
}

return t.client.Eth().SendRawTransaction(data)
}

Expand Down Expand Up @@ -200,3 +211,9 @@ func WithReceiptTimeout(receiptTimeout time.Duration) TxRelayerOption {
t.receiptTimeout = receiptTimeout
}
}

func WithWriter(writer io.Writer) TxRelayerOption {
return func(t *TxRelayerImpl) {
t.writer = writer
}
}

0 comments on commit 573dcb7

Please sign in to comment.