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.

893 lines
33 KiB

4 years ago
  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 `json:"blocksPerSlot"`
  23. // Minimum bid when no one has bid yet
  24. InitialMinimalBidding *big.Int `json:"initialMinimalBidding"`
  25. // First block where the first slot begins
  26. GenesisBlockNum int64 `json:"genesisBlockNum"`
  27. // ERC777 token with which the bids will be made
  28. TokenHEZ ethCommon.Address `json:"tokenHEZ"`
  29. // HermezRollup smartcontract address
  30. HermezRollup ethCommon.Address `json:"hermezRollup"`
  31. // Hermez Governanze Token smartcontract address who controls some parameters and collects HEZ fee
  32. // Only for test
  33. GovernanceAddress ethCommon.Address `json:"governanceAddress"`
  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 {
  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. auction *HermezAuctionProtocol.HermezAuctionProtocol
  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. auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(address, client.Client())
  244. if err != nil {
  245. return nil, err
  246. }
  247. return &AuctionClient{
  248. client: client,
  249. address: address,
  250. tokenAddress: tokenAddress,
  251. auction: auction,
  252. contractAbi: contractAbi,
  253. }, nil
  254. }
  255. // AuctionSetSlotDeadline is the interface to call the smart contract function
  256. func (c *AuctionClient) AuctionSetSlotDeadline(newDeadline uint8) (*types.Transaction, error) {
  257. var tx *types.Transaction
  258. var err error
  259. if tx, err = c.client.CallAuth(
  260. 0,
  261. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  262. return c.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() (slotDeadline uint8, err error) {
  271. if err := c.client.Call(func(ec *ethclient.Client) error {
  272. slotDeadline, err = c.auction.GetSlotDeadline(nil)
  273. return err
  274. }); err != nil {
  275. return 0, err
  276. }
  277. return slotDeadline, nil
  278. }
  279. // AuctionSetOpenAuctionSlots is the interface to call the smart contract function
  280. func (c *AuctionClient) AuctionSetOpenAuctionSlots(newOpenAuctionSlots uint16) (tx *types.Transaction, err error) {
  281. if tx, err = c.client.CallAuth(
  282. 0,
  283. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  284. return c.auction.SetOpenAuctionSlots(auth, newOpenAuctionSlots)
  285. },
  286. ); err != nil {
  287. return nil, fmt.Errorf("Failed setting openAuctionSlots: %w", err)
  288. }
  289. return tx, nil
  290. }
  291. // AuctionGetOpenAuctionSlots is the interface to call the smart contract function
  292. func (c *AuctionClient) AuctionGetOpenAuctionSlots() (openAuctionSlots uint16, err error) {
  293. if err := c.client.Call(func(ec *ethclient.Client) error {
  294. openAuctionSlots, err = c.auction.GetOpenAuctionSlots(nil)
  295. return err
  296. }); err != nil {
  297. return 0, err
  298. }
  299. return openAuctionSlots, nil
  300. }
  301. // AuctionSetClosedAuctionSlots is the interface to call the smart contract function
  302. func (c *AuctionClient) AuctionSetClosedAuctionSlots(newClosedAuctionSlots uint16) (tx *types.Transaction, err error) {
  303. if tx, err = c.client.CallAuth(
  304. 0,
  305. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  306. return c.auction.SetClosedAuctionSlots(auth, newClosedAuctionSlots)
  307. },
  308. ); err != nil {
  309. return nil, fmt.Errorf("Failed setting closedAuctionSlots: %w", err)
  310. }
  311. return tx, nil
  312. }
  313. // AuctionGetClosedAuctionSlots is the interface to call the smart contract function
  314. func (c *AuctionClient) AuctionGetClosedAuctionSlots() (closedAuctionSlots uint16, err error) {
  315. if err := c.client.Call(func(ec *ethclient.Client) error {
  316. closedAuctionSlots, err = c.auction.GetClosedAuctionSlots(nil)
  317. return err
  318. }); err != nil {
  319. return 0, err
  320. }
  321. return closedAuctionSlots, nil
  322. }
  323. // AuctionSetOutbidding is the interface to call the smart contract function
  324. func (c *AuctionClient) AuctionSetOutbidding(newOutbidding uint16) (tx *types.Transaction, err error) {
  325. if tx, err = c.client.CallAuth(
  326. 12500000, //nolint:gomnd
  327. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  328. return c.auction.SetOutbidding(auth, newOutbidding)
  329. },
  330. ); err != nil {
  331. return nil, fmt.Errorf("Failed setting setOutbidding: %w", err)
  332. }
  333. return tx, nil
  334. }
  335. // AuctionGetOutbidding is the interface to call the smart contract function
  336. func (c *AuctionClient) AuctionGetOutbidding() (outbidding uint16, err error) {
  337. if err := c.client.Call(func(ec *ethclient.Client) error {
  338. outbidding, err = c.auction.GetOutbidding(nil)
  339. return err
  340. }); err != nil {
  341. return 0, err
  342. }
  343. return outbidding, nil
  344. }
  345. // AuctionSetAllocationRatio is the interface to call the smart contract function
  346. func (c *AuctionClient) AuctionSetAllocationRatio(newAllocationRatio [3]uint16) (tx *types.Transaction, err error) {
  347. if tx, err = c.client.CallAuth(
  348. 0,
  349. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  350. return c.auction.SetAllocationRatio(auth, newAllocationRatio)
  351. },
  352. ); err != nil {
  353. return nil, fmt.Errorf("Failed setting allocationRatio: %w", err)
  354. }
  355. return tx, nil
  356. }
  357. // AuctionGetAllocationRatio is the interface to call the smart contract function
  358. func (c *AuctionClient) AuctionGetAllocationRatio() (allocationRation [3]uint16, err error) {
  359. if err := c.client.Call(func(ec *ethclient.Client) error {
  360. allocationRation, err = c.auction.GetAllocationRatio(nil)
  361. return err
  362. }); err != nil {
  363. return [3]uint16{}, err
  364. }
  365. return allocationRation, nil
  366. }
  367. // AuctionSetDonationAddress is the interface to call the smart contract function
  368. func (c *AuctionClient) AuctionSetDonationAddress(newDonationAddress ethCommon.Address) (tx *types.Transaction, err error) {
  369. if tx, err = c.client.CallAuth(
  370. 0,
  371. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  372. return c.auction.SetDonationAddress(auth, newDonationAddress)
  373. },
  374. ); err != nil {
  375. return nil, fmt.Errorf("Failed setting donationAddress: %w", err)
  376. }
  377. return tx, nil
  378. }
  379. // AuctionGetDonationAddress is the interface to call the smart contract function
  380. func (c *AuctionClient) AuctionGetDonationAddress() (donationAddress *ethCommon.Address, err error) {
  381. var _donationAddress ethCommon.Address
  382. if err := c.client.Call(func(ec *ethclient.Client) error {
  383. _donationAddress, err = c.auction.GetDonationAddress(nil)
  384. return err
  385. }); err != nil {
  386. return nil, err
  387. }
  388. return &_donationAddress, nil
  389. }
  390. // AuctionSetBootCoordinator is the interface to call the smart contract function
  391. func (c *AuctionClient) AuctionSetBootCoordinator(newBootCoordinator ethCommon.Address) (tx *types.Transaction, err error) {
  392. if tx, err = c.client.CallAuth(
  393. 0,
  394. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  395. return c.auction.SetBootCoordinator(auth, newBootCoordinator)
  396. },
  397. ); err != nil {
  398. return nil, fmt.Errorf("Failed setting bootCoordinator: %w", err)
  399. }
  400. return tx, nil
  401. }
  402. // AuctionGetBootCoordinator is the interface to call the smart contract function
  403. func (c *AuctionClient) AuctionGetBootCoordinator() (bootCoordinator *ethCommon.Address, err error) {
  404. var _bootCoordinator ethCommon.Address
  405. if err := c.client.Call(func(ec *ethclient.Client) error {
  406. _bootCoordinator, err = c.auction.GetBootCoordinator(nil)
  407. return err
  408. }); err != nil {
  409. return nil, err
  410. }
  411. return &_bootCoordinator, nil
  412. }
  413. // AuctionChangeDefaultSlotSetBid is the interface to call the smart contract function
  414. func (c *AuctionClient) AuctionChangeDefaultSlotSetBid(slotSet int64, newInitialMinBid *big.Int) (tx *types.Transaction, err error) {
  415. if tx, err = c.client.CallAuth(
  416. 0,
  417. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  418. slotSetToSend := big.NewInt(slotSet)
  419. return c.auction.ChangeDefaultSlotSetBid(auth, slotSetToSend, newInitialMinBid)
  420. },
  421. ); err != nil {
  422. return nil, fmt.Errorf("Failed changing slotSet Bid: %w", err)
  423. }
  424. return tx, nil
  425. }
  426. // AuctionGetClaimableHEZ is the interface to call the smart contract function
  427. func (c *AuctionClient) AuctionGetClaimableHEZ(claimAddress ethCommon.Address) (claimableHEZ *big.Int, err error) {
  428. if err := c.client.Call(func(ec *ethclient.Client) error {
  429. claimableHEZ, err = c.auction.GetClaimableHEZ(nil, claimAddress)
  430. return err
  431. }); err != nil {
  432. return nil, err
  433. }
  434. return claimableHEZ, nil
  435. }
  436. // AuctionSetCoordinator is the interface to call the smart contract function
  437. func (c *AuctionClient) AuctionSetCoordinator(forger ethCommon.Address, coordinatorURL string) (tx *types.Transaction, err error) {
  438. if tx, err = c.client.CallAuth(
  439. 0,
  440. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  441. return c.auction.SetCoordinator(auth, forger, coordinatorURL)
  442. },
  443. ); err != nil {
  444. return nil, fmt.Errorf("Failed set coordinator: %w", err)
  445. }
  446. return tx, nil
  447. }
  448. // AuctionGetCurrentSlotNumber is the interface to call the smart contract function
  449. func (c *AuctionClient) AuctionGetCurrentSlotNumber() (currentSlotNumber int64, err error) {
  450. var _currentSlotNumber *big.Int
  451. if err := c.client.Call(func(ec *ethclient.Client) error {
  452. _currentSlotNumber, err = c.auction.GetCurrentSlotNumber(nil)
  453. return err
  454. }); err != nil {
  455. return 0, err
  456. }
  457. return _currentSlotNumber.Int64(), nil
  458. }
  459. // AuctionGetMinBidBySlot is the interface to call the smart contract function
  460. func (c *AuctionClient) AuctionGetMinBidBySlot(slot int64) (minBid *big.Int, err error) {
  461. if err := c.client.Call(func(ec *ethclient.Client) error {
  462. slotToSend := big.NewInt(slot)
  463. minBid, err = c.auction.GetMinBidBySlot(nil, slotToSend)
  464. return err
  465. }); err != nil {
  466. return big.NewInt(0), err
  467. }
  468. return minBid, nil
  469. }
  470. // AuctionGetSlotSet is the interface to call the smart contract function
  471. func (c *AuctionClient) AuctionGetSlotSet(slot int64) (slotSet *big.Int, err error) {
  472. if err := c.client.Call(func(ec *ethclient.Client) error {
  473. slotToSend := big.NewInt(slot)
  474. slotSet, err = c.auction.GetSlotSet(nil, slotToSend)
  475. return err
  476. }); err != nil {
  477. return big.NewInt(0), err
  478. }
  479. return slotSet, nil
  480. }
  481. // AuctionGetDefaultSlotSetBid is the interface to call the smart contract function
  482. func (c *AuctionClient) AuctionGetDefaultSlotSetBid(slotSet uint8) (minBidSlotSet *big.Int, err error) {
  483. if err := c.client.Call(func(ec *ethclient.Client) error {
  484. minBidSlotSet, err = c.auction.GetDefaultSlotSetBid(nil, slotSet)
  485. return err
  486. }); err != nil {
  487. return big.NewInt(0), err
  488. }
  489. return minBidSlotSet, nil
  490. }
  491. // AuctionGetSlotNumber is the interface to call the smart contract function
  492. func (c *AuctionClient) AuctionGetSlotNumber(blockNum int64) (slot int64, err error) {
  493. var _slot *big.Int
  494. if err := c.client.Call(func(ec *ethclient.Client) error {
  495. _slot, err = c.auction.GetSlotNumber(nil, big.NewInt(blockNum))
  496. return err
  497. }); err != nil {
  498. return 0, err
  499. }
  500. return _slot.Int64(), nil
  501. }
  502. // AuctionBid is the interface to call the smart contract function
  503. func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int) (tx *types.Transaction, err error) {
  504. if tx, err = c.client.CallAuth(
  505. 0,
  506. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  507. tokens, err := ERC777.NewERC777(c.tokenAddress, ec)
  508. if err != nil {
  509. return nil, err
  510. }
  511. bidFnSignature := []byte("bid(uint128,uint128)")
  512. hash := sha3.NewLegacyKeccak256()
  513. _, err = hash.Write(bidFnSignature)
  514. if err != nil {
  515. return nil, err
  516. }
  517. methodID := hash.Sum(nil)[:4]
  518. slotBytes := make([]byte, 8)
  519. binary.BigEndian.PutUint64(slotBytes, uint64(slot))
  520. paddedSlot := ethCommon.LeftPadBytes(slotBytes, 32)
  521. paddedAmount := ethCommon.LeftPadBytes(bidAmount.Bytes(), 32)
  522. var userData []byte
  523. userData = append(userData, methodID...)
  524. userData = append(userData, paddedSlot...)
  525. userData = append(userData, paddedAmount...)
  526. return tokens.Send(auth, c.address, bidAmount, userData)
  527. },
  528. ); err != nil {
  529. return nil, fmt.Errorf("Failed bid: %w", err)
  530. }
  531. return tx, nil
  532. }
  533. // AuctionMultiBid is the interface to call the smart contract function
  534. func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool,
  535. maxBid, closedMinBid, budget *big.Int) (tx *types.Transaction, err error) {
  536. if tx, err = c.client.CallAuth(
  537. 1000000, //nolint:gomnd
  538. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  539. tokens, err := ERC777.NewERC777(c.tokenAddress, ec)
  540. if err != nil {
  541. return nil, err
  542. }
  543. multiBidFnSignature := []byte("multiBid(uint128,uint128,bool[6],uint128,uint128)")
  544. hash := sha3.NewLegacyKeccak256()
  545. _, err = hash.Write(multiBidFnSignature)
  546. if err != nil {
  547. return nil, err
  548. }
  549. methodID := hash.Sum(nil)[:4]
  550. startingSlotBytes := make([]byte, 8)
  551. binary.BigEndian.PutUint64(startingSlotBytes, uint64(startingSlot))
  552. paddedStartingSlot := ethCommon.LeftPadBytes(startingSlotBytes, 32)
  553. endingSlotBytes := make([]byte, 8)
  554. binary.BigEndian.PutUint64(endingSlotBytes, uint64(endingSlot))
  555. paddedEndingSlot := ethCommon.LeftPadBytes(endingSlotBytes, 32)
  556. paddedMinBid := ethCommon.LeftPadBytes(closedMinBid.Bytes(), 32)
  557. paddedMaxBid := ethCommon.LeftPadBytes(maxBid.Bytes(), 32)
  558. var userData []byte
  559. userData = append(userData, methodID...)
  560. userData = append(userData, paddedStartingSlot...)
  561. userData = append(userData, paddedEndingSlot...)
  562. for i := 0; i < len(slotSet); i++ {
  563. if slotSet[i] {
  564. paddedSlotSet := ethCommon.LeftPadBytes([]byte{1}, 32)
  565. userData = append(userData, paddedSlotSet...)
  566. } else {
  567. paddedSlotSet := ethCommon.LeftPadBytes([]byte{0}, 32)
  568. userData = append(userData, paddedSlotSet...)
  569. }
  570. }
  571. userData = append(userData, paddedMaxBid...)
  572. userData = append(userData, paddedMinBid...)
  573. return tokens.Send(auth, c.address, budget, userData)
  574. },
  575. ); err != nil {
  576. return nil, fmt.Errorf("Failed multibid: %w", err)
  577. }
  578. return tx, nil
  579. }
  580. // AuctionCanForge is the interface to call the smart contract function
  581. func (c *AuctionClient) AuctionCanForge(forger ethCommon.Address, blockNum int64) (canForge bool, err error) {
  582. if err := c.client.Call(func(ec *ethclient.Client) error {
  583. canForge, err = c.auction.CanForge(nil, forger, big.NewInt(blockNum))
  584. return err
  585. }); err != nil {
  586. return false, err
  587. }
  588. return canForge, nil
  589. }
  590. // AuctionClaimHEZ is the interface to call the smart contract function
  591. func (c *AuctionClient) AuctionClaimHEZ() (tx *types.Transaction, err error) {
  592. if tx, err = c.client.CallAuth(
  593. 0,
  594. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  595. return c.auction.ClaimHEZ(auth)
  596. },
  597. ); err != nil {
  598. return nil, fmt.Errorf("Failed claim HEZ: %w", err)
  599. }
  600. return tx, nil
  601. }
  602. // AuctionForge is the interface to call the smart contract function
  603. func (c *AuctionClient) AuctionForge(forger ethCommon.Address) (tx *types.Transaction, err error) {
  604. if tx, err = c.client.CallAuth(
  605. 0,
  606. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  607. return c.auction.Forge(auth, forger)
  608. },
  609. ); err != nil {
  610. return nil, fmt.Errorf("Failed forge: %w", err)
  611. }
  612. return tx, nil
  613. }
  614. // AuctionConstants returns the Constants of the Auction Smart Contract
  615. func (c *AuctionClient) AuctionConstants() (auctionConstants *AuctionConstants, err error) {
  616. auctionConstants = new(AuctionConstants)
  617. if err := c.client.Call(func(ec *ethclient.Client) error {
  618. auctionConstants.BlocksPerSlot, err = c.auction.BLOCKSPERSLOT(nil)
  619. if err != nil {
  620. return err
  621. }
  622. genesisBlock, err := c.auction.GenesisBlock(nil)
  623. if err != nil {
  624. return err
  625. }
  626. auctionConstants.GenesisBlockNum = genesisBlock.Int64()
  627. auctionConstants.HermezRollup, err = c.auction.HermezRollup(nil)
  628. if err != nil {
  629. return err
  630. }
  631. auctionConstants.InitialMinimalBidding, err = c.auction.INITIALMINIMALBIDDING(nil)
  632. if err != nil {
  633. return err
  634. }
  635. auctionConstants.TokenHEZ, err = c.auction.TokenHEZ(nil)
  636. return err
  637. }); err != nil {
  638. return nil, err
  639. }
  640. return auctionConstants, nil
  641. }
  642. // AuctionVariables returns the variables of the Auction Smart Contract
  643. func (c *AuctionClient) AuctionVariables() (auctionVariables *AuctionVariables, err error) {
  644. auctionVariables = new(AuctionVariables)
  645. if err := c.client.Call(func(ec *ethclient.Client) error {
  646. auctionVariables.AllocationRatio, err = c.AuctionGetAllocationRatio()
  647. if err != nil {
  648. return err
  649. }
  650. bootCoordinator, err := c.AuctionGetBootCoordinator()
  651. if err != nil {
  652. return err
  653. }
  654. auctionVariables.BootCoordinator = *bootCoordinator
  655. auctionVariables.ClosedAuctionSlots, err = c.AuctionGetClosedAuctionSlots()
  656. if err != nil {
  657. return err
  658. }
  659. var defaultSlotSetBid [6]*big.Int
  660. for i := uint8(0); i < 6; i++ {
  661. bid, err := c.AuctionGetDefaultSlotSetBid(i)
  662. if err != nil {
  663. return err
  664. }
  665. defaultSlotSetBid[i] = bid
  666. }
  667. auctionVariables.DefaultSlotSetBid = defaultSlotSetBid
  668. donationAddress, err := c.AuctionGetDonationAddress()
  669. if err != nil {
  670. return err
  671. }
  672. auctionVariables.DonationAddress = *donationAddress
  673. auctionVariables.OpenAuctionSlots, err = c.AuctionGetOpenAuctionSlots()
  674. if err != nil {
  675. return err
  676. }
  677. auctionVariables.Outbidding, err = c.AuctionGetOutbidding()
  678. if err != nil {
  679. return err
  680. }
  681. auctionVariables.SlotDeadline, err = c.AuctionGetSlotDeadline()
  682. return err
  683. }); err != nil {
  684. return nil, err
  685. }
  686. return auctionVariables, nil
  687. }
  688. var (
  689. logAuctionNewBid = crypto.Keccak256Hash([]byte("NewBid(uint128,uint128,address)"))
  690. logAuctionNewSlotDeadline = crypto.Keccak256Hash([]byte("NewSlotDeadline(uint8)"))
  691. logAuctionNewClosedAuctionSlots = crypto.Keccak256Hash([]byte("NewClosedAuctionSlots(uint16)"))
  692. logAuctionNewOutbidding = crypto.Keccak256Hash([]byte("NewOutbidding(uint16)"))
  693. logAuctionNewDonationAddress = crypto.Keccak256Hash([]byte("NewDonationAddress(address)"))
  694. logAuctionNewBootCoordinator = crypto.Keccak256Hash([]byte("NewBootCoordinator(address)"))
  695. logAuctionNewOpenAuctionSlots = crypto.Keccak256Hash([]byte("NewOpenAuctionSlots(uint16)"))
  696. logAuctionNewAllocationRatio = crypto.Keccak256Hash([]byte("NewAllocationRatio(uint16[3])"))
  697. logAuctionSetCoordinator = crypto.Keccak256Hash([]byte("SetCoordinator(address,address,string)"))
  698. logAuctionNewForgeAllocated = crypto.Keccak256Hash([]byte("NewForgeAllocated(address,address,uint128,uint128,uint128,uint128)"))
  699. logAuctionNewDefaultSlotSetBid = crypto.Keccak256Hash([]byte("NewDefaultSlotSetBid(uint128,uint128)"))
  700. logAuctionNewForge = crypto.Keccak256Hash([]byte("NewForge(address,uint128)"))
  701. logAuctionHEZClaimed = crypto.Keccak256Hash([]byte("HEZClaimed(address,uint128)"))
  702. )
  703. // AuctionEventsByBlock returns the events in a block that happened in the
  704. // Auction Smart Contract and the blockHash where the eents happened. If there
  705. // are no events in that block, blockHash is nil.
  706. func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *ethCommon.Hash, error) {
  707. var auctionEvents AuctionEvents
  708. var blockHash ethCommon.Hash
  709. query := ethereum.FilterQuery{
  710. FromBlock: big.NewInt(blockNum),
  711. ToBlock: big.NewInt(blockNum),
  712. Addresses: []ethCommon.Address{
  713. c.address,
  714. },
  715. Topics: [][]ethCommon.Hash{},
  716. }
  717. logs, err := c.client.client.FilterLogs(context.TODO(), query)
  718. if err != nil {
  719. return nil, nil, err
  720. }
  721. if len(logs) > 0 {
  722. blockHash = logs[0].BlockHash
  723. }
  724. for _, vLog := range logs {
  725. if vLog.BlockHash != blockHash {
  726. return nil, nil, ErrBlockHashMismatchEvent
  727. }
  728. switch vLog.Topics[0] {
  729. case logAuctionNewBid:
  730. var auxNewBid struct {
  731. Slot *big.Int
  732. BidAmount *big.Int
  733. Address ethCommon.Address
  734. }
  735. var newBid AuctionEventNewBid
  736. if err := c.contractAbi.Unpack(&auxNewBid, "NewBid", vLog.Data); err != nil {
  737. return nil, nil, err
  738. }
  739. newBid.BidAmount = auxNewBid.BidAmount
  740. newBid.Slot = new(big.Int).SetBytes(vLog.Topics[1][:]).Int64()
  741. newBid.Bidder = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  742. auctionEvents.NewBid = append(auctionEvents.NewBid, newBid)
  743. case logAuctionNewSlotDeadline:
  744. var newSlotDeadline AuctionEventNewSlotDeadline
  745. if err := c.contractAbi.Unpack(&newSlotDeadline, "NewSlotDeadline", vLog.Data); err != nil {
  746. return nil, nil, err
  747. }
  748. auctionEvents.NewSlotDeadline = append(auctionEvents.NewSlotDeadline, newSlotDeadline)
  749. case logAuctionNewClosedAuctionSlots:
  750. var newClosedAuctionSlots AuctionEventNewClosedAuctionSlots
  751. if err := c.contractAbi.Unpack(&newClosedAuctionSlots, "NewClosedAuctionSlots", vLog.Data); err != nil {
  752. return nil, nil, err
  753. }
  754. auctionEvents.NewClosedAuctionSlots = append(auctionEvents.NewClosedAuctionSlots, newClosedAuctionSlots)
  755. case logAuctionNewOutbidding:
  756. var newOutbidding AuctionEventNewOutbidding
  757. if err := c.contractAbi.Unpack(&newOutbidding, "NewOutbidding", vLog.Data); err != nil {
  758. return nil, nil, err
  759. }
  760. auctionEvents.NewOutbidding = append(auctionEvents.NewOutbidding, newOutbidding)
  761. case logAuctionNewDonationAddress:
  762. var newDonationAddress AuctionEventNewDonationAddress
  763. newDonationAddress.NewDonationAddress = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  764. auctionEvents.NewDonationAddress = append(auctionEvents.NewDonationAddress, newDonationAddress)
  765. case logAuctionNewBootCoordinator:
  766. var newBootCoordinator AuctionEventNewBootCoordinator
  767. newBootCoordinator.NewBootCoordinator = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  768. auctionEvents.NewBootCoordinator = append(auctionEvents.NewBootCoordinator, newBootCoordinator)
  769. case logAuctionNewOpenAuctionSlots:
  770. var newOpenAuctionSlots AuctionEventNewOpenAuctionSlots
  771. if err := c.contractAbi.Unpack(&newOpenAuctionSlots, "NewOpenAuctionSlots", vLog.Data); err != nil {
  772. return nil, nil, err
  773. }
  774. auctionEvents.NewOpenAuctionSlots = append(auctionEvents.NewOpenAuctionSlots, newOpenAuctionSlots)
  775. case logAuctionNewAllocationRatio:
  776. var newAllocationRatio AuctionEventNewAllocationRatio
  777. if err := c.contractAbi.Unpack(&newAllocationRatio, "NewAllocationRatio", vLog.Data); err != nil {
  778. return nil, nil, err
  779. }
  780. auctionEvents.NewAllocationRatio = append(auctionEvents.NewAllocationRatio, newAllocationRatio)
  781. case logAuctionSetCoordinator:
  782. var setCoordinator AuctionEventSetCoordinator
  783. if err := c.contractAbi.Unpack(&setCoordinator, "SetCoordinator", vLog.Data); err != nil {
  784. return nil, nil, err
  785. }
  786. setCoordinator.BidderAddress = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  787. setCoordinator.ForgerAddress = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  788. auctionEvents.SetCoordinator = append(auctionEvents.SetCoordinator, setCoordinator)
  789. case logAuctionNewForgeAllocated:
  790. var newForgeAllocated AuctionEventNewForgeAllocated
  791. if err := c.contractAbi.Unpack(&newForgeAllocated, "NewForgeAllocated", vLog.Data); err != nil {
  792. return nil, nil, err
  793. }
  794. newForgeAllocated.Bidder = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  795. newForgeAllocated.Forger = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  796. newForgeAllocated.SlotToForge = new(big.Int).SetBytes(vLog.Topics[3][:]).Int64()
  797. auctionEvents.NewForgeAllocated = append(auctionEvents.NewForgeAllocated, newForgeAllocated)
  798. case logAuctionNewDefaultSlotSetBid:
  799. var auxNewDefaultSlotSetBid struct {
  800. SlotSet *big.Int
  801. NewInitialMinBid *big.Int
  802. }
  803. var newDefaultSlotSetBid AuctionEventNewDefaultSlotSetBid
  804. if err := c.contractAbi.Unpack(&auxNewDefaultSlotSetBid, "NewDefaultSlotSetBid", vLog.Data); err != nil {
  805. return nil, nil, err
  806. }
  807. newDefaultSlotSetBid.NewInitialMinBid = auxNewDefaultSlotSetBid.NewInitialMinBid
  808. newDefaultSlotSetBid.SlotSet = auxNewDefaultSlotSetBid.SlotSet.Int64()
  809. auctionEvents.NewDefaultSlotSetBid = append(auctionEvents.NewDefaultSlotSetBid, newDefaultSlotSetBid)
  810. case logAuctionNewForge:
  811. var newForge AuctionEventNewForge
  812. newForge.Forger = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  813. newForge.SlotToForge = new(big.Int).SetBytes(vLog.Topics[2][:]).Int64()
  814. auctionEvents.NewForge = append(auctionEvents.NewForge, newForge)
  815. case logAuctionHEZClaimed:
  816. var HEZClaimed AuctionEventHEZClaimed
  817. if err := c.contractAbi.Unpack(&HEZClaimed, "HEZClaimed", vLog.Data); err != nil {
  818. return nil, nil, err
  819. }
  820. HEZClaimed.Owner = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  821. auctionEvents.HEZClaimed = append(auctionEvents.HEZClaimed, HEZClaimed)
  822. }
  823. }
  824. return &auctionEvents, &blockHash, nil
  825. }