Skip to content

Commit

Permalink
sample test to test offer updates
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikiyer56 committed Dec 7, 2024
1 parent 6a46ec2 commit fd69ebc
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
77 changes: 77 additions & 0 deletions services/horizon/internal/integration/change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration

import (
"context"
"encoding/json"
"github.com/stellar/go/ingest"
"github.com/stellar/go/ingest/ledgerbackend"
"github.com/stellar/go/keypair"
Expand Down Expand Up @@ -123,6 +124,82 @@ func TestOneTxOneOperationChanges(t *testing.T) {
tt.True(accountFromEntry(destAccChange.Pre).Balance < accountFromEntry(destAccChange.Post).Balance)
}

func TestSomething(t *testing.T) {
//tt := assert.New(t)
itest := integration.NewTest(t, integration.Config{})
master := itest.Master()
keys, accounts := itest.CreateAccounts(3, "1000")
keyA, keyB := keys[0], keys[1]
accountA, accountB := accounts[0], accounts[1]

// Some random asset
xyzAsset := txnbuild.CreditAsset{Code: "XYZ", Issuer: itest.Master().Address()}
itest.MustEstablishTrustline(keyA, accountA, xyzAsset)

itest.MustEstablishTrustline(keyB, accountB, xyzAsset)

t.Logf("*****")
paymentOperation := txnbuild.Payment{
Destination: keyA.Address(),
Asset: xyzAsset,
Amount: "2000",
}
txResp := itest.MustSubmitOperations(itest.MasterAccount(), itest.Master(), &paymentOperation)
data, _ := json.MarshalIndent(txResp, "", " ")

t.Logf("Acc A: %v, Acc B: %v", keyA.Address(), keyB.Address())

sellOfferOperationFromA := txnbuild.ManageSellOffer{
Selling: xyzAsset,
Buying: txnbuild.NativeAsset{},
Amount: "50",
Price: xdr.Price{N: 1, D: 1},
SourceAccount: keyA.Address(),
}
txResp = itest.MustSubmitMultiSigOperations(itest.MasterAccount(), []*keypair.Full{master, keyA}, &sellOfferOperationFromA)
data, _ = json.MarshalIndent(txResp, "", " ")
t.Logf("Transaction Sell Offer: %v", string(data))
t.Logf("Tx response meta xdr Sell Offer: %v", txResp.ResultMetaXdr)
t.Logf("*****")

sellOfferLedgerSeq := uint32(txResp.Ledger)

buyOfferOperationFromB := txnbuild.ManageBuyOffer{
Buying: xyzAsset,
Selling: txnbuild.NativeAsset{},
Amount: "66",
Price: xdr.Price{N: 1, D: 1},
SourceAccount: keyB.Address(),
}

txResp = itest.MustSubmitMultiSigOperations(itest.MasterAccount(), []*keypair.Full{master, keyB}, &buyOfferOperationFromB)
data, _ = json.MarshalIndent(txResp, "", " ")
t.Logf("Transaction Buy Offer: %v", string(data))
t.Logf("Tx response meta xdr Buy Offer: %v", txResp.ResultMetaXdr)
t.Logf("*****")

buyOfferLedgerSeq := uint32(txResp.Ledger)

t.Logf("Sell Offer Ledger:%v, Buy Offer Ledger: %v", sellOfferLedgerSeq, buyOfferLedgerSeq)

waitForLedgerInArchive(t, 15*time.Second, buyOfferLedgerSeq)

ledgerMap := getLedgers(itest, sellOfferLedgerSeq, buyOfferLedgerSeq)
for ledgerSeq, ledger := range ledgerMap {
t.Logf("LedgerSeq:::::::::::::::::::::: %v", ledgerSeq)
changes := getChangesFromLedger(itest, ledger)
for _, change := range changes {
if change.Reason != ingest.LedgerEntryChangeReasonOperation {
continue
}
typ := change.Type.String()
pre, _ := change.Pre.MarshalBinaryBase64()
post, _ := change.Post.MarshalBinaryBase64()
t.Logf("ledger: %v, Change - type - %v, pre: %v, post: %v", ledgerSeq, typ, pre, post)
}
}
}

func getChangesFromLedger(itest *integration.Test, ledger xdr.LedgerCloseMeta) []ingest.Change {
t := itest.CurrentTest()
changeReader, err := ingest.NewLedgerChangeReaderFromLedgerCloseMeta(itest.GetPassPhrase(), ledger)
Expand Down
19 changes: 18 additions & 1 deletion xdr/ledger_entry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package xdr

import "fmt"
import (
"encoding/base64"
"fmt"
)

// LedgerKey implements the `Keyer` interface
func (entry *LedgerEntry) LedgerKey() (LedgerKey, error) {
Expand Down Expand Up @@ -183,3 +186,17 @@ func (data *LedgerEntryData) LedgerKey() (LedgerKey, error) {

return key, nil
}

// MarshalBinaryBase64 marshals XDR into a binary form and then encodes it
// using base64.
func (e *LedgerEntry) MarshalBinaryBase64() (string, error) {
if e == nil {
return "nil", nil
}
b, err := e.MarshalBinary()
if err != nil {
return "", err
}

return base64.StdEncoding.EncodeToString(b), nil
}
14 changes: 14 additions & 0 deletions xdr/transaction_envelope.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package xdr

import "encoding/base64"

// IsFeeBump returns true if the transaction envelope is a fee bump transaction
func (e TransactionEnvelope) IsFeeBump() bool {
return e.Type == EnvelopeTypeEnvelopeTypeTxFeeBump
Expand Down Expand Up @@ -242,3 +244,15 @@ func (e TransactionEnvelope) Memo() Memo {
panic("unsupported transaction type: " + e.Type.String())
}
}

func (e *TransactionEnvelope) MarshalBinaryBase64() (string, error) {
if e == nil {
return "nil", nil
}
b, err := e.MarshalBinary()
if err != nil {
return "", err
}

return base64.StdEncoding.EncodeToString(b), nil
}

0 comments on commit fd69ebc

Please sign in to comment.