Skip to content

Commit f02af92

Browse files
committed
removed: unused code
1 parent 27c9c5c commit f02af92

File tree

3 files changed

+12
-254
lines changed

3 files changed

+12
-254
lines changed

utils/rpc/svm/calls.go

Lines changed: 7 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -1,230 +1,28 @@
11
package rpc
22

33
import (
4-
"bytes"
54
"context"
6-
"encoding/json"
75
"fmt"
8-
"io"
9-
"net/http"
10-
"time"
6+
7+
baserpc "github.com/rollchains/pchain/utils/rpc"
118
)
129

1310
// GetTransaction fetches transaction details using getTransaction RPC method
1411
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-
7912
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)
12114
if err != nil {
122-
return nil, fmt.Errorf("rpc call failed: %w", err)
15+
return nil, fmt.Errorf("getTransaction failed: %w", err)
12316
}
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-
15817
return &result, nil
15918
}
16019

16120
// GetSlot fetches current slot using getSlot RPC method
16221
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-
22422
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)
22726
}
228-
22927
return uint64(result), nil
23028
}

utils/rpc/svm/types.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ package rpc
22

33
// Transaction represents a Solana transaction response from getTransaction
44
type Transaction struct {
5-
BlockTime int64 `json:"blockTime"`
6-
Slot uint64 `json:"slot"`
7-
Signature string `json:"signature"`
8-
Status struct {
9-
Ok interface{} `json:"Ok,omitempty"`
10-
Err interface{} `json:"Err,omitempty"`
11-
} `json:"status"`
5+
Slot uint64 `json:"slot"`
126
Transaction struct {
137
Message struct {
148
AccountKeys []string `json:"accountKeys"`
@@ -20,23 +14,10 @@ type Transaction struct {
2014
} `json:"message"`
2115
} `json:"transaction"`
2216
Meta struct {
23-
Err interface{} `json:"err"`
24-
LogMessages []string `json:"logMessages"`
25-
PostTokenBalances []struct {
26-
Owner string `json:"owner"`
27-
Writable bool `json:"writable"`
28-
} `json:"postTokenBalances"`
17+
Err interface{} `json:"err"`
18+
LogMessages []string `json:"logMessages"`
2919
} `json:"meta"`
3020
}
3121

32-
// Block represents a Solana block response from getBlock
33-
type Block struct {
34-
Blockhash string `json:"blockhash"`
35-
BlockTime int64 `json:"blockTime"`
36-
ParentSlot uint64 `json:"parentSlot"`
37-
Slot uint64 `json:"slot"`
38-
Transactions []Transaction `json:"transactions"`
39-
}
40-
4122
// Slot represents a Solana slot response from getSlot
4223
type Slot uint64

0 commit comments

Comments
 (0)