Skip to content
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

Merged
merged 8 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add totalAssetPrice in GetAccount API response
  • Loading branch information
carl-h-20230331 committed Oct 3, 2022
commit 7ecd5945216d85f0ecb79614be0c9d84fa4810d3
17 changes: 9 additions & 8 deletions docs/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,15 @@ Send raw transaction

#### Account

| Name | Type | Description | Required |
|--------|-----------------------------------| ----------- | -------- |
| status | integer | | Yes |
| index | long | | Yes |
| name | string | | Yes |
| pk | string | | Yes |
| nonce | long | | Yes |
| assets | [ [AccountAsset](#accountasset) ] | | Yes |
| Name | Type | Description | Required |
|-------------------|-----------------------------------| ----------- | -------- |
| status | integer | | Yes |
| index | long | | Yes |
| name | string | | Yes |
| pk | string | | Yes |
| nonce | long | | Yes |
| assets | [ [AccountAsset](#accountasset) ] | | Yes |
| total_asset_price | string | | Yes |
Copy link
Contributor

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


#### AccountAsset

Expand Down
18 changes: 18 additions & 0 deletions service/apiserver/internal/logic/account/getaccountlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"context"
"math/big"
"sort"
"strconv"

Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

price seems to be not a good name. How about totalAssetAmount or totalAssetValue? Can you refer to some dapp (e.g., pancake, uniswap) for the name

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totalAssetValue seems better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, OK.
Thanks 👍


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
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different assets have different units. Decimal field of asset is for this, i believe.

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
})
Expand Down