Skip to content

Commit

Permalink
update withdraw response (#580)
Browse files Browse the repository at this point in the history
* update withdraw response

* update test and handle error

* update withdrawal api
  • Loading branch information
halink0803 committed Oct 4, 2021
1 parent 7e1bcdb commit 4593a12
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 49 deletions.
99 changes: 80 additions & 19 deletions accounting/binance/storage/withdrawalstorage/withdraw_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func NewDB(sugar *zap.SugaredLogger, db *sqlx.DB) (*BinanceStorage, error) {
ALTER TABLE binance_withdrawals ADD COLUMN IF NOT EXISTS account TEXT;
ALTER TABLE binance_withdrawals ADD COLUMN IF NOT EXISTS timestamp TIMESTAMP;
ALTER TABLE binance_withdrawals ADD COLUMN IF NOT EXISTS amount FLOAT;
ALTER TABLE binance_withdrawals ADD COLUMN IF NOT EXISTS address TEXT;
ALTER TABLE binance_withdrawals ADD COLUMN IF NOT EXISTS asset TEXT;
ALTER TABLE binance_withdrawals ADD COLUMN IF NOT EXISTS tx TEXT;
ALTER TABLE binance_withdrawals ADD COLUMN IF NOT EXISTS apply_time BIGINT;
ALTER TABLE binance_withdrawals ADD COLUMN IF NOT EXISTS status INTEGER;
ALTER TABLE binance_withdrawals ADD COLUMN IF NOT EXISTS tx_fee FLOAT;
`

s := &BinanceStorage{
Expand Down Expand Up @@ -68,18 +76,27 @@ func (bd *BinanceStorage) Close() error {
//UpdateWithdrawHistory save withdraw history to db
func (bd *BinanceStorage) UpdateWithdrawHistory(withdrawHistories []binance.WithdrawHistory, account string) (err error) {
var (
logger = bd.sugar.With("func", caller.GetCurrentFunctionName())
withdrawJSON []byte
ids []string
dataJSON [][]byte
timestamps []time.Time
logger = bd.sugar.With("func", caller.GetCurrentFunctionName())
withdrawJSON []byte
ids, assets, addresses, txs []string
amounts, txFees []float64
statuses, applyTimes []int64
dataJSON [][]byte
timestamps []time.Time
)
const updateQuery = `INSERT INTO binance_withdrawals (id, data, timestamp, account)
const updateQuery = `INSERT INTO binance_withdrawals (id, data, timestamp, account, amount, address, asset, tx, apply_time, status, tx_fee)
VALUES(
unnest($1::TEXT[]),
unnest($2::JSONB[]),
unnest($3::TIMESTAMP[]),
$4
$4,
unnest($5::FLOAT[]),
unnest($6::TEXT[]),
unnest($7::TEXT[]),
unnest($8::TEXT[]),
unnest($9::BIGINT[]),
unnest($10::INTEGER[]),
unnest($11::FLOAT[])
) ON CONFLICT ON CONSTRAINT binance_withdrawals_pk DO UPDATE SET data = EXCLUDED.data;
`

Expand All @@ -105,19 +122,47 @@ func (bd *BinanceStorage) UpdateWithdrawHistory(withdrawHistories []binance.With
}
timestamps = append(timestamps, timestamp)
dataJSON = append(dataJSON, withdrawJSON)
assets = append(assets, withdraw.Asset)
addresses = append(addresses, withdraw.Address)
txs = append(txs, withdraw.TxID)
amount := float64(0)
if withdraw.Amount != "" {
amount, err = strconv.ParseFloat(withdraw.Amount, 64)
if err != nil {
return err
}
}
amounts = append(amounts, amount)
txFee := float64(0)
if withdraw.TxFee != "" {
txFee, err = strconv.ParseFloat(withdraw.TxFee, 64)
if err != nil {
return err
}
}
txFees = append(txFees, txFee)
statuses = append(statuses, withdraw.Status)
applyTimes = append(applyTimes, timestamp.UnixMilli())
}

if _, err = tx.Exec(updateQuery, pq.Array(ids), pq.Array(dataJSON), pq.Array(timestamps), account); err != nil {
return
if _, err = tx.Exec(updateQuery, pq.Array(ids), pq.Array(dataJSON), pq.Array(timestamps), account, pq.Array(amounts), pq.Array(addresses), pq.Array(assets), pq.Array(txs), pq.Array(applyTimes), pq.Array(statuses), pq.Array(txFees)); err != nil {
return err
}

return
return err
}

//WithdrawRecord represent a record of binace withdraw
type WithdrawRecord struct {
Account string `db:"account"`
Data pq.ByteaArray `db:"data"`
Account string `db:"account"`
Ids pq.StringArray `db:"ids"`
Amounts pq.Float64Array `db:"amounts"`
Assets pq.StringArray `db:"assets"`
Addresses pq.StringArray `db:"addresses"`
ApplyTimes pq.Int64Array `db:"apply_times"`
Txs pq.StringArray `db:"txs"`
Statuses pq.Int64Array `db:"statuses"`
TxFees pq.Float64Array `db:"tx_fees"`
}

//GetWithdrawHistory return list of withdraw fromTime to toTime
Expand All @@ -126,23 +171,39 @@ func (bd *BinanceStorage) GetWithdrawHistory(fromTime, toTime time.Time) (map[st
logger = bd.sugar.With("func", caller.GetCurrentFunctionName())
result = make(map[string][]binance.WithdrawHistory)
dbResult []WithdrawRecord
tmp binance.WithdrawHistory
)
const selectStmt = `SELECT account, ARRAY_AGG(data) as data FROM binance_withdrawals WHERE timestamp >=$1 AND timestamp <=$2 GROUP BY account;`
const selectStmt = `SELECT account,
ARRAY_AGG(id) as ids,
ARRAY_AGG(asset) as assets,
ARRAY_AGG(address) as addresses,
ARRAY_AGG(apply_time) as apply_times,
ARRAY_AGG(tx) as txs,
ARRAY_AGG(tx_fee) as tx_fees,
ARRAY_AGG(status) as statuses,
ARRAY_AGG(amount) as amounts FROM binance_withdrawals WHERE timestamp >=$1 AND timestamp <=$2 GROUP BY account;`

logger.Debugw("querying trade history...", "query", selectStmt)

if err := bd.db.Select(&dbResult, selectStmt, fromTime, toTime); err != nil {
logger.Errorw("failed to get withdraw history", "error", err)
return result, err
}

for _, record := range dbResult {
arrResult := []binance.WithdrawHistory{}
for _, data := range record.Data {
if err := json.Unmarshal(data, &tmp); err != nil {
return result, err
}
arrResult = append(arrResult, tmp)
for index := range record.Ids {
applyTime := time.UnixMilli(record.ApplyTimes[index])
apTime := applyTime.Format("2006-01-02 15:04:05")
arrResult = append(arrResult, binance.WithdrawHistory{
ID: record.Ids[index],
Asset: record.Assets[index],
Amount: strconv.FormatFloat(record.Amounts[index], 'f', -1, 64),
Address: record.Addresses[index],
TxID: record.Txs[index],
ApplyTime: apTime,
Status: record.Statuses[index],
TxFee: strconv.FormatFloat(record.TxFees[index], 'f', -1, 64),
})
}
result[record.Account] = arrResult
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,27 @@ func TestBinanceWithdrawStorage(t *testing.T) {
TxID: "0x102556d7ebb4e8aea93dca7c61c5926946312af98d3c38e48b062e06582 4b70f",
ApplyTime: "2018-01-25 13:23:14",
Status: 6,
TxFee: "1432",
},
{
ID: "53bb6b37ce61443f9d7fd99c21652baa",
Amount: "0.64",
Address: "0xe813dee553d09567D4873d9bd5A 4914796367082",
Asset: "ETH",
TxID: "0x679d514dafb4c8eee1fce3b00a984167bed02bf69ca278e49fa4c4a8fb2308ed",
ApplyTime: "2018-03-26 4:09:12",
ApplyTime: "2018-03-26 04:09:12",
Status: 6,
TxFee: "4208",
},
{
ID: "53bb6b37ce61443f9d7fd99c21652baaa",
Amount: "0.64",
Address: "0xe813dee553d09567D4873d9bd5A 4914796367082",
Asset: "ETH",
TxID: "0x679d514dafb4c8eee1fce3b00a984167bed02bf69ca278e49fa4c4a8fb2308ed",
ApplyTime: "2018-03-26 4:09:12",
ApplyTime: "2018-03-26 04:09:12",
Status: 6,
TxFee: "29742",
},
}
expectedData = map[string][]binance.WithdrawHistory{
Expand All @@ -55,24 +58,27 @@ func TestBinanceWithdrawStorage(t *testing.T) {
TxID: "0x102556d7ebb4e8aea93dca7c61c5926946312af98d3c38e48b062e06582 4b70f",
ApplyTime: "2018-01-25 13:23:14",
Status: 6,
TxFee: "1432",
},
{
ID: "53bb6b37ce61443f9d7fd99c21652baa",
Amount: "0.64",
Address: "0xe813dee553d09567D4873d9bd5A 4914796367082",
Asset: "ETH",
TxID: "0x679d514dafb4c8eee1fce3b00a984167bed02bf69ca278e49fa4c4a8fb2308ed",
ApplyTime: "2018-03-26 4:09:12",
ApplyTime: "2018-03-26 04:09:12",
Status: 6,
TxFee: "4208",
},
{
ID: "53bb6b37ce61443f9d7fd99c21652baaa",
Amount: "0.64",
Address: "0xe813dee553d09567D4873d9bd5A 4914796367082",
Asset: "ETH",
TxID: "0x679d514dafb4c8eee1fce3b00a984167bed02bf69ca278e49fa4c4a8fb2308ed",
ApplyTime: "2018-03-26 4:09:12",
ApplyTime: "2018-03-26 04:09:12",
Status: 6,
TxFee: "29742",
},
},
}
Expand Down
55 changes: 31 additions & 24 deletions accounting/cex-withdrawal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"net/http"
"strconv"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -37,18 +38,14 @@ type queryInput struct {

// BinanceWithdrawalResponse ...
type BinanceWithdrawalResponse struct {
ID string `json:"id"`
Amount string `json:"amount"`
Address string `json:"address"`
Asset string `json:"coin"`
TxID string `json:"txId"`
ApplyTime int64 `json:"applyTime"`
Status int64 `json:"status"`
TxFee string `json:"transactionFee"`
WithrawOrderID string `json:"withdrawOrderId"`
Network string `json:"network"`
TransferType int64 `json:"transferType"`
ConfirmNumber int64 `json:"confirmNo"`
ID string `json:"id"`
Amount float64 `json:"amount"`
Address string `json:"address"`
Asset string `json:"asset"`
TxID string `json:"txId"`
ApplyTime int64 `json:"applyTime"`
Status int64 `json:"status"`
TxFee float64 `json:"transactionFee"`
}

type response struct {
Expand Down Expand Up @@ -125,19 +122,29 @@ func (sv *Server) get(c *gin.Context) {
if sErr != nil {
return
}
amount, err := strconv.ParseFloat(withdrawal.Amount, 64)
if err != nil {
sv.sugar.Errorw("failed to parse withdraw amount", "error", err)
httputil.ResponseFailure(
c,
http.StatusInternalServerError,
err,
)
return
}
txFee, err := strconv.ParseFloat(withdrawal.TxFee, 64)
if err != nil {
sv.sugar.Errorw("failed to parse tx fee", "error", err)
}
response = append(response, BinanceWithdrawalResponse{
ID: withdrawal.ID,
Amount: withdrawal.Amount,
Address: withdrawal.Address,
Asset: withdrawal.Asset,
TxID: withdrawal.TxID,
ApplyTime: applyTime.UnixMilli(),
Status: withdrawal.Status,
TxFee: withdrawal.TxFee,
WithrawOrderID: withdrawal.WithrawOrderID,
Network: withdrawal.Network,
TransferType: withdrawal.TransferType,
ConfirmNumber: withdrawal.ConfirmNumber,
ID: withdrawal.ID,
Amount: amount,
Address: withdrawal.Address,
Asset: withdrawal.Asset,
TxID: withdrawal.TxID,
ApplyTime: applyTime.UnixMilli(),
Status: withdrawal.Status,
TxFee: txFee,
})
}
binanceResponse[account] = response
Expand Down
8 changes: 6 additions & 2 deletions accounting/cex-withdrawal/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestGetHuobiWithdrawal(t *testing.T) {
Asset: "ETH",
TxID: "0xdf33b22bdb2b28b1f75ccd201a4a4m6e7g83jy5fc5d5a9d1340961598cfcb0a1",
ApplyTime: "2018-05-08 4:35:26",
TxFee: "12131",
Status: 4,
},
{
Expand All @@ -67,27 +68,30 @@ func TestGetHuobiWithdrawal(t *testing.T) {
TxID: "b3c6219639c8ae3f9cf010cdc24fw7f7yt8j1e063f9b4bd1a05cb44c4b6e2509",
ApplyTime: "2018-05-08 4:35:27",
Status: 4,
TxFee: "31231",
},
}

binanceExpectedResponse = []BinanceWithdrawalResponse{
{
ID: "7213fea8e94b4a5593d507237e5a555b",
Amount: "1",
Amount: 1,
Address: "0x6915f16f8791d0a1cc2bf47c13a6b2a92000504b",
Asset: "ETH",
TxID: "0xdf33b22bdb2b28b1f75ccd201a4a4m6e7g83jy5fc5d5a9d1340961598cfcb0a1",
ApplyTime: 1525754126000,
Status: 4,
TxFee: 12131,
},
{
ID: "7213fea8e94b4a5534ggsd237e5a555b",
Amount: "1000",
Amount: 1000,
Address: "463tWEBn5XZJSxLU34r6g7h8jtxuNcDbjLSjkn3XAXHCbLrTTErJrBWYgHJQyrCwkNgYvyV3z8zctJLPCZy24jvb3NiTcTJ",
Asset: "XMR",
TxID: "b3c6219639c8ae3f9cf010cdc24fw7f7yt8j1e063f9b4bd1a05cb44c4b6e2509",
ApplyTime: 1525754127000,
Status: 4,
TxFee: 31231,
},
}

Expand Down

0 comments on commit 4593a12

Please sign in to comment.