arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/api/dbtoapistructs.go
api/dbtoapistructs.goRaw
  1package api
  2
  3import (
  4	"encoding/base64"
  5	"strconv"
  6	"time"
  7
  8	"github.com/hermeznetwork/hermez-node/common"
  9	"github.com/hermeznetwork/hermez-node/db/historydb"
 10	"github.com/iden3/go-iden3-crypto/babyjub"
 11)
 12
 13// Commons of the API
 14
 15type pagination struct {
 16	TotalItems       int `json:"totalItems"`
 17	LastReturnedItem int `json:"lastReturnedItem"`
 18}
 19
 20type paginationer interface {
 21	GetPagination() pagination
 22	Len() int
 23}
 24
 25type errorMsg struct {
 26	Message string
 27}
 28
 29// History Tx related
 30
 31type historyTxsAPI struct {
 32	Txs        []historyTxAPI `json:"transactions"`
 33	Pagination pagination     `json:"pagination"`
 34}
 35
 36func (htx *historyTxsAPI) GetPagination() pagination { return htx.Pagination }
 37func (htx *historyTxsAPI) Len() int                  { return len(htx.Txs) }
 38
 39type l1Info struct {
 40	ToForgeL1TxsNum int64   `json:"toForgeL1TransactionsNum"`
 41	UserOrigin      bool    `json:"userOrigin"`
 42	FromEthAddr     string  `json:"fromEthereumAddress"`
 43	FromBJJ         string  `json:"fromBJJ"`
 44	LoadAmount      string  `json:"loadAmount"`
 45	LoadAmountUSD   float64 `json:"loadAmountUSD"`
 46	EthBlockNum     int64   `json:"ethereumBlockNum"`
 47}
 48
 49type l2Info struct {
 50	Fee    common.FeeSelector `json:"fee"`
 51	FeeUSD float64            `json:"feeUSD"`
 52	Nonce  common.Nonce       `json:"nonce"`
 53}
 54
 55type historyTxAPI struct {
 56	IsL1        string           `json:"L1orL2"`
 57	TxID        common.TxID      `json:"id"`
 58	Type        common.TxType    `json:"type"`
 59	Position    int              `json:"position"`
 60	FromIdx     string           `json:"fromAccountIndex"`
 61	ToIdx       string           `json:"toAccountIndex"`
 62	Amount      string           `json:"amount"`
 63	BatchNum    *common.BatchNum `json:"batchNum"`
 64	TokenID     common.TokenID   `json:"tokenId"`
 65	TokenSymbol string           `json:"tokenSymbol"`
 66	USD         float64          `json:"historicUSD"`
 67	Timestamp   time.Time        `json:"timestamp"`
 68	CurrentUSD  float64          `json:"currentUSD"`
 69	USDUpdate   time.Time        `json:"fiatUpdate"`
 70	L1Info      *l1Info          `json:"L1Info"`
 71	L2Info      *l2Info          `json:"L2Info"`
 72}
 73
 74func historyTxsToAPI(dbTxs []*historydb.HistoryTx) []historyTxAPI {
 75	apiTxs := []historyTxAPI{}
 76	for i := 0; i < len(dbTxs); i++ {
 77		apiTx := historyTxAPI{
 78			TxID:        dbTxs[i].TxID,
 79			Type:        dbTxs[i].Type,
 80			Position:    dbTxs[i].Position,
 81			FromIdx:     "hez:" + dbTxs[i].TokenSymbol + ":" + strconv.Itoa(int(dbTxs[i].FromIdx)),
 82			ToIdx:       "hez:" + dbTxs[i].TokenSymbol + ":" + strconv.Itoa(int(dbTxs[i].ToIdx)),
 83			Amount:      dbTxs[i].Amount.String(),
 84			TokenID:     dbTxs[i].TokenID,
 85			USD:         dbTxs[i].USD,
 86			BatchNum:    nil,
 87			Timestamp:   dbTxs[i].Timestamp,
 88			TokenSymbol: dbTxs[i].TokenSymbol,
 89			CurrentUSD:  dbTxs[i].CurrentUSD,
 90			USDUpdate:   dbTxs[i].USDUpdate,
 91			L1Info:      nil,
 92			L2Info:      nil,
 93		}
 94		bn := dbTxs[i].BatchNum
 95		if dbTxs[i].BatchNum != 0 {
 96			apiTx.BatchNum = &bn
 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:     "hez:" + dbTxs[i].FromEthAddr.String(),
104				FromBJJ:         bjjToString(dbTxs[i].FromBJJ),
105				LoadAmount:      dbTxs[i].LoadAmount.String(),
106				LoadAmountUSD:   dbTxs[i].LoadAmountUSD,
107				EthBlockNum:     dbTxs[i].EthBlockNum,
108			}
109		} else {
110			apiTx.IsL1 = "L2"
111			apiTx.L2Info = &l2Info{
112				Fee:    dbTxs[i].Fee,
113				FeeUSD: dbTxs[i].FeeUSD,
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}