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

[WIP] Add tx list request and response #5

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ func (c Client) GetAccountState(accountAddr string) (AccountState, error) {
return FromAccountStateBlob(accStateBlob)
}

// GetTransactionList
func (c Client) GetTransactionList(limit int) (TransactionList, error) {
// Types that are valid to be assigned to RequestedItems:
// *RequestItem_GetAccountStateRequest
// *RequestItem_GetAccountTransactionBySequenceNumberRequest
// *RequestItem_GetEventsByEventAccessPathRequest
// *RequestItem_GetTransactionsRequest
requestedItems := []*types.RequestItem{
&types.RequestItem{
RequestedItems: &types.RequestItem_GetTransactionsRequest{
GetTransactionsRequest: &types.GetTransactionsRequest{
StartVersion: 1,
Limit: uint64(limit),
FetchEvents: true,
},
},
},
}
knownVersion := uint64(0) // TODO: Does this make a difference for accounts? Or only for events? Might need to be a method parameter.
updateLedgerRequest := types.UpdateToLatestLedgerRequest{
ClientKnownVersion: knownVersion,
RequestedItems: requestedItems,
}
updateLedgerResponse, err := c.acc.UpdateToLatestLedger(context.Background(), &updateLedgerRequest)
if err != nil {
return TransactionList{}, err
}

txList := updateLedgerResponse.GetResponseItems()[0].GetGetTransactionsResponse().GetTxnListWithProof().GetTransactions()

return FromTransactionList(txList)
}

// SendTx sends a transaction to the connected validator node.
func (c Client) SendTx(tx Transaction) error {
txRequest := admission_control.SubmitTransactionRequest{
Expand Down
9 changes: 8 additions & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
}
defer c.Close()

acc := "8cd377191fe0ef113455c8e8d769f0c0147d5bb618bf195c0af31a05fbfd0969"
acc := "82258967750773ea090d125f9aeb9b9e42f6c7ff6df0742336992bfc52b4096a"
accState, err := c.GetAccountState(acc)
if err != nil {
panic(err)
Expand All @@ -23,4 +23,11 @@ func main() {
fmt.Printf("Raw account state: 0x%x\n", accState.Blob)
fmt.Println()
fmt.Printf("Account resource: %v\n", accState.AccountResource)

txlist, err := c.GetTransactionList(1)
if err != nil {
panic(err)
}
fmt.Println()
fmt.Printf("Transaction list resource: %v\n", txlist.Transactions)
}
60 changes: 60 additions & 0 deletions tx.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@
package libra

import (
"bytes"
"encoding/binary"
"fmt"

"github.com/philippgille/libra-sdk-go/rpc/types"
)

// Transaction is a transaction of Libra Coins.
type Transaction struct {
RawBytes []byte
SenderPubKey []byte
SenderSig []byte
}

// TransactionList
type TransactionList struct {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An extra type for a transaction list that only contains a slice of transactions is not necessary I think.

Transactions []Transaction
}

// FromTransactionListResourceBlob
func FromTransactionListResourceBlob(transactionListResourceBlob []byte) (TransactionListResource, error) {
result := TransactionListResource{}

r := bytes.NewReader(transactionListResourceBlob)

var balance uint64
err := binary.Read(r, binary.LittleEndian, &balance)
if err != nil {
return result, err
}
result.Balance = balance

return result, nil
}

// TransactionListResource
type TransactionListResource struct {
AuthKey []byte
Balance uint64
ReceivedEvents uint64
SentEvents uint64
SequenceNo uint64
}

// String formats the account state similarly to the Libra CLI.
// Numbers are formatted as string because the numbers are uint64,
// whose max value exceeds JSON's "save integer",
// which can lead to parsing errors.
func (tx Transaction) String() string {
return fmt.Sprintf("{\"raw_bytes\": \"0x%x\", \"sender_pub_key\": \"0x%x\", \"sender_sig\": \"0x%x\"}",
tx.RawBytes, tx.SenderPubKey, tx.SenderSig)
}

// FromAccountStateBlob converts an account state blob into an object of the AccountState struct.
func FromTransactionList(transactionList []*types.SignedTransaction) (TransactionList, error) {
results := TransactionList{}
for _, x := range transactionList {
result := Transaction{}
result.RawBytes = x.GetRawTxnBytes()
result.SenderPubKey = x.GetSenderPublicKey()
result.SenderSig = x.GetSenderSignature()
results.Transactions = append(results.Transactions, result)
}
return results, nil
}