|
1 | 1 | package rpc |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "context" |
6 | | - "encoding/json" |
7 | 5 | "fmt" |
8 | | - "io" |
9 | | - "net/http" |
10 | | - "time" |
| 6 | + |
| 7 | + baserpc "github.com/rollchains/pchain/utils/rpc" |
11 | 8 | ) |
12 | 9 |
|
13 | 10 | // GetTransaction fetches transaction details using getTransaction RPC method |
14 | 11 | func GetTransaction(ctx context.Context, rpcURL, txHash string) (*Transaction, error) { |
15 | | - request := struct { |
16 | | - JSONRPC string `json:"jsonrpc"` |
17 | | - ID int `json:"id"` |
18 | | - Method string `json:"method"` |
19 | | - Params interface{} `json:"params"` |
20 | | - }{ |
21 | | - JSONRPC: "2.0", |
22 | | - ID: 1, |
23 | | - Method: "getTransaction", |
24 | | - Params: []interface{}{txHash, map[string]interface{}{ |
25 | | - // "encoding": "json", |
26 | | - // "maxSupportedTransactionVersion": 0, |
27 | | - }}, |
28 | | - } |
29 | | - |
30 | | - requestBody, err := json.Marshal(request) |
31 | | - if err != nil { |
32 | | - return nil, fmt.Errorf("failed to marshal request: %w", err) |
33 | | - } |
34 | | - |
35 | | - req, err := http.NewRequestWithContext(ctx, "POST", rpcURL, bytes.NewBuffer(requestBody)) |
36 | | - if err != nil { |
37 | | - return nil, fmt.Errorf("failed to create request: %w", err) |
38 | | - } |
39 | | - |
40 | | - req.Header.Set("Content-Type", "application/json") |
41 | | - |
42 | | - client := &http.Client{ |
43 | | - Timeout: 30 * time.Second, |
44 | | - } |
45 | | - |
46 | | - resp, err := client.Do(req) |
47 | | - if err != nil { |
48 | | - return nil, fmt.Errorf("rpc call failed: %w", err) |
49 | | - } |
50 | | - defer resp.Body.Close() |
51 | | - |
52 | | - body, err := io.ReadAll(resp.Body) |
53 | | - if err != nil { |
54 | | - return nil, fmt.Errorf("failed to read response body: %w", err) |
55 | | - } |
56 | | - |
57 | | - var response struct { |
58 | | - JSONRPC string `json:"jsonrpc"` |
59 | | - ID int `json:"id"` |
60 | | - Result json.RawMessage `json:"result"` |
61 | | - Error *struct { |
62 | | - Code int `json:"code"` |
63 | | - Message string `json:"message"` |
64 | | - } `json:"error"` |
65 | | - } |
66 | | - |
67 | | - if err := json.Unmarshal(body, &response); err != nil { |
68 | | - return nil, fmt.Errorf("failed to decode response: %w", err) |
69 | | - } |
70 | | - |
71 | | - if response.Error != nil { |
72 | | - return nil, fmt.Errorf("rpc error: %s", response.Error.Message) |
73 | | - } |
74 | | - |
75 | | - if len(response.Result) == 0 { |
76 | | - return nil, fmt.Errorf("empty result from RPC call") |
77 | | - } |
78 | | - |
79 | 12 | var result Transaction |
80 | | - if err := json.Unmarshal(response.Result, &result); err != nil { |
81 | | - return nil, fmt.Errorf("failed to unmarshal result: %w", err) |
82 | | - } |
83 | | - |
84 | | - return &result, nil |
85 | | -} |
86 | | - |
87 | | -// GetBlock fetches block details using getBlock RPC method |
88 | | -func GetBlock(ctx context.Context, rpcURL string, slot uint64) (*Block, error) { |
89 | | - request := struct { |
90 | | - JSONRPC string `json:"jsonrpc"` |
91 | | - ID int `json:"id"` |
92 | | - Method string `json:"method"` |
93 | | - Params interface{} `json:"params"` |
94 | | - }{ |
95 | | - JSONRPC: "2.0", |
96 | | - ID: 1, |
97 | | - Method: "getBlock", |
98 | | - Params: []interface{}{slot, map[string]interface{}{ |
99 | | - "encoding": "json", |
100 | | - "maxSupportedTransactionVersion": 0, |
101 | | - }}, |
102 | | - } |
103 | | - |
104 | | - requestBody, err := json.Marshal(request) |
105 | | - if err != nil { |
106 | | - return nil, fmt.Errorf("failed to marshal request: %w", err) |
107 | | - } |
108 | | - |
109 | | - req, err := http.NewRequestWithContext(ctx, "POST", rpcURL, bytes.NewBuffer(requestBody)) |
110 | | - if err != nil { |
111 | | - return nil, fmt.Errorf("failed to create request: %w", err) |
112 | | - } |
113 | | - |
114 | | - req.Header.Set("Content-Type", "application/json") |
115 | | - |
116 | | - client := &http.Client{ |
117 | | - Timeout: 30 * time.Second, |
118 | | - } |
119 | | - |
120 | | - resp, err := client.Do(req) |
| 13 | + err := baserpc.GetClient().Call(ctx, rpcURL, "getTransaction", []interface{}{txHash}, &result) |
121 | 14 | if err != nil { |
122 | | - return nil, fmt.Errorf("rpc call failed: %w", err) |
| 15 | + return nil, fmt.Errorf("getTransaction failed: %w", err) |
123 | 16 | } |
124 | | - defer resp.Body.Close() |
125 | | - |
126 | | - body, err := io.ReadAll(resp.Body) |
127 | | - if err != nil { |
128 | | - return nil, fmt.Errorf("failed to read response body: %w", err) |
129 | | - } |
130 | | - |
131 | | - var response struct { |
132 | | - JSONRPC string `json:"jsonrpc"` |
133 | | - ID int `json:"id"` |
134 | | - Result json.RawMessage `json:"result"` |
135 | | - Error *struct { |
136 | | - Code int `json:"code"` |
137 | | - Message string `json:"message"` |
138 | | - } `json:"error"` |
139 | | - } |
140 | | - |
141 | | - if err := json.Unmarshal(body, &response); err != nil { |
142 | | - return nil, fmt.Errorf("failed to decode response: %w", err) |
143 | | - } |
144 | | - |
145 | | - if response.Error != nil { |
146 | | - return nil, fmt.Errorf("rpc error: %s", response.Error.Message) |
147 | | - } |
148 | | - |
149 | | - if len(response.Result) == 0 { |
150 | | - return nil, fmt.Errorf("empty result from RPC call") |
151 | | - } |
152 | | - |
153 | | - var result Block |
154 | | - if err := json.Unmarshal(response.Result, &result); err != nil { |
155 | | - return nil, fmt.Errorf("failed to unmarshal result: %w", err) |
156 | | - } |
157 | | - |
158 | 17 | return &result, nil |
159 | 18 | } |
160 | 19 |
|
161 | 20 | // GetSlot fetches current slot using getSlot RPC method |
162 | 21 | func GetSlot(ctx context.Context, rpcURL string) (uint64, error) { |
163 | | - request := struct { |
164 | | - JSONRPC string `json:"jsonrpc"` |
165 | | - ID int `json:"id"` |
166 | | - Method string `json:"method"` |
167 | | - Params interface{} `json:"params"` |
168 | | - }{ |
169 | | - JSONRPC: "2.0", |
170 | | - ID: 1, |
171 | | - Method: "getSlot", |
172 | | - Params: nil, |
173 | | - } |
174 | | - |
175 | | - requestBody, err := json.Marshal(request) |
176 | | - if err != nil { |
177 | | - return 0, fmt.Errorf("failed to marshal request: %w", err) |
178 | | - } |
179 | | - |
180 | | - req, err := http.NewRequestWithContext(ctx, "POST", rpcURL, bytes.NewBuffer(requestBody)) |
181 | | - if err != nil { |
182 | | - return 0, fmt.Errorf("failed to create request: %w", err) |
183 | | - } |
184 | | - |
185 | | - req.Header.Set("Content-Type", "application/json") |
186 | | - |
187 | | - client := &http.Client{ |
188 | | - Timeout: 30 * time.Second, |
189 | | - } |
190 | | - |
191 | | - resp, err := client.Do(req) |
192 | | - if err != nil { |
193 | | - return 0, fmt.Errorf("rpc call failed: %w", err) |
194 | | - } |
195 | | - defer resp.Body.Close() |
196 | | - |
197 | | - body, err := io.ReadAll(resp.Body) |
198 | | - if err != nil { |
199 | | - return 0, fmt.Errorf("failed to read response body: %w", err) |
200 | | - } |
201 | | - |
202 | | - var response struct { |
203 | | - JSONRPC string `json:"jsonrpc"` |
204 | | - ID int `json:"id"` |
205 | | - Result json.RawMessage `json:"result"` |
206 | | - Error *struct { |
207 | | - Code int `json:"code"` |
208 | | - Message string `json:"message"` |
209 | | - } `json:"error"` |
210 | | - } |
211 | | - |
212 | | - if err := json.Unmarshal(body, &response); err != nil { |
213 | | - return 0, fmt.Errorf("failed to decode response: %w", err) |
214 | | - } |
215 | | - |
216 | | - if response.Error != nil { |
217 | | - return 0, fmt.Errorf("rpc error: %s", response.Error.Message) |
218 | | - } |
219 | | - |
220 | | - if len(response.Result) == 0 { |
221 | | - return 0, fmt.Errorf("empty result from RPC call") |
222 | | - } |
223 | | - |
224 | 22 | var result Slot |
225 | | - if err := json.Unmarshal(response.Result, &result); err != nil { |
226 | | - return 0, fmt.Errorf("failed to unmarshal result: %w", err) |
| 23 | + err := baserpc.GetClient().Call(ctx, rpcURL, "getSlot", nil, &result) |
| 24 | + if err != nil { |
| 25 | + return 0, fmt.Errorf("getSlot failed: %w", err) |
227 | 26 | } |
228 | | - |
229 | 27 | return uint64(result), nil |
230 | 28 | } |
0 commit comments