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.

189 lines
8.3 KiB

  1. package eth
  2. import (
  3. "math/big"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. var wdelayerClient *WDelayerClient
  10. // var wdelayerClientKep *WDelayerClient
  11. var initWithdrawalDelay = big.NewInt(60)
  12. var newWithdrawalDelay = big.NewInt(79)
  13. var maxEmergencyModeTime = time.Hour * 24 * 7 * 26
  14. func TestWDelayerGetHermezGovernanceDAOAddress(t *testing.T) {
  15. governanceAddress, err := wdelayerClient.WDelayerGetHermezGovernanceDAOAddress()
  16. require.Nil(t, err)
  17. assert.Equal(t, &hermezGovernanceDAOAddressConst, governanceAddress)
  18. }
  19. func TestWDelayerSetHermezGovernanceDAOAddress(t *testing.T) {
  20. wdelayerClientGov, err := NewWDelayerClient(ethereumClientGovDAO, wdelayerAddressConst)
  21. require.Nil(t, err)
  22. _, err = wdelayerClientGov.WDelayerSetHermezGovernanceDAOAddress(auxAddressConst)
  23. require.Nil(t, err)
  24. auxAddress, err := wdelayerClient.WDelayerGetHermezGovernanceDAOAddress()
  25. require.Nil(t, err)
  26. assert.Equal(t, &auxAddressConst, auxAddress)
  27. currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
  28. wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
  29. assert.Equal(t, auxAddressConst, wdelayerEvents.NewHermezGovernanceDAOAddress[0].NewHermezGovernanceDAOAddress)
  30. wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerAddressConst)
  31. require.Nil(t, err)
  32. _, err = wdelayerClientAux.WDelayerSetHermezGovernanceDAOAddress(hermezGovernanceDAOAddressConst)
  33. require.Nil(t, err)
  34. }
  35. func TestWDelayerGetHermezKeeperAddress(t *testing.T) {
  36. keeperAddress, err := wdelayerClient.WDelayerGetHermezKeeperAddress()
  37. require.Nil(t, err)
  38. assert.Equal(t, &hermezKeeperAddressConst, keeperAddress)
  39. }
  40. func TestWDelayerSetHermezKeeperAddress(t *testing.T) {
  41. wdelayerClientKep, err := NewWDelayerClient(ethereumClientKep, wdelayerAddressConst)
  42. require.Nil(t, err)
  43. _, err = wdelayerClientKep.WDelayerSetHermezKeeperAddress(auxAddressConst)
  44. require.Nil(t, err)
  45. auxAddress, err := wdelayerClient.WDelayerGetHermezKeeperAddress()
  46. require.Nil(t, err)
  47. assert.Equal(t, &auxAddressConst, auxAddress)
  48. currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
  49. wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
  50. assert.Equal(t, auxAddressConst, wdelayerEvents.NewHermezKeeperAddress[0].NewHermezKeeperAddress)
  51. wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerAddressConst)
  52. require.Nil(t, err)
  53. _, err = wdelayerClientAux.WDelayerSetHermezKeeperAddress(hermezKeeperAddressConst)
  54. require.Nil(t, err)
  55. }
  56. func TestWDelayerGetWhiteHackGroupAddress(t *testing.T) {
  57. whiteHackGroupAddress, err := wdelayerClient.WDelayerGetWhiteHackGroupAddress()
  58. require.Nil(t, err)
  59. assert.Equal(t, &whiteHackGroupAddressConst, whiteHackGroupAddress)
  60. }
  61. func TestWDelayerSetWhiteHackGroupAddress(t *testing.T) {
  62. wdelayerClientWhite, err := NewWDelayerClient(ethereumClientWhite, wdelayerAddressConst)
  63. require.Nil(t, err)
  64. _, err = wdelayerClientWhite.WDelayerSetWhiteHackGroupAddress(auxAddressConst)
  65. require.Nil(t, err)
  66. auxAddress, err := wdelayerClient.WDelayerGetWhiteHackGroupAddress()
  67. require.Nil(t, err)
  68. assert.Equal(t, &auxAddressConst, auxAddress)
  69. currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
  70. wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
  71. assert.Equal(t, auxAddressConst, wdelayerEvents.NewWhiteHackGroupAddress[0].NewWhiteHackGroupAddress)
  72. wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerAddressConst)
  73. require.Nil(t, err)
  74. _, err = wdelayerClientAux.WDelayerSetWhiteHackGroupAddress(whiteHackGroupAddressConst)
  75. require.Nil(t, err)
  76. }
  77. func TestWDelayerIsEmergencyMode(t *testing.T) {
  78. emergencyMode, err := wdelayerClient.WDelayerIsEmergencyMode()
  79. require.Nil(t, err)
  80. assert.Equal(t, false, emergencyMode)
  81. }
  82. func TestWDelayerGetWithdrawalDelay(t *testing.T) {
  83. withdrawalDelay, err := wdelayerClient.WDelayerGetWithdrawalDelay()
  84. require.Nil(t, err)
  85. assert.Equal(t, initWithdrawalDelay, withdrawalDelay)
  86. }
  87. func TestWDelayerChangeWithdrawalDelay(t *testing.T) {
  88. wdelayerClientKep, err := NewWDelayerClient(ethereumClientKep, wdelayerAddressConst)
  89. require.Nil(t, err)
  90. _, err = wdelayerClientKep.WDelayerChangeWithdrawalDelay(newWithdrawalDelay.Uint64())
  91. require.Nil(t, err)
  92. withdrawalDelay, err := wdelayerClient.WDelayerGetWithdrawalDelay()
  93. require.Nil(t, err)
  94. assert.Equal(t, newWithdrawalDelay, withdrawalDelay)
  95. currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
  96. wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
  97. assert.Equal(t, newWithdrawalDelay.Uint64(), wdelayerEvents.NewWithdrawalDelay[0].WithdrawalDelay)
  98. }
  99. func TestWDelayerDeposit(t *testing.T) {
  100. amount := new(big.Int)
  101. amount.SetString("1100000000000000000", 10)
  102. wdelayerClientHermez, err := NewWDelayerClient(ethereumClientHermez, wdelayerAddressConst)
  103. require.Nil(t, err)
  104. _, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenHezAddressConst, amount)
  105. require.Nil(t, err)
  106. currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
  107. wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
  108. assert.Equal(t, amount, wdelayerEvents.Deposit[0].Amount)
  109. assert.Equal(t, auxAddressConst, wdelayerEvents.Deposit[0].Owner)
  110. assert.Equal(t, tokenHezAddressConst, wdelayerEvents.Deposit[0].Token)
  111. }
  112. func TestWDelayerDepositInfo(t *testing.T) {
  113. amount := new(big.Int)
  114. amount.SetString("1100000000000000000", 10)
  115. state, err := wdelayerClient.WDelayerDepositInfo(auxAddressConst, tokenHezAddressConst)
  116. require.Nil(t, err)
  117. assert.Equal(t, state.Amount, amount)
  118. }
  119. func TestWDelayerWithdrawal(t *testing.T) {
  120. amount := new(big.Int)
  121. amount.SetString("1100000000000000000", 10)
  122. _, err := wdelayerClient.WDelayerWithdrawal(auxAddressConst, tokenHezAddressConst)
  123. require.Contains(t, err.Error(), "Withdrawal not allowed yet")
  124. addBlocks(newWithdrawalDelay.Int64(), ethClientDialURL)
  125. _, err = wdelayerClient.WDelayerWithdrawal(auxAddressConst, tokenHezAddressConst)
  126. require.Nil(t, err)
  127. currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
  128. wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
  129. assert.Equal(t, amount, wdelayerEvents.Withdraw[0].Amount)
  130. assert.Equal(t, auxAddressConst, wdelayerEvents.Withdraw[0].Owner)
  131. assert.Equal(t, tokenHezAddressConst, wdelayerEvents.Withdraw[0].Token)
  132. }
  133. func TestWDelayerEnableEmergencyMode(t *testing.T) {
  134. wdelayerClientKep, err := NewWDelayerClient(ethereumClientKep, wdelayerAddressConst)
  135. require.Nil(t, err)
  136. _, err = wdelayerClientKep.WDelayerEnableEmergencyMode()
  137. require.Nil(t, err)
  138. emergencyMode, err := wdelayerClient.WDelayerIsEmergencyMode()
  139. require.Nil(t, err)
  140. assert.Equal(t, true, emergencyMode)
  141. currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
  142. wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
  143. auxEvent := new(WDelayerEventEmergencyModeEnabled)
  144. assert.Equal(t, auxEvent, &wdelayerEvents.EmergencyModeEnabled[0])
  145. }
  146. func TestWDelayerGetEmergencyModeStartingTime(t *testing.T) {
  147. emergencyModeStartingTime, err := wdelayerClient.WDelayerGetEmergencyModeStartingTime()
  148. require.Nil(t, err)
  149. // `emergencyModeStartingTime` is initialized to 0 in the smart
  150. // contract construction. Since we called WDelayerEnableEmergencyMode
  151. // previously, `emergencyModeStartingTime` is set to the time when the
  152. // call was made, so it's > 0.
  153. assert.True(t, emergencyModeStartingTime.Cmp(big.NewInt(0)) == 1)
  154. }
  155. func TestWDelayerEscapeHatchWithdrawal(t *testing.T) {
  156. wdelayerClientWhite, err := NewWDelayerClient(ethereumClientWhite, wdelayerAddressConst)
  157. require.Nil(t, err)
  158. _, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenHezAddressConst)
  159. require.Contains(t, err.Error(), "NO MAX_EMERGENCY_MODE_TIME")
  160. seconds := maxEmergencyModeTime.Seconds()
  161. addTime(seconds, ethClientDialURL)
  162. _, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenHezAddressConst)
  163. require.Nil(t, err)
  164. currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
  165. wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
  166. assert.Equal(t, tokenHezAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Token)
  167. assert.Equal(t, governanceAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].To)
  168. assert.Equal(t, whiteHackGroupAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Who)
  169. }