Skip to content

Commit

Permalink
feature:more info was added for this polygon version
Browse files Browse the repository at this point in the history
  • Loading branch information
Jae Liu committed Oct 9, 2021
1 parent 99762aa commit 761cd02
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
17 changes: 15 additions & 2 deletions libs/client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package libs

import (
"github.com/ethereum/go-ethereum/ethclient"
"log"

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

var (
client *ethclient.Client
client *ethclient.Client
ethClient *ethclient.Client
)

func GetClient() *ethclient.Client {
Expand All @@ -19,3 +21,14 @@ func GetClient() *ethclient.Client {
}
return client
}

func GetEthClient() *ethclient.Client {
if ethClient == nil {
var err error
ethClient, err = ethclient.Dial(EthRpcUrl)
if err != nil {
log.Fatal(err)
}
}
return ethClient
}
20 changes: 17 additions & 3 deletions libs/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cmd
import (
"bytes"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"log"
"math/big"
"math/rand"
Expand All @@ -13,6 +11,10 @@ import (
"signmap/libs/contracts/matic_data"
"signmap/libs/contracts/matic_staking"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/spf13/cobra"
)

var (
Expand All @@ -27,8 +29,12 @@ var (
Short: "(Default) Run signmap daemon .",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
privateKey := libs.GetKey("")
fromAddress := crypto.PubkeyToAddress(privateKey.PublicKey)
bindAddress := matic_data.BindAddress()

libs.GetKey("")
if bytes.Equal(matic_data.BindAddress().Bytes(), common.Address{}.Bytes()) {
if bytes.Equal(bindAddress.Bytes(), common.Address{}.Bytes()) {
println("Worker not set! please set a worker.")
os.Exit(1)
}
Expand All @@ -42,6 +48,14 @@ var (
println("Lack of balance. The balance is: ", libs.WeiToEther(balance))
os.Exit(1)
}

bindBalance := libs.GetEthBalance(bindAddress)
// print info
log.Println("Pledge Account: ", bindAddress.Hex())
log.Println("Pledge Balance: ", libs.WeiToEther(bindBalance), "ETH")
log.Println("Worker Account: ", fromAddress.Hex())
log.Println("Worker Balance: ", libs.WeiToEther(balance), "MATIC")

rand.Seed(time.Now().UnixNano())
libs.WriteLog(time.Now().Format("-20060102 15:04:05") + ". starting success!")
log.Println("Start-up success")
Expand Down
6 changes: 4 additions & 2 deletions libs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package libs

import (
"encoding/json"
"github.com/ethereum/go-ethereum/common"
"github.com/peterbourgon/diskv"
"math/big"
"os"
filepath2 "path/filepath"

"github.com/ethereum/go-ethereum/common"
"github.com/peterbourgon/diskv"
)

var (
SendTransactionValue = big.NewInt(1000000000000000000)
RpcUrl = GetBlockChainMap()[ReadConfigWithCondition("selected_chain", "1", keyInBlockChainMap)].RpcUrl
EthRpcUrl = "https://mainnet.infura.io/v3/8a12910e30bb4e4ab04cd8d761a0552d"
StakingContractAddress = common.HexToAddress(GetBlockChainMap()[ReadConfigWithCondition("selected_chain", "1", keyInBlockChainMap)].StakingContractAddress)
DataContractAddress = common.HexToAddress(GetBlockChainMap()[ReadConfigWithCondition("selected_chain", "1", keyInBlockChainMap)].DataContractAddress)
ChainId = big.NewInt(137)
Expand Down
15 changes: 13 additions & 2 deletions libs/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"log"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)

func SendTransaction() {
Expand Down Expand Up @@ -69,3 +71,12 @@ func GetBalance() *big.Int {
}
return balance
}

func GetEthBalance(addr common.Address) *big.Int {
client := GetEthClient()
balance, err := client.BalanceAt(context.Background(), addr, nil)
if err != nil {
log.Fatal(err)
}
return balance
}

0 comments on commit 761cd02

Please sign in to comment.