2018-04-16 20:15:30 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/storage"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-04-16 20:15:30 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
const feeMode = 0x0
|
|
|
|
|
|
|
|
// Assets is mapping between AssetID and the AssetState.
|
|
|
|
type Assets map[util.Uint256]*AssetState
|
|
|
|
|
|
|
|
func (a Assets) commit(b storage.Batch) error {
|
2019-09-16 09:18:13 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
2018-04-16 20:15:30 +00:00
|
|
|
for hash, state := range a {
|
2019-09-16 16:31:49 +00:00
|
|
|
state.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
2018-04-16 20:15:30 +00:00
|
|
|
}
|
|
|
|
key := storage.AppendPrefix(storage.STAsset, hash.Bytes())
|
|
|
|
b.Put(key, buf.Bytes())
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-11 09:15:16 +00:00
|
|
|
// putAssetStateIntoStore puts given asset state into the given store.
|
|
|
|
func putAssetStateIntoStore(s storage.Store, as *AssetState) error {
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
as.EncodeBinary(buf.BinWriter)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return buf.Err
|
|
|
|
}
|
|
|
|
key := storage.AppendPrefix(storage.STAsset, as.ID.Bytes())
|
|
|
|
return s.Put(key, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
2019-02-09 15:53:58 +00:00
|
|
|
// AssetState represents the state of an NEO registered Asset.
|
2018-04-16 20:15:30 +00:00
|
|
|
type AssetState struct {
|
|
|
|
ID util.Uint256
|
|
|
|
AssetType transaction.AssetType
|
|
|
|
Name string
|
|
|
|
Amount util.Fixed8
|
|
|
|
Available util.Fixed8
|
|
|
|
Precision uint8
|
|
|
|
FeeMode uint8
|
2018-11-26 21:12:33 +00:00
|
|
|
FeeAddress util.Uint160
|
2019-08-27 13:29:42 +00:00
|
|
|
Owner *keys.PublicKey
|
2018-04-16 20:15:30 +00:00
|
|
|
Admin util.Uint160
|
|
|
|
Issuer util.Uint160
|
|
|
|
Expiration uint32
|
|
|
|
IsFrozen bool
|
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// DecodeBinary implements Serializable interface.
|
|
|
|
func (a *AssetState) DecodeBinary(br *io.BinReader) {
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&a.ID)
|
|
|
|
br.ReadLE(&a.AssetType)
|
2018-04-16 20:15:30 +00:00
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
a.Name = br.ReadString()
|
2018-04-16 20:15:30 +00:00
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&a.Amount)
|
|
|
|
br.ReadLE(&a.Available)
|
|
|
|
br.ReadLE(&a.Precision)
|
|
|
|
br.ReadLE(&a.FeeMode)
|
|
|
|
br.ReadLE(&a.FeeAddress)
|
2018-04-16 20:15:30 +00:00
|
|
|
|
2019-08-27 13:29:42 +00:00
|
|
|
a.Owner = &keys.PublicKey{}
|
2019-09-16 16:31:49 +00:00
|
|
|
a.Owner.DecodeBinary(br)
|
2019-08-28 16:27:06 +00:00
|
|
|
br.ReadLE(&a.Admin)
|
|
|
|
br.ReadLE(&a.Issuer)
|
|
|
|
br.ReadLE(&a.Expiration)
|
|
|
|
br.ReadLE(&a.IsFrozen)
|
2018-04-16 20:15:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
// EncodeBinary implements Serializable interface.
|
|
|
|
func (a *AssetState) EncodeBinary(bw *io.BinWriter) {
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteLE(a.ID)
|
|
|
|
bw.WriteLE(a.AssetType)
|
|
|
|
bw.WriteString(a.Name)
|
|
|
|
bw.WriteLE(a.Amount)
|
|
|
|
bw.WriteLE(a.Available)
|
|
|
|
bw.WriteLE(a.Precision)
|
|
|
|
bw.WriteLE(a.FeeMode)
|
|
|
|
bw.WriteLE(a.FeeAddress)
|
|
|
|
|
2019-09-16 16:31:49 +00:00
|
|
|
a.Owner.EncodeBinary(bw)
|
|
|
|
|
2019-08-28 16:27:06 +00:00
|
|
|
bw.WriteLE(a.Admin)
|
|
|
|
bw.WriteLE(a.Issuer)
|
|
|
|
bw.WriteLE(a.Expiration)
|
|
|
|
bw.WriteLE(a.IsFrozen)
|
2018-04-16 20:15:30 +00:00
|
|
|
}
|
2018-11-26 21:12:33 +00:00
|
|
|
|
2019-01-22 12:14:52 +00:00
|
|
|
// GetName returns the asset name based on its type.
|
2018-11-26 21:12:33 +00:00
|
|
|
func (a *AssetState) GetName() string {
|
|
|
|
|
|
|
|
if a.AssetType == transaction.GoverningToken {
|
|
|
|
return "NEO"
|
|
|
|
} else if a.AssetType == transaction.UtilityToken {
|
|
|
|
return "NEOGas"
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.Name
|
|
|
|
}
|