forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries_asset.go
94 lines (81 loc) · 2.89 KB
/
queries_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package tickerdb
// InsertOrUpdateAsset inserts an Asset on the database (if new),
// or updates an existing one
func (s *TickerSession) InsertOrUpdateAsset(a *Asset, preserveFields []string) (err error) {
return s.performUpsertQuery(*a, "assets", "assets_code_issuer_account", preserveFields)
}
// GetAssetByCodeAndIssuerAccount searches for an Asset with the given code
// and public key, and returns its ID in case it is found.
func (s *TickerSession) GetAssetByCodeAndIssuerAccount(
code string,
issuerAccount string,
) (found bool, id int32, err error) {
var assets []Asset
tbl := s.GetTable("assets")
err = tbl.Select(
&assets,
"assets.code = ? AND assets.issuer_account = ?",
code,
issuerAccount,
).Exec()
if err != nil {
return
}
if len(assets) > 0 {
id = assets[0].ID
found = true
}
return
}
// GetAllValidAssets returns a slice with all assets in the database
// with is_valid = true
func (s *TickerSession) GetAllValidAssets() (assets []Asset, err error) {
tbl := s.GetTable("assets")
err = tbl.Select(
&assets,
"assets.is_valid = TRUE",
).Exec()
return
}
// GetAssetsWithNestedIssuer returns a slice with all assets in the database
// with is_valid = true, also adding the nested Issuer attribute
func (s *TickerSession) GetAssetsWithNestedIssuer() (assets []Asset, err error) {
const q = `
SELECT
a.code, a.issuer_account, a.type, a.num_accounts, a.auth_required, a.auth_revocable,
a.amount, a.asset_controlled_by_domain, a.anchor_asset_code, a.anchor_asset_type,
a.is_valid, a.validation_error, a.last_valid, a.last_checked, a.display_decimals,
a.name, a.description, a.conditions, a.is_asset_anchored, a.fixed_number, a.max_number,
a.is_unlimited, a.redemption_instructions, a.collateral_addresses, a.collateral_address_signatures,
a.countries, a.status, a.issuer_id, i.public_key, i.name, i.url, i.toml_url, i.federation_server,
i.auth_server, i.transfer_server, i.web_auth_endpoint, i.deposit_server, i.org_twitter
FROM assets AS a
INNER JOIN issuers AS i ON a.issuer_id = i.id
WHERE a.is_valid = TRUE
`
rows, err := s.DB.Query(q)
if err != nil {
return
}
for rows.Next() {
var (
a Asset
i Issuer
)
err = rows.Scan(
&a.Code, &a.IssuerAccount, &a.Type, &a.NumAccounts, &a.AuthRequired, &a.AuthRevocable,
&a.Amount, &a.AssetControlledByDomain, &a.AnchorAssetCode, &a.AnchorAssetType,
&a.IsValid, &a.ValidationError, &a.LastValid, &a.LastChecked, &a.DisplayDecimals,
&a.Name, &a.Desc, &a.Conditions, &a.IsAssetAnchored, &a.FixedNumber, &a.MaxNumber,
&a.IsUnlimited, &a.RedemptionInstructions, &a.CollateralAddresses, &a.CollateralAddressSignatures,
&a.Countries, &a.Status, &a.IssuerID, &i.PublicKey, &i.Name, &i.URL, &i.TOMLURL, &i.FederationServer,
&i.AuthServer, &i.TransferServer, &i.WebAuthEndpoint, &i.DepositServer, &i.OrgTwitter,
)
if err != nil {
return
}
a.Issuer = i
assets = append(assets, a)
}
return
}