arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/db/l2db/views.go
db/l2db/views.goRaw
  1package l2db
  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/api/apitypes"
 10	"github.com/hermeznetwork/hermez-node/common"
 11	"github.com/iden3/go-iden3-crypto/babyjub"
 12)
 13
 14// PoolL2TxWrite holds the necessary data to perform inserts in tx_pool
 15type PoolL2TxWrite struct {
 16	TxID        common.TxID            `meddler:"tx_id"`
 17	FromIdx     common.Idx             `meddler:"from_idx"`
 18	ToIdx       *common.Idx            `meddler:"to_idx"`
 19	ToEthAddr   *ethCommon.Address     `meddler:"to_eth_addr"`
 20	ToBJJ       *babyjub.PublicKeyComp `meddler:"to_bjj"`
 21	TokenID     common.TokenID         `meddler:"token_id"`
 22	Amount      *big.Int               `meddler:"amount,bigint"`
 23	AmountFloat float64                `meddler:"amount_f"`
 24	Fee         common.FeeSelector     `meddler:"fee"`
 25	Nonce       common.Nonce           `meddler:"nonce"`
 26	State       common.PoolL2TxState   `meddler:"state"`
 27	Signature   babyjub.SignatureComp  `meddler:"signature"`
 28	RqFromIdx   *common.Idx            `meddler:"rq_from_idx"`
 29	RqToIdx     *common.Idx            `meddler:"rq_to_idx"`
 30	RqToEthAddr *ethCommon.Address     `meddler:"rq_to_eth_addr"`
 31	RqToBJJ     *babyjub.PublicKeyComp `meddler:"rq_to_bjj"`
 32	RqTokenID   *common.TokenID        `meddler:"rq_token_id"`
 33	RqAmount    *big.Int               `meddler:"rq_amount,bigintnull"`
 34	RqFee       *common.FeeSelector    `meddler:"rq_fee"`
 35	RqNonce     *common.Nonce          `meddler:"rq_nonce"`
 36	Type        common.TxType          `meddler:"tx_type"`
 37	ClientIP    string                 `meddler:"client_ip"`
 38}
 39
 40// PoolTxAPI represents a L2 Tx pool with extra metadata used by the API
 41type PoolTxAPI struct {
 42	TxID                 common.TxID           `meddler:"tx_id"`
 43	FromIdx              apitypes.HezIdx       `meddler:"from_idx"`
 44	EffectiveFromEthAddr *apitypes.HezEthAddr  `meddler:"effective_from_eth_addr"`
 45	EffectiveFromBJJ     *apitypes.HezBJJ      `meddler:"effective_from_bjj"`
 46	ToIdx                *apitypes.HezIdx      `meddler:"to_idx"`
 47	EffectiveToEthAddr   *apitypes.HezEthAddr  `meddler:"effective_to_eth_addr"`
 48	EffectiveToBJJ       *apitypes.HezBJJ      `meddler:"effective_to_bjj"`
 49	Amount               apitypes.BigIntStr    `meddler:"amount"`
 50	Fee                  common.FeeSelector    `meddler:"fee"`
 51	Nonce                common.Nonce          `meddler:"nonce"`
 52	State                common.PoolL2TxState  `meddler:"state"`
 53	Info                 *string               `meddler:"info"`
 54	Signature            babyjub.SignatureComp `meddler:"signature"`
 55	RqFromIdx            *apitypes.HezIdx      `meddler:"rq_from_idx"`
 56	RqToIdx              *apitypes.HezIdx      `meddler:"rq_to_idx"`
 57	RqToEthAddr          *apitypes.HezEthAddr  `meddler:"rq_to_eth_addr"`
 58	RqToBJJ              *apitypes.HezBJJ      `meddler:"rq_to_bjj"`
 59	RqTokenID            *common.TokenID       `meddler:"rq_token_id"`
 60	RqAmount             *apitypes.BigIntStr   `meddler:"rq_amount"`
 61	RqFee                *common.FeeSelector   `meddler:"rq_fee"`
 62	RqNonce              *common.Nonce         `meddler:"rq_nonce"`
 63	Type                 common.TxType         `meddler:"tx_type"`
 64	// Extra read fileds
 65	BatchNum         *common.BatchNum  `meddler:"batch_num"`
 66	Timestamp        time.Time         `meddler:"timestamp,utctime"`
 67	TotalItems       uint64            `meddler:"total_items"`
 68	TokenID          common.TokenID    `meddler:"token_id"`
 69	TokenItemID      uint64            `meddler:"token_item_id"`
 70	TokenEthBlockNum int64             `meddler:"eth_block_num"`
 71	TokenEthAddr     ethCommon.Address `meddler:"eth_addr"`
 72	TokenName        string            `meddler:"name"`
 73	TokenSymbol      string            `meddler:"symbol"`
 74	TokenDecimals    uint64            `meddler:"decimals"`
 75	TokenUSD         *float64          `meddler:"usd"`
 76	TokenUSDUpdate   *time.Time        `meddler:"usd_update"`
 77}
 78
 79// MarshalJSON is used to neast some of the fields of PoolTxAPI
 80// without the need of auxiliar structs
 81func (tx PoolTxAPI) MarshalJSON() ([]byte, error) {
 82	return json.Marshal(map[string]interface{}{
 83		"id":                          tx.TxID,
 84		"type":                        tx.Type,
 85		"fromAccountIndex":            tx.FromIdx,
 86		"fromHezEthereumAddress":      tx.EffectiveFromEthAddr,
 87		"fromBJJ":                     tx.EffectiveFromBJJ,
 88		"toAccountIndex":              tx.ToIdx,
 89		"toHezEthereumAddress":        tx.EffectiveToEthAddr,
 90		"toBjj":                       tx.EffectiveToBJJ,
 91		"amount":                      tx.Amount,
 92		"fee":                         tx.Fee,
 93		"nonce":                       tx.Nonce,
 94		"state":                       tx.State,
 95		"info":                        tx.Info,
 96		"signature":                   tx.Signature,
 97		"timestamp":                   tx.Timestamp,
 98		"requestFromAccountIndex":     tx.RqFromIdx,
 99		"requestToAccountIndex":       tx.RqToIdx,
100		"requestToHezEthereumAddress": tx.RqToEthAddr,
101		"requestToBJJ":                tx.RqToBJJ,
102		"requestTokenId":              tx.RqTokenID,
103		"requestAmount":               tx.RqAmount,
104		"requestFee":                  tx.RqFee,
105		"requestNonce":                tx.RqNonce,
106		"token": map[string]interface{}{
107			"id":               tx.TokenID,
108			"itemId":           tx.TokenItemID,
109			"ethereumBlockNum": tx.TokenEthBlockNum,
110			"ethereumAddress":  tx.TokenEthAddr,
111			"name":             tx.TokenName,
112			"symbol":           tx.TokenSymbol,
113			"decimals":         tx.TokenDecimals,
114			"USD":              tx.TokenUSD,
115			"fiatUpdate":       tx.TokenUSDUpdate,
116		},
117	})
118}
119
120// AccountCreationAuthAPI represents an account creation auth in the expected format by the API
121type AccountCreationAuthAPI struct {
122	EthAddr   apitypes.HezEthAddr   `json:"hezEthereumAddress" meddler:"eth_addr" `
123	BJJ       apitypes.HezBJJ       `json:"bjj"                meddler:"bjj" `
124	Signature apitypes.EthSignature `json:"signature"          meddler:"signature" `
125	Timestamp time.Time             `json:"timestamp"          meddler:"timestamp,utctime"`
126}