2020-01-13 10:21:44 +00:00
|
|
|
package result
|
2019-11-15 19:04:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
2019-11-28 16:06:09 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/state"
|
2019-11-15 19:04:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnspentBalanceInfo wrapper is used to represent single unspent asset entry
|
|
|
|
// in `getunspents` output.
|
|
|
|
type UnspentBalanceInfo struct {
|
2019-11-28 16:06:09 +00:00
|
|
|
Unspents []state.UnspentBalance `json:"unspent"`
|
|
|
|
AssetHash util.Uint256 `json:"asset_hash"`
|
|
|
|
Asset string `json:"asset"`
|
|
|
|
AssetSymbol string `json:"asset_symbol"`
|
|
|
|
Amount util.Fixed8 `json:"amount"`
|
2019-11-15 19:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unspents wrapper is used to represent getunspents return result.
|
|
|
|
type Unspents struct {
|
|
|
|
Balance []UnspentBalanceInfo `json:"balance"`
|
|
|
|
Address string `json:"address"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalAssets stores a map of asset IDs to user-friendly strings ("NEO"/"GAS").
|
|
|
|
var GlobalAssets = map[string]string{
|
|
|
|
"c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b": "NEO",
|
|
|
|
"602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7": "GAS",
|
|
|
|
}
|
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
// NewUnspents creates a new Account wrapper using given Blockchainer.
|
|
|
|
func NewUnspents(a *state.Account, chain core.Blockchainer, addr string) Unspents {
|
2019-11-15 19:04:10 +00:00
|
|
|
res := Unspents{
|
|
|
|
Address: addr,
|
|
|
|
Balance: make([]UnspentBalanceInfo, 0, len(a.Balances)),
|
|
|
|
}
|
|
|
|
balanceValues := a.GetBalanceValues()
|
|
|
|
for k, v := range a.Balances {
|
2019-11-27 09:23:18 +00:00
|
|
|
name, ok := GlobalAssets[k.StringLE()]
|
2019-11-15 19:04:10 +00:00
|
|
|
if !ok {
|
|
|
|
as := chain.GetAssetState(k)
|
|
|
|
if as != nil {
|
|
|
|
name = as.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Balance = append(res.Balance, UnspentBalanceInfo{
|
|
|
|
Unspents: v,
|
|
|
|
AssetHash: k,
|
|
|
|
Asset: name,
|
|
|
|
AssetSymbol: name,
|
|
|
|
Amount: balanceValues[k],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|