arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
Merge pull request #338 from hermeznetwork/feature/api-amounts

Add amount success and load amount success
Eduard S eduard@iden3.io
Wed, 09 Dec 2020 17:20:21 +0100
commit

d5c753030a1731c3a98989c093f7919b2f944f7d

parent

d5f579aec5db2420467392c0214c6bc813933e1f

4 files changed, 46 insertions(+), 10 deletions(-)

jump to
M api/swagger.yml → api/swagger.yml

@@ -1750,6 +1750,22 @@ allOf:

- $ref: '#/components/schemas/BigInt' - description: Tokens transfered from L1 to L2. - example: "4900000000000000000" + amountSuccess: + type: boolean + description: >- + Indicates if the amount specified by the user has been sent propperly. If false, the amount that actualy has been sent is 0. + If the transaction hasn't been forged yet (`batchNum == null`), this value will be false, as it's unknown if it has succeed or not. + An example were this value could be false: a user send a `DepositTransfer` transaction, but when the tx is forged there are not + enougth founds in the account. In this case the transfer part of the transaction won't be effective making the amount have a real value of 0. + example: true + depositAmountSuccess: + type: boolean + description: >- + Indicates if the deposit amount specified by the user has been sent propperly. If false, the deposit amount that actualy has been sent is 0. + If the transaction hasn't been forged yet (`batchNum == null`), this value will be false, as it's unknown if it has succeed or not. + An example were this value could be false: a user send a `Deposit` transaction, but when the tx is forged the token id is not registered on the network. + In this case transaction won't be effective making the deposit amount have a real value of 0. + example: true historicDepositAmountUSD: type: number description: Load amount in USD, at the moment the transaction was made.

@@ -1764,6 +1780,8 @@ required:

- toForgeL1TransactionsNum - userOrigin - depositAmount + - amountSuccess + - depositAmountSuccess - historicDepositAmountUSD - ethereumBlockNum additionalProperties: false
M api/txshistory_test.go → api/txshistory_test.go

@@ -21,6 +21,8 @@ type testL1Info struct {

ToForgeL1TxsNum *int64 `json:"toForgeL1TransactionsNum"` UserOrigin bool `json:"userOrigin"` DepositAmount string `json:"depositAmount"` + AmountSuccess bool `json:"amountSuccess"` + DepositAmountSuccess bool `json:"depositAmountSuccess"` HistoricDepositAmountUSD *float64 `json:"historicDepositAmountUSD"` EthBlockNum int64 `json:"ethereumBlockNum"` }

@@ -123,10 +125,12 @@ Amount: l1.Amount.String(),

BatchNum: l1.BatchNum, Timestamp: getTimestamp(l1.EthBlockNum, blocks), L1Info: &testL1Info{ - ToForgeL1TxsNum: l1.ToForgeL1TxsNum, - UserOrigin: l1.UserOrigin, - DepositAmount: l1.DepositAmount.String(), - EthBlockNum: l1.EthBlockNum, + ToForgeL1TxsNum: l1.ToForgeL1TxsNum, + UserOrigin: l1.UserOrigin, + DepositAmount: l1.DepositAmount.String(), + AmountSuccess: true, + DepositAmountSuccess: true, + EthBlockNum: l1.EthBlockNum, }, Token: token, }
M db/historydb/historydb.go → db/historydb/historydb.go

@@ -879,15 +879,17 @@ // }

// GetHistoryTx returns a tx from the DB given a TxID func (hdb *HistoryDB) GetHistoryTx(txID common.TxID) (*TxAPI, error) { - // TODO: add success flags for L1s + // Warning: amount_success and deposit_amount_success have true as default for + // performance reasons. The expected default value is false (when txs are unforged) + // this case is handled at the function func (tx TxAPI) MarshalJSON() ([]byte, error) tx := &TxAPI{} err := meddler.QueryRow( hdb.db, tx, `SELECT tx.item_id, tx.is_l1, tx.id, tx.type, tx.position, hez_idx(tx.from_idx, token.symbol) AS from_idx, tx.from_eth_addr, tx.from_bjj, hez_idx(tx.to_idx, token.symbol) AS to_idx, tx.to_eth_addr, tx.to_bjj, - tx.amount, tx.token_id, tx.amount_usd, + tx.amount, tx.amount_success, tx.token_id, tx.amount_usd, tx.batch_num, tx.eth_block_num, tx.to_forge_l1_txs_num, tx.user_origin, - tx.deposit_amount, tx.deposit_amount_usd, tx.fee, tx.fee_usd, tx.nonce, + tx.deposit_amount, tx.deposit_amount_usd, tx.deposit_amount_success, tx.fee, tx.fee_usd, tx.nonce, 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, block.timestamp

@@ -905,7 +907,9 @@ ethAddr *ethCommon.Address, bjj *babyjub.PublicKey,

tokenID *common.TokenID, idx *common.Idx, batchNum *uint, txType *common.TxType, fromItem, limit *uint, order string, ) ([]TxAPI, uint64, error) { - // TODO: add success flags for L1s + // Warning: amount_success and deposit_amount_success have true as default for + // performance reasons. The expected default value is false (when txs are unforged) + // this case is handled at the function func (tx TxAPI) MarshalJSON() ([]byte, error) if ethAddr != nil && bjj != nil { return nil, 0, tracerr.Wrap(errors.New("ethAddr and bjj are incompatible")) }

@@ -914,9 +918,9 @@ var args []interface{}

queryStr := `SELECT tx.item_id, tx.is_l1, tx.id, tx.type, tx.position, hez_idx(tx.from_idx, token.symbol) AS from_idx, tx.from_eth_addr, tx.from_bjj, hez_idx(tx.to_idx, token.symbol) AS to_idx, tx.to_eth_addr, tx.to_bjj, - tx.amount, tx.token_id, tx.amount_usd, + tx.amount, tx.amount_success, tx.token_id, tx.amount_usd, tx.batch_num, tx.eth_block_num, tx.to_forge_l1_txs_num, tx.user_origin, - tx.deposit_amount, tx.deposit_amount_usd, tx.fee, tx.fee_usd, tx.nonce, + tx.deposit_amount, tx.deposit_amount_usd, tx.deposit_amount_success, tx.fee, tx.fee_usd, tx.nonce, 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, block.timestamp, count(*) OVER() AS total_items
M db/historydb/views.go → db/historydb/views.go

@@ -36,6 +36,8 @@ ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged

UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes DepositAmount *apitypes.BigIntStr `meddler:"deposit_amount"` HistoricDepositAmountUSD *float64 `meddler:"deposit_amount_usd"` + AmountSuccess bool `meddler:"amount_success"` + DepositAmountSuccess bool `meddler:"deposit_amount_success"` // L2 Fee *common.FeeSelector `meddler:"fee"` HistoricFeeUSD *float64 `meddler:"fee_usd"`

@@ -90,10 +92,18 @@ },

} if tx.IsL1 { jsonTx["L1orL2"] = "L1" + amountSuccess := tx.AmountSuccess + depositAmountSuccess := tx.DepositAmountSuccess + if tx.BatchNum == nil { + amountSuccess = false + depositAmountSuccess = false + } jsonTx["L1Info"] = map[string]interface{}{ "toForgeL1TransactionsNum": tx.ToForgeL1TxsNum, "userOrigin": tx.UserOrigin, "depositAmount": tx.DepositAmount, + "amountSuccess": amountSuccess, + "depositAmountSuccess": depositAmountSuccess, "historicDepositAmountUSD": tx.HistoricDepositAmountUSD, "ethereumBlockNum": tx.EthBlockNum, }