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.

372 lines
14 KiB

  1. package eth
  2. import (
  3. "context"
  4. "math/big"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. const slotDeadlineConst = uint8(20)
  10. const openAuctionSlotsConst = uint16(4320)
  11. const closedAuctionSlotsConst = uint16(2)
  12. const outbiddingConst = uint16(1000)
  13. const currentSlotConst = 0
  14. var allocationRatioConst [3]uint16 = [3]uint16{4000, 4000, 2000}
  15. var auctionClient *AuctionClient
  16. var genesisBlock = 91
  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 := auctionClient.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 := auctionClient.AuctionConstants()
  31. require.Nil(t, err)
  32. assert.Equal(t, auctionConstants.BlocksPerSlot, BLOCKSPERSLOT)
  33. // assert.Equal(t, auctionConstants.GenesisBlockNum, GENESISBLOCKNUM)
  34. assert.Equal(t, auctionConstants.HermezRollup, hermezRollupAddressConst)
  35. assert.Equal(t, auctionConstants.InitialMinimalBidding, INITMINBID)
  36. assert.Equal(t, auctionConstants.TokenHEZ, tokenHezAddressConst)
  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 := auctionClient.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 := auctionClient.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 := auctionClient.AuctionSetSlotDeadline(newSlotDeadline)
  61. require.Nil(t, err)
  62. slotDeadline, err := auctionClient.AuctionGetSlotDeadline()
  63. require.Nil(t, err)
  64. assert.Equal(t, newSlotDeadline, slotDeadline)
  65. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  66. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  67. assert.Equal(t, newSlotDeadline, auctionEvents.NewSlotDeadline[0].NewSlotDeadline)
  68. }
  69. func TestAuctionGetOpenAuctionSlots(t *testing.T) {
  70. openAuctionSlots, err := auctionClient.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 := auctionClient.AuctionSetOpenAuctionSlots(newOpenAuctionSlots)
  77. require.Nil(t, err)
  78. openAuctionSlots, err := auctionClient.AuctionGetOpenAuctionSlots()
  79. require.Nil(t, err)
  80. assert.Equal(t, newOpenAuctionSlots, openAuctionSlots)
  81. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  82. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  83. assert.Equal(t, newOpenAuctionSlots, auctionEvents.NewOpenAuctionSlots[0].NewOpenAuctionSlots)
  84. }
  85. func TestAuctionGetClosedAuctionSlots(t *testing.T) {
  86. closedAuctionSlots, err := auctionClient.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 := auctionClient.AuctionSetClosedAuctionSlots(newClosedAuctionSlots)
  93. require.Nil(t, err)
  94. closedAuctionSlots, err := auctionClient.AuctionGetClosedAuctionSlots()
  95. require.Nil(t, err)
  96. assert.Equal(t, newClosedAuctionSlots, closedAuctionSlots)
  97. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  98. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  99. assert.Equal(t, newClosedAuctionSlots, auctionEvents.NewClosedAuctionSlots[0].NewClosedAuctionSlots)
  100. _, err = auctionClient.AuctionSetClosedAuctionSlots(closedAuctionSlots)
  101. require.Nil(t, err)
  102. }
  103. func TestAuctionGetOutbidding(t *testing.T) {
  104. outbidding, err := auctionClient.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 := auctionClient.AuctionSetOutbidding(newOutbidding)
  111. require.Nil(t, err)
  112. outbidding, err := auctionClient.AuctionGetOutbidding()
  113. require.Nil(t, err)
  114. assert.Equal(t, newOutbidding, outbidding)
  115. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  116. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  117. assert.Equal(t, newOutbidding, auctionEvents.NewOutbidding[0].NewOutbidding)
  118. _, err = auctionClient.AuctionSetOutbidding(outbiddingConst)
  119. require.Nil(t, err)
  120. }
  121. func TestAuctionGetAllocationRatio(t *testing.T) {
  122. allocationRatio, err := auctionClient.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 := auctionClient.AuctionSetAllocationRatio(newAllocationRatio)
  129. require.Nil(t, err)
  130. allocationRatio, err := auctionClient.AuctionGetAllocationRatio()
  131. require.Nil(t, err)
  132. assert.Equal(t, newAllocationRatio, allocationRatio)
  133. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  134. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  135. assert.Equal(t, newAllocationRatio, auctionEvents.NewAllocationRatio[0].NewAllocationRatio)
  136. _, err = auctionClient.AuctionSetAllocationRatio(allocationRatioConst)
  137. require.Nil(t, err)
  138. }
  139. func TestAuctionGetDonationAddress(t *testing.T) {
  140. donationAddress, err := auctionClient.AuctionGetDonationAddress()
  141. require.Nil(t, err)
  142. assert.Equal(t, &donationAddressConst, donationAddress)
  143. }
  144. func TestAuctionGetBootCoordinator(t *testing.T) {
  145. bootCoordinator, err := auctionClient.AuctionGetBootCoordinator()
  146. require.Nil(t, err)
  147. assert.Equal(t, &bootCoordinatorAddressConst, bootCoordinator)
  148. }
  149. func TestAuctionSetDonationAddress(t *testing.T) {
  150. newDonationAddress := governanceAddressConst
  151. _, err := auctionClient.AuctionSetDonationAddress(newDonationAddress)
  152. require.Nil(t, err)
  153. donationAddress, err := auctionClient.AuctionGetDonationAddress()
  154. require.Nil(t, err)
  155. assert.Equal(t, &newDonationAddress, donationAddress)
  156. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  157. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  158. assert.Equal(t, newDonationAddress, auctionEvents.NewDonationAddress[0].NewDonationAddress)
  159. _, err = auctionClient.AuctionSetDonationAddress(donationAddressConst)
  160. require.Nil(t, err)
  161. }
  162. func TestAuctionSetBootCoordinator(t *testing.T) {
  163. newBootCoordinator := governanceAddressConst
  164. _, err := auctionClient.AuctionSetBootCoordinator(newBootCoordinator)
  165. require.Nil(t, err)
  166. bootCoordinator, err := auctionClient.AuctionGetBootCoordinator()
  167. require.Nil(t, err)
  168. assert.Equal(t, &newBootCoordinator, bootCoordinator)
  169. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  170. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  171. assert.Equal(t, newBootCoordinator, auctionEvents.NewBootCoordinator[0].NewBootCoordinator)
  172. _, err = auctionClient.AuctionSetBootCoordinator(bootCoordinatorAddressConst)
  173. require.Nil(t, err)
  174. }
  175. func TestAuctionGetSlotSet(t *testing.T) {
  176. slot := int64(10)
  177. slotSet, err := auctionClient.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 := auctionClient.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 := auctionClient.AuctionChangeDefaultSlotSetBid(slotSet, newInitialMinBid)
  193. require.Nil(t, err)
  194. minBid, err := auctionClient.AuctionGetDefaultSlotSetBid(set)
  195. require.Nil(t, err)
  196. assert.Equal(t, minBid, newInitialMinBid)
  197. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  198. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  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 = auctionClient.AuctionChangeDefaultSlotSetBid(slotSet, newMinBid)
  204. require.Nil(t, err)
  205. }
  206. func TestAuctionGetClaimableHEZ(t *testing.T) {
  207. forgerAddress := governanceAddressConst
  208. claimableHEZ, err := auctionClient.AuctionGetClaimableHEZ(forgerAddress)
  209. require.Nil(t, err)
  210. assert.Equal(t, claimableHEZ.Int64(), int64(0))
  211. }
  212. func TestAuctionIsRegisteredCoordinator(t *testing.T) {
  213. forgerAddress := governanceAddressConst
  214. registered, err := auctionClient.AuctionIsRegisteredCoordinator(forgerAddress)
  215. require.Nil(t, err)
  216. assert.Equal(t, registered, false)
  217. }
  218. func TestAuctionRegisterCoordinator(t *testing.T) {
  219. forgerAddress := governanceAddressConst
  220. _, err := auctionClient.AuctionRegisterCoordinator(forgerAddress, URL)
  221. require.Nil(t, err)
  222. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  223. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  224. assert.Equal(t, forgerAddress, auctionEvents.NewCoordinator[0].ForgerAddress)
  225. assert.Equal(t, forgerAddress, auctionEvents.NewCoordinator[0].WithdrawalAddress)
  226. assert.Equal(t, URL, auctionEvents.NewCoordinator[0].CoordinatorURL)
  227. }
  228. func TestAuctionIsRegisteredCoordinatorTrue(t *testing.T) {
  229. forgerAddress := governanceAddressConst
  230. registered, err := auctionClient.AuctionIsRegisteredCoordinator(forgerAddress)
  231. require.Nil(t, err)
  232. assert.Equal(t, registered, true)
  233. }
  234. func TestAuctionUpdateCoordinatorInfo(t *testing.T) {
  235. forgerAddress := governanceAddressConst
  236. _, err := auctionClient.AuctionUpdateCoordinatorInfo(forgerAddress, forgerAddress, newURL)
  237. require.Nil(t, err)
  238. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  239. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  240. assert.Equal(t, forgerAddress, auctionEvents.CoordinatorUpdated[0].ForgerAddress)
  241. assert.Equal(t, forgerAddress, auctionEvents.CoordinatorUpdated[0].WithdrawalAddress)
  242. assert.Equal(t, newURL, auctionEvents.CoordinatorUpdated[0].CoordinatorURL)
  243. }
  244. func TestAuctionBid(t *testing.T) {
  245. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  246. require.Nil(t, err)
  247. bidAmount := new(big.Int)
  248. bidAmount.SetString("12000000000000000000", 10)
  249. forgerAddress := governanceAddressConst
  250. _, err = auctionClient.AuctionBid(currentSlot+4, bidAmount, forgerAddress)
  251. require.Nil(t, err)
  252. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  253. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  254. assert.Equal(t, bidAmount, auctionEvents.NewBid[0].BidAmount)
  255. assert.Equal(t, forgerAddress, auctionEvents.NewBid[0].CoordinatorForger)
  256. assert.Equal(t, currentSlot+4, auctionEvents.NewBid[0].Slot)
  257. }
  258. func TestAuctionGetSlotNumber(t *testing.T) {
  259. slotConst := 4
  260. blockNum := int(BLOCKSPERSLOT)*slotConst + genesisBlock
  261. slot, err := auctionClient.AuctionGetSlotNumber(int64(blockNum))
  262. require.Nil(t, err)
  263. assert.Equal(t, slot, big.NewInt(int64(slotConst)))
  264. }
  265. func TestAuctionCanForge(t *testing.T) {
  266. slotConst := 4
  267. blockNum := int(BLOCKSPERSLOT)*slotConst + genesisBlock
  268. canForge, err := auctionClient.AuctionCanForge(governanceAddressConst, int64(blockNum))
  269. require.Nil(t, err)
  270. assert.Equal(t, canForge, true)
  271. }
  272. func TestAuctionMultiBid(t *testing.T) {
  273. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  274. require.Nil(t, err)
  275. slotSet := [6]bool{true, false, true, false, true, false}
  276. maxBid := new(big.Int)
  277. maxBid.SetString("15000000000000000000", 10)
  278. minBid := new(big.Int)
  279. minBid.SetString("11000000000000000000", 10)
  280. budget := new(big.Int)
  281. budget.SetString("45200000000000000000", 10)
  282. forgerAddress := governanceAddressConst
  283. _, err = auctionClient.AuctionMultiBid(currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, budget, forgerAddress)
  284. require.Nil(t, err)
  285. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  286. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  287. assert.Equal(t, forgerAddress, auctionEvents.NewBid[0].CoordinatorForger)
  288. assert.Equal(t, currentSlot+4, auctionEvents.NewBid[0].Slot)
  289. assert.Equal(t, forgerAddress, auctionEvents.NewBid[1].CoordinatorForger)
  290. assert.Equal(t, currentSlot+6, auctionEvents.NewBid[1].Slot)
  291. assert.Equal(t, forgerAddress, auctionEvents.NewBid[2].CoordinatorForger)
  292. assert.Equal(t, currentSlot+8, auctionEvents.NewBid[2].Slot)
  293. assert.Equal(t, forgerAddress, auctionEvents.NewBid[3].CoordinatorForger)
  294. assert.Equal(t, currentSlot+10, auctionEvents.NewBid[3].Slot)
  295. }
  296. func TestAuctionGetClaimableHEZ2(t *testing.T) {
  297. forgerAddress := governanceAddressConst
  298. amount := new(big.Int)
  299. amount.SetString("11000000000000000000", 10)
  300. claimableHEZ, err := auctionClient.AuctionGetClaimableHEZ(forgerAddress)
  301. require.Nil(t, err)
  302. assert.Equal(t, claimableHEZ, amount)
  303. }
  304. func TestAuctionClaimHEZ(t *testing.T) {
  305. amount := new(big.Int)
  306. amount.SetString("11000000000000000000", 10)
  307. _, err := auctionClient.AuctionClaimHEZ(governanceAddressConst)
  308. require.Nil(t, err)
  309. header, _ := auctionClient.client.client.HeaderByNumber(context.Background(), nil)
  310. auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(header.Number.Int64())
  311. assert.Equal(t, amount, auctionEvents.HEZClaimed[0].Amount)
  312. assert.Equal(t, governanceAddressConst, auctionEvents.HEZClaimed[0].Owner)
  313. }