Skip to content

Commit

Permalink
Add support for colon-separated asset format.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfe committed Mar 18, 2018
1 parent c01984e commit abdaf63
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
7 changes: 7 additions & 0 deletions cli/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ func TestAssets(t *testing.T) {

cli.TestCommand("asset del USD-chase")
expectOutput(t, cli, "error", "asset issuer USD-chase")

expectOutput(t, cli, "GBY7XDYKXBDHQ2B523SF7K6BNJNRYHVQMWY7AYAEKTYLCQMYVFHL57UM", "asset issuer USD:GBY7XDYKXBDHQ2B523SF7K6BNJNRYHVQMWY7AYAEKTYLCQMYVFHL57UM")
cli.TestCommand("account set citibank GBY7XDYKXBDHQ2B523SF7K6BNJNRYHVQMWY7AYAEKTYLCQMYVFHL57UM")
expectOutput(t, cli, "GBY7XDYKXBDHQ2B523SF7K6BNJNRYHVQMWY7AYAEKTYLCQMYVFHL57UM", "asset issuer USD:citibank")
expectOutput(t, cli, "USD", "asset code USD:citibank")
expectOutput(t, cli, "credit_alphanum4", "asset type USD:citibank")
expectOutput(t, cli, "credit_alphanum12", "asset type USD:citibank:credit_alphanum12")
}
51 changes: 40 additions & 11 deletions cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,51 @@ func (cli *CLI) ResolveAsset(name string) (*microstellar.Asset, error) {
return microstellar.NativeAsset, nil
}

readField := func(field string) (string, error) {
key := fmt.Sprintf("asset:%s:%s", name, field)
val, err := cli.GetVar(key)
var code, issuer, assetType string
if strings.Contains(name, ":") {
var issuerName string
parts := strings.Split(name, ":")
if len(parts) < 2 {
return nil, errors.Errorf("bad asset: %s", name)
}

code = parts[0]
issuerName = parts[1]

if len(parts) > 2 {
assetType = parts[2]
} else {
if len(code) <= 5 {
assetType = string(microstellar.Credit4Type)
} else {
assetType = string(microstellar.Credit12Type)
}
}

var err error
issuer, err = cli.ResolveAccount(logrus.Fields{"method": "ResolveAsset"}, issuerName, "address")
if err != nil {
return "", err
return nil, errors.Errorf("bad asset issuer: %v", issuerName)
}
} else {
readField := func(field string) (string, error) {
key := fmt.Sprintf("asset:%s:%s", name, field)
val, err := cli.GetVar(key)
if err != nil {
return "", err
}

return val, nil
}
return val, nil
}

code, err1 := readField("code")
issuer, err2 := readField("issuer")
assetType, err3 := readField("type")
var err1, err2, err3 error
code, err1 = readField("code")
issuer, err2 = readField("issuer")
assetType, err3 = readField("type")

if err1 != nil || err2 != nil || err3 != nil {
return nil, errors.Errorf("could not read asset: %v, %v, %v", err1, err2, err3)
if err1 != nil || err2 != nil || err3 != nil {
return nil, errors.Errorf("could not read asset: %v, %v, %v", err1, err2, err3)
}
}

var asset *microstellar.Asset
Expand Down

0 comments on commit abdaf63

Please sign in to comment.