-
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.
feat: add submit example with refactor of common functions
Signed-off-by: Ales Verbic <[email protected]>
- Loading branch information
Showing
3 changed files
with
215 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/submit" | ||
utxorpc "github.com/utxorpc/go-sdk" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
baseUrl := "https://preview.utxorpc-v0.demeter.run" | ||
// set API key for demeter | ||
client := utxorpc.CreateUtxoRPCClient(baseUrl, | ||
// set API key for demeter | ||
utxorpc.WithHeaders(map[string]string{ | ||
"dmtr-api-key": "dmtr_utxorpc1...", | ||
}), | ||
) | ||
|
||
// Set mode to "submitTx", "readMempool", "waitForTx", or "watchMempool" to select the desired example. | ||
var mode string = "waitForTx" | ||
|
||
switch mode { | ||
case "submitTx": | ||
submitTx(ctx, client, "Replace this with the signed transaction in CBOR format.") | ||
case "readMempool": | ||
readMempool(ctx, client) | ||
case "waitForTx": | ||
waitForTx(ctx, client) | ||
case "watchMempool": | ||
watchMempool(ctx, client) | ||
default: | ||
fmt.Println("Unknown mode:", mode) | ||
} | ||
} | ||
|
||
func submitTx(ctx context.Context, client *utxorpc.UtxorpcClient, txCbor string) { | ||
// Decode the transaction data from hex | ||
txRawBytes, err := hex.DecodeString(txCbor) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to decode transaction hash: %v", err)) | ||
} | ||
|
||
// Create a SubmitTxRequest with the transaction data | ||
tx := &submit.AnyChainTx{ | ||
Type: &submit.AnyChainTx_Raw{ | ||
Raw: txRawBytes, | ||
}, | ||
} | ||
|
||
// Create a list with one transaction | ||
req := connect.NewRequest(&submit.SubmitTxRequest{ | ||
Tx: []*submit.AnyChainTx{tx}, | ||
}) | ||
|
||
fmt.Println("Connecting to utxorpc host:", client.URL()) | ||
resp, err := client.Submit.SubmitTx(ctx, req) | ||
if err != nil { | ||
utxorpc.HandleError(err) | ||
} | ||
fmt.Printf("Response: %+v\n", resp) | ||
} | ||
|
||
func readMempool(ctx context.Context, client *utxorpc.UtxorpcClient) { | ||
req := connect.NewRequest(&submit.ReadMempoolRequest{}) | ||
client.AddHeadersToRequest(req) | ||
fmt.Println("Connecting to utxorpc host:", client.URL()) | ||
resp, err := client.Submit.ReadMempool(ctx, req) | ||
if err != nil { | ||
utxorpc.HandleError(err) | ||
} | ||
fmt.Printf("Response: %+v\n", resp) | ||
} | ||
|
||
func waitForTx(ctx context.Context, client *utxorpc.UtxorpcClient) { | ||
req := connect.NewRequest(&submit.WaitForTxRequest{}) | ||
client.AddHeadersToRequest(req) | ||
fmt.Println("Connecting to utxorpc host:", client.URL()) | ||
stream, err := client.Submit.WaitForTx(ctx, req) | ||
if err != nil { | ||
utxorpc.HandleError(err) | ||
} | ||
|
||
fmt.Println("Connected to utxorpc host, watching mempool...") | ||
for stream.Receive() { | ||
resp := stream.Msg() | ||
fmt.Printf("Stream response: %+v\n", resp) | ||
} | ||
|
||
if err := stream.Err(); err != nil { | ||
fmt.Println("Stream ended with error:", err) | ||
} else { | ||
fmt.Println("Stream ended normally.") | ||
} | ||
} | ||
|
||
func watchMempool(ctx context.Context, client *utxorpc.UtxorpcClient) { | ||
req := connect.NewRequest(&submit.WatchMempoolRequest{}) | ||
client.AddHeadersToRequest(req) | ||
fmt.Println("Connecting to utxorpc host:", client.URL()) | ||
stream, err := client.Submit.WatchMempool(ctx, req) | ||
if err != nil { | ||
utxorpc.HandleError(err) | ||
} | ||
|
||
fmt.Println("Connected to utxorpc host, watching mempool...") | ||
for stream.Receive() { | ||
resp := stream.Msg() | ||
fmt.Printf("Stream response: %+v\n", resp) | ||
} | ||
|
||
if err := stream.Err(); err != nil { | ||
fmt.Println("Stream ended with error:", err) | ||
} else { | ||
fmt.Println("Stream ended normally.") | ||
} | ||
} |
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