Skip to content

Commit

Permalink
get TX by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
fbsobreira committed May 20, 2020
1 parent f4c1467 commit 75a4c8c
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 4 deletions.
4 changes: 1 addition & 3 deletions cmd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@
- [ ] last
- [ ] byid
- [ ] byhash
- [ ] tx
- [ ] byid
- [ ] byhash
- [x] tx

- [x] keys
- [x] add
Expand Down
261 changes: 260 additions & 1 deletion cmd/subcommands/bc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import (
"fmt"
"time"

"github.com/fatih/structs"
"github.com/fbsobreira/gotron-sdk/pkg/address"
"github.com/fbsobreira/gotron-sdk/pkg/common"
"github.com/fbsobreira/gotron-sdk/pkg/proto/core"
"github.com/golang/protobuf/ptypes"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -59,7 +63,262 @@ func bcSub() []*cobra.Command {
},
}

return []*cobra.Command{cmdNode, cmdMT}
cmdTX := &cobra.Command{
Use: "tx <HASH>",
Short: "get tx info by hash",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
tx, err := conn.GetTransactionByID(args[0])
if err != nil {
return err
}
contracts := tx.GetRawData().GetContract()
if len(contracts) != 1 {
return fmt.Errorf("invalid contracts")
}
contract := contracts[0]

info, err := conn.GetTransactionInfoByID(args[0])
if err != nil {
return err
}

if noPrettyOutput {
fmt.Println(tx, info)
return nil
}

result := make(map[string]interface{})
t := time.Unix(info.GetBlockTimeStamp()/1000, 0)
result["txID"] = common.BytesToHexString(info.Id)
result["block"] = info.GetBlockNumber()
result["timestamp"] = info.GetBlockTimeStamp()
result["date"] = t.UTC().Format(time.RFC3339)

result["receipt"] = map[string]interface{}{
"fee": info.GetFee(),
"energyFee": info.GetReceipt().GetEnergyFee(),
"energyUsage": info.GetReceipt().GetEnergyUsage(),
"originEnergyUsage": info.GetReceipt().GetOriginEnergyUsage(),
"energyUsageTotal": info.GetReceipt().GetEnergyUsageTotal(),
"netFee": info.GetReceipt().GetNetFee(),
"netUsage": info.GetReceipt().GetNetUsage(),
}

result["contractName"] = contract.Type.String()
//parse contract
switch contract.Type {
case core.Transaction_Contract_AccountCreateContract:
var c core.AccountCreateContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_TransferContract:
var c core.TransferContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_TransferAssetContract:
var c core.TransferAssetContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_VoteWitnessContract:
var c core.VoteWitnessContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_WitnessCreateContract:
var c core.WitnessCreateContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_AssetIssueContract:
var c core.AssetIssueContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_ParticipateAssetIssueContract:
var c core.ParticipateAssetIssueContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_AccountUpdateContract:
var c core.AccountUpdateContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_FreezeBalanceContract:
var c core.FreezeBalanceContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_UnfreezeBalanceContract:
var c core.UnfreezeBalanceContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_WithdrawBalanceContract:
var c core.WithdrawBalanceContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_UnfreezeAssetContract:
var c core.UnfreezeAssetContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_UpdateAssetContract:
var c core.UpdateAssetContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)

case core.Transaction_Contract_ProposalCreateContract:
var c core.ProposalCreateContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_ProposalApproveContract:
var c core.ProposalApproveContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_ProposalDeleteContract:
var c core.ProposalDeleteContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_SetAccountIdContract:
var c core.SetAccountIdContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_CustomContract:
return fmt.Errorf("Tx inconsistent")
case core.Transaction_Contract_CreateSmartContract:
var c core.CreateSmartContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_TriggerSmartContract:
var c core.TriggerSmartContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_UpdateSettingContract:
var c core.UpdateSettingContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_ExchangeCreateContract:
var c core.ExchangeCreateContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_ExchangeInjectContract:
var c core.ExchangeInjectContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_ExchangeWithdrawContract:
var c core.ExchangeWithdrawContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_ExchangeTransactionContract:
var c core.ExchangeTransactionContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_UpdateEnergyLimitContract:
var c core.UpdateEnergyLimitContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_AccountPermissionUpdateContract:
var c core.AccountPermissionUpdateContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_ClearABIContract:
var c core.ClearABIContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_UpdateBrokerageContract:
var c core.UpdateBrokerageContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
case core.Transaction_Contract_ShieldedTransferContract:
var c core.ShieldedTransferContract
if err = ptypes.UnmarshalAny(contract.GetParameter(), &c); err != nil {
return fmt.Errorf("Tx inconsistent")
}
result["contract"] = structs.Map(c)
default:
return fmt.Errorf("Tx inconsistent")
}

c := result["contract"].(map[string]interface{})
delete(c, "XXX_NoUnkeyedLiteral")
delete(c, "XXX_sizecache")
delete(c, "XXX_unrecognized")
if v, ok := c["OwnerAddress"]; ok && len(v.([]uint8)) > 0 {
c["OwnerAddress"] = address.Address(v.([]uint8)).String()
}
if v, ok := c["ReceiverAddress"]; ok && len(v.([]uint8)) > 0 {
c["ReceiverAddress"] = address.Address(v.([]uint8)).String()
}
if v, ok := c["ToAddress"]; ok && len(v.([]uint8)) > 0 {
c["ToAddress"] = address.Address(v.([]uint8)).String()
}

if v, ok := c["Votes"]; ok {
votes := make(map[string]int64)
for _, d := range v.([]interface{}) {
dP := d.(map[string]interface{})
votes[address.Address(dP["VoteAddress"].([]uint8)).String()] = dP["VoteCount"].(int64)
}
c["Votes"] = votes
}

asJSON, _ := json.Marshal(result)
fmt.Println(common.JSONPrettyFormat(string(asJSON)))
return nil
},
}

return []*cobra.Command{cmdNode, cmdMT, cmdTX}
}

func init() {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/deckarep/golang-set v1.7.1
github.com/ethereum/go-ethereum v1.9.13
github.com/fatih/color v1.9.0
github.com/fatih/structs v1.1.0
github.com/gogo/protobuf v1.3.1
github.com/golang/protobuf v1.4.0
github.com/karalabe/hid v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=
Expand Down
10 changes: 10 additions & 0 deletions pkg/address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package address
import (
"bytes"
"crypto/ecdsa"
"encoding/base64"
"math/big"

"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -60,6 +61,15 @@ func Base58ToAddress(s string) (Address, error) {
return addr, nil
}

// Base64ToAddress returns Address with byte values of s.
func Base64ToAddress(s string) (Address, error) {
decoded, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return nil, err
}
return Address(decoded), nil
}

// String implements fmt.Stringer.
func (a Address) String() string {
if a[0] == 0 {
Expand Down

0 comments on commit 75a4c8c

Please sign in to comment.