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.

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