diff --git a/cmd/README.md b/cmd/README.md index d93fcd59..e19713a3 100644 --- a/cmd/README.md +++ b/cmd/README.md @@ -11,13 +11,14 @@ - [x] resources - [x] vote - [x] permission + - [ ] sign message - [ ] sr - [x] list - [ ] details - [x] create - [ ] update sr - - [ ] update brokerage + - [x] update brokerage [ ] proposal - [ ] create diff --git a/cmd/subcommands/sr.go b/cmd/subcommands/sr.go index 1b8e9007..0b835bca 100644 --- a/cmd/subcommands/sr.go +++ b/cmd/subcommands/sr.go @@ -3,6 +3,7 @@ package cmd import ( "encoding/json" "fmt" + "strconv" "github.com/fbsobreira/gotron-sdk/pkg/address" "github.com/fbsobreira/gotron-sdk/pkg/client/transaction" @@ -131,7 +132,64 @@ func srSub() []*cobra.Command { }, } - return []*cobra.Command{cmdList, cmdCreate} + cmdUpdateBrokerage := &cobra.Command{ + Use: "brokerage ", + Short: "up SR brokerage comission", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + if signerAddress.String() == "" { + return fmt.Errorf("no signer specified") + } + value, err := strconv.ParseInt(args[0], 10, 32) + if err != nil { + return err + } + if value < 0 || value > 100 { + return fmt.Errorf("Invalud Brokerage rande 0 > X < 100") + } + tx, err := conn.UpdateBrokerage(signerAddress.String(), int32(value)) + if err != nil { + return err + } + + var ctrlr *transaction.Controller + if useLedgerWallet { + account := keystore.Account{Address: signerAddress.GetAddress()} + ctrlr = transaction.NewController(conn, nil, &account, tx.Transaction, opts) + } else { + ks, acct, err := store.UnlockedKeystore(signerAddress.String(), passphrase) + if err != nil { + return err + } + ctrlr = transaction.NewController(conn, ks, acct, tx.Transaction, opts) + } + if err = ctrlr.ExecuteTransaction(); err != nil { + return err + } + + if noPrettyOutput { + fmt.Println(tx, ctrlr.Receipt, ctrlr.Result) + return nil + } + + result := make(map[string]interface{}) + result["from"] = signerAddress.String() + result["txID"] = common.BytesToHexString(tx.GetTxid()) + result["blockNumber"] = ctrlr.Receipt.BlockNumber + result["message"] = string(ctrlr.Result.Message) + result["receipt"] = map[string]interface{}{ + "fee": ctrlr.Receipt.Fee, + "netFee": ctrlr.Receipt.Receipt.NetFee, + "netUsage": ctrlr.Receipt.Receipt.NetUsage, + } + + asJSON, _ := json.Marshal(result) + fmt.Println(common.JSONPrettyFormat(string(asJSON))) + return nil + }, + } + + return []*cobra.Command{cmdList, cmdCreate, cmdUpdateBrokerage} } func init() { diff --git a/pkg/client/witnesses.go b/pkg/client/witnesses.go index f272ae34..9e975bc6 100644 --- a/pkg/client/witnesses.go +++ b/pkg/client/witnesses.go @@ -126,3 +126,30 @@ func (g *GrpcClient) GetWitnessBrokerage(witness string) (float64, error) { } return float64(result.Num), nil } + +// UpdateBrokerage change SR comission fees +func (g *GrpcClient) UpdateBrokerage(from string, comission int32) (*api.TransactionExtention, error) { + var err error + + contract := &core.UpdateBrokerageContract{ + Brokerage: comission, + } + if contract.OwnerAddress, err = common.DecodeCheck(from); err != nil { + return nil, err + } + + ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout) + defer cancel() + + tx, err := g.Client.UpdateBrokerage(ctx, contract) + if err != nil { + return nil, err + } + if proto.Size(tx) == 0 { + return nil, fmt.Errorf("bad transaction") + } + if tx.GetResult().GetCode() != 0 { + return nil, fmt.Errorf("%s", tx.GetResult().GetMessage()) + } + return tx, nil +}