Skip to content

Commit

Permalink
Add transaction endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Mar 22, 2024
1 parent 845f859 commit 82a1f8a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ const (
// MIMEApplicationVendorIOTASerializerV2 => bytes.
CoreEndpointOutputWithMetadata = "/outputs/{outputId}/full"

// CoreEndpointTransaction is the endpoint for getting a transaction by its transaction ID.
// GET returns the transaction.
// "Accept" header:
// MIMEApplicationJSON => json.
// MIMEApplicationVendorIOTASerializerV2 => bytes.
CoreEndpointTransaction = "/transactions/{transactionId}"

// CoreEndpointTransactionsIncludedBlock is the endpoint for getting the block that was first confirmed for a given transaction ID.
// GET returns the block.
// "Accept" header:
Expand Down Expand Up @@ -303,6 +310,7 @@ var (
CoreRouteOutput = route(CorePluginName, CoreEndpointOutput)
CoreRouteOutputMetadata = route(CorePluginName, CoreEndpointOutputMetadata)
CoreRouteOutputWithMetadata = route(CorePluginName, CoreEndpointOutputWithMetadata)
CoreRouteTransaction = route(CorePluginName, CoreEndpointTransaction)
CoreRouteTransactionsIncludedBlock = route(CorePluginName, CoreEndpointTransactionsIncludedBlock)
CoreRouteTransactionsIncludedBlockMetadata = route(CorePluginName, CoreEndpointTransactionsIncludedBlockMetadata)
CoreRouteTransactionsMetadata = route(CorePluginName, CoreEndpointTransactionsMetadata)
Expand Down
18 changes: 18 additions & 0 deletions nodeclient/http_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,24 @@ func (client *Client) OutputWithMetadataByID(ctx context.Context, outputID iotag
return outputResponse.Output, outputResponse.Metadata, nil
}

// TransactionByID gets a transaction by its ID from the node.
func (client *Client) TransactionByID(ctx context.Context, txID iotago.TransactionID) (*iotago.Transaction, error) {
query := client.endpointReplaceTransactionIDParameter(api.CoreRouteTransaction, txID)

res := new(RawDataEnvelope)
//nolint:bodyclose
if _, err := client.DoWithRequestHeaderHook(ctx, http.MethodGet, query, RequestHeaderHookAcceptIOTASerializerV2, nil, res); err != nil {
return nil, err
}

tx := new(iotago.Transaction)
if _, err := client.CommittedAPI().Decode(res.Data, tx, serix.WithValidation()); err != nil {
return nil, err
}

return tx, nil
}

// TransactionIncludedBlock get a block that included the given transaction ID in the ledger.
func (client *Client) TransactionIncludedBlock(ctx context.Context, txID iotago.TransactionID) (*iotago.Block, error) {
query := client.endpointReplaceTransactionIDParameter(api.CoreRouteTransactionsIncludedBlock, txID)
Expand Down
22 changes: 22 additions & 0 deletions nodeclient/http_api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,28 @@ func TestClient_OutputWithMetadataByID(t *testing.T) {
require.EqualValues(t, originMetadata, responseMetadata)
}

func TestClient_TestTransactionByID(t *testing.T) {
defer gock.Off()

txID := tpkg.RandTransactionID()

originTransaction := tpkg.RandTransaction(mockAPI)

mockGetBinary(api.EndpointWithNamedParameterValue(api.CoreRouteTransaction, api.ParameterTransactionID, txID.ToHex()), 200, originTransaction)

nodeAPI := nodeClient(t)
responseTransaction, err := nodeAPI.TransactionByID(context.Background(), txID)
require.NoError(t, err)

originTransactionBytes, err := mockAPI.Encode(originTransaction, serix.WithValidation())
require.NoError(t, err)

responseTransactionBytes, err := mockAPI.Encode(responseTransaction, serix.WithValidation())
require.NoError(t, err)

require.Equal(t, originTransactionBytes, responseTransactionBytes)
}

func TestClient_TransactionIncludedBlock(t *testing.T) {
defer gock.Off()

Expand Down

0 comments on commit 82a1f8a

Please sign in to comment.