2020-11-18 18:33:03 +00:00
|
|
|
package filecoin
|
2020-08-03 20:01:38 +00:00
|
|
|
|
|
|
|
import (
|
2020-08-06 22:48:28 +00:00
|
|
|
"encoding/json"
|
2020-08-03 20:01:38 +00:00
|
|
|
"fmt"
|
2020-11-14 11:25:14 +00:00
|
|
|
|
2020-11-18 18:33:03 +00:00
|
|
|
"github.com/Secured-Finance/dione/rpc/types"
|
|
|
|
|
2020-11-14 11:25:14 +00:00
|
|
|
"github.com/Secured-Finance/dione/lib"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/valyala/fasthttp"
|
2020-08-03 20:01:38 +00:00
|
|
|
)
|
|
|
|
|
2020-11-14 11:25:14 +00:00
|
|
|
var filecoinURL = "https://filecoin.infura.io/"
|
|
|
|
|
2020-08-06 22:48:28 +00:00
|
|
|
type LotusClient struct {
|
2020-11-14 11:25:14 +00:00
|
|
|
host string
|
|
|
|
projectID string
|
|
|
|
projectSecret string
|
2020-11-18 18:33:03 +00:00
|
|
|
httpClient *fasthttp.Client
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 11:25:14 +00:00
|
|
|
func NewLotusClient(pID, secret string) *LotusClient {
|
2020-08-06 22:48:28 +00:00
|
|
|
return &LotusClient{
|
2020-11-14 11:25:14 +00:00
|
|
|
host: filecoinURL,
|
|
|
|
projectID: pID,
|
|
|
|
projectSecret: secret,
|
2020-11-18 18:33:03 +00:00
|
|
|
httpClient: &fasthttp.Client{},
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 18:33:03 +00:00
|
|
|
func (c *LotusClient) GetTransaction(txHash string) ([]byte, error) {
|
2020-11-14 11:25:14 +00:00
|
|
|
req := fasthttp.AcquireRequest()
|
|
|
|
req.SetRequestURI(c.host)
|
|
|
|
req.Header.SetMethod("POST")
|
|
|
|
req.Header.SetContentType("application/json")
|
|
|
|
req.Header.Set("Authorization", "Basic "+lib.BasicAuth(c.projectID, c.projectSecret))
|
2020-11-18 18:33:03 +00:00
|
|
|
requestBody := types.NewRPCRequestBody("Filecoin.ChainGetMessage")
|
2020-08-06 22:48:28 +00:00
|
|
|
requestBody.Params = append(requestBody.Params, txHash)
|
|
|
|
body, err := json.Marshal(requestBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to marshal request body %v", err)
|
|
|
|
}
|
2020-11-14 11:25:14 +00:00
|
|
|
req.AppendBody(body)
|
|
|
|
resp := fasthttp.AcquireResponse()
|
2020-11-18 18:33:03 +00:00
|
|
|
if err = c.httpClient.Do(req, resp); err != nil {
|
2020-11-14 11:25:14 +00:00
|
|
|
logrus.Warn("Failed to construct filecoin node rpc request", err)
|
|
|
|
return nil, err
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|
2020-11-14 11:25:14 +00:00
|
|
|
bodyBytes := resp.Body()
|
2020-11-18 18:33:03 +00:00
|
|
|
logrus.Debug(string(bodyBytes))
|
|
|
|
return bodyBytes, nil
|
2020-08-03 20:01:38 +00:00
|
|
|
}
|