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.

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