Skip to content

Commit

Permalink
ledger test
Browse files Browse the repository at this point in the history
  • Loading branch information
CreatureDev committed Apr 11, 2023
1 parent c52dc5d commit 3565bda
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 16 deletions.
12 changes: 9 additions & 3 deletions model/client/clio/ledger_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
)

type LedgerRequest struct {
LedgerHash common.LedgerHash `json:"ledger_hash"`
LedgerIndex common.LedgerSpecifier `json:"ledger_index"`
LedgerHash common.LedgerHash `json:"ledger_hash,omitempty"`
LedgerIndex common.LedgerSpecifier `json:"ledger_index,omitempty"`
Full bool `json:"full"`
Accounts bool `json:"accounts"`
Transactions bool `json:"transactions"`
Expand bool `json:"expand"`
OwnerFunds bool `json:"owner_funds"`
Expand All @@ -25,6 +27,8 @@ func (r *LedgerRequest) UnmarshalJSON(data []byte) error {
type lrHelper struct {
LedgerHash common.LedgerHash `json:"ledger_hash"`
LedgerIndex json.RawMessage `json:"ledger_index"`
Full bool `json:"full"`
Accounts bool `json:"accounts"`
Transactions bool `json:"transactions"`
Expand bool `json:"expand"`
OwnerFunds bool `json:"owner_funds"`
Expand All @@ -39,14 +43,16 @@ func (r *LedgerRequest) UnmarshalJSON(data []byte) error {
}
*r = LedgerRequest{
LedgerHash: h.LedgerHash,
Accounts: h.Accounts,
Full: h.Full,
Transactions: h.Transactions,
Expand: h.Expand,
OwnerFunds: h.OwnerFunds,
Binary: h.Binary,
Queue: h.Queue,
Diff: h.Diff,
}
r.LedgerIndex, err = common.UnmarshalLedgerSpecifier(data)
r.LedgerIndex, err = common.UnmarshalLedgerSpecifier(h.LedgerIndex)
if err != nil {
return err
}
Expand Down
30 changes: 17 additions & 13 deletions model/client/clio/ledger_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
)

type LedgerResponse struct {
Ledger ClioLedger `json:"ledger"`
Ledger ClioLedger `json:"ledger"`
LedgerHash common.LedgerHash `json:"ledger_hash"`
LedgerIndex common.LedgerIndex `json:"ledger_index"`
Validated bool `json:"validated"`
}

type ClioLedger struct {
Expand All @@ -27,7 +30,7 @@ type ClioLedger struct {
ParentHash string `json:"parent_hash"`
TotalCoins types.XRPCurrencyAmount `json:"total_coins"`
TransactionHash string `json:"transaction_hash"`
Transactions []transactions.Tx `json:"transactions"`
Transactions []transactions.Tx `json:"transactions,omitempty"`
}

func (l *ClioLedger) UnmarshalJSON(data []byte) error {
Expand All @@ -53,17 +56,18 @@ func (l *ClioLedger) UnmarshalJSON(data []byte) error {
return err
}
*l = ClioLedger{
AccountHash: h.AccountHash,
CloseFlags: h.CloseFlags,
CloseTime: h.CloseTime,
CloseTimeHuman: h.CloseTimeHuman,
Closed: h.Closed,
LedgerHash: h.LedgerHash,
LedgerIndex: h.LedgerIndex,
ParentCloseTime: h.ParentCloseTime,
ParentHash: h.ParentHash,
TotalCoins: h.TotalCoins,
TransactionHash: h.TransactionHash,
AccountHash: h.AccountHash,
CloseFlags: h.CloseFlags,
CloseTime: h.CloseTime,
CloseTimeHuman: h.CloseTimeHuman,
CloseTimeResolution: h.CloseTimeResolution,
Closed: h.Closed,
LedgerHash: h.LedgerHash,
LedgerIndex: h.LedgerIndex,
ParentCloseTime: h.ParentCloseTime,
ParentHash: h.ParentHash,
TotalCoins: h.TotalCoins,
TransactionHash: h.TransactionHash,
}

for _, state := range h.AccountState {
Expand Down
75 changes: 75 additions & 0 deletions model/client/clio/ledger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package clio

import (
"testing"

"github.com/xyield/xrpl-go/model/client/common"
"github.com/xyield/xrpl-go/test"
)

func TestLedgerRequest(t *testing.T) {
s := LedgerRequest{
LedgerIndex: common.VALIDATED,
}
j := `{
"ledger_index": "validated",
"full": false,
"accounts": false,
"transactions": false,
"expand": false,
"owner_funds": false,
"binary": false,
"queue": false,
"diff": false
}`

if err := test.SerializeAndDeserialize(t, s, j); err != nil {
t.Error(err)
}
}

func TestLedgerResponse(t *testing.T) {
// TODO test AccountState
s := LedgerResponse{
Ledger: ClioLedger{
AccountHash: "10EFE192F59B3DE2A2BE5BCE2CA5DC83D066105696FCFC24C055359AAEBD6941",
CloseFlags: 0,
CloseTime: 711134782,
CloseTimeHuman: "2022-Jul-14 17:26:22.000000000 UTC",
CloseTimeResolution: 10,
Closed: true,
LedgerHash: "D3878EF6C92B84678AE2FBADC40961A161A128EA54AE59C2775CE076C2AE7A85",
LedgerIndex: "19977716",
ParentCloseTime: 711134781,
ParentHash: "D6DE54039FE5A22D86CD522F1A9B7794E487B74D9B6B8CBDE23F240F434B6749",
TotalCoins: 99987079398940307,
TransactionHash: "0000000000000000000000000000000000000000000000000000000000000000",
},
LedgerHash: "D3878EF6C92B84678AE2FBADC40961A161A128EA54AE59C2775CE076C2AE7A85",
LedgerIndex: 19977716,
Validated: true,
}
j := `{
"ledger": {
"account_hash": "10EFE192F59B3DE2A2BE5BCE2CA5DC83D066105696FCFC24C055359AAEBD6941",
"close_flags": 0,
"close_time": 711134782,
"close_time_human": "2022-Jul-14 17:26:22.000000000 UTC",
"close_time_resolution": 10,
"closed": true,
"ledger_hash": "D3878EF6C92B84678AE2FBADC40961A161A128EA54AE59C2775CE076C2AE7A85",
"ledger_index": "19977716",
"parent_close_time": 711134781,
"parent_hash": "D6DE54039FE5A22D86CD522F1A9B7794E487B74D9B6B8CBDE23F240F434B6749",
"total_coins": "99987079398940307",
"transaction_hash": "0000000000000000000000000000000000000000000000000000000000000000"
},
"ledger_hash": "D3878EF6C92B84678AE2FBADC40961A161A128EA54AE59C2775CE076C2AE7A85",
"ledger_index": 19977716,
"validated": true
}`

if err := test.SerializeAndDeserialize(t, s, j); err != nil {
t.Error(err)
}
}

0 comments on commit 3565bda

Please sign in to comment.