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
21type paginationer interface {
22 GetPagination() pagination
23 Len() int
24}
25
26type errorMsg struct {
27 Message string
28}
29
30// History Tx related
31
32type historyTxsAPI struct {
33 Txs []historyTxAPI `json:"transactions"`
34 Pagination pagination `json:"pagination"`
35}
36
37func (htx *historyTxsAPI) GetPagination() pagination { return htx.Pagination }
38func (htx *historyTxsAPI) Len() int { return len(htx.Txs) }
39
40type l1Info struct {
41 ToForgeL1TxsNum int64 `json:"toForgeL1TransactionsNum"`
42 UserOrigin bool `json:"userOrigin"`
43 FromEthAddr string `json:"fromHezEthereumAddress"`
44 FromBJJ string `json:"fromBJJ"`
45 LoadAmount string `json:"loadAmount"`
46 HistoricLoadAmountUSD *float64 `json:"historicLoadAmountUSD"`
47 EthBlockNum int64 `json:"ethereumBlockNum"`
48}
49
50type l2Info struct {
51 Fee common.FeeSelector `json:"fee"`
52 HistoricFeeUSD *float64 `json:"historicFeeUSD"`
53 Nonce common.Nonce `json:"nonce"`
54}
55
56type historyTxAPI struct {
57 IsL1 string `json:"L1orL2"`
58 TxID common.TxID `json:"id"`
59 Type common.TxType `json:"type"`
60 Position int `json:"position"`
61 FromIdx string `json:"fromAccountIndex"`
62 ToIdx string `json:"toAccountIndex"`
63 Amount string `json:"amount"`
64 BatchNum *common.BatchNum `json:"batchNum"`
65 HistoricUSD *float64 `json:"historicUSD"`
66 Timestamp time.Time `json:"timestamp"`
67 L1Info *l1Info `json:"L1Info"`
68 L2Info *l2Info `json:"L2Info"`
69 Token common.Token `json:"token"`
70}
71
72func historyTxsToAPI(dbTxs []*historydb.HistoryTx) []historyTxAPI {
73 apiTxs := []historyTxAPI{}
74 for i := 0; i < len(dbTxs); i++ {
75 apiTx := historyTxAPI{
76 TxID: dbTxs[i].TxID,
77 Type: dbTxs[i].Type,
78 Position: dbTxs[i].Position,
79 FromIdx: idxToHez(dbTxs[i].FromIdx, dbTxs[i].TokenSymbol),
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: common.Token{
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].IsL1 {
99 apiTx.IsL1 = "L1"
100 apiTx.L1Info = &l1Info{
101 ToForgeL1TxsNum: dbTxs[i].ToForgeL1TxsNum,
102 UserOrigin: dbTxs[i].UserOrigin,
103 FromEthAddr: ethAddrToHez(dbTxs[i].FromEthAddr),
104 FromBJJ: bjjToString(dbTxs[i].FromBJJ),
105 LoadAmount: dbTxs[i].LoadAmount.String(),
106 HistoricLoadAmountUSD: dbTxs[i].HistoricLoadAmountUSD,
107 EthBlockNum: dbTxs[i].EthBlockNum,
108 }
109 } else {
110 apiTx.IsL1 = "L2"
111 apiTx.L2Info = &l2Info{
112 Fee: *dbTxs[i].Fee,
113 HistoricFeeUSD: dbTxs[i].HistoricFeeUSD,
114 Nonce: *dbTxs[i].Nonce,
115 }
116 }
117 apiTxs = append(apiTxs, apiTx)
118 }
119 return apiTxs
120}
121
122func bjjToString(bjj *babyjub.PublicKey) string {
123 pkComp := [32]byte(bjj.Compress())
124 sum := pkComp[0]
125 for i := 1; i < len(pkComp); i++ {
126 sum += pkComp[i]
127 }
128 bjjSum := append(pkComp[:], sum)
129 return "hez:" + base64.RawURLEncoding.EncodeToString(bjjSum)
130}
131
132func ethAddrToHez(addr ethCommon.Address) string {
133 return "hez:" + addr.String()
134}
135
136func idxToHez(idx common.Idx, tokenSymbol string) string {
137 return "hez:" + tokenSymbol + ":" + strconv.Itoa(int(idx))
138}