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.

312 lines
11 KiB

  1. package eth
  2. import (
  3. "math/big"
  4. "os"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/accounts/keystore"
  7. "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/crypto"
  9. "github.com/ethereum/go-ethereum/ethclient"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. const slotDeadlineConst = uint8(20)
  14. const openAuctionSlotsConst = uint16(4320)
  15. const closedAuctionSlotsConst = uint16(2)
  16. const outbiddingConst = uint16(1000)
  17. const currentSlotConst = 0
  18. var allocationRatioConst [3]uint16 = [3]uint16{4000, 4000, 2000}
  19. var auctionClient *AuctionClient
  20. /*var donationAddressStr = os.Getenv("DONATION_ADDRESS")
  21. var bootCoordinatorStr = os.Getenv("BOOT_COORDINATOR_ADDRESS")
  22. var ehtClientDialURL = os.Getenv("ETHCLIENT_DIAL_URL")
  23. var auctionAddressStr = os.Getenv("AUCTION_ADDRESS")*/
  24. var integration = os.Getenv("INTEGRATION")
  25. var donationAddressStr = "0x6c365935CA8710200C7595F0a72EB6023A7706Cd"
  26. var bootCoordinatorStr = "0xc783df8a850f42e7f7e57013759c285caa701eb6"
  27. var DONATION = common.HexToAddress(donationAddressStr)
  28. var BOOTCOORDINATOR = common.HexToAddress(bootCoordinatorStr)
  29. var ehtClientDialURL = "http://localhost:8545"
  30. var auctionAddressStr = "0x3619DbE27d7c1e7E91aA738697Ae7Bc5FC3eACA5"
  31. var ownerAddressStr = "0xc783df8a850f42e7F7e57013759C285caa701eB6"
  32. var governanceAddressStr = "0xead9c93b79ae7c1591b1fb5323bd777e86e150d4"
  33. var governancePrivateKey = "d49743deccbccc5dc7baa8e69e5be03298da8688a15dd202e20f15d5e0e9a9fb"
  34. var minBidStr = "10000000000000000000"
  35. var URL = "http://localhost:3000"
  36. var newURL = "http://localhost:3002"
  37. var BLOCKSPERSLOT = uint8(40)
  38. var TOKENHEZ = common.HexToAddress("0xf4e77E5Da47AC3125140c470c71cBca77B5c638c")
  39. var HERMEZROLLUP = common.HexToAddress("0xc4905364b78a742ccce7B890A89514061E47068D")
  40. var pathKs = "test/ks"
  41. var password = "pass"
  42. func TestNewAction(t *testing.T) {
  43. key, err := crypto.HexToECDSA(governancePrivateKey)
  44. require.Nil(t, err)
  45. ks := keystore.NewKeyStore(pathKs, keystore.StandardScryptN, keystore.StandardScryptP)
  46. account, err := ks.ImportECDSA(key, password)
  47. ks.Unlock(account, password)
  48. // Init eth client
  49. ethClient, err := ethclient.Dial(ehtClientDialURL)
  50. require.Nil(t, err)
  51. ethereumClient := NewEthereumClient(ethClient, &account, ks, nil)
  52. auctionAddress := common.HexToAddress(auctionAddressStr)
  53. if integration != "" {
  54. auctionClient = NewAuctionClient(ethereumClient, auctionAddress)
  55. }
  56. }
  57. func TestAuctionGetCurrentSlotNumber(t *testing.T) {
  58. if auctionClient != nil {
  59. currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
  60. require.Nil(t, err)
  61. currentSlotInt := int(currentSlot)
  62. assert.Equal(t, currentSlotConst, currentSlotInt)
  63. }
  64. }
  65. func TestAuctionConstants(t *testing.T) {
  66. INITMINBID := new(big.Int)
  67. INITMINBID.SetString(minBidStr, 10)
  68. if auctionClient != nil {
  69. auctionConstants, err := auctionClient.AuctionConstants()
  70. require.Nil(t, err)
  71. assert.Equal(t, auctionConstants.BlocksPerSlot, BLOCKSPERSLOT)
  72. // assert.Equal(t, auctionConstants.GenesisBlockNum, GENESISBLOCKNUM)
  73. assert.Equal(t, auctionConstants.HermezRollup, HERMEZROLLUP)
  74. assert.Equal(t, auctionConstants.InitialMinimalBidding, INITMINBID)
  75. assert.Equal(t, auctionConstants.TokenHEZ, TOKENHEZ)
  76. }
  77. }
  78. func TestAuctionVariables(t *testing.T) {
  79. INITMINBID := new(big.Int)
  80. INITMINBID.SetString(minBidStr, 10)
  81. defaultSlotSetBid := [6]*big.Int{INITMINBID, INITMINBID, INITMINBID, INITMINBID, INITMINBID, INITMINBID}
  82. if auctionClient != nil {
  83. auctionVariables, err := auctionClient.AuctionVariables()
  84. require.Nil(t, err)
  85. assert.Equal(t, auctionVariables.AllocationRatio, allocationRatioConst)
  86. assert.Equal(t, auctionVariables.BootCoordinator, &BOOTCOORDINATOR)
  87. assert.Equal(t, auctionVariables.ClosedAuctionSlots, closedAuctionSlotsConst)
  88. assert.Equal(t, auctionVariables.DefaultSlotSetBid, defaultSlotSetBid)
  89. assert.Equal(t, auctionVariables.DonationAddress, &DONATION)
  90. assert.Equal(t, auctionVariables.OpenAuctionSlots, openAuctionSlotsConst)
  91. assert.Equal(t, auctionVariables.Outbidding, outbiddingConst)
  92. assert.Equal(t, auctionVariables.SlotDeadline, slotDeadlineConst)
  93. }
  94. }
  95. func TestAuctionGetSlotDeadline(t *testing.T) {
  96. if auctionClient != nil {
  97. slotDeadline, err := auctionClient.AuctionGetSlotDeadline()
  98. require.Nil(t, err)
  99. assert.Equal(t, slotDeadlineConst, slotDeadline)
  100. }
  101. }
  102. func TestAuctionSetSlotDeadline(t *testing.T) {
  103. newSlotDeadline := uint8(25)
  104. if auctionClient != nil {
  105. _, err := auctionClient.AuctionSetSlotDeadline(newSlotDeadline)
  106. require.Nil(t, err)
  107. slotDeadline, err := auctionClient.AuctionGetSlotDeadline()
  108. assert.Equal(t, newSlotDeadline, slotDeadline)
  109. _, err = auctionClient.AuctionSetSlotDeadline(slotDeadlineConst)
  110. require.Nil(t, err)
  111. }
  112. }
  113. func TestAuctionGetOpenAuctionSlots(t *testing.T) {
  114. if auctionClient != nil {
  115. openAuctionSlots, err := auctionClient.AuctionGetOpenAuctionSlots()
  116. require.Nil(t, err)
  117. assert.Equal(t, openAuctionSlotsConst, openAuctionSlots)
  118. }
  119. }
  120. func TestAuctionSetOpenAuctionSlots(t *testing.T) {
  121. newOpenAuctionSlots := uint16(4500)
  122. if auctionClient != nil {
  123. _, err := auctionClient.AuctionSetOpenAuctionSlots(newOpenAuctionSlots)
  124. require.Nil(t, err)
  125. openAuctionSlots, err := auctionClient.AuctionGetOpenAuctionSlots()
  126. assert.Equal(t, newOpenAuctionSlots, openAuctionSlots)
  127. _, err = auctionClient.AuctionSetOpenAuctionSlots(openAuctionSlotsConst)
  128. require.Nil(t, err)
  129. }
  130. }
  131. func TestAuctionGetClosedAuctionSlots(t *testing.T) {
  132. if auctionClient != nil {
  133. closedAuctionSlots, err := auctionClient.AuctionGetClosedAuctionSlots()
  134. require.Nil(t, err)
  135. assert.Equal(t, closedAuctionSlotsConst, closedAuctionSlots)
  136. }
  137. }
  138. func TestAuctionSetClosedAuctionSlots(t *testing.T) {
  139. newClosedAuctionSlots := uint16(5)
  140. if auctionClient != nil {
  141. _, err := auctionClient.AuctionSetClosedAuctionSlots(newClosedAuctionSlots)
  142. require.Nil(t, err)
  143. closedAuctionSlots, err := auctionClient.AuctionGetClosedAuctionSlots()
  144. assert.Equal(t, newClosedAuctionSlots, closedAuctionSlots)
  145. _, err = auctionClient.AuctionSetClosedAuctionSlots(closedAuctionSlotsConst)
  146. require.Nil(t, err)
  147. }
  148. }
  149. func TestAuctionGetOutbidding(t *testing.T) {
  150. if auctionClient != nil {
  151. outbidding, err := auctionClient.AuctionGetOutbidding()
  152. require.Nil(t, err)
  153. assert.Equal(t, outbiddingConst, outbidding)
  154. }
  155. }
  156. func TestAuctionSetOutbidding(t *testing.T) {
  157. newOutbidding := uint16(0xb)
  158. if auctionClient != nil {
  159. _, err := auctionClient.AuctionSetOutbidding(newOutbidding)
  160. require.Nil(t, err)
  161. outbidding, err := auctionClient.AuctionGetOutbidding()
  162. assert.Equal(t, newOutbidding, outbidding)
  163. _, err = auctionClient.AuctionSetOutbidding(outbiddingConst)
  164. require.Nil(t, err)
  165. }
  166. }
  167. func TestAuctionGetAllocationRatio(t *testing.T) {
  168. if auctionClient != nil {
  169. allocationRatio, err := auctionClient.AuctionGetAllocationRatio()
  170. require.Nil(t, err)
  171. assert.Equal(t, allocationRatioConst, allocationRatio)
  172. }
  173. }
  174. func TestAuctionSetAllocationRatio(t *testing.T) {
  175. newAllocationRatio := [3]uint16{3000, 3000, 4000}
  176. if auctionClient != nil {
  177. _, err := auctionClient.AuctionSetAllocationRatio(newAllocationRatio)
  178. require.Nil(t, err)
  179. allocationRatio, err := auctionClient.AuctionGetAllocationRatio()
  180. assert.Equal(t, newAllocationRatio, allocationRatio)
  181. _, err = auctionClient.AuctionSetAllocationRatio(allocationRatioConst)
  182. require.Nil(t, err)
  183. }
  184. }
  185. func TestAuctionGetDonationAddress(t *testing.T) {
  186. if auctionClient != nil {
  187. donationAddress, err := auctionClient.AuctionGetDonationAddress()
  188. require.Nil(t, err)
  189. donationAddressConst := common.HexToAddress(donationAddressStr)
  190. assert.Equal(t, &donationAddressConst, donationAddress)
  191. }
  192. }
  193. func TestAuctionGetBootCoordinator(t *testing.T) {
  194. if auctionClient != nil {
  195. bootCoordinator, err := auctionClient.AuctionGetBootCoordinator()
  196. require.Nil(t, err)
  197. bootCoordinatorConst := common.HexToAddress(bootCoordinatorStr)
  198. assert.Equal(t, &bootCoordinatorConst, bootCoordinator)
  199. }
  200. }
  201. func TestAuctionSetDonationAddress(t *testing.T) {
  202. newDonationAddress := common.HexToAddress(governanceAddressStr)
  203. if auctionClient != nil {
  204. _, err := auctionClient.AuctionSetDonationAddress(newDonationAddress)
  205. require.Nil(t, err)
  206. donationAddress, err := auctionClient.AuctionGetDonationAddress()
  207. assert.Equal(t, &newDonationAddress, donationAddress)
  208. donationAddressConst := common.HexToAddress(donationAddressStr)
  209. _, err = auctionClient.AuctionSetDonationAddress(donationAddressConst)
  210. require.Nil(t, err)
  211. }
  212. }
  213. func TestAuctionSetBootCoordinator(t *testing.T) {
  214. newBootCoordinator := common.HexToAddress(governanceAddressStr)
  215. if auctionClient != nil {
  216. _, err := auctionClient.AuctionSetBootCoordinator(newBootCoordinator)
  217. require.Nil(t, err)
  218. bootCoordinator, err := auctionClient.AuctionGetBootCoordinator()
  219. assert.Equal(t, &newBootCoordinator, bootCoordinator)
  220. bootCoordinatorConst := common.HexToAddress(bootCoordinatorStr)
  221. _, err = auctionClient.AuctionSetBootCoordinator(bootCoordinatorConst)
  222. require.Nil(t, err)
  223. }
  224. }
  225. func TestAuctionGetDefaultSlotSetBid(t *testing.T) {
  226. slotSet := uint8(3)
  227. if auctionClient != nil {
  228. minBid, err := auctionClient.AuctionGetDefaultSlotSetBid(slotSet)
  229. require.Nil(t, err)
  230. assert.Equal(t, minBid.String(), minBidStr)
  231. }
  232. }
  233. func TestAuctionChangeEpochMinBid(t *testing.T) {
  234. slotSet := int64(3)
  235. set := uint8(3)
  236. newInitialMinBid := new(big.Int)
  237. newInitialMinBid.SetString("20000000000000000000", 10)
  238. if auctionClient != nil {
  239. _, err := auctionClient.AuctionChangeDefaultSlotSetBid(slotSet, newInitialMinBid)
  240. require.Nil(t, err)
  241. minBid, err := auctionClient.AuctionGetDefaultSlotSetBid(set)
  242. require.Nil(t, err)
  243. assert.Equal(t, minBid, newInitialMinBid)
  244. newMinBid := new(big.Int)
  245. newMinBid.SetString("10000000000000000000", 10)
  246. _, err = auctionClient.AuctionChangeDefaultSlotSetBid(slotSet, newMinBid)
  247. require.Nil(t, err)
  248. }
  249. }
  250. func TestAuctionIsRegisteredCoordinator(t *testing.T) {
  251. forgerAddress := common.HexToAddress(governanceAddressStr)
  252. if auctionClient != nil {
  253. registered, err := auctionClient.AuctionIsRegisteredCoordinator(forgerAddress)
  254. require.Nil(t, err)
  255. assert.Equal(t, registered, false)
  256. }
  257. }
  258. func TestAuctionRegisterCoordinator(t *testing.T) {
  259. forgerAddress := common.HexToAddress(governanceAddressStr)
  260. if auctionClient != nil {
  261. _, err := auctionClient.AuctionRegisterCoordinator(forgerAddress, URL)
  262. require.Nil(t, err)
  263. }
  264. }
  265. func TestAuctionIsRegisteredCoordinatorTrue(t *testing.T) {
  266. forgerAddress := common.HexToAddress(governanceAddressStr)
  267. if auctionClient != nil {
  268. registered, err := auctionClient.AuctionIsRegisteredCoordinator(forgerAddress)
  269. require.Nil(t, err)
  270. assert.Equal(t, registered, true)
  271. }
  272. }
  273. func TestAuctionUpdateCoordinatorInfo(t *testing.T) {
  274. forgerAddress := common.HexToAddress(governanceAddressStr)
  275. if auctionClient != nil {
  276. _, err := auctionClient.AuctionUpdateCoordinatorInfo(forgerAddress, forgerAddress, newURL)
  277. require.Nil(t, err)
  278. }
  279. }