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.

374 lines
13 KiB

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