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.

409 lines
14 KiB

  1. package eth
  2. import (
  3. "fmt"
  4. "math/big"
  5. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/core/types"
  8. "github.com/ethereum/go-ethereum/ethclient"
  9. WithdrawalDelayer "github.com/hermeznetwork/hermez-node/eth/contracts/withdrawdelayer"
  10. )
  11. // WDelayerConstants are the constants of the Rollup Smart Contract
  12. type WDelayerConstants struct {
  13. // Max Withdrawal Delay
  14. MaxWithdrawalDelay uint64
  15. // Max Emergency mode time
  16. MaxEmergencyModeTime uint64
  17. // HermezRollup smartcontract address
  18. HermezRollup ethCommon.Address
  19. }
  20. // DepositState is the state of Deposit
  21. type DepositState struct {
  22. Amount *big.Int
  23. DepositTimestamp uint64
  24. }
  25. // WDelayerEventDeposit is an event of the WithdrawalDelayer Smart Contract
  26. type WDelayerEventDeposit struct {
  27. Owner ethCommon.Address
  28. Token ethCommon.Address
  29. Amount *big.Int
  30. DepositTimestamp uint64
  31. }
  32. // WDelayerEventWithdraw is an event of the WithdrawalDelayer Smart Contract
  33. type WDelayerEventWithdraw struct {
  34. Owner ethCommon.Address
  35. Token ethCommon.Address
  36. Amount *big.Int
  37. }
  38. // WDelayerEventEmergencyModeEnabled an event of the WithdrawalDelayer Smart Contract
  39. type WDelayerEventEmergencyModeEnabled struct {
  40. }
  41. // WDelayerEventNewWithdrawalDelay an event of the WithdrawalDelayer Smart Contract
  42. type WDelayerEventNewWithdrawalDelay struct {
  43. WithdrawalDelay uint64
  44. }
  45. // WDelayerEventEscapeHatchWithdrawal an event of the WithdrawalDelayer Smart Contract
  46. type WDelayerEventEscapeHatchWithdrawal struct {
  47. Who ethCommon.Address
  48. To ethCommon.Address
  49. Token ethCommon.Address
  50. }
  51. // WDelayerEventNewHermezKeeperAddress an event of the WithdrawalDelayer Smart Contract
  52. type WDelayerEventNewHermezKeeperAddress struct {
  53. NewHermezKeeperAddress ethCommon.Address
  54. }
  55. // WDelayerEventNewWhiteHackGroupAddress an event of the WithdrawalDelayer Smart Contract
  56. type WDelayerEventNewWhiteHackGroupAddress struct {
  57. NewWhiteHackGroupAddress ethCommon.Address
  58. }
  59. // WDelayerEventNewHermezGovernanceDAOAddress an event of the WithdrawalDelayer Smart Contract
  60. type WDelayerEventNewHermezGovernanceDAOAddress struct {
  61. NewHermezGovernanceDAOAddress ethCommon.Address
  62. }
  63. // WDelayerEvents is the lis of events in a block of the WithdrawalDelayer Smart Contract
  64. type WDelayerEvents struct { //nolint:structcheck
  65. Deposit []WDelayerEventDeposit
  66. Withdraw []WDelayerEventWithdraw
  67. EmergencyModeEnabled []WDelayerEventEmergencyModeEnabled
  68. NewWithdrawalDelay []WDelayerEventNewWithdrawalDelay
  69. EscapeHatchWithdrawal []WDelayerEventEscapeHatchWithdrawal
  70. NewHermezKeeperAddress []WDelayerEventNewHermezKeeperAddress
  71. NewWhiteHackGroupAddress []WDelayerEventNewWhiteHackGroupAddress
  72. NewHermezGovernanceDAOAddress []WDelayerEventNewHermezGovernanceDAOAddress
  73. }
  74. // NewWDelayerEvents creates an empty WDelayerEvents with the slices initialized.
  75. func NewWDelayerEvents() WDelayerEvents {
  76. return WDelayerEvents{
  77. Deposit: make([]WDelayerEventDeposit, 0),
  78. Withdraw: make([]WDelayerEventWithdraw, 0),
  79. EmergencyModeEnabled: make([]WDelayerEventEmergencyModeEnabled, 0),
  80. NewWithdrawalDelay: make([]WDelayerEventNewWithdrawalDelay, 0),
  81. EscapeHatchWithdrawal: make([]WDelayerEventEscapeHatchWithdrawal, 0),
  82. NewHermezKeeperAddress: make([]WDelayerEventNewHermezKeeperAddress, 0),
  83. NewWhiteHackGroupAddress: make([]WDelayerEventNewWhiteHackGroupAddress, 0),
  84. NewHermezGovernanceDAOAddress: make([]WDelayerEventNewHermezGovernanceDAOAddress, 0),
  85. }
  86. }
  87. // WDelayerInterface is the inteface to WithdrawalDelayer Smart Contract
  88. type WDelayerInterface interface {
  89. //
  90. // Smart Contract Methods
  91. //
  92. WDelayerGetHermezGovernanceDAOAddress() (*ethCommon.Address, error)
  93. WDelayerSetHermezGovernanceDAOAddress(newAddress ethCommon.Address) (*types.Transaction, error)
  94. WDelayerGetHermezKeeperAddress() (*ethCommon.Address, error)
  95. WDelayerSetHermezKeeperAddress(newAddress ethCommon.Address) (*types.Transaction, error)
  96. WDelayerGetWhiteHackGroupAddress() (*ethCommon.Address, error)
  97. WDelayerSetWhiteHackGroupAddress(newAddress ethCommon.Address) (*types.Transaction, error)
  98. WDelayerIsEmergencyMode() (bool, error)
  99. WDelayerGetWithdrawalDelay() (*big.Int, error)
  100. WDelayerGetEmergencyModeStartingTime() (*big.Int, error)
  101. WDelayerEnableEmergencyMode() (*types.Transaction, error)
  102. WDelayerChangeWithdrawalDelay(newWithdrawalDelay uint64) (*types.Transaction, error)
  103. WDelayerDepositInfo(owner, token ethCommon.Address) (*big.Int, uint64)
  104. WDelayerDeposit(onwer, token ethCommon.Address, amount *big.Int) (*types.Transaction, error)
  105. WDelayerWithdrawal(owner, token ethCommon.Address) (*types.Transaction, error)
  106. WDelayerEscapeHatchWithdrawal(to, token ethCommon.Address) (*types.Transaction, error)
  107. }
  108. //
  109. // Implementation
  110. //
  111. // WDelayerClient is the implementation of the interface to the WithdrawDelayer Smart Contract in ethereum.
  112. type WDelayerClient struct {
  113. client *EthereumClient
  114. address ethCommon.Address
  115. gasLimit uint64
  116. }
  117. // NewWDelayerClient creates a new WDelayerClient
  118. func NewWDelayerClient(client *EthereumClient, address ethCommon.Address) *WDelayerClient {
  119. return &WDelayerClient{
  120. client: client,
  121. address: address,
  122. gasLimit: 1000000, //nolint:gomnd
  123. }
  124. }
  125. // WDelayerGetHermezGovernanceDAOAddress is the interface to call the smart contract function
  126. func (c *WDelayerClient) WDelayerGetHermezGovernanceDAOAddress() (*ethCommon.Address, error) {
  127. var hermezGovernanceDAOAddress ethCommon.Address
  128. if err := c.client.Call(func(ec *ethclient.Client) error {
  129. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  130. if err != nil {
  131. return err
  132. }
  133. hermezGovernanceDAOAddress, err = wdelayer.GetHermezGovernanceDAOAddress(nil)
  134. return err
  135. }); err != nil {
  136. return nil, err
  137. }
  138. return &hermezGovernanceDAOAddress, nil
  139. }
  140. // WDelayerSetHermezGovernanceDAOAddress is the interface to call the smart contract function
  141. func (c *WDelayerClient) WDelayerSetHermezGovernanceDAOAddress(newAddress ethCommon.Address) (*types.Transaction, error) {
  142. var tx *types.Transaction
  143. var err error
  144. if tx, err = c.client.CallAuth(
  145. c.gasLimit,
  146. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  147. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  148. if err != nil {
  149. return nil, err
  150. }
  151. return wdelayer.SetHermezGovernanceDAOAddress(auth, newAddress)
  152. },
  153. ); err != nil {
  154. return nil, fmt.Errorf("Failed setting hermezGovernanceDAOAddress: %w", err)
  155. }
  156. return tx, nil
  157. }
  158. // WDelayerGetHermezKeeperAddress is the interface to call the smart contract function
  159. func (c *WDelayerClient) WDelayerGetHermezKeeperAddress() (*ethCommon.Address, error) {
  160. var hermezKeeperAddress ethCommon.Address
  161. if err := c.client.Call(func(ec *ethclient.Client) error {
  162. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  163. if err != nil {
  164. return err
  165. }
  166. hermezKeeperAddress, err = wdelayer.GetHermezKeeperAddress(nil)
  167. return err
  168. }); err != nil {
  169. return nil, err
  170. }
  171. return &hermezKeeperAddress, nil
  172. }
  173. // WDelayerSetHermezKeeperAddress is the interface to call the smart contract function
  174. func (c *WDelayerClient) WDelayerSetHermezKeeperAddress(newAddress ethCommon.Address) (*types.Transaction, error) {
  175. var tx *types.Transaction
  176. var err error
  177. if tx, err = c.client.CallAuth(
  178. c.gasLimit,
  179. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  180. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  181. if err != nil {
  182. return nil, err
  183. }
  184. return wdelayer.SetHermezKeeperAddress(auth, newAddress)
  185. },
  186. ); err != nil {
  187. return nil, fmt.Errorf("Failed setting hermezKeeperAddress: %w", err)
  188. }
  189. return tx, nil
  190. }
  191. // WDelayerGetWhiteHackGroupAddress is the interface to call the smart contract function
  192. func (c *WDelayerClient) WDelayerGetWhiteHackGroupAddress() (*ethCommon.Address, error) {
  193. var whiteHackGroupAddress ethCommon.Address
  194. if err := c.client.Call(func(ec *ethclient.Client) error {
  195. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  196. if err != nil {
  197. return err
  198. }
  199. whiteHackGroupAddress, err = wdelayer.GetWhiteHackGroupAddress(nil)
  200. return err
  201. }); err != nil {
  202. return nil, err
  203. }
  204. return &whiteHackGroupAddress, nil
  205. }
  206. // WDelayerSetWhiteHackGroupAddress is the interface to call the smart contract function
  207. func (c *WDelayerClient) WDelayerSetWhiteHackGroupAddress(newAddress ethCommon.Address) (*types.Transaction, error) {
  208. var tx *types.Transaction
  209. var err error
  210. if tx, err = c.client.CallAuth(
  211. c.gasLimit,
  212. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  213. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return wdelayer.SetWhiteHackGroupAddress(auth, newAddress)
  218. },
  219. ); err != nil {
  220. return nil, fmt.Errorf("Failed setting whiteHackGroupAddress: %w", err)
  221. }
  222. return tx, nil
  223. }
  224. // WDelayerIsEmergencyMode is the interface to call the smart contract function
  225. func (c *WDelayerClient) WDelayerIsEmergencyMode() (bool, error) {
  226. var ermergencyMode bool
  227. if err := c.client.Call(func(ec *ethclient.Client) error {
  228. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  229. if err != nil {
  230. return err
  231. }
  232. ermergencyMode, err = wdelayer.IsEmergencyMode(nil)
  233. return err
  234. }); err != nil {
  235. return false, err
  236. }
  237. return ermergencyMode, nil
  238. }
  239. // WDelayerGetWithdrawalDelay is the interface to call the smart contract function
  240. func (c *WDelayerClient) WDelayerGetWithdrawalDelay() (*big.Int, error) {
  241. var withdrawalDelay *big.Int
  242. if err := c.client.Call(func(ec *ethclient.Client) error {
  243. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  244. if err != nil {
  245. return err
  246. }
  247. withdrawalDelay, err = wdelayer.GetWithdrawalDelay(nil)
  248. return err
  249. }); err != nil {
  250. return nil, err
  251. }
  252. return withdrawalDelay, nil
  253. }
  254. // WDelayerGetEmergencyModeStartingTime is the interface to call the smart contract function
  255. func (c *WDelayerClient) WDelayerGetEmergencyModeStartingTime() (*big.Int, error) {
  256. var emergencyModeStartingTime *big.Int
  257. if err := c.client.Call(func(ec *ethclient.Client) error {
  258. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  259. if err != nil {
  260. return err
  261. }
  262. emergencyModeStartingTime, err = wdelayer.GetEmergencyModeStartingTime(nil)
  263. return err
  264. }); err != nil {
  265. return nil, err
  266. }
  267. return emergencyModeStartingTime, nil
  268. }
  269. // WDelayerEnableEmergencyMode is the interface to call the smart contract function
  270. func (c *WDelayerClient) WDelayerEnableEmergencyMode() (*types.Transaction, error) {
  271. var tx *types.Transaction
  272. var err error
  273. if tx, err = c.client.CallAuth(
  274. c.gasLimit,
  275. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  276. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  277. if err != nil {
  278. return nil, err
  279. }
  280. return wdelayer.EnableEmergencyMode(auth)
  281. },
  282. ); err != nil {
  283. return nil, fmt.Errorf("Failed setting enable emergency mode: %w", err)
  284. }
  285. return tx, nil
  286. }
  287. // WDelayerChangeWithdrawalDelay is the interface to call the smart contract function
  288. func (c *WDelayerClient) WDelayerChangeWithdrawalDelay(newWithdrawalDelay uint64) (*types.Transaction, error) {
  289. var tx *types.Transaction
  290. var err error
  291. if tx, err = c.client.CallAuth(
  292. c.gasLimit,
  293. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  294. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  295. if err != nil {
  296. return nil, err
  297. }
  298. return wdelayer.ChangeWithdrawalDelay(auth, newWithdrawalDelay)
  299. },
  300. ); err != nil {
  301. return nil, fmt.Errorf("Failed setting withdrawal delay: %w", err)
  302. }
  303. return tx, nil
  304. }
  305. // WDelayerDepositInfo is the interface to call the smart contract function
  306. func (c *WDelayerClient) WDelayerDepositInfo(owner, token ethCommon.Address) (DepositState, error) {
  307. var depositInfo DepositState
  308. if err := c.client.Call(func(ec *ethclient.Client) error {
  309. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  310. if err != nil {
  311. return err
  312. }
  313. amount, depositTimestamp, err := wdelayer.DepositInfo(nil, owner, token)
  314. depositInfo.Amount = amount
  315. depositInfo.DepositTimestamp = depositTimestamp
  316. return err
  317. }); err != nil {
  318. return depositInfo, err
  319. }
  320. return depositInfo, nil
  321. }
  322. // WDelayerDeposit is the interface to call the smart contract function
  323. func (c *WDelayerClient) WDelayerDeposit(owner, token ethCommon.Address, amount *big.Int) (*types.Transaction, error) {
  324. var tx *types.Transaction
  325. var err error
  326. if tx, err = c.client.CallAuth(
  327. c.gasLimit,
  328. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  329. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  330. if err != nil {
  331. return nil, err
  332. }
  333. return wdelayer.Deposit(auth, owner, token, amount)
  334. },
  335. ); err != nil {
  336. return nil, fmt.Errorf("Failed deposit: %w", err)
  337. }
  338. return tx, nil
  339. }
  340. // WDelayerWithdrawal is the interface to call the smart contract function
  341. func (c *WDelayerClient) WDelayerWithdrawal(owner, token ethCommon.Address) (*types.Transaction, error) {
  342. var tx *types.Transaction
  343. var err error
  344. if tx, err = c.client.CallAuth(
  345. c.gasLimit,
  346. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  347. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  348. if err != nil {
  349. return nil, err
  350. }
  351. return wdelayer.Withdrawal(auth, owner, token)
  352. },
  353. ); err != nil {
  354. return nil, fmt.Errorf("Failed withdrawal: %w", err)
  355. }
  356. return tx, nil
  357. }
  358. // WDelayerEscapeHatchWithdrawal is the interface to call the smart contract function
  359. func (c *WDelayerClient) WDelayerEscapeHatchWithdrawal(to, token ethCommon.Address) (*types.Transaction, error) {
  360. var tx *types.Transaction
  361. var err error
  362. if tx, err = c.client.CallAuth(
  363. c.gasLimit,
  364. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  365. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(c.address, ec)
  366. if err != nil {
  367. return nil, err
  368. }
  369. return wdelayer.EscapeHatchWithdrawal(auth, to, token)
  370. },
  371. ); err != nil {
  372. return nil, fmt.Errorf("Failed escapeHatchWithdrawal: %w", err)
  373. }
  374. return tx, nil
  375. }