Refactor api exits

This commit is contained in:
Arnau B
2020-10-30 12:55:30 +01:00
parent 8c9d13ffa5
commit 233ecc4704
11 changed files with 452 additions and 407 deletions

View File

@@ -891,12 +891,17 @@ func (hdb *HistoryDB) GetAllExits() ([]common.ExitInfo, error) {
return db.SlicePtrsToSlice(exits).([]common.ExitInfo), err
}
// GetExit returns a exit from the DB
func (hdb *HistoryDB) GetExit(batchNum *uint, idx *common.Idx) (*HistoryExit, error) {
exit := &HistoryExit{}
// GetExitAPI returns a exit from the DB
func (hdb *HistoryDB) GetExitAPI(batchNum *uint, idx *common.Idx) (*ExitAPI, error) {
exit := &ExitAPI{}
err := meddler.QueryRow(
hdb.db, exit, `SELECT exit_tree.*, token.token_id, token.eth_block_num AS token_block,
token.eth_addr, token.name, token.symbol, token.decimals, token.usd, token.usd_update
hdb.db, exit, `SELECT exit_tree.item_id, exit_tree.batch_num,
hez_idx(exit_tree.account_idx, token.symbol) AS account_idx,
exit_tree.merkle_proof, exit_tree.balance, exit_tree.instant_withdrawn,
exit_tree.delayed_withdraw_request, exit_tree.delayed_withdrawn,
token.token_id, token.item_id AS token_item_id,
token.eth_block_num AS token_block, token.eth_addr, token.name, token.symbol,
token.decimals, token.usd, token.usd_update
FROM exit_tree INNER JOIN account ON exit_tree.account_idx = account.idx
INNER JOIN token ON account.token_id = token.token_id
WHERE exit_tree.batch_num = $1 AND exit_tree.account_idx = $2;`, batchNum, idx,
@@ -904,20 +909,25 @@ func (hdb *HistoryDB) GetExit(batchNum *uint, idx *common.Idx) (*HistoryExit, er
return exit, err
}
// GetExits returns a list of exits from the DB and pagination info
func (hdb *HistoryDB) GetExits(
// GetExitsAPI returns a list of exits from the DB and pagination info
func (hdb *HistoryDB) GetExitsAPI(
ethAddr *ethCommon.Address, bjj *babyjub.PublicKey,
tokenID *common.TokenID, idx *common.Idx, batchNum *uint,
fromItem, limit *uint, order string,
) ([]HistoryExit, *db.Pagination, error) {
) ([]ExitAPI, *db.Pagination, error) {
if ethAddr != nil && bjj != nil {
return nil, nil, errors.New("ethAddr and bjj are incompatible")
}
var query string
var args []interface{}
queryStr := `SELECT exit_tree.*, token.token_id, token.eth_block_num AS token_block,
token.eth_addr, token.name, token.symbol, token.decimals, token.usd,
token.usd_update, COUNT(*) OVER() AS total_items, MIN(exit_tree.item_id) OVER() AS first_item, MAX(exit_tree.item_id) OVER() AS last_item
queryStr := `SELECT exit_tree.item_id, exit_tree.batch_num,
hez_idx(exit_tree.account_idx, token.symbol) AS account_idx,
exit_tree.merkle_proof, exit_tree.balance, exit_tree.instant_withdrawn,
exit_tree.delayed_withdraw_request, exit_tree.delayed_withdrawn,
token.token_id, token.item_id AS token_item_id,
token.eth_block_num AS token_block, token.eth_addr, token.name, token.symbol,
token.decimals, token.usd, token.usd_update, COUNT(*) OVER() AS total_items,
MIN(exit_tree.item_id) OVER() AS first_item, MAX(exit_tree.item_id) OVER() AS last_item
FROM exit_tree INNER JOIN account ON exit_tree.account_idx = account.idx
INNER JOIN token ON account.token_id = token.token_id `
// Apply filters
@@ -989,14 +999,14 @@ func (hdb *HistoryDB) GetExits(
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
query = hdb.db.Rebind(queryStr)
// log.Debug(query)
exits := []*HistoryExit{}
exits := []*ExitAPI{}
if err := meddler.QueryAll(hdb.db, &exits, query, args...); err != nil {
return nil, nil, err
}
if len(exits) == 0 {
return nil, nil, sql.ErrNoRows
}
return db.SlicePtrsToSlice(exits).([]HistoryExit), &db.Pagination{
return db.SlicePtrsToSlice(exits).([]ExitAPI), &db.Pagination{
TotalItems: exits[0].TotalItems,
FirstItem: exits[0].FirstItem,
LastItem: exits[0].LastItem,

View File

@@ -151,14 +151,14 @@ type TokenWithUSD struct {
LastItem int `json:"-" meddler:"last_item"`
}
// HistoryExit is a representation of a exit with additional information
// ExitAPI is a representation of a exit with additional information
// required by the API, and extracted by joining token table
type HistoryExit struct {
type ExitAPI struct {
ItemID int `meddler:"item_id"`
BatchNum common.BatchNum `meddler:"batch_num"`
AccountIdx common.Idx `meddler:"account_idx"`
AccountIdx apitypes.HezIdx `meddler:"account_idx"`
MerkleProof *merkletree.CircomVerifierProof `meddler:"merkle_proof,json"`
Balance *big.Int `meddler:"balance,bigint"`
Balance apitypes.BigIntStr `meddler:"balance"`
InstantWithdrawn *int64 `meddler:"instant_withdrawn"`
DelayedWithdrawRequest *int64 `meddler:"delayed_withdraw_request"`
DelayedWithdrawn *int64 `meddler:"delayed_withdrawn"`
@@ -166,6 +166,7 @@ type HistoryExit struct {
FirstItem int `meddler:"first_item"`
LastItem int `meddler:"last_item"`
TokenID common.TokenID `meddler:"token_id"`
TokenItemID int `meddler:"token_item_id"`
TokenEthBlockNum int64 `meddler:"token_block"`
TokenEthAddr ethCommon.Address `meddler:"eth_addr"`
TokenName string `meddler:"name"`
@@ -175,6 +176,45 @@ type HistoryExit struct {
TokenUSDUpdate *time.Time `meddler:"usd_update"`
}
// MarshalJSON is used to neast some of the fields of ExitAPI
// without the need of auxiliar structs
func (e ExitAPI) MarshalJSON() ([]byte, error) {
siblings := []string{}
for i := 0; i < len(e.MerkleProof.Siblings); i++ {
siblings = append(siblings, e.MerkleProof.Siblings[i].String())
}
return json.Marshal(map[string]interface{}{
"itemId": e.ItemID,
"batchNum": e.BatchNum,
"accountIndex": e.AccountIdx,
"merkleProof": map[string]interface{}{
"Root": e.MerkleProof.Root.String(),
"Siblings": siblings,
"OldKey": e.MerkleProof.OldKey.String(),
"OldValue": e.MerkleProof.OldValue.String(),
"IsOld0": e.MerkleProof.IsOld0,
"Key": e.MerkleProof.Key.String(),
"Value": e.MerkleProof.Value.String(),
"Fnc": e.MerkleProof.Fnc,
},
"balance": e.Balance,
"instantWithdrawn": e.InstantWithdrawn,
"delayedWithdrawRequest": e.DelayedWithdrawRequest,
"delayedWithdrawn": e.DelayedWithdrawn,
"token": map[string]interface{}{
"id": e.TokenID,
"itemId": e.TokenItemID,
"ethereumBlockNum": e.TokenEthBlockNum,
"ethereumAddress": e.TokenEthAddr,
"name": e.TokenName,
"symbol": e.TokenSymbol,
"decimals": e.TokenDecimals,
"USD": e.TokenUSD,
"fiatUpdate": e.TokenUSDUpdate,
},
})
}
// CoordinatorAPI is a representation of a coordinator with additional information
// required by the API
type CoordinatorAPI struct {
@@ -198,16 +238,15 @@ type BatchAPI struct {
Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
ForgerAddr ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
CollectedFees apitypes.CollectedFees `json:"collectedFees" meddler:"fees_collected,json"`
// CollectedFees map[common.TokenID]*big.Int `json:"collectedFees" meddler:"fees_collected,json"`
TotalFeesUSD *float64 `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
StateRoot apitypes.BigIntStr `json:"stateRoot" meddler:"state_root"`
NumAccounts int `json:"numAccounts" meddler:"num_accounts"`
ExitRoot apitypes.BigIntStr `json:"exitRoot" meddler:"exit_root"`
ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
SlotNum int64 `json:"slotNum" meddler:"slot_num"`
TotalItems int `json:"-" meddler:"total_items"`
FirstItem int `json:"-" meddler:"first_item"`
LastItem int `json:"-" meddler:"last_item"`
TotalFeesUSD *float64 `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
StateRoot apitypes.BigIntStr `json:"stateRoot" meddler:"state_root"`
NumAccounts int `json:"numAccounts" meddler:"num_accounts"`
ExitRoot apitypes.BigIntStr `json:"exitRoot" meddler:"exit_root"`
ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
SlotNum int64 `json:"slotNum" meddler:"slot_num"`
TotalItems int `json:"-" meddler:"total_items"`
FirstItem int `json:"-" meddler:"first_item"`
LastItem int `json:"-" meddler:"last_item"`
}
// Network define status of the network

View File

@@ -115,6 +115,7 @@ func (tx PoolTxAPI) MarshalJSON() ([]byte, error) {
})
}
// AccountCreationAuthAPI represents an account creation auth in the expected format by the API
type AccountCreationAuthAPI struct {
EthAddr apitypes.HezEthAddr `json:"hezEthereumAddress" meddler:"eth_addr" `
BJJ apitypes.HezBJJ `json:"bjj" meddler:"bjj" `