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.

208 lines
7.3 KiB

  1. package eth
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "math/big"
  6. "os"
  7. "strconv"
  8. "testing"
  9. "github.com/ethereum/go-ethereum/accounts"
  10. "github.com/ethereum/go-ethereum/accounts/keystore"
  11. ethCommon "github.com/ethereum/go-ethereum/common"
  12. "github.com/ethereum/go-ethereum/crypto"
  13. "github.com/ethereum/go-ethereum/ethclient"
  14. "github.com/joho/godotenv"
  15. )
  16. var errEnvVar = fmt.Errorf("Some environment variable is missing")
  17. var (
  18. ethClientDialURL = "http://localhost:8545"
  19. password = "pass"
  20. deadline, _ = new(big.Int).SetString("ffffffffffffffffffffffffffffffff", 16)
  21. )
  22. // Smart Contract Addresses
  23. var (
  24. genesisBlock int64
  25. auctionAddressConst ethCommon.Address
  26. auctionTestAddressConst ethCommon.Address
  27. tokenHEZAddressConst ethCommon.Address
  28. hermezRollupAddressConst ethCommon.Address
  29. wdelayerAddressConst ethCommon.Address
  30. wdelayerTestAddressConst ethCommon.Address
  31. tokenHEZ TokenConfig
  32. donationAddressStr = "0x6c365935CA8710200C7595F0a72EB6023A7706Cd"
  33. donationAddressConst = ethCommon.HexToAddress(donationAddressStr)
  34. bootCoordinatorAddressStr = "0xc783df8a850f42e7f7e57013759c285caa701eb6"
  35. bootCoordinatorAddressConst = ethCommon.HexToAddress(bootCoordinatorAddressStr)
  36. safetyAddressStr = "0xE5904695748fe4A84b40b3fc79De2277660BD1D3"
  37. safetyAddressConst = ethCommon.HexToAddress(safetyAddressStr)
  38. )
  39. // Ethereum Accounts
  40. var (
  41. hermezGovernanceDAOAddressSK = "2a8aede924268f84156a00761de73998dac7bf703408754b776ff3f873bcec60"
  42. hermezGovernanceDAOAddressStr = "0x84Fae3d3Cba24A97817b2a18c2421d462dbBCe9f"
  43. hermezGovernanceDAOAddressConst = ethCommon.HexToAddress(hermezGovernanceDAOAddressStr)
  44. whiteHackGroupAddressSK = "8b24fd94f1ce869d81a34b95351e7f97b2cd88a891d5c00abc33d0ec9501902e"
  45. whiteHackGroupAddressStr = "0xfa3BdC8709226Da0dA13A4d904c8b66f16c3c8BA"
  46. whiteHackGroupAddressConst = ethCommon.HexToAddress(whiteHackGroupAddressStr)
  47. hermezKeeperAddressSK = "7f307c41137d1ed409f0a7b028f6c7596f12734b1d289b58099b99d60a96efff"
  48. hermezKeeperAddressStr = "0xFbC51a9582D031f2ceaaD3959256596C5D3a5468"
  49. hermezKeeperAddressConst = ethCommon.HexToAddress(hermezKeeperAddressStr)
  50. governanceAddressSK = "d49743deccbccc5dc7baa8e69e5be03298da8688a15dd202e20f15d5e0e9a9fb"
  51. governanceAddressStr = "0xead9c93b79ae7c1591b1fb5323bd777e86e150d4"
  52. governanceAddressConst = ethCommon.HexToAddress(governanceAddressStr)
  53. auxAddressSK = "28d1bfbbafe9d1d4f5a11c3c16ab6bf9084de48d99fbac4058bdfa3c80b29089"
  54. auxAddressStr = "0x3d91185a02774C70287F6c74Dd26d13DFB58ff16"
  55. auxAddressConst = ethCommon.HexToAddress(auxAddressStr)
  56. aux2AddressSK = "28d1bfbbafe9d1d4f5a11c3c16ab6bf9084de48d99fbac4058bdfa3c80b29087"
  57. // aux2AddressStr = "0x532792b73c0c6e7565912e7039c59986f7e1dd1f"
  58. // aux2AddressConst = ethCommon.HexToAddress(aux2AddressStr)
  59. hermezRollupTestSK = "28d1bfbbafe9d1d4f5a11c3c16ab6bf9084de48d99fbac4058bdfa3c80b29088"
  60. hermezRollupTestAddressStr = "0xEa960515F8b4C237730F028cBAcF0a28E7F45dE0"
  61. hermezRollupAddressTestConst = ethCommon.HexToAddress(hermezRollupTestAddressStr)
  62. )
  63. var (
  64. accountGov *accounts.Account
  65. accountKep *accounts.Account
  66. accountWhite *accounts.Account
  67. accountGovDAO *accounts.Account
  68. accountAux *accounts.Account
  69. accountAux2 *accounts.Account
  70. accountHermez *accounts.Account
  71. ks *keystore.KeyStore
  72. ethClient *ethclient.Client
  73. ethereumClientWhite *EthereumClient
  74. ethereumClientKep *EthereumClient
  75. ethereumClientGovDAO *EthereumClient
  76. ethereumClientAux *EthereumClient
  77. ethereumClientAux2 *EthereumClient
  78. ethereumClientHermez *EthereumClient
  79. )
  80. func addKey(ks *keystore.KeyStore, skHex string) *accounts.Account {
  81. key, err := crypto.HexToECDSA(skHex)
  82. if err != nil {
  83. panic(err)
  84. }
  85. account, err := ks.ImportECDSA(key, password)
  86. if err != nil {
  87. panic(err)
  88. }
  89. err = ks.Unlock(account, password)
  90. if err != nil {
  91. panic(err)
  92. }
  93. return &account
  94. }
  95. func getEnvVariables() {
  96. err := godotenv.Load()
  97. if err != nil {
  98. fmt.Println("Variables loaded from environment")
  99. } else {
  100. fmt.Println("Variables loaded from .env file")
  101. }
  102. var auctionAddressStr = os.Getenv("AUCTION")
  103. var auctionTestAddressStr = os.Getenv("AUCTION_TEST")
  104. var tokenHEZAddressStr = os.Getenv("TOKENHEZ")
  105. var hermezRollupAddressStr = os.Getenv("HERMEZ")
  106. var wdelayerAddressStr = os.Getenv("WDELAYER")
  107. var wdelayerTestAddressStr = os.Getenv("WDELAYER_TEST")
  108. genesisBlockEnv := os.Getenv("GENESIS_BLOCK")
  109. genesisBlock, err = strconv.ParseInt(genesisBlockEnv, 10, 64)
  110. if err != nil {
  111. panic(errEnvVar)
  112. }
  113. if auctionAddressStr == "" || auctionTestAddressStr == "" || tokenHEZAddressStr == "" || hermezRollupAddressStr == "" || wdelayerAddressStr == "" || wdelayerTestAddressStr == "" || genesisBlockEnv == "" {
  114. panic(errEnvVar)
  115. }
  116. auctionAddressConst = ethCommon.HexToAddress(auctionAddressStr)
  117. auctionTestAddressConst = ethCommon.HexToAddress(auctionTestAddressStr)
  118. tokenHEZAddressConst = ethCommon.HexToAddress(tokenHEZAddressStr)
  119. hermezRollupAddressConst = ethCommon.HexToAddress(hermezRollupAddressStr)
  120. wdelayerAddressConst = ethCommon.HexToAddress(wdelayerAddressStr)
  121. wdelayerTestAddressConst = ethCommon.HexToAddress(wdelayerTestAddressStr)
  122. tokenHEZ = TokenConfig{
  123. Address: tokenHEZAddressConst,
  124. Name: "Hermez Network Token",
  125. }
  126. }
  127. func TestMain(m *testing.M) {
  128. exitVal := 0
  129. if os.Getenv("INTEGRATION") != "" {
  130. getEnvVariables()
  131. dir, err := ioutil.TempDir("", "tmpks")
  132. if err != nil {
  133. panic(err)
  134. }
  135. defer func() {
  136. if err := os.RemoveAll(dir); err != nil {
  137. panic(err)
  138. }
  139. }()
  140. ks = keystore.NewKeyStore(dir, keystore.LightScryptN, keystore.LightScryptP)
  141. // Load ethereum accounts from private keys
  142. accountGov = addKey(ks, governanceAddressSK)
  143. accountKep = addKey(ks, hermezKeeperAddressSK)
  144. accountWhite = addKey(ks, whiteHackGroupAddressSK)
  145. accountGovDAO = addKey(ks, hermezGovernanceDAOAddressSK)
  146. accountAux = addKey(ks, auxAddressSK)
  147. accountAux2 = addKey(ks, aux2AddressSK)
  148. accountHermez = addKey(ks, hermezRollupTestSK)
  149. ethClient, err = ethclient.Dial(ethClientDialURL)
  150. if err != nil {
  151. panic(err)
  152. }
  153. // Controllable Governance Address
  154. ethereumClientGov := NewEthereumClient(ethClient, accountGov, ks, nil)
  155. auctionClient, err = NewAuctionClient(ethereumClientGov, auctionAddressConst, tokenHEZ)
  156. if err != nil {
  157. panic(err)
  158. }
  159. auctionClientTest, err = NewAuctionClient(ethereumClientGov, auctionTestAddressConst, tokenHEZ)
  160. if err != nil {
  161. panic(err)
  162. }
  163. rollupClient, err = NewRollupClient(ethereumClientGov, hermezRollupAddressConst, tokenHEZ)
  164. if err != nil {
  165. panic(err)
  166. }
  167. wdelayerClient, err = NewWDelayerClient(ethereumClientGov, wdelayerAddressConst)
  168. if err != nil {
  169. panic(err)
  170. }
  171. wdelayerClientTest, err = NewWDelayerClient(ethereumClientGov, wdelayerTestAddressConst)
  172. if err != nil {
  173. panic(err)
  174. }
  175. ethereumClientKep = NewEthereumClient(ethClient, accountKep, ks, nil)
  176. ethereumClientWhite = NewEthereumClient(ethClient, accountWhite, ks, nil)
  177. ethereumClientGovDAO = NewEthereumClient(ethClient, accountGovDAO, ks, nil)
  178. ethereumClientAux = NewEthereumClient(ethClient, accountAux, ks, nil)
  179. ethereumClientAux2 = NewEthereumClient(ethClient, accountAux2, ks, nil)
  180. ethereumClientHermez = NewEthereumClient(ethClient, accountHermez, ks, nil)
  181. exitVal = m.Run()
  182. }
  183. os.Exit(exitVal)
  184. }