This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
/
asset.go
62 lines (52 loc) · 1.57 KB
/
asset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package state
import (
"fmt"
"strings"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"
)
// Asset is a Stellar asset.
type Asset string
const NativeAsset = Asset("native")
// IsNative returns true if the asset is the native asset of the stellar
// network.
func (a Asset) IsNative() bool {
return a.Asset().IsNative()
}
// Code returns the asset code of credit assets.
func (a Asset) Code() string {
return a.Asset().GetCode()
}
// Issuer returns the issuer of credit assets.
func (a Asset) Issuer() string {
return a.Asset().GetIssuer()
}
// Asset returns an asset from the stellar/go/txnbuild package with the
// same asset code and issuer, or a native asset if a native asset.
func (a Asset) Asset() txnbuild.Asset {
parts := strings.SplitN(string(a), ":", 2)
if len(parts) == 1 {
return txnbuild.NativeAsset{}
}
return txnbuild.CreditAsset{
Code: parts[0],
Issuer: parts[1],
}
}
// StringCanonical returns a string friendly representation of the asset in
// canonical form.
func (a Asset) StringCanonical() string {
if a.IsNative() {
return xdr.AssetTypeToString[xdr.AssetTypeAssetTypeNative]
}
return fmt.Sprintf("%s:%s", a.Code(), a.Issuer())
}
// EqualTrustLineAsset returns true if the canonical strings of this asset and a
// given trustline asset are equal, else false.
func (a Asset) EqualTrustLineAsset(ta xdr.TrustLineAsset) bool {
switch ta.Type {
case xdr.AssetTypeAssetTypeNative, xdr.AssetTypeAssetTypeCreditAlphanum4, xdr.AssetTypeAssetTypeCreditAlphanum12:
return ta.ToAsset().StringCanonical() == a.StringCanonical()
}
return false
}