github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/adkgo-GENESIS/gen_tools.go (about)

     1  // Copyright 2021 The adkgo Authors
     2  // This file is part of adkgo.
     3  //
     4  // adkgo is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // adkgo is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with adkgo. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20      "fmt"
    21      "log"
    22      "context"
    23      "math/big"
    24      "crypto/ecdsa"
    25      "github.com/aidoskuneen/adk-node/accounts/abi/bind"
    26      "github.com/aidoskuneen/adk-node/common"
    27      "github.com/aidoskuneen/adk-node/crypto"
    28      "github.com/aidoskuneen/adk-node/ethclient"
    29  )
    30  
    31  var adkTransactionsContract common.Address;
    32  
    33  var adk *ADKTransactions;
    34  var initialized bool; //default is false
    35  
    36  func GetADKInstance(client *ethclient.Client)(*ADKTransactions) {
    37    if (!initialized){
    38      address := common.HexToAddress(*ADKTransactionContract) // main contract
    39      var err error
    40      adk , err = NewADKTransactions(address, client)
    41      if err != nil {
    42           log.Fatal(err)
    43      } else {
    44        initialized = true
    45      }
    46    }
    47    return adk;
    48  }
    49  
    50  func GetAuth(client *ethclient.Client)(*bind.TransactOpts){
    51     publicKey := key.PrivateKey.Public()
    52     publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    53     if !ok {
    54         log.Fatal("error casting public key to ECDSA")
    55     }
    56     fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
    57     nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
    58     if err != nil {
    59         log.Fatal(err)
    60     }
    61  
    62     chainID := big.NewInt(40272) //40272 is mainnet  //40271 is testnet
    63     auth, _ := bind.NewKeyedTransactorWithChainID(key.PrivateKey,chainID)
    64     auth.Nonce = big.NewInt(int64(nonce))
    65     auth.Value = big.NewInt(0)     // in wei
    66     auth.GasLimit = uint64(100000000) // in units
    67     auth.GasPrice = big.NewInt(0)
    68     return auth
    69  }
    70  
    71  func GetAuthForContract(client *ethclient.Client)(*bind.TransactOpts){ //
    72     publicKey := key.PrivateKey.Public()
    73     publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    74     if !ok {
    75         log.Fatal("error casting public key to ECDSA")
    76     }
    77     fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
    78     nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
    79     if err != nil {
    80         log.Fatal(err)
    81     }
    82  
    83     chainID := big.NewInt(40272)  //40272 is mainnet   //40271 is testnet
    84     auth, _ := bind.NewKeyedTransactorWithChainID(key.PrivateKey,chainID)
    85     auth.Nonce = big.NewInt(int64(nonce))
    86     auth.Value = big.NewInt(0)     // in wei
    87     auth.GasLimit = uint64(100000000) // in units
    88     auth.GasPrice = big.NewInt(0)  // genesis account does not need GAS, all others do
    89     return auth
    90  }
    91  
    92  func GetBlockNumber(client *ethclient.Client)(int64) {
    93      fmt.Println("GetBlockNumber") // 5671744
    94      header, err := client.HeaderByNumber(context.Background(), nil)
    95      if err != nil {
    96        log.Fatal(err)
    97        return -1
    98      }
    99      return header.Number.Int64()
   100  }
   101  
   102  func toByte32(s []byte) ([32]byte) {
   103      ret := [32]byte{}
   104      if len(s) >= 32 {
   105          for i := 0; i < 32; i++ {
   106            ret[i] = s[i]
   107          }
   108      }
   109      return ret
   110  }