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 string `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.String(),
77 Type: dbTxs[i].Type,
78 Position: dbTxs[i].Position,
79 ToIdx: idxToHez(dbTxs[i].ToIdx, dbTxs[i].TokenSymbol),
80 Amount: dbTxs[i].Amount.String(),
81 HistoricUSD: dbTxs[i].HistoricUSD,
82 BatchNum: dbTxs[i].BatchNum,
83 Timestamp: dbTxs[i].Timestamp,
84 Token: common.Token{
85 TokenID: dbTxs[i].TokenID,
86 EthBlockNum: dbTxs[i].TokenEthBlockNum,
87 EthAddr: dbTxs[i].TokenEthAddr,
88 Name: dbTxs[i].TokenName,
89 Symbol: dbTxs[i].TokenSymbol,
90 Decimals: dbTxs[i].TokenDecimals,
91 USD: dbTxs[i].TokenUSD,
92 USDUpdate: dbTxs[i].TokenUSDUpdate,
93 },
94 L1Info: nil,
95 L2Info: nil,
96 }
97 if dbTxs[i].FromIdx != nil {
98 fromIdx := new(string)
99 *fromIdx = idxToHez(*dbTxs[i].FromIdx, dbTxs[i].TokenSymbol)
100 apiTx.FromIdx = fromIdx
101 }
102 if dbTxs[i].IsL1 {
103 apiTx.IsL1 = "L1"
104 apiTx.L1Info = &l1Info{
105 ToForgeL1TxsNum: dbTxs[i].ToForgeL1TxsNum,
106 UserOrigin: *dbTxs[i].UserOrigin,
107 FromEthAddr: ethAddrToHez(*dbTxs[i].FromEthAddr),
108 FromBJJ: bjjToString(dbTxs[i].FromBJJ),
109 LoadAmount: dbTxs[i].LoadAmount.String(),
110 HistoricLoadAmountUSD: dbTxs[i].HistoricLoadAmountUSD,
111 EthBlockNum: dbTxs[i].EthBlockNum,
112 }
113 } else {
114 apiTx.IsL1 = "L2"
115 apiTx.L2Info = &l2Info{
116 Fee: *dbTxs[i].Fee,
117 HistoricFeeUSD: dbTxs[i].HistoricFeeUSD,
118 Nonce: *dbTxs[i].Nonce,
119 }
120 }
121 apiTxs = append(apiTxs, apiTx)
122 }
123 return apiTxs
124}
125
126func bjjToString(bjj *babyjub.PublicKey) string {
127 pkComp := [32]byte(bjj.Compress())
128 sum := pkComp[0]
129 for i := 1; i < len(pkComp); i++ {
130 sum += pkComp[i]
131 }
132 bjjSum := append(pkComp[:], sum)
133 return "hez:" + base64.RawURLEncoding.EncodeToString(bjjSum)
134}
135
136func ethAddrToHez(addr ethCommon.Address) string {
137 return "hez:" + addr.String()
138}
139
140func idxToHez(idx common.Idx, tokenSymbol string) string {
141 return "hez:" + tokenSymbol + ":" + strconv.Itoa(int(idx))
142}