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

https://github.com/fbsobreira/gotron-sdk/issues/103 #107

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
28 changes: 20 additions & 8 deletions cmd/subcommands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -117,13 +117,19 @@ func initConfig() {

// LoadConfig loads config file in yaml format
func LoadConfig() (*Config, error) {
in, err := ioutil.ReadFile(DefaultConfigFile)
readConfig := &Config{}
if err == nil {
if err := yaml.Unmarshal(in, readConfig); err != nil {
return readConfig, err
}
file, err := os.Open(DefaultConfigFile)
if err != nil {
return readConfig, err
}

in, err := io.ReadAll(file)
if err != nil {
return readConfig, err
}

err = yaml.Unmarshal(in, readConfig)

return readConfig, err
}

Expand All @@ -133,8 +139,14 @@ func SaveConfig(conf *Config) error {
if err != nil {
return err
}
if err := ioutil.WriteFile(DefaultConfigFile, out, 0600); err != nil {
panic(fmt.Sprintf("Failed to write to config file %s.", DefaultConfigFile))
file, err := os.OpenFile(DefaultConfigFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
panic(fmt.Sprintf("Failed to open config file %s for writing: %v", DefaultConfigFile, err))
}
defer file.Close()

if _, err := io.WriteString(file, string(out)); err != nil {
panic(fmt.Sprintf("Failed to write to config file %s: %v", DefaultConfigFile, err))
}
return nil
}
21 changes: 17 additions & 4 deletions cmd/subcommands/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"math"
"os"

"github.com/fbsobreira/gotron-sdk/pkg/address"
"github.com/fbsobreira/gotron-sdk/pkg/client/transaction"
Expand Down Expand Up @@ -39,7 +40,13 @@ func contractSub() []*cobra.Command {

if abiSTR == "" {
if abiFile != "" {
abiBytes, err := ioutil.ReadFile(abiFile)
file, err := os.Open(abiFile)
if err != nil {
return fmt.Errorf("cannot open ABI file: %s %v", abiFile, err)
}
defer file.Close()

abiBytes, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("cannot read ABI file: %s %v", abiFile, err)
}
Expand All @@ -55,7 +62,13 @@ func contractSub() []*cobra.Command {

if bcSTR == "" {
if bcFile != "" {
bcBytes, err := ioutil.ReadFile(bcFile)
file, err := os.Open(bcFile)
if err != nil {
return fmt.Errorf("cannot open Bytecode file: %s %v", bcFile, err)
}
defer file.Close()

bcBytes, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("cannot read Bytecode file: %s %v", bcFile, err)
}
Expand Down Expand Up @@ -165,7 +178,7 @@ func contractSub() []*cobra.Command {

result := make(map[string]interface{})
//TODO: parse based on contract ABI
result["Result"] = common.ToHex(cResult[0])
result["Result"] = common.BytesToHexString(cResult[0])

asJSON, _ := json.Marshal(result)
fmt.Println(common.JSONPrettyFormat(string(asJSON)))
Expand Down
4 changes: 2 additions & 2 deletions cmd/subcommands/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,15 @@ func exchangeSub() []*cobra.Command {
}
ratio = (float64(e.SecondTokenBalance) + tokenValue1) / float64(e.FirstTokenBalance)
default:
return fmt.Errorf("Token ID provided does not match excahnge %s/%s", T1, T2)
return fmt.Errorf("token ID provided does not match excahnge %s/%s", T1, T2)
}
if expectedAmount != 0 {
expectedAmount = expectedAmount * math.Pow10(tokenDecimal)
} else {
expectedAmount = math.Floor(tokenValue1/ratio + 0.5)
}
} else {
return fmt.Errorf("Cannot fetch echange info: %+v", err)
return fmt.Errorf("cannot fetch exchange info: %+v", err)
}

tx, err := conn.ExchangeTrade(
Expand Down
8 changes: 4 additions & 4 deletions cmd/subcommands/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/fbsobreira/gotron-sdk/pkg/account"
"github.com/fbsobreira/gotron-sdk/pkg/address"
"github.com/fbsobreira/gotron-sdk/pkg/common"
c "github.com/fbsobreira/gotron-sdk/pkg/common"
"golang.org/x/crypto/ssh/terminal"

"golang.org/x/term"

"github.com/fbsobreira/gotron-sdk/pkg/ledger"
"github.com/fbsobreira/gotron-sdk/pkg/mnemonic"
Expand All @@ -31,7 +31,7 @@ var (
recoverFromMnemonic bool
passphrase string
ppPrompt = fmt.Sprintf(
"prompt for passphrase, otherwise use default passphrase: \"`%s`\"", c.DefaultPassphrase,
"prompt for passphrase, otherwise use default passphrase: \"`%s`\"", common.DefaultPassphrase,
)
)

Expand Down Expand Up @@ -250,7 +250,7 @@ func keysSub() []*cobra.Command {
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Enter privete key hex format:")
data, err := terminal.ReadPassword(int(os.Stdin.Fd()))
data, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return err
}
Expand Down
46 changes: 29 additions & 17 deletions cmd/subcommands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path"
"regexp"
"strings"
"time"

color "github.com/fatih/color"
"github.com/fbsobreira/gotron-sdk/pkg/client"
"github.com/fbsobreira/gotron-sdk/pkg/client/transaction"
"github.com/fbsobreira/gotron-sdk/pkg/common"
c "github.com/fbsobreira/gotron-sdk/pkg/common"
"github.com/fbsobreira/gotron-sdk/pkg/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -39,12 +37,12 @@ var (
passphraseFilePath string
defaultKeystoreDir string
node string
keyStoreDir string
givenFilePath string
timeout uint32
withTLS bool
apiKey string
conn *client.GrpcClient
// keyStoreDir string
givenFilePath string
timeout uint32
withTLS bool
apiKey string
conn *client.GrpcClient
// RootCmd is single entry point of the CLI
RootCmd = &cobra.Command{
Use: "tronctl",
Expand Down Expand Up @@ -152,7 +150,7 @@ var (
VersionWrapDump = ""
versionLink = "https://api.github.com/repos/fbsobreira/gotron-sdk/releases/latest"
versionTagLink = "https://api.github.com/repos/fbsobreira/gotron-sdk/git/ref/tags/"
versionFormat = regexp.MustCompile("v[0-9]+-[a-z0-9]{7}")
// versionFormat = regexp.MustCompile("v[0-9]+-[a-z0-9]{7}")
)

// GitHubReleaseAssets json struct
Expand Down Expand Up @@ -188,7 +186,9 @@ func getGitVersion() (string, error) {
return "", err
}

defer resp.Body.Close()
if resp != nil {
defer resp.Body.Close()
}
// if error, no op
if resp != nil && resp.StatusCode == 200 {
buf := new(bytes.Buffer)
Expand All @@ -213,7 +213,7 @@ func getGitVersion() (string, error) {

if releaseTag.DATA.SHA[:8] != commit[1] {
warnMsg := fmt.Sprintf("Warning: Using outdated version. Redownload to upgrade to %s\n", release.TagName)
fmt.Fprintf(os.Stderr, color.RedString(warnMsg))
fmt.Fprintf(os.Stderr, "%s", color.RedString(warnMsg))
return release.TagName, fmt.Errorf(warnMsg)
}
return release.TagName, nil
Expand Down Expand Up @@ -251,7 +251,7 @@ func findAddress(value string) (tronAddress, error) {
if acc, err := store.AddressFromAccountName(value); err == nil {
return tronAddress{acc}, nil
}
return address, fmt.Errorf("Invalid address/Invalid account name: %s", value)
return address, fmt.Errorf("invalid address/invalid account name: %s", value)
}
return address, nil
}
Expand All @@ -278,7 +278,13 @@ func getPassphrase() (string, error) {
if _, err := os.Stat(passphraseFilePath); os.IsNotExist(err) {
return "", fmt.Errorf("passphrase file not found at `%s`", passphraseFilePath)
}
dat, err := ioutil.ReadFile(passphraseFilePath)
file, err := os.Open(passphraseFilePath)
if err != nil {
return "", err
}
defer file.Close()

dat, err := io.ReadAll(file)
if err != nil {
return "", err
}
Expand All @@ -292,7 +298,7 @@ func getPassphrase() (string, error) {
}
return string(pass), nil
} else {
return c.DefaultPassphrase, nil
return common.DefaultPassphrase, nil
}
}

Expand All @@ -304,7 +310,13 @@ func getPassphraseWithConfirm() (string, error) {
if _, err := os.Stat(passphraseFilePath); os.IsNotExist(err) {
return "", fmt.Errorf("passphrase file not found at `%s`", passphraseFilePath)
}
dat, err := ioutil.ReadFile(passphraseFilePath)
file, err := os.Open(passphraseFilePath)
if err != nil {
return "", err
}
defer file.Close()

dat, err := io.ReadAll(file)
if err != nil {
return "", err
}
Expand All @@ -327,6 +339,6 @@ func getPassphraseWithConfirm() (string, error) {
fmt.Println("") // provide feedback when passphrase is entered.
return string(repeatPass), nil
} else {
return c.DefaultPassphrase, nil
return common.DefaultPassphrase, nil
}
}
2 changes: 1 addition & 1 deletion cmd/subcommands/sr.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func srSub() []*cobra.Command {
return err
}
if value < 0 || value > 100 {
return fmt.Errorf("Invalud Brokerage rande 0 > X < 100")
return fmt.Errorf("invalid brokerage range 0 > X < 100")
}
tx, err := conn.UpdateBrokerage(signerAddress.String(), int32(value))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion e2e/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (

func TestKeyStore(t *testing.T) {
fmt.Println("Hello world")
t.Errorf("Testing pipeline")
// t.Errorf("Testing pipeline")
}
23 changes: 13 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@ module github.com/fbsobreira/gotron-sdk
go 1.19

require (
github.com/araddon/dateparse v0.0.0-20200409225146-d820a6159ab1
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/btcsuite/btcd/btcec/v2 v2.2.0
github.com/deckarep/golang-set v1.8.0
github.com/ethereum/go-ethereum v1.12.2
github.com/fatih/color v1.9.0
github.com/fatih/color v1.13.0
github.com/fatih/structs v1.1.0
github.com/mitchellh/go-homedir v1.1.0
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.9.1
github.com/rjeczalik/notify v0.9.3
github.com/shengdoushi/base58 v1.0.0
github.com/spf13/cobra v1.0.0
github.com/spf13/cobra v1.3.0
github.com/stretchr/testify v1.8.1
github.com/tyler-smith/go-bip39 v1.1.0
github.com/zondax/hid v0.9.1
go.uber.org/zap v1.15.0
go.uber.org/zap v1.21.0
golang.org/x/crypto v0.9.0
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987
google.golang.org/grpc v1.37.0
golang.org/x/term v0.8.0
google.golang.org/genproto v0.0.0-20220222213610-43724f9ea8cf
google.golang.org/grpc v1.44.0
google.golang.org/protobuf v1.28.1
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
Expand All @@ -39,12 +41,13 @@ require (
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.3 // indirect
go.uber.org/atomic v1.6.0 // indirect
go.uber.org/multierr v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/fbsobreira/gotron-sdk v0.0.0-20230907131216-1e824406fe8c => github.com/sunbankio/gotron-sdk v0.0.0-20231003155243-a269b0d040c3
Loading