Skip to content

Commit

Permalink
fix: remove unused cache binance (#1304)
Browse files Browse the repository at this point in the history
  • Loading branch information
trkhoi authored Jan 5, 2024
1 parent 1cdfd2b commit 6326b41
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 78 deletions.
99 changes: 24 additions & 75 deletions pkg/entities/wallet_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strconv"
"strings"
"time"

"github.com/defipod/mochi/pkg/consts"
"github.com/defipod/mochi/pkg/logger"
Expand Down Expand Up @@ -77,56 +76,31 @@ func mergeAsset(userAsset, fundingAsset []response.BinanceUserAssetResponse) []r
}

func (e *Entity) GetStakingProduct(profileId, apiKey, apiSecret string) (res []response.BinanceUserAssetResponse, err error) {
// redis cache
value, err := e.cache.HashGet(fmt.Sprintf("binance-staking-position-%s-%s", profileId, apiKey))
pos, err := e.svc.Binance.GetStakingProductPosition(apiKey, apiSecret)
if err != nil {
e.log.Error(err, "[entities.GetStakingProduct] Failed to get cache user data binance")
e.log.Error(err, "[entities.GetStakingProduct] Failed to get staking product position")
return nil, err
}

if len(value) == 0 {
pos, err := e.svc.Binance.GetStakingProductPosition(apiKey, apiSecret)
for _, p := range pos {
amount, err := strconv.ParseFloat(p.Amount, 64)
if err != nil {
e.log.Error(err, "[entities.GetStakingProduct] Failed to get staking product position")
return nil, err
}

for _, p := range pos {
amount, err := strconv.ParseFloat(p.Amount, 64)
if err != nil {
return nil, err
}

rewardAmt, err := strconv.ParseFloat(p.RewardAmt, 64)
if err != nil {
return nil, err
}

rawData, _ := json.Marshal(p)

res = append(res, response.BinanceUserAssetResponse{
Asset: p.Asset,
Free: fmt.Sprint(amount + rewardAmt),
BtcValuation: "0",
DetailString: string(rawData),
})
}

tmp, _ := json.Marshal(res)
encodeData := map[string]string{
"data": string(tmp),
}

err = e.cache.HashSet(fmt.Sprintf("binance-staking-position-%s-%s", profileId, apiKey), encodeData, 30*time.Minute)
if err != nil {
e.log.Error(err, "Failed to set cache data wallet")
return nil, err
}
} else {
err = json.Unmarshal([]byte(value["data"]), &res)
rewardAmt, err := strconv.ParseFloat(p.RewardAmt, 64)
if err != nil {
return nil, err
}

rawData, _ := json.Marshal(p)

res = append(res, response.BinanceUserAssetResponse{
Asset: p.Asset,
Free: fmt.Sprint(amount + rewardAmt),
BtcValuation: "0",
DetailString: string(rawData),
})
}

for i, r := range res {
Expand All @@ -143,46 +117,21 @@ func (e *Entity) GetStakingProduct(profileId, apiKey, apiSecret string) (res []r
}

func (e *Entity) GetLendingAccount(profileId, apiKey, apiSecret string) (res []response.BinanceUserAssetResponse, err error) {
// redis cache
value, err := e.cache.HashGet(fmt.Sprintf("binance-lending-account-%s-%s", profileId, apiKey))
lendingAcc, err := e.svc.Binance.GetLendingAccount(apiKey, apiSecret)
if err != nil {
e.log.Error(err, "[entities.GetLendingAccount] Failed to get cache user data binance")
e.log.Error(err, "[entities.GetLendingAccount] Failed to get lending account")
return nil, err
}

if len(value) == 0 {
lendingAcc, err := e.svc.Binance.GetLendingAccount(apiKey, apiSecret)
if err != nil {
e.log.Error(err, "[entities.GetLendingAccount] Failed to get lending account")
return nil, err
}

for _, l := range lendingAcc.PositionAmountVos {
rawData, _ := json.Marshal(l)

res = append(res, response.BinanceUserAssetResponse{
Asset: l.Asset,
Free: l.Amount,
BtcValuation: l.AmountInBTC,
DetailString: string(rawData),
})
}

tmp, _ := json.Marshal(res)
encodeData := map[string]string{
"data": string(tmp),
}
for _, l := range lendingAcc.PositionAmountVos {
rawData, _ := json.Marshal(l)

err = e.cache.HashSet(fmt.Sprintf("binance-lending-account-%s-%s", profileId, apiKey), encodeData, 30*time.Minute)
if err != nil {
e.log.Error(err, "Failed to set cache data wallet")
return nil, err
}
} else {
err = json.Unmarshal([]byte(value["data"]), &res)
if err != nil {
return nil, err
}
res = append(res, response.BinanceUserAssetResponse{
Asset: l.Asset,
Free: l.Amount,
BtcValuation: l.AmountInBTC,
DetailString: string(rawData),
})
}

for i, r := range res {
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/binance/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func GetFundingAsset(apiKey, apiSecret string) (assets []response.BinanceUserAss
func GetStakingProductPosition(apiKey, apiSecret string) (pos []response.BinanceStakingProductPosition, err error) {
q := map[string]string{
"timestamp": strconv.Itoa(int(time.Now().UnixMilli())),
"product": "STAKING",
"product": "L_DEFI",
}
queryString := butils.QueryString(q, apiSecret)

Expand Down
4 changes: 2 additions & 2 deletions pkg/service/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ func (b *Binance) GetFutureAccount(apiKey, apiSecret string) (*response.BinanceF
}

func (b *Binance) GetFutureAccountInfo(apiKey, apiSecret string) ([]response.BinanceFuturePositionInfo, error) {
b.logger.Debug("start binance.GetFutureAccount()")
defer b.logger.Debug("end binance.GetFutureAccount()")
b.logger.Debug("start binance.GetFutureAccountInfo()")
defer b.logger.Debug("end binance.GetFutureAccountInfo()")

res := []response.BinanceFuturePositionInfo{}

Expand Down

0 comments on commit 6326b41

Please sign in to comment.