Skip to content

Commit

Permalink
Update SR brokerage
Browse files Browse the repository at this point in the history
  • Loading branch information
fbsobreira committed Oct 30, 2020
1 parent 5dae825 commit 389aec8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 59 additions & 1 deletion cmd/subcommands/sr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -131,7 +132,64 @@ func srSub() []*cobra.Command {
},
}

return []*cobra.Command{cmdList, cmdCreate}
cmdUpdateBrokerage := &cobra.Command{
Use: "brokerage <new_comission>",
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() {
Expand Down
27 changes: 27 additions & 0 deletions pkg/client/witnesses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 389aec8

Please sign in to comment.