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.

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