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.

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