Skip to content

Commit

Permalink
add: console to get latest blk, with timeout test
Browse files Browse the repository at this point in the history
  • Loading branch information
hthuz committed Jul 31, 2024
1 parent 61a294b commit 628bd10
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
60 changes: 60 additions & 0 deletions go/eth/cmd/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"context"
"eth/config"
"fmt"
"log"
"time"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/spf13/cobra"
)

// blockCmd represents the block command
var blockCmd = &cobra.Command{
Use: "block",
Short: "Get Latest Block",
Run: func(cmd *cobra.Command, args []string) {
client, err := ethclient.Dial(config.PublicNode)
if err != nil {
log.Fatal("dail error", err)
}

ctx, cancel := context.WithCancel(context.Background())
go func() {
var s string
for s != "exit" {
fmt.Print(">")
fmt.Scanln(&s)
if s == "s" {
ctx_timeout, _ := context.WithTimeout(context.Background(), 10*time.Second)
no, err := client.BlockNumber(ctx_timeout)
if err != nil {
log.Println("get block number error", err)
}
fmt.Println("no:", no)
}
}
cancel()
}()
<-ctx.Done()
},
}

func init() {
rootCmd.AddCommand(blockCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// blockCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// blockCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
69 changes: 69 additions & 0 deletions go/eth/cmd/contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"context"
"eth/config"
"eth/contract"
"fmt"
"log"
"math/big"
"os"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/spf13/cobra"
)

// contractCmd represents the contract command
var contractCmd = &cobra.Command{
Use: "contract",
Short: "Deploying and Interact with contract",
Run: func(cmd *cobra.Command, args []string) {
deploy()
},
}

func deploy() {
file, err := os.Open(config.Account1KeyStorePath + "/UTC--2024-07-02T07-08-41.129070472Z--6a62503be31643a98c5e5c7a584238e6fc815793")
if err != nil {
log.Fatal(err)
}
client, _ := ethclient.Dial(config.TestNode)
chainid, _ := client.ChainID(context.Background())

auth, err := bind.NewTransactorWithChainID(file, "123456", chainid)
if err != nil {
log.Fatal(err)
}
fmt.Println(auth)
address, tx, instance, err := contract.DeploySimpleERC20(auth, client, big.NewInt(100000))
if err != nil {
log.Fatal(err)
}
fmt.Println("deployed addr ", address)
fmt.Println("deploying tx ", tx)

balance, err := instance.BalanceOf(&bind.CallOpts{Pending: true}, common.HexToAddress(config.TestAccount1))
if err != nil {
log.Fatal(err)
}
fmt.Println("balanced ", balance)
}

func init() {
rootCmd.AddCommand(contractCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// contractCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// contractCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
30 changes: 30 additions & 0 deletions go/eth/cmd/getlogs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/spf13/cobra"
)

// getlogsCmd represents the getlogs command
var getlogsCmd = &cobra.Command{
Use: "getlogs",
Short: "Call get logs",
Run: func(cmd *cobra.Command, args []string) {
},
}

func init() {
rootCmd.AddCommand(getlogsCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// getlogsCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// getlogsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

0 comments on commit 628bd10

Please sign in to comment.