You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.2 KiB

  1. package eth
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "math/big"
  7. "os"
  8. "strconv"
  9. "testing"
  10. "github.com/ethereum/go-ethereum/accounts"
  11. "github.com/ethereum/go-ethereum/accounts/keystore"
  12. ethCommon "github.com/ethereum/go-ethereum/common"
  13. "github.com/ethereum/go-ethereum/ethclient"
  14. "github.com/joho/godotenv"
  15. hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
  16. )
  17. var errEnvVar = fmt.Errorf("Some environment variable is missing")
  18. var (
  19. ethClientDialURL = "http://localhost:8545"
  20. password = "pass"
  21. deadline, _ = new(big.Int).SetString("ffffffffffffffffffffffffffffffff", 16)
  22. mnemonic = "explain tackle mirror kit van hammer degree position ginger unfair soup bonus"
  23. )
  24. func genAcc(w *hdwallet.Wallet, ks *keystore.KeyStore, i int) (*accounts.Account, ethCommon.Address) {
  25. path := hdwallet.MustParseDerivationPath(fmt.Sprintf("m/44'/60'/0'/0/%d", i))
  26. account, err := w.Derive(path, false)
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. key, err := w.PrivateKey(account)
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34. _, err = ks.ImportECDSA(key, password)
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. if err := ks.Unlock(account, password); err != nil {
  39. log.Fatal(err)
  40. }
  41. return &account, account.Address
  42. }
  43. // Smart Contract Addresses
  44. var (
  45. genesisBlock int64
  46. auctionAddressConst ethCommon.Address
  47. auctionTestAddressConst ethCommon.Address
  48. tokenHEZAddressConst ethCommon.Address
  49. hermezRollupAddressConst ethCommon.Address
  50. wdelayerAddressConst ethCommon.Address
  51. wdelayerTestAddressConst ethCommon.Address
  52. tokenHEZ TokenConfig
  53. donationAccount *accounts.Account
  54. donationAddressConst ethCommon.Address
  55. bootCoordinatorAccount *accounts.Account
  56. bootCoordinatorAddressConst ethCommon.Address
  57. )
  58. // Ethereum Accounts
  59. var (
  60. emergencyCouncilAccount *accounts.Account
  61. emergencyCouncilAddressConst ethCommon.Address
  62. governanceAccount *accounts.Account
  63. governanceAddressConst ethCommon.Address
  64. auxAccount *accounts.Account
  65. auxAddressConst ethCommon.Address
  66. aux2Account *accounts.Account
  67. aux2AddressConst ethCommon.Address
  68. hermezRollupTestAccount *accounts.Account
  69. hermezRollupTestAddressConst ethCommon.Address
  70. )
  71. var (
  72. ks *keystore.KeyStore
  73. ethClient *ethclient.Client
  74. ethereumClientEmergencyCouncil *EthereumClient
  75. ethereumClientAux *EthereumClient
  76. ethereumClientAux2 *EthereumClient
  77. ethereumClientHermez *EthereumClient
  78. )
  79. func getEnvVariables() {
  80. err := godotenv.Load()
  81. if err != nil {
  82. fmt.Println("Variables loaded from environment")
  83. } else {
  84. fmt.Println("Variables loaded from .env file")
  85. }
  86. var auctionAddressStr = os.Getenv("AUCTION")
  87. var auctionTestAddressStr = os.Getenv("AUCTION_TEST")
  88. var tokenHEZAddressStr = os.Getenv("TOKENHEZ")
  89. var hermezRollupAddressStr = os.Getenv("HERMEZ")
  90. var wdelayerAddressStr = os.Getenv("WDELAYER")
  91. var wdelayerTestAddressStr = os.Getenv("WDELAYER_TEST")
  92. genesisBlockEnv := os.Getenv("GENESIS_BLOCK")
  93. genesisBlock, err = strconv.ParseInt(genesisBlockEnv, 10, 64)
  94. if err != nil {
  95. log.Fatal(errEnvVar)
  96. }
  97. if auctionAddressStr == "" || auctionTestAddressStr == "" || tokenHEZAddressStr == "" || hermezRollupAddressStr == "" || wdelayerAddressStr == "" || wdelayerTestAddressStr == "" || genesisBlockEnv == "" {
  98. log.Fatal(errEnvVar)
  99. }
  100. auctionAddressConst = ethCommon.HexToAddress(auctionAddressStr)
  101. auctionTestAddressConst = ethCommon.HexToAddress(auctionTestAddressStr)
  102. tokenHEZAddressConst = ethCommon.HexToAddress(tokenHEZAddressStr)
  103. hermezRollupAddressConst = ethCommon.HexToAddress(hermezRollupAddressStr)
  104. wdelayerAddressConst = ethCommon.HexToAddress(wdelayerAddressStr)
  105. wdelayerTestAddressConst = ethCommon.HexToAddress(wdelayerTestAddressStr)
  106. tokenHEZ = TokenConfig{
  107. Address: tokenHEZAddressConst,
  108. Name: "Hermez Network Token",
  109. }
  110. }
  111. func TestMain(m *testing.M) {
  112. exitVal := 0
  113. if os.Getenv("INTEGRATION") != "" {
  114. getEnvVariables()
  115. dir, err := ioutil.TempDir("", "tmpks")
  116. if err != nil {
  117. log.Fatal(err)
  118. }
  119. defer func() {
  120. if err := os.RemoveAll(dir); err != nil {
  121. log.Fatal(err)
  122. }
  123. }()
  124. ks = keystore.NewKeyStore(dir, keystore.LightScryptN, keystore.LightScryptP)
  125. w, err := hdwallet.NewFromMnemonic(mnemonic)
  126. if err != nil {
  127. log.Fatal(err)
  128. }
  129. // Create ethereum accounts from mnemonic and load private keys
  130. // into the keystore
  131. bootCoordinatorAccount, bootCoordinatorAddressConst = genAcc(w, ks, 0)
  132. governanceAccount, governanceAddressConst = genAcc(w, ks, 1)
  133. emergencyCouncilAccount, emergencyCouncilAddressConst = genAcc(w, ks, 2)
  134. donationAccount, donationAddressConst = genAcc(w, ks, 3)
  135. hermezRollupTestAccount, hermezRollupTestAddressConst = genAcc(w, ks, 4)
  136. auxAccount, auxAddressConst = genAcc(w, ks, 5)
  137. aux2Account, aux2AddressConst = genAcc(w, ks, 6)
  138. ethClient, err = ethclient.Dial(ethClientDialURL)
  139. if err != nil {
  140. log.Fatal(err)
  141. }
  142. // Controllable Governance Address
  143. ethereumClientGov, err := NewEthereumClient(ethClient, governanceAccount, ks, nil)
  144. if err != nil {
  145. log.Fatal(err)
  146. }
  147. auctionClient, err = NewAuctionClient(ethereumClientGov, auctionAddressConst, tokenHEZ)
  148. if err != nil {
  149. log.Fatal(err)
  150. }
  151. auctionClientTest, err = NewAuctionClient(ethereumClientGov, auctionTestAddressConst, tokenHEZ)
  152. if err != nil {
  153. log.Fatal(err)
  154. }
  155. rollupClient, err = NewRollupClient(ethereumClientGov, hermezRollupAddressConst, tokenHEZ)
  156. if err != nil {
  157. log.Fatal(err)
  158. }
  159. wdelayerClient, err = NewWDelayerClient(ethereumClientGov, wdelayerAddressConst)
  160. if err != nil {
  161. log.Fatal(err)
  162. }
  163. wdelayerClientTest, err = NewWDelayerClient(ethereumClientGov, wdelayerTestAddressConst)
  164. if err != nil {
  165. log.Fatal(err)
  166. }
  167. ethereumClientEmergencyCouncil, err = NewEthereumClient(ethClient, emergencyCouncilAccount, ks, nil)
  168. if err != nil {
  169. log.Fatal(err)
  170. }
  171. ethereumClientAux, err = NewEthereumClient(ethClient, auxAccount, ks, nil)
  172. if err != nil {
  173. log.Fatal(err)
  174. }
  175. ethereumClientAux2, err = NewEthereumClient(ethClient, aux2Account, ks, nil)
  176. if err != nil {
  177. log.Fatal(err)
  178. }
  179. ethereumClientHermez, err = NewEthereumClient(ethClient, hermezRollupTestAccount, ks, nil)
  180. if err != nil {
  181. log.Fatal(err)
  182. }
  183. exitVal = m.Run()
  184. }
  185. os.Exit(exitVal)
  186. }