Merge pull request #1258 from jakeatdocforce/code_samples_page_mduo13

Code samples page expanding on mduo13 work
This commit is contained in:
Rome Reginelli
2022-05-16 17:43:36 -07:00
committed by GitHub
87 changed files with 704 additions and 260 deletions

View File

@@ -363,7 +363,7 @@ WS_HANDLERS["transaction"] = log_tx
以下のサンプルコードは、上に示したすべてのトランザクションのタイプのトランザクションメタデータを確認し、アカウントが受け取ったXRPの金額をレポートします。
```js
{% include '_code-samples/monitor-payments-websocket/read-amount-received.js' %}
{% include '_code-samples/monitor-payments-websocket/js/read-amount-received.js' %}
```
{{ start_step("Read Payments") }}
@@ -480,106 +480,10 @@ $("#tx_read").click((event) => {
<!-- MULTICODE_BLOCK_START -->
*Go*
_Go_
```go
package main
// Connect to the XRPL Ledger using websocket and subscribe to an account
// translation from the JavaScript example to Go
// https://developers.ripple.com/monitor-incoming-payments-with-websocket.html
// This example uses the Gorilla websocket library to create a websocket client
// install: go get github.com/gorilla/websocket
import (
"encoding/json"
"flag"
"log"
"net/url"
"os"
"os/signal"
"time"
"github.com/gorilla/websocket"
)
// websocket address
var addr = flag.String("addr", "s.altnet.rippletest.net:51233", "http service address")
// Payload object
type message struct {
Command string `json:"command"`
Accounts []string `json:"accounts"`
}
func main() {
flag.Parse()
log.SetFlags(0)
var m message
// check for interrupts and cleanly close the connection
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "ws", Host: *addr, Path: "/"}
log.Printf("connecting to %s", u.String())
// make the connection
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
// on exit close
defer c.Close()
done := make(chan struct{})
// send a subscribe command and a target XRPL account
m.Command = "subscribe"
m.Accounts = append(m.Accounts, "rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM")
// struct to JSON marshalling
msg, _ := json.Marshal(m)
// write to the websocket
err = c.WriteMessage(websocket.TextMessage, []byte(string(msg)))
if err != nil {
log.Println("write:", err)
return
}
// read from the websocket
_, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
// print the response from the XRP Ledger
log.Printf("recv: %s", message)
// handle interrupt
for {
select {
case <-done:
return
case <-interrupt:
log.Println("interrupt")
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
select {
case <-done:
case <-time.After(time.Second):
}
return
}
}
}
{% include '_code-samples/monitor-payments-websocket/go/monitor-incoming-payments.go' %}
```
<!-- MULTICODE_BLOCK_END -->

View File

@@ -358,7 +358,7 @@ When you subscribe to an account, you get messages for _all transactions to or f
The following sample code looks at transaction metadata of all the above transaction types to report how much XRP an account received:
```js
{% include '_code-samples/monitor-payments-websocket/read-amount-received.js' %}
{% include '_code-samples/monitor-payments-websocket/js/read-amount-received.js' %}
```
{{ start_step("Read Payments") }}
@@ -475,106 +475,10 @@ Many programming languages have libraries for sending and receiving data over a
<!-- MULTICODE_BLOCK_START -->
*Go*
_Go_
```go
package main
// Connect to the XRPL Ledger using websocket and subscribe to an account
// translation from the JavaScript example to Go
// https://developers.ripple.com/monitor-incoming-payments-with-websocket.html
// This example uses the Gorilla websocket library to create a websocket client
// install: go get github.com/gorilla/websocket
import (
"encoding/json"
"flag"
"log"
"net/url"
"os"
"os/signal"
"time"
"github.com/gorilla/websocket"
)
// websocket address
var addr = flag.String("addr", "s.altnet.rippletest.net:51233", "http service address")
// Payload object
type message struct {
Command string `json:"command"`
Accounts []string `json:"accounts"`
}
func main() {
flag.Parse()
log.SetFlags(0)
var m message
// check for interrupts and cleanly close the connection
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "ws", Host: *addr, Path: "/"}
log.Printf("connecting to %s", u.String())
// make the connection
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
// on exit close
defer c.Close()
done := make(chan struct{})
// send a subscribe command and a target XRPL account
m.Command = "subscribe"
m.Accounts = append(m.Accounts, "rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM")
// struct to JSON marshalling
msg, _ := json.Marshal(m)
// write to the websocket
err = c.WriteMessage(websocket.TextMessage, []byte(string(msg)))
if err != nil {
log.Println("write:", err)
return
}
// read from the websocket
_, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
// print the response from the XRP Ledger
log.Printf("recv: %s", message)
// handle interrupt
for {
select {
case <-done:
return
case <-interrupt:
log.Println("interrupt")
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
select {
case <-done:
case <-time.After(time.Second):
}
return
}
}
}
{% include '_code-samples/monitor-payments-websocket/go/monitor-incoming-payments.go' %}
```
<!-- MULTICODE_BLOCK_END -->

View File

@@ -59,7 +59,7 @@ For example:
_JavaScript_
{{ include_code("_code-samples/require-destination-tags/require-destination-tags.js", language="js", start_with="// Send AccountSet", end_before="// Confirm Account") }}
{{ include_code("_code-samples/require-destination-tags/js/require-destination-tags.js", language="js", start_with="// Send AccountSet", end_before="// Confirm Account") }}
<!-- MULTICODE_BLOCK_END -->
@@ -88,7 +88,7 @@ After the transaction is validated, you can check your account's settings to con
_JavaScript_
{{ include_code("_code-samples/require-destination-tags/require-destination-tags.js", language="js", start_with="// Confirm Account", end_before="// End main()") }}
{{ include_code("_code-samples/require-destination-tags/js/require-destination-tags.js", language="js", start_with="// Confirm Account", end_before="// End main()") }}
<!-- MULTICODE_BLOCK_END -->

View File

@@ -38,7 +38,7 @@ To get started, create a new folder on your local disk and install the JavaScrip
```
Download and expand the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/quickstart.zip) archive.
Download and expand the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/js/quickstart.zip) archive.
## Usage
@@ -89,7 +89,7 @@ To transfer XRP between accounts:
# Code Walkthrough
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/quickstart.zip) in the source repository for this website.
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/js/quickstart.zip) in the source repository for this website.

View File

@@ -26,7 +26,7 @@ This example shows how to:
![Test harness with currency transfer](img/quickstart5.png)
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/quickstart.zip) archive to try each of the samples in your own browser.
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/js/quickstart.zip) archive to try each of the samples in your own browser.
## Usage
@@ -78,7 +78,7 @@ To transfer an issued currency token, once you have created a TrustLine:
# Code Walkthrough
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/quickstart.zip) archive to try each of the samples in your own browser.
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/js/quickstart.zip) archive to try each of the samples in your own browser.
## ripplex2-send-currency.js

View File

@@ -26,7 +26,7 @@ This example shows how to:
# Usage
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/quickstart.zip) archive to try the sample in your own browser.
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/js/quickstart.zip) archive to try the sample in your own browser.
## Get Accounts
@@ -100,7 +100,7 @@ To permanently destroy a NFToken:
# Code Walkthrough
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/quickstart.zip) archive to examine the code samples.
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/js/quickstart.zip) archive to examine the code samples.
## ripplex3-mint-nfts.js

View File

@@ -27,7 +27,7 @@ This example shows how to:
![Quickstart form with NFToken transfer fields](img/quickstart13.png)
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/quickstart.zip) archive to try each of the samples in your own browser.
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/js/quickstart.zip) archive to try each of the samples in your own browser.
# Usage
@@ -158,7 +158,7 @@ To cancel a buy or sell offer that you have created:
# Code Walkthrough
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/quickstart.zip) archive to try each of the samples in your own browser.
You can download the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/js/quickstart.zip) archive to try each of the samples in your own browser.
## Create Sell Offer

View File

@@ -52,7 +52,7 @@ To get started, create a new folder on your local disk and install the JavaScrip
```
Download and expand the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/quickstart.zip) archive.
Download and expand the [Quickstart Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/content/_code-samples/quickstart/js/quickstart.zip) archive.
---