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.

442 lines
22 KiB

3 years ago
3 years ago
3 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
  1. package historydb
  2. import (
  3. "encoding/json"
  4. "math/big"
  5. "time"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/api/apitypes"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/iden3/go-iden3-crypto/babyjub"
  10. "github.com/iden3/go-merkletree"
  11. )
  12. // TxAPI is a representation of a generic Tx with additional information
  13. // required by the API, and extracted by joining block and token tables
  14. type TxAPI struct {
  15. // Generic
  16. IsL1 bool `meddler:"is_l1"`
  17. TxID common.TxID `meddler:"id"`
  18. ItemID uint64 `meddler:"item_id"`
  19. Type common.TxType `meddler:"type"`
  20. Position int `meddler:"position"`
  21. FromIdx *apitypes.HezIdx `meddler:"from_idx"`
  22. FromEthAddr *apitypes.HezEthAddr `meddler:"from_eth_addr"`
  23. FromBJJ *apitypes.HezBJJ `meddler:"from_bjj"`
  24. ToIdx apitypes.HezIdx `meddler:"to_idx"`
  25. ToEthAddr *apitypes.HezEthAddr `meddler:"to_eth_addr"`
  26. ToBJJ *apitypes.HezBJJ `meddler:"to_bjj"`
  27. Amount apitypes.BigIntStr `meddler:"amount"`
  28. HistoricUSD *float64 `meddler:"amount_usd"`
  29. BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  30. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  31. // L1
  32. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  33. UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
  34. DepositAmount *apitypes.BigIntStr `meddler:"deposit_amount"`
  35. HistoricDepositAmountUSD *float64 `meddler:"deposit_amount_usd"`
  36. AmountSuccess bool `meddler:"amount_success"`
  37. DepositAmountSuccess bool `meddler:"deposit_amount_success"`
  38. // L2
  39. Fee *common.FeeSelector `meddler:"fee"`
  40. HistoricFeeUSD *float64 `meddler:"fee_usd"`
  41. Nonce *common.Nonce `meddler:"nonce"`
  42. // API extras
  43. Timestamp time.Time `meddler:"timestamp,utctime"`
  44. TotalItems uint64 `meddler:"total_items"`
  45. FirstItem uint64 `meddler:"first_item"`
  46. LastItem uint64 `meddler:"last_item"`
  47. TokenID common.TokenID `meddler:"token_id"`
  48. TokenItemID uint64 `meddler:"token_item_id"`
  49. TokenEthBlockNum int64 `meddler:"token_block"`
  50. TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
  51. TokenName string `meddler:"name"`
  52. TokenSymbol string `meddler:"symbol"`
  53. TokenDecimals uint64 `meddler:"decimals"`
  54. TokenUSD *float64 `meddler:"usd"`
  55. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  56. }
  57. // MarshalJSON is used to neast some of the fields of TxAPI
  58. // without the need of auxiliar structs
  59. func (tx TxAPI) MarshalJSON() ([]byte, error) {
  60. jsonTx := map[string]interface{}{
  61. "id": tx.TxID,
  62. "itemId": tx.ItemID,
  63. "type": tx.Type,
  64. "position": tx.Position,
  65. "fromAccountIndex": tx.FromIdx,
  66. "fromHezEthereumAddress": tx.FromEthAddr,
  67. "fromBJJ": tx.FromBJJ,
  68. "toAccountIndex": tx.ToIdx,
  69. "toHezEthereumAddress": tx.ToEthAddr,
  70. "toBJJ": tx.ToBJJ,
  71. "amount": tx.Amount,
  72. "batchNum": tx.BatchNum,
  73. "historicUSD": tx.HistoricUSD,
  74. "timestamp": tx.Timestamp,
  75. "L1Info": nil,
  76. "L2Info": nil,
  77. "token": map[string]interface{}{
  78. "id": tx.TokenID,
  79. "itemId": tx.TokenItemID,
  80. "ethereumBlockNum": tx.TokenEthBlockNum,
  81. "ethereumAddress": tx.TokenEthAddr,
  82. "name": tx.TokenName,
  83. "symbol": tx.TokenSymbol,
  84. "decimals": tx.TokenDecimals,
  85. "USD": tx.TokenUSD,
  86. "fiatUpdate": tx.TokenUSDUpdate,
  87. },
  88. }
  89. if tx.IsL1 {
  90. jsonTx["L1orL2"] = "L1"
  91. amountSuccess := tx.AmountSuccess
  92. depositAmountSuccess := tx.DepositAmountSuccess
  93. if tx.BatchNum == nil {
  94. amountSuccess = false
  95. depositAmountSuccess = false
  96. }
  97. jsonTx["L1Info"] = map[string]interface{}{
  98. "toForgeL1TransactionsNum": tx.ToForgeL1TxsNum,
  99. "userOrigin": tx.UserOrigin,
  100. "depositAmount": tx.DepositAmount,
  101. "amountSuccess": amountSuccess,
  102. "depositAmountSuccess": depositAmountSuccess,
  103. "historicDepositAmountUSD": tx.HistoricDepositAmountUSD,
  104. "ethereumBlockNum": tx.EthBlockNum,
  105. }
  106. } else {
  107. jsonTx["L1orL2"] = "L2"
  108. jsonTx["L2Info"] = map[string]interface{}{
  109. "fee": tx.Fee,
  110. "historicFeeUSD": tx.HistoricFeeUSD,
  111. "nonce": tx.Nonce,
  112. }
  113. }
  114. return json.Marshal(jsonTx)
  115. }
  116. // txWrite is an representatiion that merges common.L1Tx and common.L2Tx
  117. // in order to perform inserts into tx table
  118. // EffectiveAmount and EffectiveDepositAmount are not set since they have default values in the DB
  119. type txWrite struct {
  120. // Generic
  121. IsL1 bool `meddler:"is_l1"`
  122. TxID common.TxID `meddler:"id"`
  123. Type common.TxType `meddler:"type"`
  124. Position int `meddler:"position"`
  125. FromIdx *common.Idx `meddler:"from_idx"`
  126. EffectiveFromIdx *common.Idx `meddler:"effective_from_idx"`
  127. ToIdx common.Idx `meddler:"to_idx"`
  128. Amount *big.Int `meddler:"amount,bigint"`
  129. AmountFloat float64 `meddler:"amount_f"`
  130. TokenID common.TokenID `meddler:"token_id"`
  131. BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  132. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  133. // L1
  134. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  135. UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
  136. FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
  137. FromBJJ *babyjub.PublicKeyComp `meddler:"from_bjj"`
  138. DepositAmount *big.Int `meddler:"deposit_amount,bigintnull"`
  139. DepositAmountFloat *float64 `meddler:"deposit_amount_f"`
  140. // L2
  141. Fee *common.FeeSelector `meddler:"fee"`
  142. Nonce *common.Nonce `meddler:"nonce"`
  143. }
  144. // TokenSymbolAndAddr token representation with only Eth addr and symbol
  145. type TokenSymbolAndAddr struct {
  146. Symbol string `meddler:"symbol"`
  147. Addr ethCommon.Address `meddler:"eth_addr"`
  148. }
  149. // TokenWithUSD add USD info to common.Token
  150. type TokenWithUSD struct {
  151. ItemID uint64 `json:"itemId" meddler:"item_id"`
  152. TokenID common.TokenID `json:"id" meddler:"token_id"`
  153. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` // Ethereum block number in which this token was registered
  154. EthAddr ethCommon.Address `json:"ethereumAddress" meddler:"eth_addr"`
  155. Name string `json:"name" meddler:"name"`
  156. Symbol string `json:"symbol" meddler:"symbol"`
  157. Decimals uint64 `json:"decimals" meddler:"decimals"`
  158. USD *float64 `json:"USD" meddler:"usd"`
  159. USDUpdate *time.Time `json:"fiatUpdate" meddler:"usd_update,utctime"`
  160. TotalItems uint64 `json:"-" meddler:"total_items"`
  161. FirstItem uint64 `json:"-" meddler:"first_item"`
  162. LastItem uint64 `json:"-" meddler:"last_item"`
  163. }
  164. // ExitAPI is a representation of a exit with additional information
  165. // required by the API, and extracted by joining token table
  166. type ExitAPI struct {
  167. ItemID uint64 `meddler:"item_id"`
  168. BatchNum common.BatchNum `meddler:"batch_num"`
  169. AccountIdx apitypes.HezIdx `meddler:"account_idx"`
  170. EthAddr *apitypes.HezEthAddr `meddler:"eth_addr"`
  171. BJJ *apitypes.HezBJJ `meddler:"bjj"`
  172. MerkleProof *merkletree.CircomVerifierProof `meddler:"merkle_proof,json"`
  173. Balance apitypes.BigIntStr `meddler:"balance"`
  174. InstantWithdrawn *int64 `meddler:"instant_withdrawn"`
  175. DelayedWithdrawRequest *int64 `meddler:"delayed_withdraw_request"`
  176. DelayedWithdrawn *int64 `meddler:"delayed_withdrawn"`
  177. TotalItems uint64 `meddler:"total_items"`
  178. FirstItem uint64 `meddler:"first_item"`
  179. LastItem uint64 `meddler:"last_item"`
  180. TokenID common.TokenID `meddler:"token_id"`
  181. TokenItemID uint64 `meddler:"token_item_id"`
  182. TokenEthBlockNum int64 `meddler:"token_block"`
  183. TokenEthAddr ethCommon.Address `meddler:"token_eth_addr"`
  184. TokenName string `meddler:"name"`
  185. TokenSymbol string `meddler:"symbol"`
  186. TokenDecimals uint64 `meddler:"decimals"`
  187. TokenUSD *float64 `meddler:"usd"`
  188. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  189. }
  190. // MarshalJSON is used to neast some of the fields of ExitAPI
  191. // without the need of auxiliar structs
  192. func (e ExitAPI) MarshalJSON() ([]byte, error) {
  193. return json.Marshal(map[string]interface{}{
  194. "itemId": e.ItemID,
  195. "batchNum": e.BatchNum,
  196. "accountIndex": e.AccountIdx,
  197. "bjj": e.BJJ,
  198. "hezEthereumAddress": e.EthAddr,
  199. "merkleProof": e.MerkleProof,
  200. "balance": e.Balance,
  201. "instantWithdraw": e.InstantWithdrawn,
  202. "delayedWithdrawRequest": e.DelayedWithdrawRequest,
  203. "delayedWithdraw": e.DelayedWithdrawn,
  204. "token": map[string]interface{}{
  205. "id": e.TokenID,
  206. "itemId": e.TokenItemID,
  207. "ethereumBlockNum": e.TokenEthBlockNum,
  208. "ethereumAddress": e.TokenEthAddr,
  209. "name": e.TokenName,
  210. "symbol": e.TokenSymbol,
  211. "decimals": e.TokenDecimals,
  212. "USD": e.TokenUSD,
  213. "fiatUpdate": e.TokenUSDUpdate,
  214. },
  215. })
  216. }
  217. // CoordinatorAPI is a representation of a coordinator with additional information
  218. // required by the API
  219. type CoordinatorAPI struct {
  220. ItemID uint64 `json:"itemId" meddler:"item_id"`
  221. Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
  222. Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
  223. EthBlockNum int64 `json:"ethereumBlock" meddler:"eth_block_num"`
  224. URL string `json:"URL" meddler:"url"`
  225. TotalItems uint64 `json:"-" meddler:"total_items"`
  226. FirstItem uint64 `json:"-" meddler:"first_item"`
  227. LastItem uint64 `json:"-" meddler:"last_item"`
  228. }
  229. // AccountAPI is a representation of a account with additional information
  230. // required by the API
  231. type AccountAPI struct {
  232. ItemID uint64 `meddler:"item_id"`
  233. Idx apitypes.HezIdx `meddler:"idx"`
  234. BatchNum common.BatchNum `meddler:"batch_num"`
  235. PublicKey apitypes.HezBJJ `meddler:"bjj"`
  236. EthAddr apitypes.HezEthAddr `meddler:"eth_addr"`
  237. Nonce common.Nonce `meddler:"nonce"` // max of 40 bits used
  238. Balance *apitypes.BigIntStr `meddler:"balance"` // max of 192 bits used
  239. TotalItems uint64 `meddler:"total_items"`
  240. FirstItem uint64 `meddler:"first_item"`
  241. LastItem uint64 `meddler:"last_item"`
  242. TokenID common.TokenID `meddler:"token_id"`
  243. TokenItemID int `meddler:"token_item_id"`
  244. TokenEthBlockNum int64 `meddler:"token_block"`
  245. TokenEthAddr ethCommon.Address `meddler:"token_eth_addr"`
  246. TokenName string `meddler:"name"`
  247. TokenSymbol string `meddler:"symbol"`
  248. TokenDecimals uint64 `meddler:"decimals"`
  249. TokenUSD *float64 `meddler:"usd"`
  250. TokenUSDUpdate *time.Time `meddler:"usd_update"`
  251. }
  252. // MarshalJSON is used to neast some of the fields of AccountAPI
  253. // without the need of auxiliar structs
  254. func (account AccountAPI) MarshalJSON() ([]byte, error) {
  255. jsonAccount := map[string]interface{}{
  256. "itemId": account.ItemID,
  257. "accountIndex": account.Idx,
  258. "nonce": account.Nonce,
  259. "balance": account.Balance,
  260. "bjj": account.PublicKey,
  261. "hezEthereumAddress": account.EthAddr,
  262. "token": map[string]interface{}{
  263. "id": account.TokenID,
  264. "itemId": account.TokenItemID,
  265. "ethereumBlockNum": account.TokenEthBlockNum,
  266. "ethereumAddress": account.TokenEthAddr,
  267. "name": account.TokenName,
  268. "symbol": account.TokenSymbol,
  269. "decimals": account.TokenDecimals,
  270. "USD": account.TokenUSD,
  271. "fiatUpdate": account.TokenUSDUpdate,
  272. },
  273. }
  274. return json.Marshal(jsonAccount)
  275. }
  276. // BatchAPI is a representation of a batch with additional information
  277. // required by the API, and extracted by joining block table
  278. type BatchAPI struct {
  279. ItemID uint64 `json:"itemId" meddler:"item_id"`
  280. BatchNum common.BatchNum `json:"batchNum" meddler:"batch_num"`
  281. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  282. EthBlockHash ethCommon.Hash `json:"ethereumBlockHash" meddler:"hash"`
  283. Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
  284. ForgerAddr ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
  285. CollectedFeesDB map[common.TokenID]*big.Int `json:"-" meddler:"fees_collected,json"`
  286. CollectedFeesAPI apitypes.CollectedFeesAPI `json:"collectedFees" meddler:"-"`
  287. TotalFeesUSD *float64 `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
  288. StateRoot apitypes.BigIntStr `json:"stateRoot" meddler:"state_root"`
  289. NumAccounts int `json:"numAccounts" meddler:"num_accounts"`
  290. ExitRoot apitypes.BigIntStr `json:"exitRoot" meddler:"exit_root"`
  291. ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
  292. SlotNum int64 `json:"slotNum" meddler:"slot_num"`
  293. ForgedTxs int `json:"forgedTransactions" meddler:"forged_txs"`
  294. TotalItems uint64 `json:"-" meddler:"total_items"`
  295. FirstItem uint64 `json:"-" meddler:"first_item"`
  296. LastItem uint64 `json:"-" meddler:"last_item"`
  297. }
  298. // MetricsAPI define metrics of the network
  299. type MetricsAPI struct {
  300. TransactionsPerBatch float64 `json:"transactionsPerBatch"`
  301. BatchFrequency float64 `json:"batchFrequency"`
  302. TransactionsPerSecond float64 `json:"transactionsPerSecond"`
  303. TokenAccounts int64 `json:"tokenAccounts"`
  304. Wallets int64 `json:"wallets"`
  305. AvgTransactionFee float64 `json:"avgTransactionFee"`
  306. EstimatedTimeToForgeL1 float64 `json:"estimatedTimeToForgeL1" meddler:"estimated_time_to_forge_l1"`
  307. }
  308. // BidAPI is a representation of a bid with additional information
  309. // required by the API
  310. type BidAPI struct {
  311. ItemID uint64 `json:"itemId" meddler:"item_id"`
  312. SlotNum int64 `json:"slotNum" meddler:"slot_num"`
  313. BidValue apitypes.BigIntStr `json:"bidValue" meddler:"bid_value"`
  314. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  315. Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
  316. Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
  317. URL string `json:"URL" meddler:"url"`
  318. Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
  319. TotalItems uint64 `json:"-" meddler:"total_items"`
  320. FirstItem uint64 `json:"-" meddler:"first_item"`
  321. LastItem uint64 `json:"-" meddler:"last_item"`
  322. }
  323. // MinBidInfo gives information of the minum bid for specific slot(s)
  324. type MinBidInfo struct {
  325. DefaultSlotSetBid [6]*big.Int `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"`
  326. DefaultSlotSetBidSlotNum int64 `json:"-" meddler:"default_slot_set_bid_slot_num"`
  327. }
  328. // BucketUpdateAPI are the bucket updates (tracking the withdrawals value changes)
  329. // in Rollup Smart Contract
  330. type BucketUpdateAPI struct {
  331. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  332. NumBucket int `json:"numBucket" meddler:"num_bucket"`
  333. BlockStamp int64 `json:"blockStamp" meddler:"block_stamp"`
  334. Withdrawals *apitypes.BigIntStr `json:"withdrawals" meddler:"withdrawals"`
  335. }
  336. // BucketParamsAPI are the parameter variables of each Bucket of Rollup Smart
  337. // Contract
  338. type BucketParamsAPI struct {
  339. CeilUSD *apitypes.BigIntStr `json:"ceilUSD"`
  340. Withdrawals *apitypes.BigIntStr `json:"withdrawals"`
  341. BlockWithdrawalRate *apitypes.BigIntStr `json:"blockWithdrawalRate"`
  342. MaxWithdrawals *apitypes.BigIntStr `json:"maxWithdrawals"`
  343. }
  344. // RollupVariablesAPI are the variables of the Rollup Smart Contract
  345. type RollupVariablesAPI struct {
  346. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  347. FeeAddToken *apitypes.BigIntStr `json:"feeAddToken" meddler:"fee_add_token" validate:"required"`
  348. ForgeL1L2BatchTimeout int64 `json:"forgeL1L2BatchTimeout" meddler:"forge_l1_timeout" validate:"required"`
  349. WithdrawalDelay uint64 `json:"withdrawalDelay" meddler:"withdrawal_delay" validate:"required"`
  350. Buckets [common.RollupConstNumBuckets]BucketParamsAPI `json:"buckets" meddler:"buckets,json"`
  351. SafeMode bool `json:"safeMode" meddler:"safe_mode"`
  352. }
  353. // NewRollupVariablesAPI creates a RollupVariablesAPI from common.RollupVariables
  354. func NewRollupVariablesAPI(rollupVariables *common.RollupVariables) *RollupVariablesAPI {
  355. rollupVars := RollupVariablesAPI{
  356. EthBlockNum: rollupVariables.EthBlockNum,
  357. FeeAddToken: apitypes.NewBigIntStr(rollupVariables.FeeAddToken),
  358. ForgeL1L2BatchTimeout: rollupVariables.ForgeL1L2BatchTimeout,
  359. WithdrawalDelay: rollupVariables.WithdrawalDelay,
  360. SafeMode: rollupVariables.SafeMode,
  361. }
  362. for i, bucket := range rollupVariables.Buckets {
  363. rollupVars.Buckets[i] = BucketParamsAPI{
  364. CeilUSD: apitypes.NewBigIntStr(bucket.CeilUSD),
  365. Withdrawals: apitypes.NewBigIntStr(bucket.Withdrawals),
  366. BlockWithdrawalRate: apitypes.NewBigIntStr(bucket.BlockWithdrawalRate),
  367. MaxWithdrawals: apitypes.NewBigIntStr(bucket.MaxWithdrawals),
  368. }
  369. }
  370. return &rollupVars
  371. }
  372. // AuctionVariablesAPI are the variables of the Auction Smart Contract
  373. type AuctionVariablesAPI struct {
  374. EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
  375. // DonationAddress Address where the donations will be sent
  376. DonationAddress ethCommon.Address `json:"donationAddress" meddler:"donation_address" validate:"required"`
  377. // BootCoordinator Address of the boot coordinator
  378. BootCoordinator ethCommon.Address `json:"bootCoordinator" meddler:"boot_coordinator" validate:"required"`
  379. // BootCoordinatorURL URL of the boot coordinator
  380. BootCoordinatorURL string `json:"bootCoordinatorUrl" meddler:"boot_coordinator_url" validate:"required"`
  381. // DefaultSlotSetBid The minimum bid value in a series of 6 slots
  382. DefaultSlotSetBid [6]*apitypes.BigIntStr `json:"defaultSlotSetBid" meddler:"default_slot_set_bid,json" validate:"required"`
  383. // DefaultSlotSetBidSlotNum SlotNum at which the new default_slot_set_bid applies
  384. DefaultSlotSetBidSlotNum int64 `json:"defaultSlotSetBidSlotNum" meddler:"default_slot_set_bid_slot_num"`
  385. // ClosedAuctionSlots Distance (#slots) to the closest slot to which you can bid ( 2 Slots = 2 * 40 Blocks = 20 min )
  386. ClosedAuctionSlots uint16 `json:"closedAuctionSlots" meddler:"closed_auction_slots" validate:"required"`
  387. // OpenAuctionSlots Distance (#slots) to the farthest slot to which you can bid (30 days = 4320 slots )
  388. OpenAuctionSlots uint16 `json:"openAuctionSlots" meddler:"open_auction_slots" validate:"required"`
  389. // AllocationRatio How the HEZ tokens deposited by the slot winner are distributed (Burn: 40% - Donation: 40% - HGT: 20%)
  390. AllocationRatio [3]uint16 `json:"allocationRatio" meddler:"allocation_ratio,json" validate:"required"`
  391. // Outbidding Minimum outbid (percentage) over the previous one to consider it valid
  392. Outbidding uint16 `json:"outbidding" meddler:"outbidding" validate:"required"`
  393. // SlotDeadline Number of blocks at the end of a slot in which any coordinator can forge if the winner has not forged one before
  394. SlotDeadline uint8 `json:"slotDeadline" meddler:"slot_deadline" validate:"required"`
  395. }
  396. // NewAuctionVariablesAPI creates a AuctionVariablesAPI from common.AuctionVariables
  397. func NewAuctionVariablesAPI(auctionVariables *common.AuctionVariables) *AuctionVariablesAPI {
  398. auctionVars := AuctionVariablesAPI{
  399. EthBlockNum: auctionVariables.EthBlockNum,
  400. DonationAddress: auctionVariables.DonationAddress,
  401. BootCoordinator: auctionVariables.BootCoordinator,
  402. BootCoordinatorURL: auctionVariables.BootCoordinatorURL,
  403. DefaultSlotSetBidSlotNum: auctionVariables.DefaultSlotSetBidSlotNum,
  404. ClosedAuctionSlots: auctionVariables.ClosedAuctionSlots,
  405. OpenAuctionSlots: auctionVariables.OpenAuctionSlots,
  406. Outbidding: auctionVariables.Outbidding,
  407. SlotDeadline: auctionVariables.SlotDeadline,
  408. }
  409. for i, slot := range auctionVariables.DefaultSlotSetBid {
  410. auctionVars.DefaultSlotSetBid[i] = apitypes.NewBigIntStr(slot)
  411. }
  412. for i, ratio := range auctionVariables.AllocationRatio {
  413. auctionVars.AllocationRatio[i] = ratio
  414. }
  415. return &auctionVars
  416. }