hermez-node/api/dbtoapistructs.go
api/dbtoapistructs.goRaw
1package api
2
3import (
4 "encoding/base64"
5 "strconv"
6 "time"
7
8 ethCommon "github.com/ethereum/go-ethereum/common"
9 "github.com/hermeznetwork/hermez-node/common"
10 "github.com/hermeznetwork/hermez-node/db/historydb"
11 "github.com/iden3/go-iden3-crypto/babyjub"
12)
13
14// Commons of the API
15
16type pagination struct {
17 TotalItems int `json:"totalItems"`
18 LastReturnedItem int `json:"lastReturnedItem"`
19}
20
21//nolint:govet this is a temp patch to avoid running the test
22type paginationer interface {
23 GetPagination() pagination
24 Len() int
25}
26
27type errorMsg struct {
28 Message string
29}
30
31// History Tx related
32
33type historyTxsAPI struct {
34 Txs []historyTxAPI `json:"transactions"`
35 Pagination pagination `json:"pagination"`
36}
37
38func (htx *historyTxsAPI) GetPagination() pagination { return htx.Pagination }
39func (htx *historyTxsAPI) Len() int { return len(htx.Txs) }
40
41type l1Info struct {
42 ToForgeL1TxsNum *int64 `json:"toForgeL1TransactionsNum"`
43 UserOrigin bool `json:"userOrigin"`
44 FromEthAddr string `json:"fromHezEthereumAddress"`
45 FromBJJ string `json:"fromBJJ"`
46 LoadAmount string `json:"loadAmount"`
47 HistoricLoadAmountUSD *float64 `json:"historicLoadAmountUSD"`
48 EthBlockNum int64 `json:"ethereumBlockNum"`
49}
50
51type l2Info struct {
52 Fee common.FeeSelector `json:"fee"`
53 HistoricFeeUSD *float64 `json:"historicFeeUSD"`
54 Nonce common.Nonce `json:"nonce"`
55}
56
57type historyTxAPI struct {
58 IsL1 string `json:"L1orL2"`
59 TxID string `json:"id"`
60 Type common.TxType `json:"type"`
61 Position int `json:"position"`
62 FromIdx *string `json:"fromAccountIndex"`
63 ToIdx string `json:"toAccountIndex"`
64 Amount string `json:"amount"`
65 BatchNum *common.BatchNum `json:"batchNum"`
66 HistoricUSD *float64 `json:"historicUSD"`
67 Timestamp time.Time `json:"timestamp"`
68 L1Info *l1Info `json:"L1Info"`
69 L2Info *l2Info `json:"L2Info"`
70 Token historydb.TokenRead `json:"token"`
71}
72
73func historyTxsToAPI(dbTxs []historydb.HistoryTx) []historyTxAPI {
74 apiTxs := []historyTxAPI{}
75 for i := 0; i < len(dbTxs); i++ {
76 apiTx := historyTxAPI{
77 TxID: dbTxs[i].TxID.String(),
78 Type: dbTxs[i].Type,
79 Position: dbTxs[i].Position,
80 ToIdx: idxToHez(dbTxs[i].ToIdx, dbTxs[i].TokenSymbol),
81 Amount: dbTxs[i].Amount.String(),
82 HistoricUSD: dbTxs[i].HistoricUSD,
83 BatchNum: dbTxs[i].BatchNum,
84 Timestamp: dbTxs[i].Timestamp,
85 Token: historydb.TokenRead{
86 TokenID: dbTxs[i].TokenID,
87 EthBlockNum: dbTxs[i].TokenEthBlockNum,
88 EthAddr: dbTxs[i].TokenEthAddr,
89 Name: dbTxs[i].TokenName,
90 Symbol: dbTxs[i].TokenSymbol,
91 Decimals: dbTxs[i].TokenDecimals,
92 USD: dbTxs[i].TokenUSD,
93 USDUpdate: dbTxs[i].TokenUSDUpdate,
94 },
95 L1Info: nil,
96 L2Info: nil,
97 }
98 if dbTxs[i].FromIdx != nil {
99 fromIdx := new(string)
100 *fromIdx = idxToHez(*dbTxs[i].FromIdx, dbTxs[i].TokenSymbol)
101 apiTx.FromIdx = fromIdx
102 }
103 if dbTxs[i].IsL1 {
104 apiTx.IsL1 = "L1"
105 apiTx.L1Info = &l1Info{
106 ToForgeL1TxsNum: dbTxs[i].ToForgeL1TxsNum,
107 UserOrigin: *dbTxs[i].UserOrigin,
108 FromEthAddr: ethAddrToHez(*dbTxs[i].FromEthAddr),
109 FromBJJ: bjjToString(dbTxs[i].FromBJJ),
110 LoadAmount: dbTxs[i].LoadAmount.String(),
111 HistoricLoadAmountUSD: dbTxs[i].HistoricLoadAmountUSD,
112 EthBlockNum: dbTxs[i].EthBlockNum,
113 }
114 } else {
115 apiTx.IsL1 = "L2"
116 apiTx.L2Info = &l2Info{
117 Fee: *dbTxs[i].Fee,
118 HistoricFeeUSD: dbTxs[i].HistoricFeeUSD,
119 Nonce: *dbTxs[i].Nonce,
120 }
121 }
122 apiTxs = append(apiTxs, apiTx)
123 }
124 return apiTxs
125}
126
127func bjjToString(bjj *babyjub.PublicKey) string {
128 pkComp := [32]byte(bjj.Compress())
129 sum := pkComp[0]
130 for i := 1; i < len(pkComp); i++ {
131 sum += pkComp[i]
132 }
133 bjjSum := append(pkComp[:], sum)
134 return "hez:" + base64.RawURLEncoding.EncodeToString(bjjSum)
135}
136
137func ethAddrToHez(addr ethCommon.Address) string {
138 return "hez:" + addr.String()
139}
140
141func idxToHez(idx common.Idx, tokenSymbol string) string {
142 return "hez:" + tokenSymbol + ":" + strconv.Itoa(int(idx))
143}