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.

360 lines
14 KiB

  1. package eth
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. const slotDeadlineConst = uint8(20)
  9. const openAuctionSlotsConst = uint16(4320)
  10. const closedAuctionSlotsConst = uint16(2)
  11. const outbiddingConst = uint16(1000)
  12. const currentSlotConst = 0
  13. var allocationRatioConst [3]uint16 = [3]uint16{4000, 4000, 2000}
  14. var auctionClientTest *AuctionClient
  15. //var genesisBlock = 93
  16. var genesisBlock = 100
  17. var minBidStr = "10000000000000000000"
  18. var URL = "http://localhost:3000"
  19. var newURL = "http://localhost:3002"
  20. var BLOCKSPERSLOT = uint8(40)
  21. func TestAuctionGetCurrentSlotNumber(t *testing.T) {
  22. currentSlot, err := auctionClientTest.AuctionGetCurrentSlotNumber()
  23. require.Nil(t, err)
  24. currentSlotInt := int(currentSlot)
  25. assert.Equal(t, currentSlotConst, currentSlotInt)
  26. }
  27. func TestAuctionConstants(t *testing.T) {
  28. INITMINBID := new(big.Int)
  29. INITMINBID.SetString(minBidStr, 10)
  30. auctionConstants, err := auctionClientTest.AuctionConstants()
  31. require.Nil(t, err)
  32. assert.Equal(t, auctionConstants.BlocksPerSlot, BLOCKSPERSLOT)
  33. assert.Equal(t, auctionConstants.GenesisBlockNum, int64(genesisBlock))
  34. assert.Equal(t, auctionConstants.HermezRollup, hermezRollupAddressTestConst)
  35. assert.Equal(t, auctionConstants.InitialMinimalBidding, INITMINBID)
  36. assert.Equal(t, auctionConstants.TokenHEZ, tokenERC777AddressConst)
  37. }
  38. func TestAuctionVariables(t *testing.T) {
  39. INITMINBID := new(big.Int)
  40. INITMINBID.SetString(minBidStr, 10)
  41. defaultSlotSetBid := [6]*big.Int{INITMINBID, INITMINBID, INITMINBID, INITMINBID, INITMINBID, INITMINBID}
  42. auctionVariables, err := auctionClientTest.AuctionVariables()
  43. require.Nil(t, err)
  44. assert.Equal(t, auctionVariables.AllocationRatio, allocationRatioConst)
  45. assert.Equal(t, auctionVariables.BootCoordinator, bootCoordinatorAddressConst)
  46. assert.Equal(t, auctionVariables.ClosedAuctionSlots, closedAuctionSlotsConst)
  47. assert.Equal(t, auctionVariables.DefaultSlotSetBid, defaultSlotSetBid)
  48. assert.Equal(t, auctionVariables.DonationAddress, donationAddressConst)
  49. assert.Equal(t, auctionVariables.OpenAuctionSlots, openAuctionSlotsConst)
  50. assert.Equal(t, auctionVariables.Outbidding, outbiddingConst)
  51. assert.Equal(t, auctionVariables.SlotDeadline, slotDeadlineConst)
  52. }
  53. func TestAuctionGetSlotDeadline(t *testing.T) {
  54. slotDeadline, err := auctionClientTest.AuctionGetSlotDeadline()
  55. require.Nil(t, err)
  56. assert.Equal(t, slotDeadlineConst, slotDeadline)
  57. }
  58. func TestAuctionSetSlotDeadline(t *testing.T) {
  59. newSlotDeadline := uint8(25)
  60. _, err := auctionClientTest.AuctionSetSlotDeadline(newSlotDeadline)
  61. require.Nil(t, err)
  62. slotDeadline, err := auctionClientTest.AuctionGetSlotDeadline()
  63. require.Nil(t, err)
  64. assert.Equal(t, newSlotDeadline, slotDeadline)
  65. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  66. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  67. assert.Equal(t, newSlotDeadline, auctionEvents.NewSlotDeadline[0].NewSlotDeadline)
  68. }
  69. func TestAuctionGetOpenAuctionSlots(t *testing.T) {
  70. openAuctionSlots, err := auctionClientTest.AuctionGetOpenAuctionSlots()
  71. require.Nil(t, err)
  72. assert.Equal(t, openAuctionSlotsConst, openAuctionSlots)
  73. }
  74. func TestAuctionSetOpenAuctionSlots(t *testing.T) {
  75. newOpenAuctionSlots := uint16(4500)
  76. _, err := auctionClientTest.AuctionSetOpenAuctionSlots(newOpenAuctionSlots)
  77. require.Nil(t, err)
  78. openAuctionSlots, err := auctionClientTest.AuctionGetOpenAuctionSlots()
  79. require.Nil(t, err)
  80. assert.Equal(t, newOpenAuctionSlots, openAuctionSlots)
  81. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  82. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  83. assert.Equal(t, newOpenAuctionSlots, auctionEvents.NewOpenAuctionSlots[0].NewOpenAuctionSlots)
  84. }
  85. func TestAuctionGetClosedAuctionSlots(t *testing.T) {
  86. closedAuctionSlots, err := auctionClientTest.AuctionGetClosedAuctionSlots()
  87. require.Nil(t, err)
  88. assert.Equal(t, closedAuctionSlotsConst, closedAuctionSlots)
  89. }
  90. func TestAuctionSetClosedAuctionSlots(t *testing.T) {
  91. newClosedAuctionSlots := uint16(1)
  92. _, err := auctionClientTest.AuctionSetClosedAuctionSlots(newClosedAuctionSlots)
  93. require.Nil(t, err)
  94. closedAuctionSlots, err := auctionClientTest.AuctionGetClosedAuctionSlots()
  95. require.Nil(t, err)
  96. assert.Equal(t, newClosedAuctionSlots, closedAuctionSlots)
  97. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  98. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  99. assert.Equal(t, newClosedAuctionSlots, auctionEvents.NewClosedAuctionSlots[0].NewClosedAuctionSlots)
  100. _, err = auctionClientTest.AuctionSetClosedAuctionSlots(closedAuctionSlots)
  101. require.Nil(t, err)
  102. }
  103. func TestAuctionGetOutbidding(t *testing.T) {
  104. outbidding, err := auctionClientTest.AuctionGetOutbidding()
  105. require.Nil(t, err)
  106. assert.Equal(t, outbiddingConst, outbidding)
  107. }
  108. func TestAuctionSetOutbidding(t *testing.T) {
  109. newOutbidding := uint16(0xb)
  110. _, err := auctionClientTest.AuctionSetOutbidding(newOutbidding)
  111. require.Nil(t, err)
  112. outbidding, err := auctionClientTest.AuctionGetOutbidding()
  113. require.Nil(t, err)
  114. assert.Equal(t, newOutbidding, outbidding)
  115. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  116. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  117. assert.Equal(t, newOutbidding, auctionEvents.NewOutbidding[0].NewOutbidding)
  118. _, err = auctionClientTest.AuctionSetOutbidding(outbiddingConst)
  119. require.Nil(t, err)
  120. }
  121. func TestAuctionGetAllocationRatio(t *testing.T) {
  122. allocationRatio, err := auctionClientTest.AuctionGetAllocationRatio()
  123. require.Nil(t, err)
  124. assert.Equal(t, allocationRatioConst, allocationRatio)
  125. }
  126. func TestAuctionSetAllocationRatio(t *testing.T) {
  127. newAllocationRatio := [3]uint16{3000, 3000, 4000}
  128. _, err := auctionClientTest.AuctionSetAllocationRatio(newAllocationRatio)
  129. require.Nil(t, err)
  130. allocationRatio, err := auctionClientTest.AuctionGetAllocationRatio()
  131. require.Nil(t, err)
  132. assert.Equal(t, newAllocationRatio, allocationRatio)
  133. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  134. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  135. assert.Equal(t, newAllocationRatio, auctionEvents.NewAllocationRatio[0].NewAllocationRatio)
  136. _, err = auctionClientTest.AuctionSetAllocationRatio(allocationRatioConst)
  137. require.Nil(t, err)
  138. }
  139. func TestAuctionGetDonationAddress(t *testing.T) {
  140. donationAddress, err := auctionClientTest.AuctionGetDonationAddress()
  141. require.Nil(t, err)
  142. assert.Equal(t, &donationAddressConst, donationAddress)
  143. }
  144. func TestAuctionGetBootCoordinator(t *testing.T) {
  145. bootCoordinator, err := auctionClientTest.AuctionGetBootCoordinator()
  146. require.Nil(t, err)
  147. assert.Equal(t, &bootCoordinatorAddressConst, bootCoordinator)
  148. }
  149. func TestAuctionSetDonationAddress(t *testing.T) {
  150. newDonationAddress := governanceAddressConst
  151. _, err := auctionClientTest.AuctionSetDonationAddress(newDonationAddress)
  152. require.Nil(t, err)
  153. donationAddress, err := auctionClientTest.AuctionGetDonationAddress()
  154. require.Nil(t, err)
  155. assert.Equal(t, &newDonationAddress, donationAddress)
  156. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  157. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  158. assert.Equal(t, newDonationAddress, auctionEvents.NewDonationAddress[0].NewDonationAddress)
  159. _, err = auctionClientTest.AuctionSetDonationAddress(donationAddressConst)
  160. require.Nil(t, err)
  161. }
  162. func TestAuctionSetBootCoordinator(t *testing.T) {
  163. newBootCoordinator := governanceAddressConst
  164. _, err := auctionClientTest.AuctionSetBootCoordinator(newBootCoordinator)
  165. require.Nil(t, err)
  166. bootCoordinator, err := auctionClientTest.AuctionGetBootCoordinator()
  167. require.Nil(t, err)
  168. assert.Equal(t, &newBootCoordinator, bootCoordinator)
  169. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  170. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  171. assert.Equal(t, newBootCoordinator, auctionEvents.NewBootCoordinator[0].NewBootCoordinator)
  172. _, err = auctionClientTest.AuctionSetBootCoordinator(bootCoordinatorAddressConst)
  173. require.Nil(t, err)
  174. }
  175. func TestAuctionGetSlotSet(t *testing.T) {
  176. slot := int64(10)
  177. slotSet, err := auctionClientTest.AuctionGetSlotSet(slot)
  178. require.Nil(t, err)
  179. assert.Equal(t, slotSet, big.NewInt(4))
  180. }
  181. func TestAuctionGetDefaultSlotSetBid(t *testing.T) {
  182. slotSet := uint8(3)
  183. minBid, err := auctionClientTest.AuctionGetDefaultSlotSetBid(slotSet)
  184. require.Nil(t, err)
  185. assert.Equal(t, minBid.String(), minBidStr)
  186. }
  187. func TestAuctionChangeDefaultSlotSetBid(t *testing.T) {
  188. slotSet := int64(3)
  189. set := uint8(3)
  190. newInitialMinBid := new(big.Int)
  191. newInitialMinBid.SetString("20000000000000000000", 10)
  192. _, err := auctionClientTest.AuctionChangeDefaultSlotSetBid(slotSet, newInitialMinBid)
  193. require.Nil(t, err)
  194. minBid, err := auctionClientTest.AuctionGetDefaultSlotSetBid(set)
  195. require.Nil(t, err)
  196. assert.Equal(t, minBid, newInitialMinBid)
  197. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  198. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  199. assert.Equal(t, slotSet, auctionEvents.NewDefaultSlotSetBid[0].SlotSet)
  200. assert.Equal(t, newInitialMinBid, auctionEvents.NewDefaultSlotSetBid[0].NewInitialMinBid)
  201. newMinBid := new(big.Int)
  202. newMinBid.SetString("10000000000000000000", 10)
  203. _, err = auctionClientTest.AuctionChangeDefaultSlotSetBid(slotSet, newMinBid)
  204. require.Nil(t, err)
  205. }
  206. func TestAuctionGetClaimableHEZ(t *testing.T) {
  207. forgerAddress := governanceAddressConst
  208. claimableHEZ, err := auctionClientTest.AuctionGetClaimableHEZ(forgerAddress)
  209. require.Nil(t, err)
  210. assert.Equal(t, claimableHEZ.Int64(), int64(0))
  211. }
  212. func TestAuctionRegisterCoordinator(t *testing.T) {
  213. forgerAddress := governanceAddressConst
  214. _, err := auctionClientTest.AuctionSetCoordinator(forgerAddress, URL)
  215. require.Nil(t, err)
  216. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  217. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  218. assert.Equal(t, forgerAddress, auctionEvents.SetCoordinator[0].ForgerAddress)
  219. assert.Equal(t, forgerAddress, auctionEvents.SetCoordinator[0].BidderAddress)
  220. assert.Equal(t, URL, auctionEvents.SetCoordinator[0].CoordinatorURL)
  221. }
  222. func TestAuctionBid(t *testing.T) {
  223. currentSlot, err := auctionClientTest.AuctionGetCurrentSlotNumber()
  224. require.Nil(t, err)
  225. bidAmount := new(big.Int)
  226. bidAmount.SetString("12000000000000000000", 10)
  227. forgerAddress := governanceAddressConst
  228. _, err = auctionClientTest.AuctionBid(currentSlot+4, bidAmount)
  229. require.Nil(t, err)
  230. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  231. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  232. assert.Equal(t, bidAmount, auctionEvents.NewBid[0].BidAmount)
  233. assert.Equal(t, forgerAddress, auctionEvents.NewBid[0].Bidder)
  234. assert.Equal(t, currentSlot+4, auctionEvents.NewBid[0].Slot)
  235. }
  236. func TestAuctionGetSlotNumber(t *testing.T) {
  237. slotConst := 4
  238. blockNum := int(BLOCKSPERSLOT)*slotConst + genesisBlock
  239. slot, err := auctionClientTest.AuctionGetSlotNumber(int64(blockNum))
  240. require.Nil(t, err)
  241. assert.Equal(t, slot, int64(slotConst))
  242. }
  243. func TestAuctionCanForge(t *testing.T) {
  244. slotConst := 4
  245. blockNum := int(BLOCKSPERSLOT)*slotConst + genesisBlock
  246. canForge, err := auctionClientTest.AuctionCanForge(governanceAddressConst, int64(blockNum))
  247. require.Nil(t, err)
  248. assert.Equal(t, canForge, true)
  249. }
  250. func TestAuctionMultiBid(t *testing.T) {
  251. currentSlot, err := auctionClientTest.AuctionGetCurrentSlotNumber()
  252. require.Nil(t, err)
  253. slotSet := [6]bool{true, false, true, false, true, false}
  254. maxBid := new(big.Int)
  255. maxBid.SetString("15000000000000000000", 10)
  256. minBid := new(big.Int)
  257. minBid.SetString("11000000000000000000", 10)
  258. budget := new(big.Int)
  259. budget.SetString("45200000000000000000", 10)
  260. forgerAddress := governanceAddressConst
  261. _, err = auctionClientTest.AuctionMultiBid(currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, budget)
  262. require.Nil(t, err)
  263. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  264. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  265. assert.Equal(t, forgerAddress, auctionEvents.NewBid[0].Bidder)
  266. assert.Equal(t, currentSlot+4, auctionEvents.NewBid[0].Slot)
  267. assert.Equal(t, forgerAddress, auctionEvents.NewBid[1].Bidder)
  268. assert.Equal(t, currentSlot+6, auctionEvents.NewBid[1].Slot)
  269. assert.Equal(t, forgerAddress, auctionEvents.NewBid[2].Bidder)
  270. assert.Equal(t, currentSlot+8, auctionEvents.NewBid[2].Slot)
  271. assert.Equal(t, forgerAddress, auctionEvents.NewBid[3].Bidder)
  272. assert.Equal(t, currentSlot+10, auctionEvents.NewBid[3].Slot)
  273. }
  274. func TestAuctionGetClaimableHEZ2(t *testing.T) {
  275. forgerAddress := governanceAddressConst
  276. amount := new(big.Int)
  277. amount.SetString("11000000000000000000", 10)
  278. claimableHEZ, err := auctionClientTest.AuctionGetClaimableHEZ(forgerAddress)
  279. require.Nil(t, err)
  280. assert.Equal(t, claimableHEZ, amount)
  281. }
  282. func TestAuctionClaimHEZ(t *testing.T) {
  283. amount := new(big.Int)
  284. amount.SetString("11000000000000000000", 10)
  285. _, err := auctionClientTest.AuctionClaimHEZ()
  286. require.Nil(t, err)
  287. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  288. auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
  289. assert.Equal(t, amount, auctionEvents.HEZClaimed[0].Amount)
  290. assert.Equal(t, governanceAddressConst, auctionEvents.HEZClaimed[0].Owner)
  291. }
  292. func TestAuctionForge(t *testing.T) {
  293. auctionClientTestHermez, err := NewAuctionClient(ethereumClientHermez, auctionTestAddressConst, tokenERC777AddressConst)
  294. require.Nil(t, err)
  295. slotConst := 4
  296. blockNum := int64(int(BLOCKSPERSLOT)*slotConst + genesisBlock)
  297. currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
  298. blocksToAdd := blockNum - currentBlockNum
  299. addBlocks(blocksToAdd, ethClientDialURL)
  300. currentBlockNum, _ = auctionClientTest.client.EthCurrentBlock()
  301. assert.Equal(t, currentBlockNum, blockNum)
  302. _, err = auctionClientTestHermez.AuctionForge(bootCoordinatorAddressConst)
  303. require.Contains(t, err.Error(), "Can't forge")
  304. _, err = auctionClientTestHermez.AuctionForge(governanceAddressConst)
  305. require.Nil(t, err)
  306. }