-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add totalAssetValue in GetAccount API response #214
Changes from 1 commit
7ecd594
2ab97e7
5461dff
c97f7ab
caa955d
b9b239f
23faf83
4ec828c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package account | |
|
||
import ( | ||
"context" | ||
"math/big" | ||
"sort" | ||
"strconv" | ||
|
||
|
@@ -76,6 +77,9 @@ func (l *GetAccountLogic) GetAccount(req *types.ReqGetAccount) (resp *types.Acco | |
Nonce: account.Nonce, | ||
Assets: make([]*types.AccountAsset, 0, len(account.AssetInfo)), | ||
} | ||
|
||
totalAssetPrice := big.NewFloat(0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, OK. |
||
|
||
for _, asset := range account.AssetInfo { | ||
if asset.AssetId > maxAssetId { | ||
continue //it is used for offer related, or empty balance; max ip id should be less than max asset id | ||
|
@@ -104,9 +108,23 @@ func (l *GetAccountLogic) GetAccount(req *types.ReqGetAccount) (resp *types.Acco | |
Balance: asset.Balance.String(), | ||
Price: strconv.FormatFloat(assetPrice, 'E', -1, 64), | ||
}) | ||
|
||
// BNB for example: | ||
// 1. Convert unit of balance from wei to BNB | ||
// 2. Calculate the result of (BNB balance * price per BNB) | ||
balanceInFloat := new(big.Float).SetInt(asset.Balance) | ||
unitConversion := new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Different assets have different units. |
||
assetTotalPrice := balanceInFloat.Mul( | ||
new(big.Float).Quo(balanceInFloat, new(big.Float).SetInt(unitConversion)), | ||
big.NewFloat(assetPrice), | ||
) | ||
|
||
totalAssetPrice = totalAssetPrice.Add(totalAssetPrice, assetTotalPrice) | ||
} | ||
} | ||
|
||
resp.TotalAssetPrice = totalAssetPrice.Text('f', -1) | ||
|
||
sort.Slice(resp.Assets, func(i, j int) bool { | ||
return resp.Assets[i].Id < resp.Assets[j].Id | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to change the doc