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/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}
 38
 39// PoolTxAPI represents a L2 Tx pool with extra metadata used by the API
 40type PoolTxAPI struct {
 41	TxID                 common.TxID           `meddler:"tx_id"`
 42	FromIdx              apitypes.HezIdx       `meddler:"from_idx"`
 43	EffectiveFromEthAddr *apitypes.HezEthAddr  `meddler:"effective_from_eth_addr"`
 44	EffectiveFromBJJ     *apitypes.HezBJJ      `meddler:"effective_from_bjj"`
 45	ToIdx                *apitypes.HezIdx      `meddler:"to_idx"`
 46	EffectiveToEthAddr   *apitypes.HezEthAddr  `meddler:"effective_to_eth_addr"`
 47	EffectiveToBJJ       *apitypes.HezBJJ      `meddler:"effective_to_bjj"`
 48	Amount               apitypes.BigIntStr    `meddler:"amount"`
 49	Fee                  common.FeeSelector    `meddler:"fee"`
 50	Nonce                common.Nonce          `meddler:"nonce"`
 51	State                common.PoolL2TxState  `meddler:"state"`
 52	Info                 *string               `meddler:"info"`
 53	Signature            babyjub.SignatureComp `meddler:"signature"`
 54	RqFromIdx            *apitypes.HezIdx      `meddler:"rq_from_idx"`
 55	RqToIdx              *apitypes.HezIdx      `meddler:"rq_to_idx"`
 56	RqToEthAddr          *apitypes.HezEthAddr  `meddler:"rq_to_eth_addr"`
 57	RqToBJJ              *apitypes.HezBJJ      `meddler:"rq_to_bjj"`
 58	RqTokenID            *common.TokenID       `meddler:"rq_token_id"`
 59	RqAmount             *apitypes.BigIntStr   `meddler:"rq_amount"`
 60	RqFee                *common.FeeSelector   `meddler:"rq_fee"`
 61	RqNonce              *common.Nonce         `meddler:"rq_nonce"`
 62	Type                 common.TxType         `meddler:"tx_type"`
 63	// Extra read fileds
 64	BatchNum         *common.BatchNum  `meddler:"batch_num"`
 65	Timestamp        time.Time         `meddler:"timestamp,utctime"`
 66	TotalItems       uint64            `meddler:"total_items"`
 67	TokenID          common.TokenID    `meddler:"token_id"`
 68	TokenItemID      uint64            `meddler:"token_item_id"`
 69	TokenEthBlockNum int64             `meddler:"eth_block_num"`
 70	TokenEthAddr     ethCommon.Address `meddler:"eth_addr"`
 71	TokenName        string            `meddler:"name"`
 72	TokenSymbol      string            `meddler:"symbol"`
 73	TokenDecimals    uint64            `meddler:"decimals"`
 74	TokenUSD         *float64          `meddler:"usd"`
 75	TokenUSDUpdate   *time.Time        `meddler:"usd_update"`
 76}
 77
 78// MarshalJSON is used to neast some of the fields of PoolTxAPI
 79// without the need of auxiliar structs
 80func (tx PoolTxAPI) MarshalJSON() ([]byte, error) {
 81	return json.Marshal(map[string]interface{}{
 82		"id":                          tx.TxID,
 83		"type":                        tx.Type,
 84		"fromAccountIndex":            tx.FromIdx,
 85		"fromHezEthereumAddress":      tx.EffectiveFromEthAddr,
 86		"fromBJJ":                     tx.EffectiveFromBJJ,
 87		"toAccountIndex":              tx.ToIdx,
 88		"toHezEthereumAddress":        tx.EffectiveToEthAddr,
 89		"toBjj":                       tx.EffectiveToBJJ,
 90		"amount":                      tx.Amount,
 91		"fee":                         tx.Fee,
 92		"nonce":                       tx.Nonce,
 93		"state":                       tx.State,
 94		"info":                        tx.Info,
 95		"signature":                   tx.Signature,
 96		"timestamp":                   tx.Timestamp,
 97		"batchNum":                    tx.BatchNum,
 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}