-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: console to get latest blk, with timeout test
- Loading branch information
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |