From 22e04c5a683e8255832e3b70d9334c4e3582a4a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Tue, 10 Dec 2024 16:01:38 +0200 Subject: [PATCH] Add some unit tests. --- .../services/transactionsTransformer_test.go | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/server/services/transactionsTransformer_test.go b/server/services/transactionsTransformer_test.go index 46e2a0f4..6d18d2c3 100644 --- a/server/services/transactionsTransformer_test.go +++ b/server/services/transactionsTransformer_test.go @@ -220,6 +220,69 @@ func TestTransactionsTransformer_NormalTxToRosettaTx(t *testing.T) { require.NoError(t, err) require.Equal(t, expectedRosettaTx, rosettaTx) }) + + t.Run("relayed tx V3", func(t *testing.T) { + tx := &transaction.ApiTransactionResult{ + Hash: "aaaa", + Sender: testscommon.TestAddressAlice, + Receiver: testscommon.TestAddressBob, + RelayerAddress: testscommon.TestAddressCarol, + Value: "1234", + InitiallyPaidFee: "50000000000000", + } + + expectedRosettaTx := &types.Transaction{ + TransactionIdentifier: hashToTransactionIdentifier("aaaa"), + Operations: []*types.Operation{ + { + Type: opTransfer, + Account: addressToAccountIdentifier(testscommon.TestAddressAlice), + Amount: extension.valueToNativeAmount("-1234"), + }, + { + Type: opTransfer, + Account: addressToAccountIdentifier(testscommon.TestAddressBob), + Amount: extension.valueToNativeAmount("1234"), + }, + { + Type: opFee, + Account: addressToAccountIdentifier(testscommon.TestAddressCarol), + Amount: extension.valueToNativeAmount("-50000000000000"), + }, + }, + Metadata: extractTransactionMetadata(tx), + } + + rosettaTx, err := transformer.normalTxToRosetta(tx) + require.NoError(t, err) + require.Equal(t, expectedRosettaTx, rosettaTx) + }) +} + +func TestTransactionsTransformer_decideFeePayer(t *testing.T) { + networkProvider := testscommon.NewNetworkProviderMock() + transformer := newTransactionsTransformer(networkProvider) + + t.Run("when fee payer is sender", func(t *testing.T) { + tx := &transaction.ApiTransactionResult{ + Sender: testscommon.TestAddressAlice, + Receiver: testscommon.TestAddressBob, + } + + feePayer := transformer.decideFeePayer(tx) + require.Equal(t, testscommon.TestAddressAlice, feePayer) + }) + + t.Run("when fee payer is relayer", func(t *testing.T) { + tx := &transaction.ApiTransactionResult{ + Sender: testscommon.TestAddressAlice, + Receiver: testscommon.TestAddressBob, + RelayerAddress: testscommon.TestAddressCarol, + } + + feePayer := transformer.decideFeePayer(tx) + require.Equal(t, testscommon.TestAddressCarol, feePayer) + }) } func TestTransactionsTransformer_ExtractInnerTxOperationsIfRelayedCompletelyIntrashardWithSignalError(t *testing.T) {