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

[services][feat] #81: Add test for ingesting path payments #84

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
131 changes: 131 additions & 0 deletions internal/services/ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"
"github.com/stellar/wallet-backend/internal/apptracker"
Expand Down Expand Up @@ -177,6 +178,9 @@ func TestIngestPayments(t *testing.T) {
ingestService, _ := NewIngestService(models, "ingestionLedger", &mockAppTracker, &mockRPCService, &mockRouter, tssStore)
srcAccount := keypair.MustRandom().Address()
destAccount := keypair.MustRandom().Address()
usdIssuer := keypair.MustRandom().Address()
eurIssuer := keypair.MustRandom().Address()

t.Run("test_op_payment", func(t *testing.T) {
_ = models.Account.Insert(context.Background(), srcAccount)
paymentOp := txnbuild.Payment{
Expand Down Expand Up @@ -214,4 +218,131 @@ func TestIngestPayments(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, payments[0].TransactionHash, "abcd")
})

t.Run("test_op_path_payment_send", func(t *testing.T) {
err := models.Account.Insert(context.Background(), srcAccount)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpicking here but I don't really think you require this line. This is not a test to see if models are working. I'd replace it with _ := models.Account.Insert(context.Background(), srcAccount)


path := []txnbuild.Asset{
txnbuild.CreditAsset{Code: "USD", Issuer: usdIssuer},
txnbuild.CreditAsset{Code: "EUR", Issuer: eurIssuer},
}

pathPaymentOp := txnbuild.PathPaymentStrictSend{
SourceAccount: srcAccount,
Destination: destAccount,
DestMin: "9",
SendAmount: "10",
SendAsset: txnbuild.NativeAsset{},
DestAsset: txnbuild.NativeAsset{},
Path: path,
}
transaction, err := txnbuild.NewTransaction(txnbuild.TransactionParams{
SourceAccount: &txnbuild.SimpleAccount{
AccountID: keypair.MustRandom().Address(),
},
Operations: []txnbuild.Operation{&pathPaymentOp},
Preconditions: txnbuild.Preconditions{TimeBounds: txnbuild.NewTimeout(10)},
})
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above. This can be replaced with:

	transaction, _ := txnbuild.NewTransaction(txnbuild.TransactionParams{
		SourceAccount: &txnbuild.SimpleAccount{
			AccountID: keypair.MustRandom().Address(),
		},
		Operations:    []txnbuild.Operation{&pathPaymentOp},
		Preconditions: txnbuild.Preconditions{TimeBounds: txnbuild.NewTimeout(10)},
	})


signer := keypair.MustRandom()
errSigner := models.Account.Insert(context.Background(), signer.Address())
require.NoError(t, errSigner)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above


signedTx, err := transaction.Sign(network.TestNetworkPassphrase, signer)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above


txEnvXDR, err := signedTx.Base64()
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above


ledgerTransaction := entities.Transaction{
Status: entities.SuccessStatus,
Hash: "efgh",
ApplicationOrder: 1,
FeeBump: false,
EnvelopeXDR: txEnvXDR,
ResultXDR: "AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAANAAAAAAAAAAAAAAAAXF0V022EgeGIo6QNVbMVvHdxvvl2MfZcVZUJpfph+0QAAAAAAAAAAAX14QAAAAAA",
Ledger: 1,
}

ledgerTransactions := []entities.Transaction{ledgerTransaction}

err = ingestService.ingestPayments(context.Background(), ledgerTransactions)
require.NoError(t, err)

payments, _, _, err := models.Payments.GetPaymentsPaginated(context.Background(), srcAccount, "", "", data.ASC, 1)
require.NoError(t, err)
require.NotEmpty(t, payments, "Expected at least one payment")
assert.Equal(t, payments[0].TransactionHash, ledgerTransaction.Hash)
assert.Equal(t, payments[0].SrcAmount, int64(100000000))
assert.Equal(t, payments[0].SrcAssetType, xdr.AssetTypeAssetTypeNative.String())
assert.Equal(t, payments[0].ToAddress, destAccount)
assert.Equal(t, payments[0].FromAddress, srcAccount)
assert.Equal(t, payments[0].SrcAssetCode, "XLM")
assert.Equal(t, payments[0].DestAssetCode, "XLM")
})

t.Run("test_op_path_payment_receive", func(t *testing.T) {
err := models.Account.Insert(context.Background(), srcAccount)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above


path := []txnbuild.Asset{
txnbuild.CreditAsset{Code: "USD", Issuer: usdIssuer},
txnbuild.CreditAsset{Code: "EUR", Issuer: eurIssuer},
}

pathPaymentOp := txnbuild.PathPaymentStrictReceive{
SourceAccount: srcAccount,
Destination: destAccount,
SendMax: "11",
DestAmount: "10",
SendAsset: txnbuild.NativeAsset{},
DestAsset: txnbuild.NativeAsset{},
Path: path,
}
transaction, err := txnbuild.NewTransaction(txnbuild.TransactionParams{
SourceAccount: &txnbuild.SimpleAccount{
AccountID: keypair.MustRandom().Address(),
},
Operations: []txnbuild.Operation{&pathPaymentOp},
Preconditions: txnbuild.Preconditions{TimeBounds: txnbuild.NewTimeout(10)},
})
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above


signer := keypair.MustRandom()
errSigner := models.Account.Insert(context.Background(), signer.Address())
require.NoError(t, errSigner)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above


signedTx, err := transaction.Sign(network.TestNetworkPassphrase, signer)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above


txEnvXDR, err := signedTx.Base64()
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above


ledgerTransaction := entities.Transaction{
Status: entities.SuccessStatus,
Hash: "efgh",
ApplicationOrder: 1,
FeeBump: false,
EnvelopeXDR: txEnvXDR,
ResultXDR: "AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAACAAAAAAAAAAAAAAAAjOiEfRh4kaFVQDu/CSTZLMtnyg0DbNowZ/G2nLES3KwAAAAAAAAAAAVdSoAAAAAA",
Ledger: 1,
}

ledgerTransactions := []entities.Transaction{ledgerTransaction}

err = ingestService.ingestPayments(context.Background(), ledgerTransactions)
require.NoError(t, err)

payments, _, _, err := models.Payments.GetPaymentsPaginated(context.Background(), srcAccount, "", "", data.ASC, 1)
require.NoError(t, err)
require.NotEmpty(t, payments, "Expected at least one payment")
assert.Equal(t, payments[0].TransactionHash, ledgerTransaction.Hash)
assert.Equal(t, payments[0].SrcAssetType, xdr.AssetTypeAssetTypeNative.String())
assert.Equal(t, payments[0].ToAddress, destAccount)
assert.Equal(t, payments[0].FromAddress, srcAccount)
assert.Equal(t, payments[0].SrcAssetCode, "XLM")
assert.Equal(t, payments[0].DestAssetCode, "XLM")
})
}