Skip to content

Commit

Permalink
improve message content
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbcdev committed Apr 13, 2024
1 parent e1bc6a9 commit a51170c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Lazy Rest API setting
```bash
export DEVD_COSMOS_REST='https://cosmos.example.com:1317'
```
_By setting this environment variable, you don't need to pass `--rest` flag everytime for non-localhost Rest API
_By setting this environment variable, you don't need to pass `--rest` flag everytime for non-localhost Rest API_

#### Query account balance

Expand Down Expand Up @@ -194,7 +194,7 @@ _Assumption: no access list, not contract creation, Homestead, EIP-2028 (Istanbu

### Notes:

- Output messages are printed via stdout, while messages with prefixes `WARN:` and `ERR:` are printed via stderr. So for integration with other tools, to omit stderr, forward stdout only.
- Output messages are printed via stdout, while messages with prefixes `INF:` `WARN:` and `ERR:` are printed via stderr. So for integration with other tools, to omit stderr, forward stdout only.
> Eg: `devd c a cosmos1... 1> /tmp/output.txt`
- When passing arguments into command via both argument and pipe, the argument will be used.
> Eg: `echo 123 | devd c d2h 456` will convert `456` to hexadecimal, not `123`.
18 changes: 13 additions & 5 deletions cmd/query/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func GetQueryBalanceCommand() *cobra.Command {
display, high, low, err := utils.ConvertNumberIntoDisplayWithExponent(nativeBalance, 18)
utils.ExitOnErr(err, "failed to convert number into display with exponent")

printRow("Native", "-", "(native)", display, nativeBalance.String(), "18", high.String(), low.String(), "")
printRow("native", "-", "(native)", display, nativeBalance.String(), "18", high.String(), low.String(), "")

for i := 1; i < len(evmAddrs); i++ {
contractAddr := evmAddrs[i]
Expand All @@ -63,9 +63,13 @@ func GetQueryBalanceCommand() *cobra.Command {
}

if fetchErc20ModuleAndVfbc && restApiEndpoint != "" {
erc20TokenPairs, err := fetchErc20ModuleTokenPairsFromRest(restApiEndpoint)
erc20TokenPairs, statusCode, err := fetchErc20ModuleTokenPairsFromRest(restApiEndpoint)
if err != nil {
utils.PrintlnStdErr("ERR:", err)
if statusCode == 501 {
utils.PrintlnStdErr("WARN: `x/erc20` module is not available on the chain")
} else {
utils.PrintlnStdErr("ERR:", err)
}
} else {
for _, erc20TokenPair := range erc20TokenPairs {
if !erc20TokenPair.Enabled {
Expand All @@ -87,9 +91,13 @@ func GetQueryBalanceCommand() *cobra.Command {
}
}

vfcbPairs, err := fetchVirtualFrontierBankContractPairsFromRest(restApiEndpoint)
vfcbPairs, statusCode, err := fetchVirtualFrontierBankContractPairsFromRest(restApiEndpoint)
if err != nil {
utils.PrintlnStdErr("ERR:", err)
if statusCode == 501 {
utils.PrintlnStdErr("WARN: virtual frontier contract feature is not available on the chain")
} else {
utils.PrintlnStdErr("ERR:", err)
}
} else {
for _, vfbcPair := range vfcbPairs {
if !vfbcPair.Enabled {
Expand Down
28 changes: 19 additions & 9 deletions cmd/query/cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func mustGetRest(cmd *cobra.Command) (rest string) {

rest = strings.TrimSuffix(rest, "/")

fmt.Println("Connecting to", rest, fmt.Sprintf("(from %s)", inputSource))
utils.PrintlnStdErr("INF: Connecting to Cosmos Rest-API", rest, fmt.Sprintf("(from %s)", inputSource))

// pre-flight check to ensure the connection is working
_, err := http.Get(rest)
Expand All @@ -42,16 +42,19 @@ func mustGetRest(cmd *cobra.Command) (rest string) {
return
}

func fetchErc20ModuleTokenPairsFromRest(rest string) (erc20ModuleTokenPairs []Erc20ModuleTokenPair, err error) {
func fetchErc20ModuleTokenPairsFromRest(rest string) (erc20ModuleTokenPairs []Erc20ModuleTokenPair, statusCode int, err error) {
var resp *http.Response
resp, err = http.Get(rest + "/evmos/erc20/v1/token_pairs")
if err != nil {
err = errors.Wrap(err, "failed to fetch ERC-20 module token pairs")
return
}

statusCode = resp.StatusCode

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch ERC-20 module token pairs! Status code: %d", resp.StatusCode)
err = fmt.Errorf("failed to fetch ERC-20 module token pairs! Status code: %d", resp.StatusCode)
return
}

type responseStruct struct {
Expand All @@ -64,13 +67,15 @@ func fetchErc20ModuleTokenPairsFromRest(rest string) (erc20ModuleTokenPairs []Er

bz, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body of ERC-20 module token pairs")
err = errors.Wrap(err, "failed to read response body of ERC-20 module token pairs")
return
}

var response responseStruct
err = json.Unmarshal(bz, &response)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal response body of ERC-20 module token pairs")
err = errors.Wrap(err, "failed to unmarshal response body of ERC-20 module token pairs")
return
}

erc20ModuleTokenPairs = response.TokenPairs
Expand All @@ -83,16 +88,19 @@ type Erc20ModuleTokenPair struct {
Enabled bool `json:"enabled"`
}

func fetchVirtualFrontierBankContractPairsFromRest(rest string) (vfbcPairs []VfbcTokenPair, err error) {
func fetchVirtualFrontierBankContractPairsFromRest(rest string) (vfbcPairs []VfbcTokenPair, statusCode int, err error) {
var resp *http.Response
resp, err = http.Get(rest + "/ethermint/evm/v1/virtual_frontier_bank_contracts")
if err != nil {
err = errors.Wrap(err, "failed to fetch VFBC pairs")
return
}

statusCode = resp.StatusCode

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch VFBC pairs! Status code: %d", resp.StatusCode)
err = fmt.Errorf("failed to fetch VFBC pairs! Status code: %d", resp.StatusCode)
return
}

type responseStruct struct {
Expand All @@ -105,13 +113,15 @@ func fetchVirtualFrontierBankContractPairsFromRest(rest string) (vfbcPairs []Vfb

bz, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body of VFBC pairs")
err = errors.Wrap(err, "failed to read response body of VFBC pairs")
return
}

var response responseStruct
err = json.Unmarshal(bz, &response)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal response body of VFBC pairs")
err = errors.Wrap(err, "failed to unmarshal response body of VFBC pairs")
return
}

vfbcPairs = response.Pairs
Expand Down
2 changes: 1 addition & 1 deletion cmd/query/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func mustGetEthClient(cmd *cobra.Command, fallbackDeprecatedFlagHost bool) (ethC
inputSource = "default"
}

fmt.Println("Connecting to", rpc, fmt.Sprintf("(from %s)", inputSource))
utils.PrintlnStdErr("INF: Connecting to EVM Json-RPC", rpc, fmt.Sprintf("(from %s)", inputSource))

ethClient8545, err = ethclient.Dial(rpc)
utils.ExitOnErr(err, "failed to connect to EVM Json-RPC")
Expand Down
2 changes: 1 addition & 1 deletion constants/varcons.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package constants

//goland:noinspection GoSnakeCaseUsage
var (
VERSION = "2.1.1"
VERSION = "2.1.2"
)

0 comments on commit a51170c

Please sign in to comment.