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.

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