Skip to content

Commit bb0838d

Browse files
Merge pull request #2 from dipdup-net/refactoring/node-rpc
Refactoring: tezos node RPC
2 parents e6f296d + 791e34a commit bb0838d

26 files changed

+1922
-557
lines changed

README.md

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,7 @@ if err := config.Parse("config.yaml", &cfg); err != nil {
5050

5151
### `node`
5252

53-
Simple Tezos RPC API wrapper.
54-
55-
```go
56-
import "github.com/dipdup-net/go-lib/node"
57-
58-
rpc := node.NewNodeRPC(url, node.WithTimeout(timeout))
59-
60-
ctx, cancel := context.WithCancel(context.Background())
61-
defer cancel()
62-
63-
// example with context
64-
constants, err := rpc.Constants(node.WithContext(ctx))
65-
if err != nil {
66-
panic(err)
67-
}
68-
69-
// example without context
70-
constants, err := rpc.Constants()
71-
if err != nil {
72-
panic(err)
73-
}
74-
75-
```
53+
Simple Tezos RPC API wrapper. Docs you can find [here](node/README.md)
7654

7755
### `database`
7856

node/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Tezos RPC client
2+
3+
The library realize almost all RPC methods of Tezos node.
4+
5+
## Usage
6+
7+
### Simple example
8+
9+
```go
10+
rpc := node.NewRPC("https://rpc.tzkt.io/mainnet", "main")
11+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
12+
defer cancel()
13+
14+
block, err := rpc.Block(ctx, "head")
15+
if err != nil {
16+
panic(err)
17+
}
18+
log.Printf("%##v", block)
19+
```
20+
21+
You can use main RPC constructor where chain id set by default to `main`
22+
23+
```go
24+
rpc := node.NewMainRPC("https://rpc.tzkt.io/mainnet")
25+
```
26+
27+
### Usage certain API part
28+
29+
RPC struct contains some internal parts: `Chain`, `Block`, `Context`, `Config`, `General`, `Protocols`, `Inject` and `Network`. You can use it without creation of full RPC client.
30+
31+
```go
32+
rpc := node.NewMainBlockRPC("https://rpc.tzkt.io/mainnet")
33+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
34+
defer cancel()
35+
36+
block, err := rpc.Block(ctx, "head")
37+
if err != nil {
38+
panic(err)
39+
}
40+
log.Printf("%##v", block)
41+
```
42+
43+
### Interfaces
44+
45+
For testing purpose RPC was wrapped by interfaces. Also each part of RPC has interface. You can mock it with code generation tools.
46+
47+
Interfaces list:
48+
* `BlockAPI`
49+
* `ChainAPI`
50+
* `ContextAPI`
51+
* `ConfigAPI`
52+
* `GeneralAPI`
53+
* `ProtocolsAPI`
54+
* `NetworkAPI`
55+
* `InjectAPI`

node/api.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package node
2+
3+
import jsoniter "github.com/json-iterator/go"
4+
5+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
6+
7+
// API -
8+
type API interface {
9+
BlockAPI
10+
ChainAPI
11+
ContextAPI
12+
ConfigAPI
13+
GeneralAPI
14+
ProtocolsAPI
15+
NetworkAPI
16+
InjectAPI
17+
}
18+
19+
// RPC -
20+
type RPC struct {
21+
*BlockRPC
22+
*Chain
23+
*Context
24+
*Config
25+
*General
26+
*Protocols
27+
*Network
28+
*Inject
29+
}
30+
31+
// NewRPC -
32+
func NewRPC(baseURL, chainID string) *RPC {
33+
return &RPC{
34+
BlockRPC: NewBlockRPC(baseURL, chainID),
35+
Chain: NewChain(baseURL, chainID),
36+
Context: NewContext(baseURL, chainID),
37+
Config: NewConfig(baseURL),
38+
General: NewGeneral(baseURL),
39+
Protocols: NewProtocols(baseURL),
40+
Network: NewNetwork(baseURL),
41+
Inject: NewInject(baseURL),
42+
}
43+
}
44+
45+
// NewMainRPC -
46+
func NewMainRPC(baseURL string) *RPC {
47+
return &RPC{
48+
BlockRPC: NewMainBlockRPC(baseURL),
49+
Chain: NewMainChain(baseURL),
50+
Context: NewMainContext(baseURL),
51+
Config: NewConfig(baseURL),
52+
General: NewGeneral(baseURL),
53+
Protocols: NewProtocols(baseURL),
54+
Network: NewNetwork(baseURL),
55+
Inject: NewInject(baseURL),
56+
}
57+
}

0 commit comments

Comments
 (0)