Skip to content

Commit 3354e07

Browse files
authored
refactor: forward submit to a remote endpoint (#118)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent ea03bdd commit 3354e07

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

internal/api/tx.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package api
1616

1717
import (
18-
"context"
1918
"encoding/hex"
2019
"encoding/json"
2120
"fmt"
@@ -124,17 +123,12 @@ func (a *Api) handleTxSubmit(w http.ResponseWriter, r *http.Request) {
124123
}
125124
defer r.Body.Close() //nolint:errcheck
126125

127-
submit, err := txbuilder.OgmiosClient().
128-
SubmitTx(context.Background(), hex.EncodeToString(txRawBytes))
126+
txHash, err := txbuilder.SubmitTx(txRawBytes)
129127
if err != nil {
130128
http.Error(w, fmt.Sprintf("%s", err), http.StatusBadRequest)
131129
return
132130
}
133-
if submit.Error != nil {
134-
http.Error(w, submit.Error.Message, submit.Error.Code)
135-
return
136-
}
137131
w.Header().Set("Content-Type", "application/json")
138-
resp, _ := json.Marshal(submit.ID)
132+
resp, _ := json.Marshal(txHash)
139133
_, _ = w.Write(resp)
140134
}

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type ApiConfig struct {
105105
type TxBuilderConfig struct {
106106
KupoUrl string `yaml:"kupoUrl" envconfig:"TXBUILDER_KUPO_URL"`
107107
OgmiosUrl string `yaml:"ogmiosUrl" envconfig:"TXBUILDER_OGMIOS_URL"`
108+
SubmitUrl string `yaml:"submitUrl" envconfig:"TXBUILDER_SUBMIT_URL"`
108109
ProviderAddress string `yaml:"providerAddress" envconfig:"TXBUILDER_PROVIDER_ADDRESS"`
109110
ScriptRefInput string `yaml:"scriptRefInput" envconfig:"TXBUILDER_SCRIPT_REF_INPUT"`
110111
}

internal/txbuilder/submit.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2025 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package txbuilder
16+
17+
import (
18+
"bytes"
19+
"errors"
20+
"io"
21+
"net/http"
22+
"time"
23+
24+
"github.com/blinklabs-io/vpn-indexer/internal/config"
25+
)
26+
27+
func SubmitTx(txRawBytes []byte) (string, error) {
28+
cfg := config.GetConfig()
29+
client := createHTTPClient()
30+
body := bytes.NewBuffer(txRawBytes)
31+
req, err := http.NewRequest(
32+
http.MethodPost,
33+
cfg.TxBuilder.SubmitUrl,
34+
body,
35+
)
36+
if err != nil {
37+
return "", err
38+
}
39+
req.Header.Add("Content-Type", "application/cbor")
40+
resp, err := client.Do(req)
41+
if err != nil {
42+
return "", err
43+
}
44+
respBody, err := io.ReadAll(resp.Body)
45+
if err != nil {
46+
return "", err
47+
}
48+
if resp.Body != nil {
49+
_ = resp.Body.Close()
50+
}
51+
if resp.StatusCode == http.StatusAccepted {
52+
return string(respBody), nil
53+
}
54+
return "", errors.New("empty body returned")
55+
}
56+
57+
// createHTTPClient with custom timeout
58+
func createHTTPClient() *http.Client {
59+
return &http.Client{
60+
Timeout: 60 * time.Second,
61+
Transport: &http.Transport{
62+
MaxIdleConns: 100,
63+
IdleConnTimeout: 90 * time.Second,
64+
DisableCompression: false,
65+
TLSHandshakeTimeout: 10 * time.Second,
66+
ExpectContinueTimeout: 1 * time.Second,
67+
},
68+
}
69+
}

0 commit comments

Comments
 (0)