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.

1029 lines
36 KiB

  1. package eth
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "fmt"
  6. "math/big"
  7. "strings"
  8. "github.com/ethereum/go-ethereum"
  9. "github.com/ethereum/go-ethereum/accounts/abi"
  10. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  11. ethCommon "github.com/ethereum/go-ethereum/common"
  12. "github.com/ethereum/go-ethereum/core/types"
  13. "github.com/ethereum/go-ethereum/crypto"
  14. "github.com/ethereum/go-ethereum/ethclient"
  15. HermezAuctionProtocol "github.com/hermeznetwork/hermez-node/eth/contracts/auction"
  16. ERC777 "github.com/hermeznetwork/hermez-node/eth/contracts/erc777"
  17. "golang.org/x/crypto/sha3"
  18. )
  19. // AuctionConstants are the constants of the Rollup Smart Contract
  20. type AuctionConstants struct {
  21. // Blocks per slot
  22. BlocksPerSlot uint8
  23. // Minimum bid when no one has bid yet
  24. InitialMinimalBidding *big.Int
  25. // First block where the first slot begins
  26. GenesisBlockNum int64
  27. // ERC777 token with which the bids will be made
  28. TokenHEZ ethCommon.Address
  29. // HermezRollup smartcontract address
  30. HermezRollup ethCommon.Address
  31. // Hermez Governanze Token smartcontract address who controls some parameters and collects HEZ fee
  32. // Only for test
  33. GovernanceAddress ethCommon.Address
  34. }
  35. // SlotState is the state of a slot
  36. type SlotState struct {
  37. Bidder ethCommon.Address
  38. Fulfilled bool
  39. BidAmount *big.Int
  40. ClosedMinBid *big.Int
  41. }
  42. // NewSlotState returns an empty SlotState
  43. func NewSlotState() *SlotState {
  44. return &SlotState{
  45. Bidder: ethCommon.Address{},
  46. Fulfilled: false,
  47. BidAmount: big.NewInt(0),
  48. ClosedMinBid: big.NewInt(0),
  49. }
  50. }
  51. // Coordinator is the details of the Coordinator identified by the forger address
  52. type Coordinator struct {
  53. Forger ethCommon.Address
  54. URL string
  55. }
  56. // AuctionVariables are the variables of the Auction Smart Contract
  57. type AuctionVariables struct {
  58. // Boot Coordinator Address
  59. DonationAddress ethCommon.Address
  60. // Boot Coordinator Address
  61. BootCoordinator ethCommon.Address
  62. // The minimum bid value in a series of 6 slots
  63. DefaultSlotSetBid [6]*big.Int
  64. // Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
  65. ClosedAuctionSlots uint16
  66. // Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
  67. OpenAuctionSlots uint16
  68. // How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
  69. AllocationRatio [3]uint16
  70. // Minimum outbid (percentage) over the previous one to consider it valid
  71. Outbidding uint16
  72. // Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
  73. SlotDeadline uint8
  74. }
  75. // AuctionState represents the state of the Rollup in the Smart Contract
  76. type AuctionState struct {
  77. // Mapping to control slot state
  78. Slots map[int64]*SlotState
  79. // Mapping to control balances pending to claim
  80. PendingBalances map[ethCommon.Address]*big.Int
  81. // Mapping to register all the coordinators. The address used for the mapping is the forger address
  82. Coordinators map[ethCommon.Address]*Coordinator
  83. }
  84. // AuctionEventNewBid is an event of the Auction Smart Contract
  85. type AuctionEventNewBid struct {
  86. Slot int64
  87. BidAmount *big.Int
  88. Bidder ethCommon.Address
  89. }
  90. // AuctionEventNewSlotDeadline is an event of the Auction Smart Contract
  91. type AuctionEventNewSlotDeadline struct {
  92. NewSlotDeadline uint8
  93. }
  94. // AuctionEventNewClosedAuctionSlots is an event of the Auction Smart Contract
  95. type AuctionEventNewClosedAuctionSlots struct {
  96. NewClosedAuctionSlots uint16
  97. }
  98. // AuctionEventNewOutbidding is an event of the Auction Smart Contract
  99. type AuctionEventNewOutbidding struct {
  100. NewOutbidding uint16
  101. }
  102. // AuctionEventNewDonationAddress is an event of the Auction Smart Contract
  103. type AuctionEventNewDonationAddress struct {
  104. NewDonationAddress ethCommon.Address
  105. }
  106. // AuctionEventNewBootCoordinator is an event of the Auction Smart Contract
  107. type AuctionEventNewBootCoordinator struct {
  108. NewBootCoordinator ethCommon.Address
  109. }
  110. // AuctionEventNewOpenAuctionSlots is an event of the Auction Smart Contract
  111. type AuctionEventNewOpenAuctionSlots struct {
  112. NewOpenAuctionSlots uint16
  113. }
  114. // AuctionEventNewAllocationRatio is an event of the Auction Smart Contract
  115. type AuctionEventNewAllocationRatio struct {
  116. NewAllocationRatio [3]uint16
  117. }
  118. // AuctionEventSetCoordinator is an event of the Auction Smart Contract
  119. type AuctionEventSetCoordinator struct {
  120. BidderAddress ethCommon.Address
  121. ForgerAddress ethCommon.Address
  122. CoordinatorURL string
  123. }
  124. // AuctionEventNewForgeAllocated is an event of the Auction Smart Contract
  125. type AuctionEventNewForgeAllocated struct {
  126. Bidder ethCommon.Address
  127. Forger ethCommon.Address
  128. SlotToForge int64
  129. BurnAmount *big.Int
  130. DonationAmount *big.Int
  131. GovernanceAmount *big.Int
  132. }
  133. // AuctionEventNewDefaultSlotSetBid is an event of the Auction Smart Contract
  134. type AuctionEventNewDefaultSlotSetBid struct {
  135. SlotSet int64
  136. NewInitialMinBid *big.Int
  137. }
  138. // AuctionEventNewForge is an event of the Auction Smart Contract
  139. type AuctionEventNewForge struct {
  140. Forger ethCommon.Address
  141. SlotToForge int64
  142. }
  143. // AuctionEventHEZClaimed is an event of the Auction Smart Contract
  144. type AuctionEventHEZClaimed struct {
  145. Owner ethCommon.Address
  146. Amount *big.Int
  147. }
  148. // AuctionEvents is the list of events in a block of the Auction Smart Contract
  149. type AuctionEvents struct { //nolint:structcheck
  150. NewBid []AuctionEventNewBid
  151. NewSlotDeadline []AuctionEventNewSlotDeadline
  152. NewClosedAuctionSlots []AuctionEventNewClosedAuctionSlots
  153. NewOutbidding []AuctionEventNewOutbidding
  154. NewDonationAddress []AuctionEventNewDonationAddress
  155. NewBootCoordinator []AuctionEventNewBootCoordinator
  156. NewOpenAuctionSlots []AuctionEventNewOpenAuctionSlots
  157. NewAllocationRatio []AuctionEventNewAllocationRatio
  158. SetCoordinator []AuctionEventSetCoordinator
  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. SetCoordinator: make([]AuctionEventSetCoordinator, 0),
  176. NewForgeAllocated: make([]AuctionEventNewForgeAllocated, 0),
  177. NewDefaultSlotSetBid: make([]AuctionEventNewDefaultSlotSetBid, 0),
  178. NewForge: make([]AuctionEventNewForge, 0),
  179. HEZClaimed: make([]AuctionEventHEZClaimed, 0),
  180. }
  181. }
  182. // AuctionInterface is the inteface to to Auction Smart Contract
  183. type AuctionInterface interface {
  184. //
  185. // Smart Contract Methods
  186. //
  187. // Getter/Setter, where Setter is onlyOwner
  188. AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error)
  189. AuctionGetSlotDeadline() (uint8, error)
  190. AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error)
  191. AuctionGetOpenAuctionSlots() (uint16, error)
  192. AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error)
  193. AuctionGetClosedAuctionSlots() (uint16, error)
  194. AuctionSetOutbidding(newOutbidding uint16) (*types.Transaction, error)
  195. AuctionGetOutbidding() (uint16, error)
  196. AuctionSetAllocationRatio(newAllocationRatio [3]uint16) (*types.Transaction, error)
  197. AuctionGetAllocationRatio() ([3]uint16, error)
  198. AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error)
  199. AuctionGetDonationAddress() (*ethCommon.Address, error)
  200. AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error)
  201. AuctionGetBootCoordinator() (*ethCommon.Address, error)
  202. AuctionChangeDefaultSlotSetBid(slotSet int64, newInitialMinBid *big.Int) (*types.Transaction, error)
  203. // Coordinator Management
  204. AuctionSetCoordinator(forger ethCommon.Address, coordinatorURL string) (*types.Transaction, error)
  205. // Slot Info
  206. AuctionGetSlotNumber(blockNum int64) (int64, error)
  207. AuctionGetCurrentSlotNumber() (int64, error)
  208. AuctionGetMinBidBySlot(slot int64) (*big.Int, error)
  209. AuctionGetDefaultSlotSetBid(slotSet uint8) (*big.Int, error)
  210. AuctionGetSlotSet(slot int64) (*big.Int, error)
  211. // Bidding
  212. AuctionBid(slot int64, bidAmount *big.Int) (*types.Transaction, error)
  213. AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int) (*types.Transaction, error)
  214. // Forge
  215. AuctionCanForge(forger ethCommon.Address, blockNum int64) (bool, error)
  216. AuctionForge(forger ethCommon.Address) (*types.Transaction, error)
  217. // Fees
  218. AuctionClaimHEZ() (*types.Transaction, error)
  219. AuctionGetClaimableHEZ(bidder ethCommon.Address) (*big.Int, error)
  220. //
  221. // Smart Contract Status
  222. //
  223. AuctionConstants() (*AuctionConstants, error)
  224. AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error)
  225. }
  226. //
  227. // Implementation
  228. //
  229. // AuctionClient is the implementation of the interface to the Auction Smart Contract in ethereum.
  230. type AuctionClient struct {
  231. client *EthereumClient
  232. address ethCommon.Address
  233. tokenAddress ethCommon.Address
  234. gasLimit uint64
  235. contractAbi abi.ABI
  236. }
  237. // NewAuctionClient creates a new AuctionClient. `tokenAddress` is the address of the HEZ tokens.
  238. func NewAuctionClient(client *EthereumClient, address, tokenAddress ethCommon.Address) (*AuctionClient, error) {
  239. contractAbi, err := abi.JSON(strings.NewReader(string(HermezAuctionProtocol.HermezAuctionProtocolABI)))
  240. if err != nil {
  241. return nil, err
  242. }
  243. return &AuctionClient{
  244. client: client,
  245. address: address,
  246. tokenAddress: tokenAddress,
  247. gasLimit: 1000000, //nolint:gomnd
  248. contractAbi: contractAbi,
  249. }, nil
  250. }
  251. // AuctionSetSlotDeadline is the interface to call the smart contract function
  252. func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
  253. var tx *types.Transaction
  254. var err error
  255. if tx, err = c.client.CallAuth(
  256. c.gasLimit,
  257. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  258. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  259. if err != nil {
  260. return nil, err
  261. }
  262. return auction.SetSlotDeadline(auth, newDeadline)
  263. },
  264. ); err != nil {
  265. return nil, fmt.Errorf("Failed setting slotDeadline: %w", err)
  266. }
  267. return tx, nil
  268. }
  269. // AuctionGetSlotDeadline is the interface to call the smart contract function
  270. func (c *AuctionClient) AuctionGetSlotDeadline() (uint8, error) {
  271. var slotDeadline uint8
  272. if err := c.client.Call(func(ec *ethclient.Client) error {
  273. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  274. if err != nil {
  275. return err
  276. }
  277. slotDeadline, err = auction.GetSlotDeadline(nil)
  278. return err
  279. }); err != nil {
  280. return 0, err
  281. }
  282. return slotDeadline, nil
  283. }
  284. // AuctionSetOpenAuctionSlots is the interface to call the smart contract function
  285. func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (*types.Transaction, error) {
  286. var tx *types.Transaction
  287. var err error
  288. if tx, err = c.client.CallAuth(
  289. c.gasLimit,
  290. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  291. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  292. if err != nil {
  293. return nil, err
  294. }
  295. return auction.SetOpenAuctionSlots(auth, newOpenAuctionSlots)
  296. },
  297. ); err != nil {
  298. return nil, fmt.Errorf("Failed setting openAuctionSlots: %w", err)
  299. }
  300. return tx, nil
  301. }
  302. // AuctionGetOpenAuctionSlots is the interface to call the smart contract function
  303. func (c *AuctionClient) AuctionGetOpenAuctionSlots() (uint16, error) {
  304. var openAuctionSlots uint16
  305. if err := c.client.Call(func(ec *ethclient.Client) error {
  306. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  307. if err != nil {
  308. return err
  309. }
  310. openAuctionSlots, err = auction.GetOpenAuctionSlots(nil)
  311. return err
  312. }); err != nil {
  313. return 0, err
  314. }
  315. return openAuctionSlots, nil
  316. }
  317. // AuctionSetClosedAuctionSlots is the interface to call the smart contract function
  318. func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (*types.Transaction, error) {
  319. var tx *types.Transaction
  320. var err error
  321. if tx, err = c.client.CallAuth(
  322. c.gasLimit,
  323. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  324. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  325. if err != nil {
  326. return nil, err
  327. }
  328. return auction.SetClosedAuctionSlots(auth, newClosedAuctionSlots)
  329. },
  330. ); err != nil {
  331. return nil, fmt.Errorf("Failed setting closedAuctionSlots: %w", err)
  332. }
  333. return tx, nil
  334. }
  335. // AuctionGetClosedAuctionSlots is the interface to call the smart contract function
  336. func (c *AuctionClient) AuctionGetClosedAuctionSlots() (uint16, error) {
  337. var closedAuctionSlots uint16
  338. if err := c.client.Call(func(ec *ethclient.Client) error {
  339. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  340. if err != nil {
  341. return err
  342. }
  343. closedAuctionSlots, err = auction.GetClosedAuctionSlots(nil)
  344. return err
  345. }); err != nil {
  346. return 0, err
  347. }
  348. return closedAuctionSlots, nil
  349. }
  350. // AuctionSetOutbidding is the interface to call the smart contract function
  351. func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint16) (*types.Transaction, error) {
  352. var tx *types.Transaction
  353. var err error
  354. if tx, err = c.client.CallAuth(
  355. 12500000, //nolint:gomnd
  356. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  357. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  358. if err != nil {
  359. return nil, err
  360. }
  361. return auction.SetOutbidding(auth, newOutbidding)
  362. },
  363. ); err != nil {
  364. return nil, fmt.Errorf("Failed setting setOutbidding: %w", err)
  365. }
  366. return tx, nil
  367. }
  368. // AuctionGetOutbidding is the interface to call the smart contract function
  369. func (c *AuctionClient) AuctionGetOutbidding() (uint16, error) {
  370. var outbidding uint16
  371. if err := c.client.Call(func(ec *ethclient.Client) error {
  372. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  373. if err != nil {
  374. return err
  375. }
  376. outbidding, err = auction.GetOutbidding(nil)
  377. return err
  378. }); err != nil {
  379. return 0, err
  380. }
  381. return outbidding, nil
  382. }
  383. // AuctionSetAllocationRatio is the interface to call the smart contract function
  384. func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint16) (*types.Transaction, error) {
  385. var tx *types.Transaction
  386. var err error
  387. if tx, err = c.client.CallAuth(
  388. c.gasLimit,
  389. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  390. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  391. if err != nil {
  392. return nil, err
  393. }
  394. return auction.SetAllocationRatio(auth, newAllocationRatio)
  395. },
  396. ); err != nil {
  397. return nil, fmt.Errorf("Failed setting allocationRatio: %w", err)
  398. }
  399. return tx, nil
  400. }
  401. // AuctionGetAllocationRatio is the interface to call the smart contract function
  402. func (c *AuctionClient) AuctionGetAllocationRatio() ([3]uint16, error) {
  403. var allocationRation [3]uint16
  404. if err := c.client.Call(func(ec *ethclient.Client) error {
  405. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  406. if err != nil {
  407. return err
  408. }
  409. allocationRation, err = auction.GetAllocationRatio(nil)
  410. return err
  411. }); err != nil {
  412. return [3]uint16{}, err
  413. }
  414. return allocationRation, nil
  415. }
  416. // AuctionSetDonationAddress is the interface to call the smart contract function
  417. func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (*types.Transaction, error) {
  418. var tx *types.Transaction
  419. var err error
  420. if tx, err = c.client.CallAuth(
  421. c.gasLimit,
  422. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  423. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  424. if err != nil {
  425. return nil, err
  426. }
  427. return auction.SetDonationAddress(auth, newDonationAddress)
  428. },
  429. ); err != nil {
  430. return nil, fmt.Errorf("Failed setting donationAddress: %w", err)
  431. }
  432. return tx, nil
  433. }
  434. // AuctionGetDonationAddress is the interface to call the smart contract function
  435. func (c *AuctionClient) AuctionGetDonationAddress() (*ethCommon.Address, error) {
  436. var donationAddress ethCommon.Address
  437. if err := c.client.Call(func(ec *ethclient.Client) error {
  438. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  439. if err != nil {
  440. return err
  441. }
  442. donationAddress, err = auction.GetDonationAddress(nil)
  443. return err
  444. }); err != nil {
  445. return nil, err
  446. }
  447. return &donationAddress, nil
  448. }
  449. // AuctionSetBootCoordinator is the interface to call the smart contract function
  450. func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (*types.Transaction, error) {
  451. var tx *types.Transaction
  452. var err error
  453. if tx, err = c.client.CallAuth(
  454. c.gasLimit,
  455. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  456. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  457. if err != nil {
  458. return nil, err
  459. }
  460. return auction.SetBootCoordinator(auth, newBootCoordinator)
  461. },
  462. ); err != nil {
  463. return nil, fmt.Errorf("Failed setting bootCoordinator: %w", err)
  464. }
  465. return tx, nil
  466. }
  467. // AuctionGetBootCoordinator is the interface to call the smart contract function
  468. func (c *AuctionClient) AuctionGetBootCoordinator() (*ethCommon.Address, error) {
  469. var bootCoordinator ethCommon.Address
  470. if err := c.client.Call(func(ec *ethclient.Client) error {
  471. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  472. if err != nil {
  473. return err
  474. }
  475. bootCoordinator, err = auction.GetBootCoordinator(nil)
  476. return err
  477. }); err != nil {
  478. return nil, err
  479. }
  480. return &bootCoordinator, nil
  481. }
  482. // AuctionChangeDefaultSlotSetBid is the interface to call the smart contract function
  483. func (c *AuctionClient) AuctionChangeDefaultSlotSetBid(slotSet int64, newInitialMinBid *big.Int) (*types.Transaction, error) {
  484. var tx *types.Transaction
  485. var err error
  486. if tx, err = c.client.CallAuth(
  487. c.gasLimit,
  488. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  489. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  490. if err != nil {
  491. return nil, err
  492. }
  493. slotSetToSend := big.NewInt(slotSet)
  494. return auction.ChangeDefaultSlotSetBid(auth, slotSetToSend, newInitialMinBid)
  495. },
  496. ); err != nil {
  497. return nil, fmt.Errorf("Failed changing slotSet Bid: %w", err)
  498. }
  499. return tx, nil
  500. }
  501. // AuctionGetClaimableHEZ is the interface to call the smart contract function
  502. func (c *AuctionClient) AuctionGetClaimableHEZ(claimAddress ethCommon.Address) (*big.Int, error) {
  503. var claimableHEZ *big.Int
  504. if err := c.client.Call(func(ec *ethclient.Client) error {
  505. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  506. if err != nil {
  507. return err
  508. }
  509. claimableHEZ, err = auction.GetClaimableHEZ(nil, claimAddress)
  510. return err
  511. }); err != nil {
  512. return nil, err
  513. }
  514. return claimableHEZ, nil
  515. }
  516. // AuctionSetCoordinator is the interface to call the smart contract function
  517. func (c *AuctionClient) AuctionSetCoordinator(forger ethCommon.Address, coordinatorURL string) (*types.Transaction, error) {
  518. var tx *types.Transaction
  519. var err error
  520. if tx, err = c.client.CallAuth(
  521. c.gasLimit,
  522. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  523. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  524. if err != nil {
  525. return nil, err
  526. }
  527. return auction.SetCoordinator(auth, forger, coordinatorURL)
  528. },
  529. ); err != nil {
  530. return nil, fmt.Errorf("Failed set coordinator: %w", err)
  531. }
  532. return tx, nil
  533. }
  534. // AuctionGetCurrentSlotNumber is the interface to call the smart contract function
  535. func (c *AuctionClient) AuctionGetCurrentSlotNumber() (int64, error) {
  536. var _currentSlotNumber *big.Int
  537. if err := c.client.Call(func(ec *ethclient.Client) error {
  538. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  539. if err != nil {
  540. return err
  541. }
  542. _currentSlotNumber, err = auction.GetCurrentSlotNumber(nil)
  543. return err
  544. }); err != nil {
  545. return 0, err
  546. }
  547. currentSlotNumber := _currentSlotNumber.Int64()
  548. return currentSlotNumber, nil
  549. }
  550. // AuctionGetMinBidBySlot is the interface to call the smart contract function
  551. func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (*big.Int, error) {
  552. var minBid *big.Int
  553. if err := c.client.Call(func(ec *ethclient.Client) error {
  554. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  555. if err != nil {
  556. return err
  557. }
  558. slotToSend := big.NewInt(slot)
  559. minBid, err = auction.GetMinBidBySlot(nil, slotToSend)
  560. return err
  561. }); err != nil {
  562. return big.NewInt(0), err
  563. }
  564. return minBid, nil
  565. }
  566. // AuctionGetSlotSet is the interface to call the smart contract function
  567. func (c *AuctionClient) AuctionGetSlotSet(slot int64) (*big.Int, error) {
  568. var slotSet *big.Int
  569. if err := c.client.Call(func(ec *ethclient.Client) error {
  570. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  571. if err != nil {
  572. return err
  573. }
  574. slotToSend := big.NewInt(slot)
  575. slotSet, err = auction.GetSlotSet(nil, slotToSend)
  576. return err
  577. }); err != nil {
  578. return big.NewInt(0), err
  579. }
  580. return slotSet, nil
  581. }
  582. // AuctionGetDefaultSlotSetBid is the interface to call the smart contract function
  583. func (c *AuctionClient) AuctionGetDefaultSlotSetBid(slotSet uint8) (*big.Int, error) {
  584. var minBidSlotSet *big.Int
  585. if err := c.client.Call(func(ec *ethclient.Client) error {
  586. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  587. if err != nil {
  588. return err
  589. }
  590. minBidSlotSet, err = auction.GetDefaultSlotSetBid(nil, slotSet)
  591. return err
  592. }); err != nil {
  593. return big.NewInt(0), err
  594. }
  595. return minBidSlotSet, nil
  596. }
  597. // AuctionGetSlotNumber is the interface to call the smart contract function
  598. func (c *AuctionClient) AuctionGetSlotNumber(blockNum int64) (int64, error) {
  599. var slot *big.Int
  600. if err := c.client.Call(func(ec *ethclient.Client) error {
  601. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  602. if err != nil {
  603. return err
  604. }
  605. blockNumBig := big.NewInt(blockNum)
  606. slot, err = auction.GetSlotNumber(nil, blockNumBig)
  607. return err
  608. }); err != nil {
  609. return 0, err
  610. }
  611. return slot.Int64(), nil
  612. }
  613. // AuctionBid is the interface to call the smart contract function
  614. func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int) (*types.Transaction, error) {
  615. var tx *types.Transaction
  616. var err error
  617. if tx, err = c.client.CallAuth(
  618. c.gasLimit,
  619. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  620. tokens, err := ERC777.NewERC777(c.tokenAddress, ec)
  621. if err != nil {
  622. return nil, err
  623. }
  624. bidFnSignature := []byte("bid(uint128,uint128)")
  625. hash := sha3.NewLegacyKeccak256()
  626. _, err = hash.Write(bidFnSignature)
  627. if err != nil {
  628. return nil, err
  629. }
  630. methodID := hash.Sum(nil)[:4]
  631. slotBytes := make([]byte, 8)
  632. binary.BigEndian.PutUint64(slotBytes, uint64(slot))
  633. paddedSlot := ethCommon.LeftPadBytes(slotBytes, 32)
  634. paddedAmount := ethCommon.LeftPadBytes(bidAmount.Bytes(), 32)
  635. var userData []byte
  636. userData = append(userData, methodID...)
  637. userData = append(userData, paddedSlot...)
  638. userData = append(userData, paddedAmount...)
  639. return tokens.Send(auth, c.address, bidAmount, userData)
  640. },
  641. ); err != nil {
  642. return nil, fmt.Errorf("Failed bid: %w", err)
  643. }
  644. return tx, nil
  645. }
  646. // AuctionMultiBid is the interface to call the smart contract function
  647. func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int) (*types.Transaction, error) {
  648. var tx *types.Transaction
  649. var err error
  650. if tx, err = c.client.CallAuth(
  651. c.gasLimit,
  652. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  653. tokens, err := ERC777.NewERC777(c.tokenAddress, ec)
  654. if err != nil {
  655. return nil, err
  656. }
  657. multiBidFnSignature := []byte("multiBid(uint128,uint128,bool[6],uint128,uint128)")
  658. hash := sha3.NewLegacyKeccak256()
  659. _, err = hash.Write(multiBidFnSignature)
  660. if err != nil {
  661. return nil, err
  662. }
  663. methodID := hash.Sum(nil)[:4]
  664. startingSlotBytes := make([]byte, 8)
  665. binary.BigEndian.PutUint64(startingSlotBytes, uint64(startingSlot))
  666. paddedStartingSlot := ethCommon.LeftPadBytes(startingSlotBytes, 32)
  667. endingSlotBytes := make([]byte, 8)
  668. binary.BigEndian.PutUint64(endingSlotBytes, uint64(endingSlot))
  669. paddedEndingSlot := ethCommon.LeftPadBytes(endingSlotBytes, 32)
  670. paddedMinBid := ethCommon.LeftPadBytes(closedMinBid.Bytes(), 32)
  671. paddedMaxBid := ethCommon.LeftPadBytes(maxBid.Bytes(), 32)
  672. var userData []byte
  673. userData = append(userData, methodID...)
  674. userData = append(userData, paddedStartingSlot...)
  675. userData = append(userData, paddedEndingSlot...)
  676. for i := 0; i < len(slotSet); i++ {
  677. if slotSet[i] {
  678. paddedSlotSet := ethCommon.LeftPadBytes([]byte{1}, 32)
  679. userData = append(userData, paddedSlotSet...)
  680. } else {
  681. paddedSlotSet := ethCommon.LeftPadBytes([]byte{0}, 32)
  682. userData = append(userData, paddedSlotSet...)
  683. }
  684. }
  685. userData = append(userData, paddedMaxBid...)
  686. userData = append(userData, paddedMinBid...)
  687. return tokens.Send(auth, c.address, budget, userData)
  688. },
  689. ); err != nil {
  690. return nil, fmt.Errorf("Failed multibid: %w", err)
  691. }
  692. return tx, nil
  693. }
  694. // AuctionCanForge is the interface to call the smart contract function
  695. func (c *AuctionClient) AuctionCanForge(forger ethCommon.Address, blockNum int64) (bool, error) {
  696. var canForge bool
  697. if err := c.client.Call(func(ec *ethclient.Client) error {
  698. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  699. if err != nil {
  700. return err
  701. }
  702. canForge, err = auction.CanForge(nil, forger, big.NewInt(blockNum))
  703. return err
  704. }); err != nil {
  705. return false, err
  706. }
  707. return canForge, nil
  708. }
  709. // AuctionClaimHEZ is the interface to call the smart contract function
  710. func (c *AuctionClient) AuctionClaimHEZ() (*types.Transaction, error) {
  711. var tx *types.Transaction
  712. var err error
  713. if tx, err = c.client.CallAuth(
  714. c.gasLimit,
  715. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  716. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  717. if err != nil {
  718. return nil, err
  719. }
  720. return auction.ClaimHEZ(auth)
  721. },
  722. ); err != nil {
  723. return nil, fmt.Errorf("Failed claim HEZ: %w", err)
  724. }
  725. return tx, nil
  726. }
  727. // AuctionForge is the interface to call the smart contract function
  728. func (c *AuctionClient) AuctionForge(forger ethCommon.Address) (*types.Transaction, error) {
  729. var tx *types.Transaction
  730. var err error
  731. if tx, err = c.client.CallAuth(
  732. c.gasLimit,
  733. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  734. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  735. if err != nil {
  736. return nil, err
  737. }
  738. return auction.Forge(auth, forger)
  739. },
  740. ); err != nil {
  741. return nil, fmt.Errorf("Failed forge: %w", err)
  742. }
  743. return tx, nil
  744. }
  745. // AuctionConstants returns the Constants of the Auction Smart Contract
  746. func (c *AuctionClient) AuctionConstants() (*AuctionConstants, error) {
  747. auctionConstants := new(AuctionConstants)
  748. if err := c.client.Call(func(ec *ethclient.Client) error {
  749. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
  750. if err != nil {
  751. return err
  752. }
  753. auctionConstants.BlocksPerSlot, err = auction.BLOCKSPERSLOT(nil)
  754. if err != nil {
  755. return err
  756. }
  757. genesisBlock, err := auction.GenesisBlock(nil)
  758. if err != nil {
  759. return err
  760. }
  761. auctionConstants.GenesisBlockNum = genesisBlock.Int64()
  762. auctionConstants.HermezRollup, err = auction.HermezRollup(nil)
  763. if err != nil {
  764. return err
  765. }
  766. auctionConstants.InitialMinimalBidding, err = auction.INITIALMINIMALBIDDING(nil)
  767. if err != nil {
  768. return err
  769. }
  770. auctionConstants.TokenHEZ, err = auction.TokenHEZ(nil)
  771. return err
  772. }); err != nil {
  773. return nil, err
  774. }
  775. return auctionConstants, nil
  776. }
  777. // AuctionVariables returns the variables of the Auction Smart Contract
  778. func (c *AuctionClient) AuctionVariables() (*AuctionVariables, error) {
  779. auctionVariables := new(AuctionVariables)
  780. if err := c.client.Call(func(ec *ethclient.Client) error {
  781. var err error
  782. auctionVariables.AllocationRatio, err = c.AuctionGetAllocationRatio()
  783. if err != nil {
  784. return err
  785. }
  786. bootCoordinator, err := c.AuctionGetBootCoordinator()
  787. if err != nil {
  788. return err
  789. }
  790. auctionVariables.BootCoordinator = *bootCoordinator
  791. auctionVariables.ClosedAuctionSlots, err = c.AuctionGetClosedAuctionSlots()
  792. if err != nil {
  793. return err
  794. }
  795. var defaultSlotSetBid [6]*big.Int
  796. for i := uint8(0); i < 6; i++ {
  797. bid, err := c.AuctionGetDefaultSlotSetBid(i)
  798. if err != nil {
  799. return err
  800. }
  801. defaultSlotSetBid[i] = bid
  802. }
  803. auctionVariables.DefaultSlotSetBid = defaultSlotSetBid
  804. donationAddress, err := c.AuctionGetDonationAddress()
  805. if err != nil {
  806. return err
  807. }
  808. auctionVariables.DonationAddress = *donationAddress
  809. auctionVariables.OpenAuctionSlots, err = c.AuctionGetOpenAuctionSlots()
  810. if err != nil {
  811. return err
  812. }
  813. auctionVariables.Outbidding, err = c.AuctionGetOutbidding()
  814. if err != nil {
  815. return err
  816. }
  817. auctionVariables.SlotDeadline, err = c.AuctionGetSlotDeadline()
  818. return err
  819. }); err != nil {
  820. return nil, err
  821. }
  822. return auctionVariables, nil
  823. }
  824. var (
  825. logAuctionNewBid = crypto.Keccak256Hash([]byte("NewBid(uint128,uint128,address)"))
  826. logAuctionNewSlotDeadline = crypto.Keccak256Hash([]byte("NewSlotDeadline(uint8)"))
  827. logAuctionNewClosedAuctionSlots = crypto.Keccak256Hash([]byte("NewClosedAuctionSlots(uint16)"))
  828. logAuctionNewOutbidding = crypto.Keccak256Hash([]byte("NewOutbidding(uint16)"))
  829. logAuctionNewDonationAddress = crypto.Keccak256Hash([]byte("NewDonationAddress(address)"))
  830. logAuctionNewBootCoordinator = crypto.Keccak256Hash([]byte("NewBootCoordinator(address)"))
  831. logAuctionNewOpenAuctionSlots = crypto.Keccak256Hash([]byte("NewOpenAuctionSlots(uint16)"))
  832. logAuctionNewAllocationRatio = crypto.Keccak256Hash([]byte("NewAllocationRatio(uint16[3])"))
  833. logAuctionSetCoordinator = crypto.Keccak256Hash([]byte("SetCoordinator(address,address,string)"))
  834. logAuctionNewForgeAllocated = crypto.Keccak256Hash([]byte("NewForgeAllocated(address,address,uint128,uint128,uint128,uint128)"))
  835. logAuctionNewDefaultSlotSetBid = crypto.Keccak256Hash([]byte("NewDefaultSlotSetBid(uint128,uint128)"))
  836. logAuctionNewForge = crypto.Keccak256Hash([]byte("NewForge(address,uint128)"))
  837. logAuctionHEZClaimed = crypto.Keccak256Hash([]byte("HEZClaimed(address,uint128)"))
  838. )
  839. // AuctionEventsByBlock returns the events in a block that happened in the
  840. // Auction Smart Contract and the blockHash where the eents happened. If there
  841. // are no events in that block, blockHash is nil.
  842. func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error) {
  843. var auctionEvents AuctionEvents
  844. var blockHash ethCommon.Hash
  845. query := ethereum.FilterQuery{
  846. FromBlock: big.NewInt(blockNum),
  847. ToBlock: big.NewInt(blockNum),
  848. Addresses: []ethCommon.Address{
  849. c.address,
  850. },
  851. Topics: [][]ethCommon.Hash{},
  852. }
  853. logs, err := c.client.client.FilterLogs(context.TODO(), query)
  854. if err != nil {
  855. return nil, nil, err
  856. }
  857. if len(logs) > 0 {
  858. blockHash = logs[0].BlockHash
  859. }
  860. for _, vLog := range logs {
  861. if vLog.BlockHash != blockHash {
  862. return nil, nil, ErrBlockHashMismatchEvent
  863. }
  864. switch vLog.Topics[0] {
  865. case logAuctionNewBid:
  866. var auxNewBid struct {
  867. Slot *big.Int
  868. BidAmount *big.Int
  869. Address ethCommon.Address
  870. }
  871. var newBid AuctionEventNewBid
  872. if err := c.contractAbi.Unpack(&auxNewBid, "NewBid", vLog.Data); err != nil {
  873. return nil, nil, err
  874. }
  875. newBid.BidAmount = auxNewBid.BidAmount
  876. newBid.Slot = new(big.Int).SetBytes(vLog.Topics[1][:]).Int64()
  877. newBid.Bidder = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  878. auctionEvents.NewBid = append(auctionEvents.NewBid, newBid)
  879. case logAuctionNewSlotDeadline:
  880. var newSlotDeadline AuctionEventNewSlotDeadline
  881. if err := c.contractAbi.Unpack(&newSlotDeadline, "NewSlotDeadline", vLog.Data); err != nil {
  882. return nil, nil, err
  883. }
  884. auctionEvents.NewSlotDeadline = append(auctionEvents.NewSlotDeadline, newSlotDeadline)
  885. case logAuctionNewClosedAuctionSlots:
  886. var newClosedAuctionSlots AuctionEventNewClosedAuctionSlots
  887. if err := c.contractAbi.Unpack(&newClosedAuctionSlots, "NewClosedAuctionSlots", vLog.Data); err != nil {
  888. return nil, nil, err
  889. }
  890. auctionEvents.NewClosedAuctionSlots = append(auctionEvents.NewClosedAuctionSlots, newClosedAuctionSlots)
  891. case logAuctionNewOutbidding:
  892. var newOutbidding AuctionEventNewOutbidding
  893. if err := c.contractAbi.Unpack(&newOutbidding, "NewOutbidding", vLog.Data); err != nil {
  894. return nil, nil, err
  895. }
  896. auctionEvents.NewOutbidding = append(auctionEvents.NewOutbidding, newOutbidding)
  897. case logAuctionNewDonationAddress:
  898. var newDonationAddress AuctionEventNewDonationAddress
  899. newDonationAddress.NewDonationAddress = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  900. auctionEvents.NewDonationAddress = append(auctionEvents.NewDonationAddress, newDonationAddress)
  901. case logAuctionNewBootCoordinator:
  902. var newBootCoordinator AuctionEventNewBootCoordinator
  903. newBootCoordinator.NewBootCoordinator = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  904. auctionEvents.NewBootCoordinator = append(auctionEvents.NewBootCoordinator, newBootCoordinator)
  905. case logAuctionNewOpenAuctionSlots:
  906. var newOpenAuctionSlots AuctionEventNewOpenAuctionSlots
  907. if err := c.contractAbi.Unpack(&newOpenAuctionSlots, "NewOpenAuctionSlots", vLog.Data); err != nil {
  908. return nil, nil, err
  909. }
  910. auctionEvents.NewOpenAuctionSlots = append(auctionEvents.NewOpenAuctionSlots, newOpenAuctionSlots)
  911. case logAuctionNewAllocationRatio:
  912. var newAllocationRatio AuctionEventNewAllocationRatio
  913. if err := c.contractAbi.Unpack(&newAllocationRatio, "NewAllocationRatio", vLog.Data); err != nil {
  914. return nil, nil, err
  915. }
  916. auctionEvents.NewAllocationRatio = append(auctionEvents.NewAllocationRatio, newAllocationRatio)
  917. case logAuctionSetCoordinator:
  918. var setCoordinator AuctionEventSetCoordinator
  919. if err := c.contractAbi.Unpack(&setCoordinator, "SetCoordinator", vLog.Data); err != nil {
  920. return nil, nil, err
  921. }
  922. setCoordinator.BidderAddress = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  923. setCoordinator.ForgerAddress = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  924. auctionEvents.SetCoordinator = append(auctionEvents.SetCoordinator, setCoordinator)
  925. case logAuctionNewForgeAllocated:
  926. var newForgeAllocated AuctionEventNewForgeAllocated
  927. if err := c.contractAbi.Unpack(&newForgeAllocated, "NewForgeAllocated", vLog.Data); err != nil {
  928. return nil, nil, err
  929. }
  930. newForgeAllocated.Bidder = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  931. newForgeAllocated.Forger = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  932. newForgeAllocated.SlotToForge = new(big.Int).SetBytes(vLog.Topics[3][:]).Int64()
  933. auctionEvents.NewForgeAllocated = append(auctionEvents.NewForgeAllocated, newForgeAllocated)
  934. case logAuctionNewDefaultSlotSetBid:
  935. var auxNewDefaultSlotSetBid struct {
  936. SlotSet *big.Int
  937. NewInitialMinBid *big.Int
  938. }
  939. var newDefaultSlotSetBid AuctionEventNewDefaultSlotSetBid
  940. if err := c.contractAbi.Unpack(&auxNewDefaultSlotSetBid, "NewDefaultSlotSetBid", vLog.Data); err != nil {
  941. return nil, nil, err
  942. }
  943. newDefaultSlotSetBid.NewInitialMinBid = auxNewDefaultSlotSetBid.NewInitialMinBid
  944. newDefaultSlotSetBid.SlotSet = auxNewDefaultSlotSetBid.SlotSet.Int64()
  945. auctionEvents.NewDefaultSlotSetBid = append(auctionEvents.NewDefaultSlotSetBid, newDefaultSlotSetBid)
  946. case logAuctionNewForge:
  947. var newForge AuctionEventNewForge
  948. newForge.Forger = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  949. newForge.SlotToForge = new(big.Int).SetBytes(vLog.Topics[2][:]).Int64()
  950. auctionEvents.NewForge = append(auctionEvents.NewForge, newForge)
  951. case logAuctionHEZClaimed:
  952. var HEZClaimed AuctionEventHEZClaimed
  953. if err := c.contractAbi.Unpack(&HEZClaimed, "HEZClaimed", vLog.Data); err != nil {
  954. return nil, nil, err
  955. }
  956. HEZClaimed.Owner = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  957. auctionEvents.HEZClaimed = append(auctionEvents.HEZClaimed, HEZClaimed)
  958. }
  959. }
  960. return &auctionEvents, &blockHash, nil
  961. }