Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simlify core symbol config #148

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 5 additions & 50 deletions eosc/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import (
"strings"
"time"

"go.uber.org/zap"

yaml2json "github.com/bronze1man/go-yaml2json"
"github.com/eoscanada/eos-go"
"github.com/eoscanada/eos-go/ecc"
"github.com/eoscanada/eos-go/sudo"
"github.com/eoscanada/eosc/cli"
eosvault "github.com/eoscanada/eosc/vault"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

Expand Down Expand Up @@ -88,56 +85,14 @@ func getAPI() *eos.API {
return api
}

var coreSymbolIsCached bool
var coreSymbol eos.Symbol
var sym string
var dec uint8

func getCoreSymbol() eos.Symbol {
if coreSymbolIsCached {
return coreSymbol
}

// In the event of a failure, we do not want to re-perform an API call,
// so let's record the fact that getCoreSymbol is cached right here.
// The init core symbol will take care of setting an approriate core
// symbol from global flag and reporting the error.
coreSymbolIsCached = true
if err := initCoreSymbol(); err != nil {
coreSymbol = eos.EOSSymbol
zlog.Debug(
"unable to retrieve core symbol from API, falling back to default",
zap.Error(err),
zap.Stringer("default", coreSymbol),
)
}

return coreSymbol
}

func initCoreSymbol() error {
resp, err := getAPI().GetTableRows(eos.GetTableRowsRequest{
Code: "eosio",
Scope: "eosio",
Table: "rammarket",
JSON: true,
})

if err != nil {
return fmt.Errorf("unable to fetch table: %s", err)
}

result := gjson.GetBytes(resp.Rows, "0.quote.balance")
if !result.Exists() {
return errors.New("table has not expected format")
}

asset, err := eos.NewAsset(result.String())
if !result.Exists() {
return fmt.Errorf("quote balance asset %q is not valid: %s", result.String(), err)
}
sym = viper.GetString("global-core-symbol")
dec = uint8(viper.GetInt("global-core-decimals"))

zlog.Debug("Retrieved core symbol from API, using it as default core symbol", zap.Stringer("symbol", asset.Symbol))
coreSymbol = asset.Symbol
return nil
return eos.Symbol{Precision: dec, Symbol: sym}
}

func sanitizeAPIURL(input string) string {
Expand Down
25 changes: 13 additions & 12 deletions eosc/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,23 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

RootCmd.PersistentFlags().StringP("core-symbol", "c", "", "Core symbol to use for all commands (default inferred from API if possible, 4,EOS otherwise)")
RootCmd.PersistentFlags().BoolP("debug", "", false, "Enables verbose API debug messages")
RootCmd.PersistentFlags().StringP("vault-file", "", "./eosc-vault.json", "Wallet file that contains encrypted key material")
RootCmd.PersistentFlags().StringSliceP("wallet-url", "", []string{}, "Base URL to wallet endpoint. You can pass this multiple times to use the multi-signer (will use each wallet to sign multi-sig transactions).")
RootCmd.PersistentFlags().StringP("core-symbol", "S", "EOS", "Core symbol to use for all commands")
RootCmd.PersistentFlags().IntP("core-decimals", "D", 4, "Core symbol decimals")
RootCmd.PersistentFlags().BoolP("debug", "v", false, "Enables verbose API debug messages")
RootCmd.PersistentFlags().StringP("vault-file", "W", "./eosc-vault.json", "Wallet file that contains encrypted key material")
RootCmd.PersistentFlags().StringSliceP("wallet-url", "", []string{"http://127.0.0.1:6666"}, "Base URL to wallet endpoint")
RootCmd.PersistentFlags().StringP("api-url", "u", "https://mainnet.eoscanada.com", "API endpoint of eos.io blockchain node")
RootCmd.PersistentFlags().StringSliceP("permission", "p", []string{}, "Permission to sign transactions with. Optionally specify more than one, or separate by comma")
RootCmd.PersistentFlags().StringSliceP("http-header", "H", []string{}, "HTTP header to add to a request. Optionally repeat this option to specify multiple headers")
RootCmd.PersistentFlags().StringP("kms-gcp-keypath", "", "", "Path to the cryptoKeys within a keyRing on GCP")
RootCmd.PersistentFlags().StringP("write-transaction", "", "", "Do not broadcast the transaction produced, but write it in json to the given filename instead.")
RootCmd.PersistentFlags().StringP("offline-head-block", "", "", "Provide a recent block ID (long-form hex) for TaPoS. Use all --offline options to sign transactions offline.")
RootCmd.PersistentFlags().StringP("offline-chain-id", "", "", "Chain ID to sign transaction with. Use all --offline- options to sign transactions offline.")
RootCmd.PersistentFlags().StringSliceP("offline-sign-key", "", []string{}, "Public key to use to sign transaction. Must be in your vault or wallet. Use all --offline- options to sign transactions offline.")
RootCmd.PersistentFlags().BoolP("skip-sign", "", false, "Do not sign the transaction. Use with --write-transaction.")
RootCmd.PersistentFlags().IntP("expiration", "", 30, "Set time before transaction expires, in seconds. Defaults to 30 seconds.")
RootCmd.PersistentFlags().IntP("delay-sec", "", 0, "Set time to wait before transaction is executed, in seconds. Defaults to 0 second.")
RootCmd.PersistentFlags().BoolP("sudo-wrap", "", false, "Wrap the transaction in a eosio.sudo exec. Useful to BPs, with --write-transaction and --skip-sign to then submit as a multisig proposition.")
RootCmd.PersistentFlags().StringP("write-transaction", "", "", "Do not broadcast the transaction produced, but write it in json to the given filename instead")
RootCmd.PersistentFlags().StringP("offline-head-block", "B", "", "Provide a recent block ID (long-form hex) for TaPoS. Use all --offline options to sign transactions offline")
RootCmd.PersistentFlags().StringP("offline-chain-id", "C", "", "Chain ID to sign transaction with. Use all --offline- options to sign transactions offline")
RootCmd.PersistentFlags().StringSliceP("offline-sign-key", "K", []string{}, "Public key to use to sign transaction. Must be in your vault or wallet")
RootCmd.PersistentFlags().BoolP("skip-sign", "", false, "Do not sign the transaction. Use with --write-transaction")
RootCmd.PersistentFlags().IntP("expiration", "", 30, "Set time before transaction expires, in seconds")
RootCmd.PersistentFlags().IntP("delay-sec", "", 0, "Set time to wait before transaction is executed, in seconds. Default 0")
RootCmd.PersistentFlags().BoolP("sudo-wrap", "", false, "Wrap the transaction in a eosio.sudo exec")
}

func initConfig() {
Expand Down