From 3f5eaba2ef45e2c654a249fb7dee4e3b3b06ae07 Mon Sep 17 00:00:00 2001 From: Michal Nicpon Date: Fri, 5 Dec 2025 17:11:13 +0000 Subject: [PATCH] feat add transaction cbor endpoint --- api_transaction_test.go | 25 +++++++++++++++++-- api_transactions.go | 25 ++++++++++++++++++- client.go | 1 + .../json/transactions/transaction_cbor.json | 3 +++ testdata/transactioncborintegration.golden | 3 +++ 5 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 testdata/json/transactions/transaction_cbor.json create mode 100644 testdata/transactioncborintegration.golden diff --git a/api_transaction_test.go b/api_transaction_test.go index 974b4b4..cf81cb9 100644 --- a/api_transaction_test.go +++ b/api_transaction_test.go @@ -39,6 +39,15 @@ func TestTransactionContentUnmarshal(t *testing.T) { testStructGotWant(t, fp, &got, &want) } +func TestTransactionCBORUnmarshal(t *testing.T) { + want := blockfrost.TransactionCBOR{ + Cbor: "83a40081825820c84dbd7b780ff9be2bcc6990f043030fe649342f9fa7ba1bfa6fb5c0079501a400018282582b82d818582183581cf810f5ffa3613a7edcc1e733d0d73c89645d02221648a031436fd840a0001a4f7ed7fd1a45bb41e082581d619fc8d50cd36af0e35f43db0e2f220e4ccb774925f2ef3542875b37701a000f4240021a00030d40031a00f9c7cca102818458205e3eb96f2cfd7844b5eed5498fe487fc5271178fb251ebf3d33c5d8f49036d9358403c9ef2ce22b8a3fce622472f84c932679f95e0f3e139378fe9d6ab5442c60a9b87de374a0bc37667b1bf8d5c6bffb436ffd84b0fa56a7c86342962b9c293390758208ed6ca9faabd8499f021c2e51745436c99f9eab6c92f4b09b255bdd9f39bae2a41a0f6", + } + fp := filepath.Join(testdata, "json", "transactions", "transaction_cbor.json") + got := blockfrost.TransactionCBOR{} + testStructGotWant(t, fp, &got, &want) +} + func TestTransactionStakeAddressCertUnmarshall(t *testing.T) { fp := filepath.Join(testdata, "json", "transactions", "tx_stakeaddr_cert.json") want := []blockfrost.TransactionStakeAddressCert{ @@ -118,6 +127,19 @@ func TestTransactionIntegration(t *testing.T) { testIntUtil(t, fp, &got, &want) } +func TestTransactionCBORIntegration(t *testing.T) { + hash := "6e5f825c82c1c6d6b77f2a14092f3b78c8f1b66db6f4cf8caec1555b6f967b3b" + api := blockfrost.NewAPIClient(blockfrost.APIClientOptions{}) + + got, err := api.TransactionCBOR(context.TODO(), hash) + if err != nil { + t.Fatal(err) + } + fp := filepath.Join(testdata, strings.ToLower(strings.TrimPrefix(t.Name(), "Test"))+".golden") + want := blockfrost.TransactionCBOR{} + testIntUtil(t, fp, &got, &want) +} + func TestTransactionUTXOs(t *testing.T) { hash := "6d619f41ba2e11b78c0d5647fb71ee5df05622fda1748a9124446226e54e6b50" api := blockfrost.NewAPIClient(blockfrost.APIClientOptions{}) @@ -270,7 +292,6 @@ func TestTransactionPoolRetirementsIntegration(t *testing.T) { } func TestTransactionEvaluateIntegration(t *testing.T) { - api := blockfrost.NewAPIClient(blockfrost.APIClientOptions{}) got, err := api.TransactionEvaluate(context.TODO(), tx_cbor) if err != nil { @@ -285,8 +306,8 @@ func TestTransactionEvaluateIntegration(t *testing.T) { testIntUtil(t, fp, &got, &want) } -func TestTransactionEvaluateUTXOsIntegration(t *testing.T) { +func TestTransactionEvaluateUTXOsIntegration(t *testing.T) { additionalUtxoSet := blockfrost.AdditionalUtxoSet{ { TxIn: blockfrost.AdditionalUtxoSetTxIn{ diff --git a/api_transactions.go b/api_transactions.go index d6265f0..39e5be7 100644 --- a/api_transactions.go +++ b/api_transactions.go @@ -99,6 +99,10 @@ type TransactionContent struct { WithdrawalCount int `json:"withdrawal_count"` } +type TransactionCBOR struct { + Cbor string `json:"cbor"` +} + type TxAmount struct { // The quantity of the unit Quantity string `json:"quantity"` @@ -348,6 +352,26 @@ func (c *apiClient) Transaction(ctx context.Context, hash string) (tc Transactio return tc, nil } +func (c *apiClient) TransactionCBOR(ctx context.Context, hash string) (tc TransactionCBOR, err error) { + requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceCbor)) + if err != nil { + return + } + req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil) + if err != nil { + return + } + res, err := c.handleRequest(req) + if err != nil { + return + } + defer res.Body.Close() + if err = json.NewDecoder(res.Body).Decode(&tc); err != nil { + return + } + return tc, nil +} + func (c *apiClient) TransactionUTXOs(ctx context.Context, hash string) (tu TransactionUTXOs, err error) { requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxUTXOs)) if err != nil { @@ -611,7 +635,6 @@ func (c *apiClient) TransactionSubmit(ctx context.Context, cbor []byte) (hash st func (c *apiClient) TransactionEvaluate(ctx context.Context, cbor []byte) (jsonResponse OgmiosResponse, err error) { requestUrl, err := url.Parse(fmt.Sprintf("%s/%s", c.server, resourceTxEvaluate)) - if err != nil { return } diff --git a/client.go b/client.go index 65deed1..7fb1a80 100644 --- a/client.go +++ b/client.go @@ -173,6 +173,7 @@ type APIClient interface { PoolUpdates(ctx context.Context, poolID string, query APIQueryParams) ([]PoolUpdate, error) PoolUpdatesAll(ctx context.Context, poolId string) <-chan PoolUpdateResult Transaction(ctx context.Context, hash string) (TransactionContent, error) + TransactionCBOR(ctx context.Context, hash string) (TransactionCBOR, error) TransactionUTXOs(ctx context.Context, hash string) (TransactionUTXOs, error) TransactionStakeAddressCerts(ctx context.Context, hash string) ([]TransactionStakeAddressCert, error) TransactionWithdrawals(ctx context.Context, hash string) ([]TransactionWidthrawal, error) diff --git a/testdata/json/transactions/transaction_cbor.json b/testdata/json/transactions/transaction_cbor.json new file mode 100644 index 0000000..6f007fd --- /dev/null +++ b/testdata/json/transactions/transaction_cbor.json @@ -0,0 +1,3 @@ +{ + "cbor": "83a40081825820c84dbd7b780ff9be2bcc6990f043030fe649342f9fa7ba1bfa6fb5c0079501a400018282582b82d818582183581cf810f5ffa3613a7edcc1e733d0d73c89645d02221648a031436fd840a0001a4f7ed7fd1a45bb41e082581d619fc8d50cd36af0e35f43db0e2f220e4ccb774925f2ef3542875b37701a000f4240021a00030d40031a00f9c7cca102818458205e3eb96f2cfd7844b5eed5498fe487fc5271178fb251ebf3d33c5d8f49036d9358403c9ef2ce22b8a3fce622472f84c932679f95e0f3e139378fe9d6ab5442c60a9b87de374a0bc37667b1bf8d5c6bffb436ffd84b0fa56a7c86342962b9c293390758208ed6ca9faabd8499f021c2e51745436c99f9eab6c92f4b09b255bdd9f39bae2a41a0f6" +} diff --git a/testdata/transactioncborintegration.golden b/testdata/transactioncborintegration.golden new file mode 100644 index 0000000..4bd93e2 --- /dev/null +++ b/testdata/transactioncborintegration.golden @@ -0,0 +1,3 @@ +{ + "cbor": "83a4008282582041844abf9fa0e0fc46d630fbb3d99ae21e631387c14a366cec973bdfd1ca98aa00825820ef14f5a183dd72ca968a08a87ab3592dda7932fbba662be27e2a81db1906de2001018282584c82d818584283581c2ffe248da0231ec3c59674b11ae014a359c3d798a39edc02e11accefa101581e581ce6945e2c0be0cba471a1b08c798eb92a6b141b1db087bcda32ef1c75001ae11367521a9208088082584c82d818584283581cf2c29e3bebf516d2ac60216e65f08cec4f49e9cd78fbf4235d124fbaa101581e581c0ddde05acafb9f01b827c5b6439338b7f8a285dd04f17694dcfa26b7001a8642a97f1a0156df90021a0002c8d5031a00d3e1d9a10281845820748e425472fe2849e427571421036ec2ec8c5a5a3bf1cb53fdda52f99571360158407b3145eeabdf3faf7cc5fb396373b562506c20370670337bb934179781c61a2202546b6c0031f595d3092afe0e797c7ea4eea847f74a6a40caf2bc45e0587501582029cb5060793f932ba07669e6c0d38142df2ce5f6f031e0b9b50a197d7b1c8f495822a101581e581c0ddde05acafb9fd2050251b6adf0521e6e396c69f3b98b353fb69744f6" +}