github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/adkgo-GENESIS/main-genesis.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      "os"
    22      "encoding/json"
    23  	"time"
    24      "log"
    25  //    "strings"
    26      "io/ioutil"
    27      "math/big"
    28  //    "regexp"
    29      "flag"
    30      //"math"
    31      "github.com/aidoskuneen/adk-node/ethclient"
    32      "github.com/aidoskuneen/adk-node/accounts/keystore"
    33      "strconv"
    34  )
    35  
    36  type RichList1 struct {
    37      Addresses []string `json:"addresses"`
    38      Balances []string `json:"balances"`
    39      Milestone  string `json:"milestone"`
    40      MilestoneIndex   int64  `json:"milestoneIndex"`
    41      Duration   int64  `json:"duration"`
    42  }
    43  
    44  // IntPow calculates n to the mth power. Since the result is an int, it is assumed that m is a positive power
    45  func IntPow(n, m int64) int64 {
    46      if m == 0 {
    47          return 1
    48      }
    49      result := n
    50      var i int64
    51      for i = 2; i <= m; i++ {
    52          result *= n
    53      }
    54      return result
    55  }
    56  
    57  const defaultMilestone = "999999999999999999999999999999999999999999999999999999999999999999999999999999999"
    58  
    59  var nodeClient *ethclient.Client
    60  var nodeLink *string //"http://localhost:8545"
    61  var apiServe *string //":14266"
    62  var ADKTransactionContract *string; // 0x****
    63  var key *keystore.Key;
    64  
    65  func main() {
    66  
    67    //initialize
    68  
    69    v_pwd := flag.String("password", "12345678", "specify the password for the key file genesis_account.json")
    70    v_genesis_filename := flag.String("genesis-file", "genesis_richlist.json", "specify the snapshot json to load ('richlist')")
    71    v_genesis_account := flag.String("genesis-account", "genesis_account.json", "specify the account")
    72    nodeLink = flag.String("adk-node", "http://localhost:8545", "specify the connection to the adk-node backend")
    73    ADKTransactionContract = flag.String("mesh-contract", "0x533e5eE8429FCFdBe907408F38Ef91a77573CfD1", "specify the main mesh contract")
    74  
    75    flag.Parse()
    76  
    77   // LOAD GENESIS SNAPSHOT TO USE
    78  
    79    var richList RichList1
    80  
    81    jsonFile, err := os.Open(*v_genesis_filename)
    82    if err != nil {
    83        log.Fatalf("Error opening file: %s", err)
    84    }
    85    fmt.Println("Successfully Opened ", v_genesis_filename)
    86    byteValue, _ := ioutil.ReadAll(jsonFile)
    87  
    88    fmt.Println("Read ", len(byteValue), " bytes")
    89    stringValue := string(byteValue)
    90    // surrounding all numbers with "" as we have to avoid float loading/rounding issues, and handle "e" numbers
    91  
    92     if err := json.Unmarshal([]byte(stringValue), &richList); err != nil {
    93          panic(err)
    94      }
    95  
    96     defer jsonFile.Close()
    97  
    98     fmt.Println("Checking totals...")
    99     //
   100     addressToValueMap := make(map[string]int64)
   101  
   102     for i, addr := range richList.Addresses {
   103          val, err := strconv.ParseInt(richList.Balances[i], 10, 64)
   104          if err == nil {
   105              addressToValueMap[addr] += val
   106          } else {
   107            log.Panic("Error parsing balance: ",addr ,richList.Balances[i])
   108          }
   109     }
   110     // check totals -  must contain entire ADK balance
   111     var total int64
   112     total = 0
   113  
   114     var addresses_as_array []string
   115  
   116     a_idx := 0
   117     for _ad , _val := range addressToValueMap {
   118       addresses_as_array = append(addresses_as_array, _ad)
   119       total+= _val
   120  	 a_idx++
   121     }
   122  
   123     if (total != 887167045722474){
   124         fmt.Println(total)
   125         log.Fatalf("Total is not 887167045722474 (8871670.45722474 ADK). Something's wrong: %v",total)
   126     }
   127     fmt.Println("Total OK: " + fmt.Sprintf("%v", total))
   128  
   129     //return
   130     // Now prepare balances, deploy genesis contracts
   131  
   132     jsonFileKEY, err2 := os.Open(*v_genesis_account)
   133     if err2 != nil {
   134         log.Fatalf("Error opening file: %s", err2)
   135     }
   136     fmt.Println("Successfully Opened genesis_account.json")
   137     byteValueKEY, _ := ioutil.ReadAll(jsonFileKEY)
   138  
   139     key, err2 = keystore.DecryptKey(byteValueKEY, *v_pwd)
   140     if err2 != nil {
   141         log.Fatalf("Error DecryptKey: %s", err2)
   142     }
   143  
   144     fmt.Println("Connecting to ADKgo node db " + *nodeLink)
   145     client, err := ethclient.Dial(*nodeLink)
   146     fmt.Println("Block: " + fmt.Sprintf("%v", GetBlockNumber(client)))
   147  
   148     fmt.Println("Deploying ADK Genesis Contracts ")
   149  
   150     authContract := GetAuthForContract(client);
   151  
   152     vADKTokenAddress, _, c_ADKToken, errC := DeployADKToken(authContract,  client)
   153  
   154      if errC != nil {
   155        log.Fatalf("Error DeployADKToken: %s", errC)
   156      }
   157        vAGSClaimContract, _ := c_ADKToken.AGSClaimContract(nil)
   158  	  vADKTransactionsAddress, _ := c_ADKToken.ADKTransactionsContract(nil)
   159  
   160      for vADKTransactionsAddress.Hex() == "0x0000000000000000000000000000000000000000" || vAGSClaimContract.Hex() == "0x0000000000000000000000000000000000000000" {
   161  	   fmt.Println("waiting for contract to be mined...")
   162         time.Sleep(5 * time.Second)
   163         c_ADKToken , _ = NewADKToken(vADKTokenAddress, client)
   164  	   vAGSClaimContract, _ = c_ADKToken.AGSClaimContract(nil)
   165  	   vADKTransactionsAddress, _ = c_ADKToken.ADKTransactionsContract(nil)
   166  	}
   167       fmt.Println("Deployed ADKToken Contract as "+vADKTokenAddress.Hex())
   168       fmt.Println("Deployed ADKCLAIM Contract as "+vAGSClaimContract.Hex())
   169       fmt.Println("Deployed ADKTransactions Contract as "+vADKTransactionsAddress.Hex())
   170  
   171       fmt.Println("setting genesis balances from mesh snapshot...")
   172  
   173       cADKTransactions , err := NewADKTransactions(vADKTransactionsAddress, client)
   174  	 
   175  	 cAGSContract , err := NewAGSClaim(vAGSClaimContract, client)
   176  	 
   177       t_opt := GetAuth(client)
   178       cntAddrs := len(addressToValueMap)
   179  
   180  	 idx := 0
   181  
   182  	 checkTotal := big.NewInt(0)
   183  	 big_10000000000 := big.NewInt(10)
   184  	 big_10000000000.Exp(big_10000000000,big.NewInt(10),nil)
   185  	
   186  	 for idx < cntAddrs - cntAddrs % 10 { // bulk
   187  		addresses_bulk := ""
   188  		var _vals [10]*big.Int
   189  		var _vals_claim [10]*big.Int
   190  	
   191  		for idx10 := 0; idx10 < 10; idx10++ {
   192  			addresses_bulk += addresses_as_array[idx]
   193  			_vals[idx10] = big.NewInt(addressToValueMap[addresses_as_array[idx]])
   194  			_vals_claim[idx10] = new(big.Int).Mul(_vals[idx10],big_10000000000)  // add 10 zeros to convert to correct AGS
   195  			fmt.Printf("Bulk Setting (%v/%v): %s %v\n", idx, cntAddrs, addresses_as_array[idx], _vals[idx10])
   196  			checkTotal.Add(checkTotal, _vals[idx10])
   197  			idx++
   198          }
   199  		_ , errBal := cADKTransactions.ADMLoadADKBalancesBulk(t_opt, addresses_bulk, _vals[0], _vals[1], _vals[2], _vals[3], _vals[4], _vals[5], _vals[6], _vals[7], _vals[8], _vals[9] )
   200  		if errBal != nil {
   201  			log.Fatalf("Error Setting Balance: %s", errBal)
   202  		}
   203  		t_opt.Nonce.Add(t_opt.Nonce,big.NewInt(1))
   204  
   205  		// AGS
   206  		_ , errBal2 := cAGSContract.ADMSetClaimableAmountBulk(t_opt, addresses_bulk, _vals_claim[0], _vals_claim[1], _vals_claim[2], _vals_claim[3], _vals_claim[4], _vals_claim[5], _vals_claim[6], _vals_claim[7], _vals_claim[8], _vals_claim[9] )
   207  		if errBal2 != nil {
   208  			log.Fatalf("Error Setting AGS Claim Balance: %s", errBal2)
   209  		}
   210  		t_opt.Nonce.Add(t_opt.Nonce,big.NewInt(1))
   211  	 }
   212  
   213  	 for idx < cntAddrs  { // remaining non-bulk
   214  	    _bigIntVal := big.NewInt(addressToValueMap[addresses_as_array[idx]])
   215  		_bigIntValClaim := new(big.Int).Mul( _bigIntVal, big_10000000000)  // add 10 zeros to convert to correct AGS
   216  			
   217  		fmt.Printf("Setting (%v/%v): %s %v\n", idx, cntAddrs, addresses_as_array[idx], _bigIntVal)
   218  		checkTotal.Add(checkTotal, _bigIntVal)
   219  		_ , errBal := cADKTransactions.ADMLoadADKBalances(t_opt, addresses_as_array[idx], _bigIntVal)
   220  		if errBal != nil {
   221  			log.Fatalf("Error Setting Balance: %s", errBal)
   222  		}
   223  		t_opt.Nonce.Add(t_opt.Nonce,big.NewInt(1))
   224  		
   225  		// AGS 
   226  		fmt.Printf("Setting AGS: %v\n", _bigIntValClaim)
   227  		_ , errBal2 := cAGSContract.ADMSetClaimableAmount(t_opt, addresses_as_array[idx], _bigIntValClaim)
   228  		if errBal2 != nil {
   229  			log.Fatalf("Error Setting AGS Balance: %s", errBal2)
   230  		}
   231  		t_opt.Nonce.Add(t_opt.Nonce,big.NewInt(1))
   232  		
   233  		idx++
   234  		
   235  	 }
   236  
   237  	 fmt.Print ("Balances loaded:",checkTotal)
   238  
   239       fmt.Print("completed")
   240  }