forked from block-vision/sui-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subscribe event or transaction by websocket
- Loading branch information
1 parent
9aeb437
commit 5c79b52
Showing
11 changed files
with
332 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package wsconn | ||
|
||
type SubscriptionResp struct { | ||
Jsonrpc string `json:"jsonrpc"` | ||
Result int64 `json:"result"` | ||
Id int64 `json:"id"` | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package wsconn | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/block-vision/sui-go-sdk/models" | ||
"github.com/gorilla/websocket" | ||
"github.com/tidwall/gjson" | ||
"log" | ||
"time" | ||
) | ||
|
||
type WsConn struct { | ||
Conn *websocket.Conn | ||
wsUrl string | ||
} | ||
|
||
type CallOp struct { | ||
Method string | ||
Params []interface{} | ||
} | ||
|
||
func NewWsConn(wsUrl string) *WsConn { | ||
dialer := websocket.Dialer{} | ||
conn, _, err := dialer.Dial(wsUrl, nil) | ||
|
||
if err != nil { | ||
log.Fatal("Error connecting to Websocket Server:", err, wsUrl) | ||
} | ||
|
||
return &WsConn{ | ||
Conn: conn, | ||
wsUrl: wsUrl, | ||
} | ||
} | ||
|
||
func (w *WsConn) Call(ctx context.Context, op CallOp, receiveMsgCh chan []byte) error { | ||
jsonRPCCall := models.JsonRPCRequest{ | ||
JsonRPC: "2.0", | ||
ID: time.Now().UnixMilli(), | ||
Method: op.Method, | ||
Params: op.Params, | ||
} | ||
|
||
callBytes, err := json.Marshal(jsonRPCCall) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = w.Conn.WriteMessage(websocket.TextMessage, callBytes) | ||
if nil != err { | ||
return err | ||
} | ||
|
||
_, messageData, err := w.Conn.ReadMessage() | ||
if nil != err { | ||
return err | ||
} | ||
|
||
var rsp SubscriptionResp | ||
if gjson.ParseBytes(messageData).Get("error").Exists() { | ||
return fmt.Errorf(gjson.ParseBytes(messageData).Get("error").String()) | ||
} | ||
|
||
err = json.Unmarshal([]byte(gjson.ParseBytes(messageData).String()), &rsp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("establish successfully, subscriptionID: %d, Waiting to accept data...\n", rsp.Result) | ||
|
||
go func(conn *websocket.Conn) { | ||
for { | ||
messageType, messageData, err := conn.ReadMessage() | ||
if nil != err { | ||
log.Println(err) | ||
break | ||
} | ||
switch messageType { | ||
case websocket.TextMessage: | ||
receiveMsgCh <- messageData | ||
|
||
default: | ||
continue | ||
} | ||
} | ||
}(w.Conn) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/block-vision/sui-go-sdk/constant" | ||
"github.com/block-vision/sui-go-sdk/models" | ||
"github.com/block-vision/sui-go-sdk/sui" | ||
"github.com/block-vision/sui-go-sdk/utils" | ||
) | ||
|
||
func main() { | ||
go SubscribeEvent() | ||
go SubscribeTransaction() | ||
select {} | ||
} | ||
|
||
func SubscribeEvent() { | ||
var ctx = context.Background() | ||
var cli = sui.NewSuiWebsocketClient(constant.WssBvTestnetEndpoint) | ||
|
||
receiveMsgCh := make(chan models.SuiEventResponse, 10) | ||
err := cli.SubscribeEvent(ctx, models.SuiXSubscribeEventsRequest{ | ||
SuiEventFilter: map[string]interface{}{ | ||
"All": []string{}, | ||
}, | ||
}, receiveMsgCh) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for { | ||
select { | ||
case msg := <-receiveMsgCh: | ||
utils.PrettyPrint(msg) | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} | ||
|
||
func SubscribeTransaction() { | ||
var ctx = context.Background() | ||
var cli = sui.NewSuiWebsocketClient(constant.WssBvTestnetEndpoint) | ||
|
||
receiveMsgCh := make(chan models.SuiEffects, 10) | ||
err := cli.SubscribeTransaction(ctx, models.SuiXSubscribeTransactionsRequest{ | ||
TransactionFilter: models.TransactionFilterByFromAddress{ | ||
FromAddress: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
}, | ||
}, receiveMsgCh) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for { | ||
select { | ||
case msg := <-receiveMsgCh: | ||
utils.PrettyPrint(msg) | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package sui | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/block-vision/sui-go-sdk/common/wsconn" | ||
"github.com/block-vision/sui-go-sdk/models" | ||
"github.com/tidwall/gjson" | ||
"log" | ||
) | ||
|
||
type ISubscribeAPI interface { | ||
SubscribeEvent(ctx context.Context, req models.SuiXSubscribeEventsRequest, msgCh chan models.SuiEventResponse) error | ||
SubscribeTransaction(ctx context.Context, req models.SuiXSubscribeTransactionsRequest, msgCh chan models.SuiEffects) error | ||
} | ||
|
||
type suiSubscribeImpl struct { | ||
conn *wsconn.WsConn | ||
} | ||
|
||
// SubscribeEvent implements the method `suix_subscribeEvent`, subscribe to a stream of Sui event. | ||
func (s *suiSubscribeImpl) SubscribeEvent(ctx context.Context, req models.SuiXSubscribeEventsRequest, msgCh chan models.SuiEventResponse) error { | ||
rsp := make(chan []byte, 10) | ||
err := s.conn.Call(ctx, wsconn.CallOp{ | ||
Method: "suix_subscribeEvent", | ||
Params: []interface{}{ | ||
req.SuiEventFilter, | ||
}, | ||
}, rsp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
for { | ||
select { | ||
case messageData := <-rsp: | ||
var result models.SuiEventResponse | ||
if gjson.ParseBytes(messageData).Get("error").Exists() { | ||
log.Fatal(gjson.ParseBytes(messageData).Get("error").String()) | ||
} | ||
|
||
err := json.Unmarshal([]byte(gjson.ParseBytes(messageData).Get("params.result").String()), &result) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
msgCh <- result | ||
} | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
// SubscribeTransaction implements the method `suix_subscribeTransaction`, subscribe to a stream of Sui transaction effects. | ||
func (s *suiSubscribeImpl) SubscribeTransaction(ctx context.Context, req models.SuiXSubscribeTransactionsRequest, msgCh chan models.SuiEffects) error { | ||
rsp := make(chan []byte, 10) | ||
err := s.conn.Call(ctx, wsconn.CallOp{ | ||
Method: "suix_subscribeTransaction", | ||
Params: []interface{}{ | ||
req.TransactionFilter, | ||
}, | ||
}, rsp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
for { | ||
select { | ||
case messageData := <-rsp: | ||
var result models.SuiEffects | ||
if gjson.ParseBytes(messageData).Get("error").Exists() { | ||
log.Fatal(gjson.ParseBytes(messageData).Get("error").String()) | ||
} | ||
|
||
err := json.Unmarshal([]byte(gjson.ParseBytes(messageData).Get("params.result").String()), &result) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
msgCh <- result | ||
} | ||
} | ||
}() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) BlockVision, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package sui | ||
|
||
import ( | ||
"github.com/block-vision/sui-go-sdk/common/wsconn" | ||
) | ||
|
||
// ISuiWebsocketAPI defines the subscription API related interface, and then implement it by the WebsocketClient. | ||
type ISuiWebsocketAPI interface { | ||
ISubscribeAPI | ||
} | ||
|
||
// WebsocketClient implements SuiWebsocketAPI related interfaces. | ||
type WebsocketClient struct { | ||
ISubscribeAPI | ||
} | ||
|
||
// NewSuiWebsocketClient instantiates the WebsocketClient to call the methods of each module. | ||
func NewSuiWebsocketClient(rpcUrl string) ISuiWebsocketAPI { | ||
conn := wsconn.NewWsConn(rpcUrl) | ||
return &WebsocketClient{ | ||
ISubscribeAPI: &suiSubscribeImpl{ | ||
conn: conn, | ||
}, | ||
} | ||
} |