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.

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