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.

707 lines
24 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. HermezAuctionProtocol "github.com/hermeznetwork/hermez-node/eth/contracts/auction"
  10. "github.com/hermeznetwork/hermez-node/log"
  11. )
  12. // AuctionConstants are the constants of the Rollup Smart Contract
  13. type AuctionConstants struct {
  14. // Blocks to wait before starting with the first slot
  15. DelayGenesis uint16
  16. // Blocks per slot
  17. BlocksPerSlot uint8
  18. // Minimum bid when no one has bid yet
  19. InitialMinimalBidding *big.Int
  20. // First block where the first slot begins
  21. GenesisBlockNum int64
  22. // Hermez Governanze Token smartcontract address who controls some parameters and collects HEZ fee
  23. GovernanceAddress ethCommon.Address
  24. // ERC777 token with which the bids will be made
  25. TokenHEZ ethCommon.Address
  26. // HermezRollup smartcontract address
  27. HermezRollup ethCommon.Address
  28. }
  29. // SlotState is the state of a slot
  30. type SlotState struct {
  31. Forger ethCommon.Address
  32. BidAmount *big.Int
  33. ClosedMinBid *big.Int
  34. Fulfilled bool
  35. }
  36. // NewSlotState returns an empty SlotState
  37. func NewSlotState() *SlotState {
  38. return &SlotState{
  39. Forger: ethCommon.Address{},
  40. BidAmount: big.NewInt(0),
  41. ClosedMinBid: big.NewInt(0),
  42. Fulfilled: false,
  43. }
  44. }
  45. // Coordinator is the details of the Coordinator identified by the forger address
  46. type Coordinator struct {
  47. WithdrawalAddress ethCommon.Address
  48. URL string
  49. }
  50. // AuctionVariables are the variables of the Auction Smart Contract
  51. type AuctionVariables struct {
  52. // Boot Coordinator Address
  53. DonationAddress ethCommon.Address
  54. // Boot Coordinator Address
  55. BootCoordinator ethCommon.Address
  56. // The minimum bid value in a series of 6 slots
  57. DefaultSlotSetBid [6]*big.Int
  58. // Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
  59. ClosedAuctionSlots uint16
  60. // Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
  61. OpenAuctionSlots uint16
  62. // How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
  63. AllocationRatio [3]uint16
  64. // Minimum outbid (percentage) over the previous one to consider it valid
  65. Outbidding uint16
  66. // Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
  67. SlotDeadline uint8
  68. }
  69. // AuctionState represents the state of the Rollup in the Smart Contract
  70. type AuctionState struct {
  71. // Mapping to control slot state
  72. Slots map[int64]*SlotState
  73. // Mapping to control balances pending to claim
  74. PendingBalances map[ethCommon.Address]*big.Int
  75. // Mapping to register all the coordinators. The address used for the mapping is the forger address
  76. Coordinators map[ethCommon.Address]*Coordinator
  77. }
  78. // AuctionEventNewBid is an event of the Auction Smart Contract
  79. type AuctionEventNewBid struct {
  80. Slot int64
  81. BidAmount *big.Int
  82. CoordinatorForger ethCommon.Address
  83. }
  84. // AuctionEventNewSlotDeadline is an event of the Auction Smart Contract
  85. type AuctionEventNewSlotDeadline struct {
  86. NewSlotDeadline uint8
  87. }
  88. // AuctionEventNewClosedAuctionSlots is an event of the Auction Smart Contract
  89. type AuctionEventNewClosedAuctionSlots struct {
  90. NewClosedAuctionSlots uint16
  91. }
  92. // AuctionEventNewOutbidding is an event of the Auction Smart Contract
  93. type AuctionEventNewOutbidding struct {
  94. NewOutbidding uint16
  95. }
  96. // AuctionEventNewDonationAddress is an event of the Auction Smart Contract
  97. type AuctionEventNewDonationAddress struct {
  98. NewDonationAddress ethCommon.Address
  99. }
  100. // AuctionEventNewBootCoordinator is an event of the Auction Smart Contract
  101. type AuctionEventNewBootCoordinator struct {
  102. NewBootCoordinator ethCommon.Address
  103. }
  104. // AuctionEventNewOpenAuctionSlots is an event of the Auction Smart Contract
  105. type AuctionEventNewOpenAuctionSlots struct {
  106. NewOpenAuctionSlots uint16
  107. }
  108. // AuctionEventNewAllocationRatio is an event of the Auction Smart Contract
  109. type AuctionEventNewAllocationRatio struct {
  110. NewAllocationRatio [3]uint16
  111. }
  112. // AuctionEventNewCoordinator is an event of the Auction Smart Contract
  113. type AuctionEventNewCoordinator struct {
  114. ForgerAddress ethCommon.Address
  115. WithdrawalAddress ethCommon.Address
  116. URL string
  117. }
  118. // AuctionEventCoordinatorUpdated is an event of the Auction Smart Contract
  119. type AuctionEventCoordinatorUpdated struct {
  120. ForgerAddress ethCommon.Address
  121. WithdrawalAddress ethCommon.Address
  122. URL string
  123. }
  124. // AuctionEventNewForgeAllocated is an event of the Auction Smart Contract
  125. type AuctionEventNewForgeAllocated struct {
  126. Forger ethCommon.Address
  127. CurrentSlot int64
  128. BurnAmount *big.Int
  129. DonationAmount *big.Int
  130. GovernanceAmount *big.Int
  131. }
  132. // AuctionEventNewDefaultSlotSetBid is an event of the Auction Smart Contract
  133. type AuctionEventNewDefaultSlotSetBid struct {
  134. SlotSet int64
  135. NewInitialMinBid *big.Int
  136. }
  137. // AuctionEventNewForge is an event of the Auction Smart Contract
  138. type AuctionEventNewForge struct {
  139. Forger ethCommon.Address
  140. CurrentSlot int64
  141. }
  142. // AuctionEventHEZClaimed is an event of the Auction Smart Contract
  143. type AuctionEventHEZClaimed struct {
  144. Owner ethCommon.Address
  145. Amount *big.Int
  146. }
  147. // AuctionEvents is the list of events in a block of the Auction Smart Contract
  148. type AuctionEvents struct { //nolint:structcheck
  149. NewBid []AuctionEventNewBid
  150. NewSlotDeadline []AuctionEventNewSlotDeadline
  151. NewClosedAuctionSlots []AuctionEventNewClosedAuctionSlots
  152. NewOutbidding []AuctionEventNewOutbidding
  153. NewDonationAddress []AuctionEventNewDonationAddress
  154. NewBootCoordinator []AuctionEventNewBootCoordinator
  155. NewOpenAuctionSlots []AuctionEventNewOpenAuctionSlots
  156. NewAllocationRatio []AuctionEventNewAllocationRatio
  157. NewCoordinator []AuctionEventNewCoordinator
  158. CoordinatorUpdated []AuctionEventCoordinatorUpdated
  159. NewForgeAllocated []AuctionEventNewForgeAllocated
  160. NewDefaultSlotSetBid []AuctionEventNewDefaultSlotSetBid
  161. NewForge []AuctionEventNewForge
  162. HEZClaimed []AuctionEventHEZClaimed
  163. }
  164. // NewAuctionEvents creates an empty AuctionEvents with the slices initialized.
  165. func NewAuctionEvents() AuctionEvents {
  166. return AuctionEvents{
  167. NewBid: make([]AuctionEventNewBid, 0),
  168. NewSlotDeadline: make([]AuctionEventNewSlotDeadline, 0),
  169. NewClosedAuctionSlots: make([]AuctionEventNewClosedAuctionSlots, 0),
  170. NewOutbidding: make([]AuctionEventNewOutbidding, 0),
  171. NewDonationAddress: make([]AuctionEventNewDonationAddress, 0),
  172. NewBootCoordinator: make([]AuctionEventNewBootCoordinator, 0),
  173. NewOpenAuctionSlots: make([]AuctionEventNewOpenAuctionSlots, 0),
  174. NewAllocationRatio: make([]AuctionEventNewAllocationRatio, 0),
  175. NewCoordinator: make([]AuctionEventNewCoordinator, 0),
  176. CoordinatorUpdated: make([]AuctionEventCoordinatorUpdated, 0),
  177. NewForgeAllocated: make([]AuctionEventNewForgeAllocated, 0),
  178. NewDefaultSlotSetBid: make([]AuctionEventNewDefaultSlotSetBid, 0),
  179. NewForge: make([]AuctionEventNewForge, 0),
  180. HEZClaimed: make([]AuctionEventHEZClaimed, 0),
  181. }
  182. }
  183. // AuctionInterface is the inteface to to Auction Smart Contract
  184. type AuctionInterface interface {
  185. //
  186. // Smart Contract Methods
  187. //
  188. // Getter/Setter, where Setter is onlyOwner
  189. AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error)
  190. AuctionGetSlotDeadline() (uint8, error)
  191. AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error)
  192. AuctionGetOpenAuctionSlots() (uint16, error)
  193. AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error)
  194. AuctionGetClosedAuctionSlots() (uint16, error)
  195. AuctionSetOutbidding(newOutbidding uint16) (*types.Transaction, error)
  196. AuctionGetOutbidding() (uint16, error)
  197. AuctionSetAllocationRatio(newAllocationRatio [3]uint16) (*types.Transaction, error)
  198. AuctionGetAllocationRatio() ([3]uint16, error)
  199. AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error)
  200. AuctionGetDonationAddress() (*ethCommon.Address, error)
  201. AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error)
  202. AuctionGetBootCoordinator() (*ethCommon.Address, error)
  203. AuctionChangeDefaultSlotSetBid(slotSet int64, newInitialMinBid *big.Int) (*types.Transaction, error)
  204. // Coordinator Management
  205. AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error)
  206. AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error)
  207. AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error)
  208. // Slot Info
  209. AuctionGetCurrentSlotNumber() (int64, error)
  210. AuctionGetMinBidBySlot(slot int64) (*big.Int, error)
  211. AuctionGetDefaultSlotSetBid(slotSet uint8) (*big.Int, error)
  212. // Bidding
  213. // AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int,
  214. // userData, operatorData []byte) error // Only called from another smart contract
  215. AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error)
  216. AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool,
  217. maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error)
  218. // Forge
  219. AuctionCanForge(forger ethCommon.Address) (bool, error)
  220. // AuctionForge(forger ethCommon.Address) (bool, error) // Only called from another smart contract
  221. // Fees
  222. AuctionClaimHEZ() (*types.Transaction, error)
  223. //
  224. // Smart Contract Status
  225. //
  226. AuctionConstants() (*AuctionConstants, error)
  227. AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error)
  228. }
  229. //
  230. // Implementation
  231. //
  232. // AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum.
  233. type AuctionClient struct {
  234. client *EthereumClient
  235. address ethCommon.Address
  236. }
  237. // NewAuctionClient creates a new AuctionClient
  238. func NewAuctionClient(client *EthereumClient, address ethCommon.Address) *AuctionClient {
  239. return &AuctionClient{
  240. client: client,
  241. address: address,
  242. }
  243. }
  244. // AuctionSetSlotDeadline is the interface to call the smart contract function
  245. func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
  246. var tx *types.Transaction
  247. var err error
  248. if tx, err = c.client.CallAuth(
  249. 1000000,
  250. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  251. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  252. if err != nil {
  253. return nil, err
  254. }
  255. return auction.SetSlotDeadline(auth, newDeadline)
  256. },
  257. ); err != nil {
  258. return nil, fmt.Errorf("Failed setting slotDeadline: %w", err)
  259. }
  260. return tx, nil
  261. }
  262. // AuctionGetSlotDeadline is the interface to call the smart contract function
  263. func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) {
  264. var slotDeadline uint8
  265. if err := c.client.Call(func(ec *ethclient.Client) error {
  266. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  267. if err != nil {
  268. return err
  269. }
  270. slotDeadline, err = auction.GetSlotDeadline(nil)
  271. return err
  272. }); err != nil {
  273. return 0, err
  274. }
  275. return slotDeadline, nil
  276. }
  277. // AuctionSetOpenAuctionSlots is the interface to call the smart contract function
  278. func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) {
  279. var tx *types.Transaction
  280. var err error
  281. if tx, err = c.client.CallAuth(
  282. 1000000,
  283. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  284. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  285. if err != nil {
  286. return nil, err
  287. }
  288. return auction.SetOpenAuctionSlots(auth, newOpenAuctionSlots)
  289. },
  290. ); err != nil {
  291. return nil, fmt.Errorf("Failed setting openAuctionSlots: %w", err)
  292. }
  293. return tx, nil
  294. }
  295. // AuctionGetOpenAuctionSlots is the interface to call the smart contract function
  296. func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) {
  297. var openAuctionSlots uint16
  298. if err := c.client.Call(func(ec *ethclient.Client) error {
  299. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  300. if err != nil {
  301. return err
  302. }
  303. openAuctionSlots, err = auction.GetOpenAuctionSlots(nil)
  304. return err
  305. }); err != nil {
  306. return 0, err
  307. }
  308. return openAuctionSlots, nil
  309. }
  310. // AuctionSetClosedAuctionSlots is the interface to call the smart contract function
  311. func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) {
  312. var tx *types.Transaction
  313. var err error
  314. if tx, err = c.client.CallAuth(
  315. 1000000,
  316. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  317. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  318. if err != nil {
  319. return nil, err
  320. }
  321. return auction.SetClosedAuctionSlots(auth, newClosedAuctionSlots)
  322. },
  323. ); err != nil {
  324. return nil, fmt.Errorf("Failed setting closedAuctionSlots: %w", err)
  325. }
  326. return tx, nil
  327. }
  328. // AuctionGetClosedAuctionSlots is the interface to call the smart contract function
  329. func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) {
  330. var closedAuctionSlots uint16
  331. if err := c.client.Call(func(ec *ethclient.Client) error {
  332. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  333. if err != nil {
  334. return err
  335. }
  336. closedAuctionSlots, err = auction.GetClosedAuctionSlots(nil)
  337. return err
  338. }); err != nil {
  339. return 0, err
  340. }
  341. return closedAuctionSlots, nil
  342. }
  343. // AuctionSetOutbidding is the interface to call the smart contract function
  344. func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint8) (*types.Transaction, error) {
  345. var tx *types.Transaction
  346. var err error
  347. if tx, err = c.client.CallAuth(
  348. 1000000,
  349. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  350. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  351. if err != nil {
  352. return nil, err
  353. }
  354. return auction.SetOutbidding(auth, newOutbidding)
  355. },
  356. ); err != nil {
  357. return nil, fmt.Errorf("Failed setting setOutbidding: %w", err)
  358. }
  359. return tx, nil
  360. }
  361. // AuctionGetOutbidding is the interface to call the smart contract function
  362. func (c *AuctionClient) AuctionGetOutbidding() (uint16, error) {
  363. // TODO: Update
  364. // var outbidding uint8
  365. // if err := c.client.Call(func(ec *ethclient.Client) error {
  366. // auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  367. // if err != nil {
  368. // return err
  369. // }
  370. // outbidding, err = auction.GetOutbidding(nil)
  371. // return err
  372. // }); err != nil {
  373. // return 0, err
  374. // }
  375. // return outbidding, nil
  376. log.Error("TODO")
  377. return 0, errTODO
  378. }
  379. // AuctionSetAllocationRatio is the interface to call the smart contract function
  380. func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint8) (*types.Transaction, error) {
  381. var tx *types.Transaction
  382. var err error
  383. if tx, err = c.client.CallAuth(
  384. 1000000,
  385. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  386. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  387. if err != nil {
  388. return nil, err
  389. }
  390. return auction.SetAllocationRatio(auth, newAllocationRatio)
  391. },
  392. ); err != nil {
  393. return nil, fmt.Errorf("Failed setting allocationRatio: %w", err)
  394. }
  395. return tx, nil
  396. }
  397. // AuctionGetAllocationRatio is the interface to call the smart contract function
  398. func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint16, error) {
  399. // TODO: Update
  400. // var allocationRation [3]uint8
  401. // if err := c.client.Call(func(ec *ethclient.Client) error {
  402. // auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  403. // if err != nil {
  404. // return err
  405. // }
  406. // allocationRation, err = auction.GetAllocationRatio(nil)
  407. // return err
  408. // }); err != nil {
  409. // return [3]uint8{}, err
  410. // }
  411. // return allocationRation, nil
  412. log.Error("TODO")
  413. return [3]uint16{}, errTODO
  414. }
  415. // AuctionSetDonationAddress is the interface to call the smart contract function
  416. func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) {
  417. var tx *types.Transaction
  418. var err error
  419. if tx, err = c.client.CallAuth(
  420. 1000000,
  421. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  422. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  423. if err != nil {
  424. return nil, err
  425. }
  426. return auction.SetDonationAddress(auth, newDonationAddress)
  427. },
  428. ); err != nil {
  429. return nil, fmt.Errorf("Failed setting donationAddress: %w", err)
  430. }
  431. return tx, nil
  432. }
  433. // AuctionGetDonationAddress is the interface to call the smart contract function
  434. func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) {
  435. var donationAddress ethCommon.Address
  436. if err := c.client.Call(func(ec *ethclient.Client) error {
  437. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  438. if err != nil {
  439. return err
  440. }
  441. donationAddress, err = auction.GetDonationAddress(nil)
  442. return err
  443. }); err != nil {
  444. return nil, err
  445. }
  446. return &donationAddress, nil
  447. }
  448. // AuctionSetBootCoordinator is the interface to call the smart contract function
  449. func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) {
  450. var tx *types.Transaction
  451. var err error
  452. if tx, err = c.client.CallAuth(
  453. 1000000,
  454. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  455. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  456. if err != nil {
  457. return nil, err
  458. }
  459. return auction.SetBootCoordinator(auth, newBootCoordinator)
  460. },
  461. ); err != nil {
  462. return nil, fmt.Errorf("Failed setting bootCoordinator: %w", err)
  463. }
  464. return tx, nil
  465. }
  466. // AuctionGetBootCoordinator is the interface to call the smart contract function
  467. func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
  468. var bootCoordinator ethCommon.Address
  469. if err := c.client.Call(func(ec *ethclient.Client) error {
  470. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  471. if err != nil {
  472. return err
  473. }
  474. bootCoordinator, err = auction.GetBootCoordinator(nil)
  475. return err
  476. }); err != nil {
  477. return nil, err
  478. }
  479. return &bootCoordinator, nil
  480. }
  481. // AuctionChangeEpochMinBid is the interface to call the smart contract function
  482. func (c *AuctionClient) AuctionChangeEpochMinBid(slotEpoch int64, newInitialMinBid *big.Int) (*types.Transaction, error) {
  483. var tx *types.Transaction
  484. var err error
  485. if tx, err = c.client.CallAuth(
  486. 1000000,
  487. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  488. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  489. if err != nil {
  490. return nil, err
  491. }
  492. slotEpochToSend := big.NewInt(slotEpoch)
  493. fmt.Println(slotEpochToSend)
  494. fmt.Println(newInitialMinBid)
  495. return auction.ChangeEpochMinBid(auth, slotEpochToSend, newInitialMinBid)
  496. },
  497. ); err != nil {
  498. return nil, fmt.Errorf("Failed changing epoch minBid: %w", err)
  499. }
  500. fmt.Println(tx)
  501. return tx, nil
  502. }
  503. // AuctionRegisterCoordinator is the interface to call the smart contract function
  504. func (c *AuctionClient) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error) {
  505. var tx *types.Transaction
  506. var err error
  507. if tx, err = c.client.CallAuth(
  508. 1000000,
  509. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  510. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  511. if err != nil {
  512. return nil, err
  513. }
  514. return auction.RegisterCoordinator(auth, forgerAddress, URL)
  515. },
  516. ); err != nil {
  517. return nil, fmt.Errorf("Failed register coordinator: %w", err)
  518. }
  519. return tx, nil
  520. }
  521. // AuctionIsRegisteredCoordinator is the interface to call the smart contract function
  522. func (c *AuctionClient) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
  523. var registered bool
  524. if err := c.client.Call(func(ec *ethclient.Client) error {
  525. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  526. if err != nil {
  527. return err
  528. }
  529. registered, err = auction.IsRegisteredCoordinator(nil, forgerAddress)
  530. return err
  531. }); err != nil {
  532. return false, err
  533. }
  534. return registered, nil
  535. }
  536. // AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
  537. func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) {
  538. var tx *types.Transaction
  539. var err error
  540. if tx, err = c.client.CallAuth(
  541. 1000000,
  542. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  543. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  544. if err != nil {
  545. return nil, err
  546. }
  547. return auction.UpdateCoordinatorInfo(auth, forgerAddress, newWithdrawAddress, newURL)
  548. },
  549. ); err != nil {
  550. return nil, fmt.Errorf("Failed update coordinator info: %w", err)
  551. }
  552. return tx, nil
  553. }
  554. // AuctionGetCurrentSlotNumber is the interface to call the smart contract function
  555. func (c *AuctionClient) AuctionGetCurrentSlotNumber() (int64, error) {
  556. var _currentSlotNumber *big.Int
  557. if err := c.client.Call(func(ec *ethclient.Client) error {
  558. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  559. if err != nil {
  560. return err
  561. }
  562. _currentSlotNumber, err = auction.GetCurrentSlotNumber(nil)
  563. return err
  564. }); err != nil {
  565. return 0, err
  566. }
  567. currentSlotNumber := _currentSlotNumber.Int64()
  568. return currentSlotNumber, nil
  569. }
  570. // AuctionGetMinBidBySlot is the interface to call the smart contract function
  571. func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
  572. var minBid *big.Int
  573. if err := c.client.Call(func(ec *ethclient.Client) error {
  574. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  575. if err != nil {
  576. return err
  577. }
  578. slotToSend := big.NewInt(slot)
  579. minBid, err = auction.GetMinBidBySlot(nil, slotToSend)
  580. return err
  581. }); err != nil {
  582. return big.NewInt(0), err
  583. }
  584. return minBid, nil
  585. }
  586. // AuctionGetDefaultSlotSetBid is the interface to call the smart contract function
  587. func (c *AuctionClient) AuctionGetDefaultSlotSetBid(slotSet uint8) (*big.Int, error) {
  588. // TODO: Update
  589. // var DefaultSlotSetBid *big.Int
  590. // if err := c.client.Call(func(ec *ethclient.Client) error {
  591. // auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  592. // if err != nil {
  593. // return err
  594. // }
  595. // defaultSlotSetBid, err = auction.GetDefaultSlotSetBid(nil, slotSet)
  596. // return err
  597. // }); err != nil {
  598. // return big.NewInt(0), err
  599. // }
  600. // return defaultSlotSetBid, nil
  601. log.Error("TODO")
  602. return nil, errTODO
  603. }
  604. // AuctionTokensReceived is the interface to call the smart contract function
  605. // func (c *AuctionClient) AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int, userData, operatorData []byte) error {
  606. // return errTODO
  607. // }
  608. // AuctionBid is the interface to call the smart contract function
  609. func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
  610. log.Error("TODO")
  611. return nil, errTODO
  612. }
  613. // AuctionMultiBid is the interface to call the smart contract function
  614. func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
  615. log.Error("TODO")
  616. return nil, errTODO
  617. }
  618. // AuctionCanForge is the interface to call the smart contract function
  619. func (c *AuctionClient) AuctionCanForge(forger ethCommon.Address) (bool, error) {
  620. log.Error("TODO")
  621. return false, errTODO
  622. }
  623. // AuctionForge is the interface to call the smart contract function
  624. // func (c *AuctionClient) AuctionForge(forger ethCommon.Address) (bool, error) {
  625. // return false, errTODO
  626. // }
  627. // AuctionClaimHEZ is the interface to call the smart contract function
  628. func (c *AuctionClient) AuctionClaimHEZ() (*types.Transaction, error) {
  629. log.Error("TODO")
  630. return nil, errTODO
  631. }
  632. // AuctionConstants returns the Constants of the Auction Smart Contract
  633. func (c *AuctionClient) AuctionConstants() (*AuctionConstants, error) {
  634. log.Error("TODO")
  635. return nil, errTODO
  636. }
  637. // AuctionEventsByBlock returns the events in a block that happened in the Auction Smart Contract
  638. func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error) {
  639. log.Error("TODO")
  640. return nil, nil, errTODO
  641. }