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.

213 lines
6.6 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. safetyAccount *accounts.Account
  58. safetyAddressConst ethCommon.Address
  59. )
  60. // Ethereum Accounts
  61. var (
  62. hermezGovernanceDAOAccount *accounts.Account
  63. hermezGovernanceDAOAddressConst ethCommon.Address
  64. whiteHackGroupAccount *accounts.Account
  65. whiteHackGroupAddressConst ethCommon.Address
  66. hermezKeeperAccount *accounts.Account
  67. hermezKeeperAddressConst ethCommon.Address
  68. governanceAccount *accounts.Account
  69. governanceAddressConst ethCommon.Address
  70. auxAccount *accounts.Account
  71. auxAddressConst ethCommon.Address
  72. aux2Account *accounts.Account
  73. aux2AddressConst ethCommon.Address
  74. hermezRollupTestAccount *accounts.Account
  75. hermezRollupTestAddressConst ethCommon.Address
  76. )
  77. var (
  78. ks *keystore.KeyStore
  79. ethClient *ethclient.Client
  80. ethereumClientWhite *EthereumClient
  81. ethereumClientKep *EthereumClient
  82. ethereumClientGovDAO *EthereumClient
  83. ethereumClientAux *EthereumClient
  84. ethereumClientAux2 *EthereumClient
  85. ethereumClientHermez *EthereumClient
  86. )
  87. func getEnvVariables() {
  88. err := godotenv.Load()
  89. if err != nil {
  90. fmt.Println("Variables loaded from environment")
  91. } else {
  92. fmt.Println("Variables loaded from .env file")
  93. }
  94. var auctionAddressStr = os.Getenv("AUCTION")
  95. var auctionTestAddressStr = os.Getenv("AUCTION_TEST")
  96. var tokenHEZAddressStr = os.Getenv("TOKENHEZ")
  97. var hermezRollupAddressStr = os.Getenv("HERMEZ")
  98. var wdelayerAddressStr = os.Getenv("WDELAYER")
  99. var wdelayerTestAddressStr = os.Getenv("WDELAYER_TEST")
  100. genesisBlockEnv := os.Getenv("GENESIS_BLOCK")
  101. genesisBlock, err = strconv.ParseInt(genesisBlockEnv, 10, 64)
  102. if err != nil {
  103. log.Fatal(errEnvVar)
  104. }
  105. if auctionAddressStr == "" || auctionTestAddressStr == "" || tokenHEZAddressStr == "" || hermezRollupAddressStr == "" || wdelayerAddressStr == "" || wdelayerTestAddressStr == "" || genesisBlockEnv == "" {
  106. log.Fatal(errEnvVar)
  107. }
  108. auctionAddressConst = ethCommon.HexToAddress(auctionAddressStr)
  109. auctionTestAddressConst = ethCommon.HexToAddress(auctionTestAddressStr)
  110. tokenHEZAddressConst = ethCommon.HexToAddress(tokenHEZAddressStr)
  111. hermezRollupAddressConst = ethCommon.HexToAddress(hermezRollupAddressStr)
  112. wdelayerAddressConst = ethCommon.HexToAddress(wdelayerAddressStr)
  113. wdelayerTestAddressConst = ethCommon.HexToAddress(wdelayerTestAddressStr)
  114. tokenHEZ = TokenConfig{
  115. Address: tokenHEZAddressConst,
  116. Name: "Hermez Network Token",
  117. }
  118. }
  119. func TestMain(m *testing.M) {
  120. exitVal := 0
  121. if os.Getenv("INTEGRATION") != "" {
  122. getEnvVariables()
  123. dir, err := ioutil.TempDir("", "tmpks")
  124. if err != nil {
  125. log.Fatal(err)
  126. }
  127. defer func() {
  128. if err := os.RemoveAll(dir); err != nil {
  129. log.Fatal(err)
  130. }
  131. }()
  132. ks = keystore.NewKeyStore(dir, keystore.LightScryptN, keystore.LightScryptP)
  133. w, err := hdwallet.NewFromMnemonic(mnemonic)
  134. if err != nil {
  135. log.Fatal(err)
  136. }
  137. // Create ethereum accounts from mnemonic and load private keys
  138. // into the keystore
  139. bootCoordinatorAccount, bootCoordinatorAddressConst = genAcc(w, ks, 0)
  140. governanceAccount, governanceAddressConst = genAcc(w, ks, 1)
  141. safetyAccount, safetyAddressConst = genAcc(w, ks, 2)
  142. hermezKeeperAccount, hermezKeeperAddressConst = genAcc(w, ks, 6)
  143. hermezGovernanceDAOAccount, hermezGovernanceDAOAddressConst = genAcc(w, ks, 7)
  144. whiteHackGroupAccount, whiteHackGroupAddressConst = genAcc(w, ks, 8)
  145. donationAccount, donationAddressConst = genAcc(w, ks, 9)
  146. aux2Account, aux2AddressConst = genAcc(w, ks, 11)
  147. hermezRollupTestAccount, hermezRollupTestAddressConst = genAcc(w, ks, 12)
  148. auxAccount, auxAddressConst = genAcc(w, ks, 13)
  149. ethClient, err = ethclient.Dial(ethClientDialURL)
  150. if err != nil {
  151. log.Fatal(err)
  152. }
  153. // Controllable Governance Address
  154. ethereumClientGov := NewEthereumClient(ethClient, governanceAccount, ks, nil)
  155. auctionClient, err = NewAuctionClient(ethereumClientGov, auctionAddressConst, tokenHEZ)
  156. if err != nil {
  157. log.Fatal(err)
  158. }
  159. auctionClientTest, err = NewAuctionClient(ethereumClientGov, auctionTestAddressConst, tokenHEZ)
  160. if err != nil {
  161. log.Fatal(err)
  162. }
  163. rollupClient, err = NewRollupClient(ethereumClientGov, hermezRollupAddressConst, tokenHEZ)
  164. if err != nil {
  165. log.Fatal(err)
  166. }
  167. wdelayerClient, err = NewWDelayerClient(ethereumClientGov, wdelayerAddressConst)
  168. if err != nil {
  169. log.Fatal(err)
  170. }
  171. wdelayerClientTest, err = NewWDelayerClient(ethereumClientGov, wdelayerTestAddressConst)
  172. if err != nil {
  173. log.Fatal(err)
  174. }
  175. ethereumClientKep = NewEthereumClient(ethClient, hermezKeeperAccount, ks, nil)
  176. ethereumClientWhite = NewEthereumClient(ethClient, whiteHackGroupAccount, ks, nil)
  177. ethereumClientGovDAO = NewEthereumClient(ethClient, hermezGovernanceDAOAccount, ks, nil)
  178. ethereumClientAux = NewEthereumClient(ethClient, auxAccount, ks, nil)
  179. ethereumClientAux2 = NewEthereumClient(ethClient, aux2Account, ks, nil)
  180. ethereumClientHermez = NewEthereumClient(ethClient, hermezRollupTestAccount, ks, nil)
  181. exitVal = m.Run()
  182. }
  183. os.Exit(exitVal)
  184. }