hermez-node/db/historydb/views.go
db/historydb/views.goRaw
1package historydb
2
3import (
4 "encoding/json"
5 "math/big"
6 "time"
7
8 ethCommon "github.com/ethereum/go-ethereum/common"
9 "github.com/hermeznetwork/hermez-node/apitypes"
10 "github.com/hermeznetwork/hermez-node/common"
11 "github.com/iden3/go-iden3-crypto/babyjub"
12 "github.com/iden3/go-merkletree"
13)
14
15// TxAPI is a representation of a generic Tx with additional information
16// required by the API, and extracted by joining block and token tables
17type TxAPI struct {
18 // Generic
19 IsL1 bool `meddler:"is_l1"`
20 TxID common.TxID `meddler:"id"`
21 ItemID int `meddler:"item_id"`
22 Type common.TxType `meddler:"type"`
23 Position int `meddler:"position"`
24 FromIdx *apitypes.HezIdx `meddler:"from_idx"`
25 FromEthAddr *apitypes.HezEthAddr `meddler:"from_eth_addr"`
26 FromBJJ *apitypes.HezBJJ `meddler:"from_bjj"`
27 ToIdx apitypes.HezIdx `meddler:"to_idx"`
28 ToEthAddr *apitypes.HezEthAddr `meddler:"to_eth_addr"`
29 ToBJJ *apitypes.HezBJJ `meddler:"to_bjj"`
30 Amount apitypes.BigIntStr `meddler:"amount"`
31 HistoricUSD *float64 `meddler:"amount_usd"`
32 BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
33 EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
34 // L1
35 ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
36 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
37 LoadAmount *apitypes.BigIntStr `meddler:"load_amount"`
38 HistoricLoadAmountUSD *float64 `meddler:"load_amount_usd"`
39 // L2
40 Fee *common.FeeSelector `meddler:"fee"`
41 HistoricFeeUSD *float64 `meddler:"fee_usd"`
42 Nonce *common.Nonce `meddler:"nonce"`
43 // API extras
44 Timestamp time.Time `meddler:"timestamp,utctime"`
45 TotalItems int `meddler:"total_items"`
46 FirstItem int `meddler:"first_item"`
47 LastItem int `meddler:"last_item"`
48 TokenID common.TokenID `meddler:"token_id"`
49 TokenItemID int `meddler:"token_item_id"`
50 TokenEthBlockNum int64 `meddler:"token_block"`
51 TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
52 TokenName string `meddler:"name"`
53 TokenSymbol string `meddler:"symbol"`
54 TokenDecimals uint64 `meddler:"decimals"`
55 TokenUSD *float64 `meddler:"usd"`
56 TokenUSDUpdate *time.Time `meddler:"usd_update"`
57}
58
59// MarshalJSON is used to neast some of the fields of TxAPI
60// without the need of auxiliar structs
61func (tx TxAPI) MarshalJSON() ([]byte, error) {
62 jsonTx := map[string]interface{}{
63 "id": tx.TxID,
64 "itemId": tx.ItemID,
65 "type": tx.Type,
66 "position": tx.Position,
67 "fromAccountIndex": tx.FromIdx,
68 "fromHezEthereumAddress": tx.FromEthAddr,
69 "fromBJJ": tx.FromBJJ,
70 "toAccountIndex": tx.ToIdx,
71 "toHezEthereumAddress": tx.ToEthAddr,
72 "toBJJ": tx.ToBJJ,
73 "amount": tx.Amount,
74 "batchNum": tx.BatchNum,
75 "historicUSD": tx.HistoricUSD,
76 "timestamp": tx.Timestamp,
77 "L1Info": nil,
78 "L2Info": nil,
79 "token": map[string]interface{}{
80 "id": tx.TokenID,
81 "itemId": tx.TokenItemID,
82 "ethereumBlockNum": tx.TokenEthBlockNum,
83 "ethereumAddress": tx.TokenEthAddr,
84 "name": tx.TokenName,
85 "symbol": tx.TokenSymbol,
86 "decimals": tx.TokenDecimals,
87 "USD": tx.TokenUSD,
88 "fiatUpdate": tx.TokenUSDUpdate,
89 },
90 }
91 if tx.IsL1 {
92 jsonTx["L1orL2"] = "L1"
93 jsonTx["L1Info"] = map[string]interface{}{
94 "toForgeL1TransactionsNum": tx.ToForgeL1TxsNum,
95 "userOrigin": tx.UserOrigin,
96 "loadAmount": tx.LoadAmount,
97 "historicLoadAmountUSD": tx.HistoricLoadAmountUSD,
98 "ethereumBlockNum": tx.EthBlockNum,
99 }
100 } else {
101 jsonTx["L1orL2"] = "L2"
102 jsonTx["L2Info"] = map[string]interface{}{
103 "fee": tx.Fee,
104 "historicFeeUSD": tx.HistoricFeeUSD,
105 "nonce": tx.Nonce,
106 }
107 }
108 return json.Marshal(jsonTx)
109}
110
111// txWrite is an representatiion that merges common.L1Tx and common.L2Tx
112// in order to perform inserts into tx table
113type txWrite struct {
114 // Generic
115 IsL1 bool `meddler:"is_l1"`
116 TxID common.TxID `meddler:"id"`
117 Type common.TxType `meddler:"type"`
118 Position int `meddler:"position"`
119 FromIdx *common.Idx `meddler:"from_idx"`
120 ToIdx common.Idx `meddler:"to_idx"`
121 Amount *big.Int `meddler:"amount,bigint"`
122 AmountFloat float64 `meddler:"amount_f"`
123 TokenID common.TokenID `meddler:"token_id"`
124 BatchNum *common.BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
125 EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
126 // L1
127 ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
128 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
129 FromEthAddr *ethCommon.Address `meddler:"from_eth_addr"`
130 FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
131 LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
132 LoadAmountFloat *float64 `meddler:"load_amount_f"`
133 // L2
134 Fee *common.FeeSelector `meddler:"fee"`
135 Nonce *common.Nonce `meddler:"nonce"`
136}
137
138// TokenWithUSD add USD info to common.Token
139type TokenWithUSD struct {
140 ItemID int `json:"itemId" meddler:"item_id"`
141 TokenID common.TokenID `json:"id" meddler:"token_id"`
142 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"` // Ethereum block number in which this token was registered
143 EthAddr ethCommon.Address `json:"ethereumAddress" meddler:"eth_addr"`
144 Name string `json:"name" meddler:"name"`
145 Symbol string `json:"symbol" meddler:"symbol"`
146 Decimals uint64 `json:"decimals" meddler:"decimals"`
147 USD *float64 `json:"USD" meddler:"usd"`
148 USDUpdate *time.Time `json:"fiatUpdate" meddler:"usd_update,utctime"`
149 TotalItems int `json:"-" meddler:"total_items"`
150 FirstItem int `json:"-" meddler:"first_item"`
151 LastItem int `json:"-" meddler:"last_item"`
152}
153
154// HistoryExit is a representation of a exit with additional information
155// required by the API, and extracted by joining token table
156type HistoryExit struct {
157 ItemID int `meddler:"item_id"`
158 BatchNum common.BatchNum `meddler:"batch_num"`
159 AccountIdx common.Idx `meddler:"account_idx"`
160 MerkleProof *merkletree.CircomVerifierProof `meddler:"merkle_proof,json"`
161 Balance *big.Int `meddler:"balance,bigint"`
162 InstantWithdrawn *int64 `meddler:"instant_withdrawn"`
163 DelayedWithdrawRequest *int64 `meddler:"delayed_withdraw_request"`
164 DelayedWithdrawn *int64 `meddler:"delayed_withdrawn"`
165 TotalItems int `meddler:"total_items"`
166 FirstItem int `meddler:"first_item"`
167 LastItem int `meddler:"last_item"`
168 TokenID common.TokenID `meddler:"token_id"`
169 TokenEthBlockNum int64 `meddler:"token_block"`
170 TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
171 TokenName string `meddler:"name"`
172 TokenSymbol string `meddler:"symbol"`
173 TokenDecimals uint64 `meddler:"decimals"`
174 TokenUSD *float64 `meddler:"usd"`
175 TokenUSDUpdate *time.Time `meddler:"usd_update"`
176}
177
178// CoordinatorAPI is a representation of a coordinator with additional information
179// required by the API
180type CoordinatorAPI struct {
181 ItemID int `json:"itemId" meddler:"item_id"`
182 Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
183 Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
184 EthBlockNum int64 `json:"ethereumBlock" meddler:"eth_block_num"`
185 URL string `json:"URL" meddler:"url"`
186 TotalItems int `json:"-" meddler:"total_items"`
187 FirstItem int `json:"-" meddler:"first_item"`
188 LastItem int `json:"-" meddler:"last_item"`
189}
190
191// BatchAPI is a representation of a batch with additional information
192// required by the API, and extracted by joining block table
193type BatchAPI struct {
194 ItemID int `json:"itemId" meddler:"item_id"`
195 BatchNum common.BatchNum `json:"batchNum" meddler:"batch_num"`
196 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
197 EthBlockHash ethCommon.Hash `json:"ethereumBlockHash" meddler:"hash"`
198 Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
199 ForgerAddr ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
200 CollectedFees apitypes.CollectedFees `json:"collectedFees" meddler:"fees_collected,json"`
201 // CollectedFees map[common.TokenID]*big.Int `json:"collectedFees" meddler:"fees_collected,json"`
202 TotalFeesUSD *float64 `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
203 StateRoot apitypes.BigIntStr `json:"stateRoot" meddler:"state_root"`
204 NumAccounts int `json:"numAccounts" meddler:"num_accounts"`
205 ExitRoot apitypes.BigIntStr `json:"exitRoot" meddler:"exit_root"`
206 ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
207 SlotNum int64 `json:"slotNum" meddler:"slot_num"`
208 TotalItems int `json:"-" meddler:"total_items"`
209 FirstItem int `json:"-" meddler:"first_item"`
210 LastItem int `json:"-" meddler:"last_item"`
211}
212
213// Network define status of the network
214type Network struct {
215 LastBlock int64 `json:"lastBlock"`
216 LastBatch BatchAPI `json:"lastBatch"`
217 CurrentSlot int64 `json:"currentSlot"`
218 NextForgers []CoordinatorAPI `json:"nextForgers"`
219}
220
221// Metrics define metrics of the network
222type Metrics struct {
223 TransactionsPerBatch float64 `json:"transactionsPerBatch"`
224 BatchFrequency float64 `json:"batchFrequency"`
225 TransactionsPerSecond float64 `json:"transactionsPerSecond"`
226 TotalAccounts int64 `json:"totalAccounts"`
227 TotalBJJs int64 `json:"totalBJJs"`
228 AvgTransactionFee float64 `json:"avgTransactionFee"`
229}
230
231// BidAPI is a representation of a bid with additional information
232// required by the API
233type BidAPI struct {
234 ItemID int `json:"itemId" meddler:"item_id"`
235 SlotNum int64 `json:"slotNum" meddler:"slot_num"`
236 BidValue apitypes.BigIntStr `json:"bidValue" meddler:"bid_value"`
237 EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
238 Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
239 Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
240 URL string `json:"URL" meddler:"url"`
241 Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
242 TotalItems int `json:"-" meddler:"total_items"`
243 FirstItem int `json:"-" meddler:"first_item"`
244 LastItem int `json:"-" meddler:"last_item"`
245}