|
| 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