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.

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