Skip to content

Commit

Permalink
Merge pull request #27 from sidan-lab/main
Browse files Browse the repository at this point in the history
fix: fixing tx submission
  • Loading branch information
Vardominator authored Feb 15, 2024
2 parents bafd9af + a790718 commit 26cca2c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
11 changes: 11 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,14 @@ func (c *Client) post(url string, body interface{}) (*http.Response, error) {
req.Header.Set("Content-Type", "application/json")
return c.HTTPClient.Do(req)
}

func (c *Client) postBuffer(url string, buffer []byte) (*http.Response, error) {
req, err := http.NewRequest("POST", c.BaseUrl+url, bytes.NewBuffer(buffer))
req.Header.Set("Accept", "application/cbor")
req.Header.Add("api-key", c.apiKey)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/cbor")
return c.HTTPClient.Do(req)
}
38 changes: 24 additions & 14 deletions client/txmanager.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package client

import (
"encoding/hex"
"encoding/json"
"fmt"
"io"

"github.com/maestro-org/go-sdk/models"
)
Expand All @@ -22,34 +24,42 @@ func (c *Client) TxManagerHistory() (*[]models.TxManagerState, error) {
return &txManagerStates, nil
}

func (c *Client) TxManagerSubmit(cbor string) (models.BasicResponse, error) {
func (c *Client) TxManagerSubmit(txHex string) (string, error) {
url := "/txmanager"
resp, err := c.post(url, cbor)
txBuffer, err := hex.DecodeString(txHex)
if err != nil {
return models.BasicResponse{}, err
return "", err
}
resp, err := c.postBuffer(url, txBuffer)
if err != nil {
return "", err
}
defer resp.Body.Close()
var submitTx models.BasicResponse
err = json.NewDecoder(resp.Body).Decode(&submitTx)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return models.BasicResponse{}, err
return "", err
}
return submitTx, nil
txHash := string(bodyBytes)
return txHash, nil
}

func (c *Client) TxManagerSubmitTurbo(cbor string) (models.BasicResponse, error) {
func (c *Client) TxManagerSubmitTurbo(txHex string) (string, error) {
url := "/txmanager/turbosubmit"
resp, err := c.post(url, cbor)
txBuffer, err := hex.DecodeString(txHex)
if err != nil {
return "", err
}
resp, err := c.postBuffer(url, txBuffer)
if err != nil {
return models.BasicResponse{}, err
return "", err
}
defer resp.Body.Close()
var submitTx models.BasicResponse
err = json.NewDecoder(resp.Body).Decode(&submitTx)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return models.BasicResponse{}, err
return "", err
}
return submitTx, nil
txHash := string(bodyBytes)
return txHash, nil
}

func (c *Client) TxManagerState(txHash string) (*models.TxManagerState, error) {
Expand Down

0 comments on commit 26cca2c

Please sign in to comment.