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	Signature            babyjub.SignatureComp `meddler:"signature"`
 53	RqFromIdx            *apitypes.HezIdx      `meddler:"rq_from_idx"`
 54	RqToIdx              *apitypes.HezIdx      `meddler:"rq_to_idx"`
 55	RqToEthAddr          *apitypes.HezEthAddr  `meddler:"rq_to_eth_addr"`
 56	RqToBJJ              *apitypes.HezBJJ      `meddler:"rq_to_bjj"`
 57	RqTokenID            *common.TokenID       `meddler:"rq_token_id"`
 58	RqAmount             *apitypes.BigIntStr   `meddler:"rq_amount"`
 59	RqFee                *common.FeeSelector   `meddler:"rq_fee"`
 60	RqNonce              *common.Nonce         `meddler:"rq_nonce"`
 61	Type                 common.TxType         `meddler:"tx_type"`
 62	// Extra read fileds
 63	BatchNum         *common.BatchNum  `meddler:"batch_num"`
 64	Timestamp        time.Time         `meddler:"timestamp,utctime"`
 65	TotalItems       uint64            `meddler:"total_items"`
 66	TokenID          common.TokenID    `meddler:"token_id"`
 67	TokenItemID      uint64            `meddler:"token_item_id"`
 68	TokenEthBlockNum int64             `meddler:"eth_block_num"`
 69	TokenEthAddr     ethCommon.Address `meddler:"eth_addr"`
 70	TokenName        string            `meddler:"name"`
 71	TokenSymbol      string            `meddler:"symbol"`
 72	TokenDecimals    uint64            `meddler:"decimals"`
 73	TokenUSD         *float64          `meddler:"usd"`
 74	TokenUSDUpdate   *time.Time        `meddler:"usd_update"`
 75}
 76
 77// MarshalJSON is used to neast some of the fields of PoolTxAPI
 78// without the need of auxiliar structs
 79func (tx PoolTxAPI) MarshalJSON() ([]byte, error) {
 80	return json.Marshal(map[string]interface{}{
 81		"id":                          tx.TxID,
 82		"type":                        tx.Type,
 83		"fromAccountIndex":            tx.FromIdx,
 84		"fromHezEthereumAddress":      tx.EffectiveFromEthAddr,
 85		"fromBJJ":                     tx.EffectiveFromBJJ,
 86		"toAccountIndex":              tx.ToIdx,
 87		"toHezEthereumAddress":        tx.EffectiveToEthAddr,
 88		"toBjj":                       tx.EffectiveToBJJ,
 89		"amount":                      tx.Amount,
 90		"fee":                         tx.Fee,
 91		"nonce":                       tx.Nonce,
 92		"state":                       tx.State,
 93		"signature":                   tx.Signature,
 94		"timestamp":                   tx.Timestamp,
 95		"batchNum":                    tx.BatchNum,
 96		"requestFromAccountIndex":     tx.RqFromIdx,
 97		"requestToAccountIndex":       tx.RqToIdx,
 98		"requestToHezEthereumAddress": tx.RqToEthAddr,
 99		"requestToBJJ":                tx.RqToBJJ,
100		"requestTokenId":              tx.RqTokenID,
101		"requestAmount":               tx.RqAmount,
102		"requestFee":                  tx.RqFee,
103		"requestNonce":                tx.RqNonce,
104		"token": map[string]interface{}{
105			"id":               tx.TokenID,
106			"itemId":           tx.TokenItemID,
107			"ethereumBlockNum": tx.TokenEthBlockNum,
108			"ethereumAddress":  tx.TokenEthAddr,
109			"name":             tx.TokenName,
110			"symbol":           tx.TokenSymbol,
111			"decimals":         tx.TokenDecimals,
112			"USD":              tx.TokenUSD,
113			"fiatUpdate":       tx.TokenUSDUpdate,
114		},
115	})
116}
117
118// AccountCreationAuthAPI represents an account creation auth in the expected format by the API
119type AccountCreationAuthAPI struct {
120	EthAddr   apitypes.HezEthAddr   `json:"hezEthereumAddress" meddler:"eth_addr" `
121	BJJ       apitypes.HezBJJ       `json:"bjj"                meddler:"bjj" `
122	Signature apitypes.EthSignature `json:"signature"          meddler:"signature" `
123	Timestamp time.Time             `json:"timestamp"          meddler:"timestamp,utctime"`
124}