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.

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