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.

375 lines
13 KiB

  1. package eth
  2. import (
  3. "io/ioutil"
  4. "math/big"
  5. "os"
  6. "testing"
  7. "github.com/ethereum/go-ethereum/accounts/keystore"
  8. "github.com/ethereum/go-ethereum/common"
  9. "github.com/ethereum/go-ethereum/crypto"
  10. "github.com/ethereum/go-ethereum/ethclient"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. const slotDeadlineConst = uint8(20)
  15. const openAuctionSlotsConst = uint16(4320)
  16. const closedAuctionSlotsConst = uint16(2)
  17. const outbiddingConst = uint16(1000)
  18. const currentSlotConst = 0
  19. var allocationRatioConst [3]uint16 = [3]uint16{4000, 4000, 2000}
  20. var auctionClient *AuctionClient
  21. /*var donationAddressStr = os.Getenv("DONATION_ADDRESS")
  22. var bootCoordinatorStr = os.Getenv("BOOT_COORDINATOR_ADDRESS")
  23. var auctionAddressStr = os.Getenv("AUCTION_ADDRESS")
  24. var tokenHezStr = os.Getenv("TOKEN_ADDRESS")
  25. var hermezStr = os.Getenv("HERMEZ_ADDRESS")
  26. var governanceAddressStr = os.Getenv("GOV_ADDRESS")
  27. var governancePrivateKey = os.Getenv("GOV_PK")
  28. var ehtClientDialURL = os.Getenv("ETHCLIENT_DIAL_URL")*/
  29. var integration = os.Getenv("INTEGRATION")
  30. var donationAddressStr = "0x6c365935CA8710200C7595F0a72EB6023A7706Cd"
  31. var bootCoordinatorStr = "0xc783df8a850f42e7f7e57013759c285caa701eb6"
  32. var auctionAddressStr = "0x3619DbE27d7c1e7E91aA738697Ae7Bc5FC3eACA5"
  33. var tokenHezStr = "0xf4e77E5Da47AC3125140c470c71cBca77B5c638c" //nolint:gosec
  34. var hermezStr = "0xc4905364b78a742ccce7B890A89514061E47068D"
  35. var governanceAddressStr = "0xead9c93b79ae7c1591b1fb5323bd777e86e150d4"
  36. var governancePrivateKey = "d49743deccbccc5dc7baa8e69e5be03298da8688a15dd202e20f15d5e0e9a9fb"
  37. var ehtClientDialURL = "http://localhost:8545"
  38. var DONATION = common.HexToAddress(donationAddressStr)
  39. var BOOTCOORDINATOR = common.HexToAddress(bootCoordinatorStr)
  40. var TOKENHEZ = common.HexToAddress(tokenHezStr)
  41. var HERMEZROLLUP = common.HexToAddress(hermezStr)
  42. var minBidStr = "10000000000000000000"
  43. var URL = "http://localhost:3000"
  44. var newURL = "http://localhost:3002"
  45. var BLOCKSPERSLOT = uint8(40)
  46. var password = "pass"
  47. func TestNewAction(t *testing.T) {
  48. key, err := crypto.HexToECDSA(governancePrivateKey)
  49. require.Nil(t, err)
  50. dir, err := ioutil.TempDir("", "tmpks")
  51. require.Nil(t, err)
  52. ks := keystore.NewKeyStore(dir, keystore.StandardScryptN, keystore.StandardScryptP)
  53. account, err := ks.ImportECDSA(key, password)
  54. require.Nil(t, err)
  55. err = ks.Unlock(account, password)
  56. require.Nil(t, err)
  57. // Init eth client
  58. ethClient, err := ethclient.Dial(ehtClientDialURL)
  59. require.Nil(t, err)
  60. ethereumClient := NewEthereumClient(ethClient, &account, ks, nil)
  61. auctionAddress := common.HexToAddress(auctionAddressStr)
  62. tokenAddress := common.HexToAddress(tokenHezStr)
  63. if integration != "" {
  64. auctionClient = NewAuctionClient(ethereumClient, auctionAddress, tokenAddress)
  65. }
  66. }
  67. func TestAuctionGetCurrentSlotNumber(t *testing.T) {
  68. if auctionClient != nil {
  69. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  70. require.Nil(t, err)
  71. currentSlotInt := int(currentSlot)
  72. assert.Equal(t, currentSlotConst, currentSlotInt)
  73. }
  74. }
  75. func TestAuctionConstants(t *testing.T) {
  76. INITMINBID := new(big.Int)
  77. INITMINBID.SetString(minBidStr, 10)
  78. if auctionClient != nil {
  79. auctionConstants, err := auctionClient.AuctionConstants()
  80. require.Nil(t, err)
  81. assert.Equal(t, auctionConstants.BlocksPerSlot, BLOCKSPERSLOT)
  82. // assert.Equal(t, auctionConstants.GenesisBlockNum, GENESISBLOCKNUM)
  83. assert.Equal(t, auctionConstants.HermezRollup, HERMEZROLLUP)
  84. assert.Equal(t, auctionConstants.InitialMinimalBidding, INITMINBID)
  85. assert.Equal(t, auctionConstants.TokenHEZ, TOKENHEZ)
  86. }
  87. }
  88. func TestAuctionVariables(t *testing.T) {
  89. INITMINBID := new(big.Int)
  90. INITMINBID.SetString(minBidStr, 10)
  91. defaultSlotSetBid := [6]*big.Int{INITMINBID, INITMINBID, INITMINBID, INITMINBID, INITMINBID, INITMINBID}
  92. if auctionClient != nil {
  93. auctionVariables, err := auctionClient.AuctionVariables()
  94. require.Nil(t, err)
  95. assert.Equal(t, auctionVariables.AllocationRatio, allocationRatioConst)
  96. assert.Equal(t, auctionVariables.BootCoordinator, BOOTCOORDINATOR)
  97. assert.Equal(t, auctionVariables.ClosedAuctionSlots, closedAuctionSlotsConst)
  98. assert.Equal(t, auctionVariables.DefaultSlotSetBid, defaultSlotSetBid)
  99. assert.Equal(t, auctionVariables.DonationAddress, DONATION)
  100. assert.Equal(t, auctionVariables.OpenAuctionSlots, openAuctionSlotsConst)
  101. assert.Equal(t, auctionVariables.Outbidding, outbiddingConst)
  102. assert.Equal(t, auctionVariables.SlotDeadline, slotDeadlineConst)
  103. }
  104. }
  105. func TestAuctionGetSlotDeadline(t *testing.T) {
  106. if auctionClient != nil {
  107. slotDeadline, err := auctionClient.AuctionGetSlotDeadline()
  108. require.Nil(t, err)
  109. assert.Equal(t, slotDeadlineConst, slotDeadline)
  110. }
  111. }
  112. func TestAuctionSetSlotDeadline(t *testing.T) {
  113. newSlotDeadline := uint8(25)
  114. if auctionClient != nil {
  115. _, err := auctionClient.AuctionSetSlotDeadline(newSlotDeadline)
  116. require.Nil(t, err)
  117. slotDeadline, err := auctionClient.AuctionGetSlotDeadline()
  118. require.Nil(t, err)
  119. assert.Equal(t, newSlotDeadline, slotDeadline)
  120. _, err = auctionClient.AuctionSetSlotDeadline(slotDeadlineConst)
  121. require.Nil(t, err)
  122. }
  123. }
  124. func TestAuctionGetOpenAuctionSlots(t *testing.T) {
  125. if auctionClient != nil {
  126. openAuctionSlots, err := auctionClient.AuctionGetOpenAuctionSlots()
  127. require.Nil(t, err)
  128. assert.Equal(t, openAuctionSlotsConst, openAuctionSlots)
  129. }
  130. }
  131. func TestAuctionSetOpenAuctionSlots(t *testing.T) {
  132. newOpenAuctionSlots := uint16(4500)
  133. if auctionClient != nil {
  134. _, err := auctionClient.AuctionSetOpenAuctionSlots(newOpenAuctionSlots)
  135. require.Nil(t, err)
  136. openAuctionSlots, err := auctionClient.AuctionGetOpenAuctionSlots()
  137. require.Nil(t, err)
  138. assert.Equal(t, newOpenAuctionSlots, openAuctionSlots)
  139. _, err = auctionClient.AuctionSetOpenAuctionSlots(openAuctionSlotsConst)
  140. require.Nil(t, err)
  141. }
  142. }
  143. func TestAuctionGetClosedAuctionSlots(t *testing.T) {
  144. if auctionClient != nil {
  145. closedAuctionSlots, err := auctionClient.AuctionGetClosedAuctionSlots()
  146. require.Nil(t, err)
  147. assert.Equal(t, closedAuctionSlotsConst, closedAuctionSlots)
  148. }
  149. }
  150. func TestAuctionSetClosedAuctionSlots(t *testing.T) {
  151. newClosedAuctionSlots := uint16(5)
  152. if auctionClient != nil {
  153. _, err := auctionClient.AuctionSetClosedAuctionSlots(newClosedAuctionSlots)
  154. require.Nil(t, err)
  155. closedAuctionSlots, err := auctionClient.AuctionGetClosedAuctionSlots()
  156. require.Nil(t, err)
  157. assert.Equal(t, newClosedAuctionSlots, closedAuctionSlots)
  158. _, err = auctionClient.AuctionSetClosedAuctionSlots(closedAuctionSlotsConst)
  159. require.Nil(t, err)
  160. }
  161. }
  162. func TestAuctionGetOutbidding(t *testing.T) {
  163. if auctionClient != nil {
  164. outbidding, err := auctionClient.AuctionGetOutbidding()
  165. require.Nil(t, err)
  166. assert.Equal(t, outbiddingConst, outbidding)
  167. }
  168. }
  169. func TestAuctionSetOutbidding(t *testing.T) {
  170. newOutbidding := uint16(0xb)
  171. if auctionClient != nil {
  172. _, err := auctionClient.AuctionSetOutbidding(newOutbidding)
  173. require.Nil(t, err)
  174. outbidding, err := auctionClient.AuctionGetOutbidding()
  175. require.Nil(t, err)
  176. assert.Equal(t, newOutbidding, outbidding)
  177. _, err = auctionClient.AuctionSetOutbidding(outbiddingConst)
  178. require.Nil(t, err)
  179. }
  180. }
  181. func TestAuctionGetAllocationRatio(t *testing.T) {
  182. if auctionClient != nil {
  183. allocationRatio, err := auctionClient.AuctionGetAllocationRatio()
  184. require.Nil(t, err)
  185. assert.Equal(t, allocationRatioConst, allocationRatio)
  186. }
  187. }
  188. func TestAuctionSetAllocationRatio(t *testing.T) {
  189. newAllocationRatio := [3]uint16{3000, 3000, 4000}
  190. if auctionClient != nil {
  191. _, err := auctionClient.AuctionSetAllocationRatio(newAllocationRatio)
  192. require.Nil(t, err)
  193. allocationRatio, err := auctionClient.AuctionGetAllocationRatio()
  194. require.Nil(t, err)
  195. assert.Equal(t, newAllocationRatio, allocationRatio)
  196. _, err = auctionClient.AuctionSetAllocationRatio(allocationRatioConst)
  197. require.Nil(t, err)
  198. }
  199. }
  200. func TestAuctionGetDonationAddress(t *testing.T) {
  201. if auctionClient != nil {
  202. donationAddress, err := auctionClient.AuctionGetDonationAddress()
  203. require.Nil(t, err)
  204. donationAddressConst := common.HexToAddress(donationAddressStr)
  205. assert.Equal(t, &donationAddressConst, donationAddress)
  206. }
  207. }
  208. func TestAuctionGetBootCoordinator(t *testing.T) {
  209. if auctionClient != nil {
  210. bootCoordinator, err := auctionClient.AuctionGetBootCoordinator()
  211. require.Nil(t, err)
  212. bootCoordinatorConst := common.HexToAddress(bootCoordinatorStr)
  213. assert.Equal(t, &bootCoordinatorConst, bootCoordinator)
  214. }
  215. }
  216. func TestAuctionSetDonationAddress(t *testing.T) {
  217. newDonationAddress := common.HexToAddress(governanceAddressStr)
  218. if auctionClient != nil {
  219. _, err := auctionClient.AuctionSetDonationAddress(newDonationAddress)
  220. require.Nil(t, err)
  221. donationAddress, err := auctionClient.AuctionGetDonationAddress()
  222. require.Nil(t, err)
  223. assert.Equal(t, &newDonationAddress, donationAddress)
  224. donationAddressConst := common.HexToAddress(donationAddressStr)
  225. _, err = auctionClient.AuctionSetDonationAddress(donationAddressConst)
  226. require.Nil(t, err)
  227. }
  228. }
  229. func TestAuctionSetBootCoordinator(t *testing.T) {
  230. newBootCoordinator := common.HexToAddress(governanceAddressStr)
  231. if auctionClient != nil {
  232. _, err := auctionClient.AuctionSetBootCoordinator(newBootCoordinator)
  233. require.Nil(t, err)
  234. bootCoordinator, err := auctionClient.AuctionGetBootCoordinator()
  235. require.Nil(t, err)
  236. assert.Equal(t, &newBootCoordinator, bootCoordinator)
  237. bootCoordinatorConst := common.HexToAddress(bootCoordinatorStr)
  238. _, err = auctionClient.AuctionSetBootCoordinator(bootCoordinatorConst)
  239. require.Nil(t, err)
  240. }
  241. }
  242. func TestAuctionGetSlotSet(t *testing.T) {
  243. slot := int64(10)
  244. if auctionClient != nil {
  245. slotSet, err := auctionClient.AuctionGetSlotSet(slot)
  246. require.Nil(t, err)
  247. assert.Equal(t, slotSet, big.NewInt(4))
  248. }
  249. }
  250. func TestAuctionGetDefaultSlotSetBid(t *testing.T) {
  251. slotSet := uint8(3)
  252. if auctionClient != nil {
  253. minBid, err := auctionClient.AuctionGetDefaultSlotSetBid(slotSet)
  254. require.Nil(t, err)
  255. assert.Equal(t, minBid.String(), minBidStr)
  256. }
  257. }
  258. func TestAuctionChangeDefaultSlotSetBid(t *testing.T) {
  259. slotSet := int64(3)
  260. set := uint8(3)
  261. newInitialMinBid := new(big.Int)
  262. newInitialMinBid.SetString("20000000000000000000", 10)
  263. if auctionClient != nil {
  264. _, err := auctionClient.AuctionChangeDefaultSlotSetBid(slotSet, newInitialMinBid)
  265. require.Nil(t, err)
  266. minBid, err := auctionClient.AuctionGetDefaultSlotSetBid(set)
  267. require.Nil(t, err)
  268. assert.Equal(t, minBid, newInitialMinBid)
  269. newMinBid := new(big.Int)
  270. newMinBid.SetString("10000000000000000000", 10)
  271. _, err = auctionClient.AuctionChangeDefaultSlotSetBid(slotSet, newMinBid)
  272. require.Nil(t, err)
  273. }
  274. }
  275. func TestAuctionGetClaimableHEZ(t *testing.T) {
  276. forgerAddress := common.HexToAddress(governanceAddressStr)
  277. if auctionClient != nil {
  278. claimableHEZ, err := auctionClient.AuctionGetClaimableHEZ(forgerAddress)
  279. require.Nil(t, err)
  280. assert.Equal(t, claimableHEZ.Int64(), int64(0))
  281. }
  282. }
  283. func TestAuctionIsRegisteredCoordinator(t *testing.T) {
  284. forgerAddress := common.HexToAddress(governanceAddressStr)
  285. if auctionClient != nil {
  286. registered, err := auctionClient.AuctionIsRegisteredCoordinator(forgerAddress)
  287. require.Nil(t, err)
  288. assert.Equal(t, registered, false)
  289. }
  290. }
  291. func TestAuctionRegisterCoordinator(t *testing.T) {
  292. forgerAddress := common.HexToAddress(governanceAddressStr)
  293. if auctionClient != nil {
  294. _, err := auctionClient.AuctionRegisterCoordinator(forgerAddress, URL)
  295. require.Nil(t, err)
  296. }
  297. }
  298. func TestAuctionIsRegisteredCoordinatorTrue(t *testing.T) {
  299. forgerAddress := common.HexToAddress(governanceAddressStr)
  300. if auctionClient != nil {
  301. registered, err := auctionClient.AuctionIsRegisteredCoordinator(forgerAddress)
  302. require.Nil(t, err)
  303. assert.Equal(t, registered, true)
  304. }
  305. }
  306. func TestAuctionUpdateCoordinatorInfo(t *testing.T) {
  307. forgerAddress := common.HexToAddress(governanceAddressStr)
  308. if auctionClient != nil {
  309. _, err := auctionClient.AuctionUpdateCoordinatorInfo(forgerAddress, forgerAddress, newURL)
  310. require.Nil(t, err)
  311. }
  312. }
  313. func TestAuctionBid(t *testing.T) {
  314. if auctionClient != nil {
  315. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  316. require.Nil(t, err)
  317. bidAmount := new(big.Int)
  318. bidAmount.SetString("11000000000000000000", 10)
  319. forgerAddress := common.HexToAddress(governanceAddressStr)
  320. _, err = auctionClient.AuctionBid(currentSlot+4, bidAmount, forgerAddress)
  321. require.Nil(t, err)
  322. }
  323. }
  324. func TestAuctionMultiBid(t *testing.T) {
  325. if auctionClient != nil {
  326. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  327. require.Nil(t, err)
  328. slotSet := [6]bool{false, true, false, true, false, true}
  329. maxBid := new(big.Int)
  330. maxBid.SetString("11000000000000000000", 10)
  331. minBid := new(big.Int)
  332. minBid.SetString("11000000000000000000", 10)
  333. budget := new(big.Int)
  334. budget.SetString("110000000000000000000", 10)
  335. forgerAddress := common.HexToAddress(governanceAddressStr)
  336. _, err = auctionClient.AuctionMultiBid(currentSlot+5, currentSlot+10, slotSet, maxBid, minBid, budget, forgerAddress)
  337. require.Nil(t, err)
  338. }
  339. }