Skip to content

Commit

Permalink
Upgrade references to use latest modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Dec 19, 2023
1 parent 81a2f89 commit ca72e86
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
44 changes: 24 additions & 20 deletions docs/reference/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,54 +28,57 @@ using modern Javascript, but `await` calls can also be rendered with promises.
// That site exposes a global StellarSdk object you can use.
// To run this code in the Chrome, open the console tab in the DevTools.
// The hotkey to open the DevTools console is Ctrl+Shift+J or (Cmd+Opt+J on Mac).
const StellarSdk = require('stellar-sdk');
const {
Horizon,
Networks,
Asset,
Keypair,
Operation,
TransactionBuilder,
} = require('@stellar/stellar-sdk');

// The source account is the account we will be signing and sending from.
const sourceSecretKey = 'SCZANGBA5YHTNYVVV4C3U252E2B6P6F5T3U6MM63WBSBZATAQI3EBTQ4';

// Derive Keypair object and public key (that starts with a G) from the secret
const sourceKeypair = StellarSdk.Keypair.fromSecret(sourceSecretKey);
const sourceKeypair = Keypair.fromSecret(sourceSecretKey);
const sourcePublicKey = sourceKeypair.publicKey();

const receiverPublicKey = 'GAIRISXKPLOWZBMFRPU5XRGUUX3VMA3ZEWKBM5MSNRU3CHV6P4PYZ74D';

// Configure StellarSdk to talk to the horizon instance hosted by Stellar.org
// Configure the server to the Horizon instance hosted by Stellar.org
// To use the live network, set the hostname to 'horizon.stellar.org'
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
const server = new Horizon.Server('https://horizon-testnet.stellar.org');

(async function main() {
// Transactions require a valid sequence number that is specific to this account.
// We can fetch the current sequence number for the source account from Horizon.
const account = await server.loadAccount(sourcePublicKey);


// Right now, there's one function that fetches the base fee.
// In the future, we'll have functions that are smarter about suggesting fees,
// e.g.: `fetchCheapFee`, `fetchAverageFee`, `fetchPriorityFee`, etc.
const fee = await server.fetchBaseFee();


const transaction = new StellarSdk.TransactionBuilder(account, {
const transaction = new TransactionBuilder(account, {
fee,
// Uncomment the following line to build transactions for the live network. Be
// sure to also change the horizon hostname.
// networkPassphrase: StellarSdk.Networks.PUBLIC,
networkPassphrase: StellarSdk.Networks.TESTNET
networkPassphrase: Networks.TESTNET
})
// Add a payment operation to the transaction
.addOperation(StellarSdk.Operation.payment({
.addOperation(Operation.payment({
destination: receiverPublicKey,
// The term native asset refers to lumens
asset: StellarSdk.Asset.native(),
asset: Asset.native(),
// Specify 350.1234567 lumens. Lumens are divisible to seven digits past
// the decimal. They are represented in JS Stellar SDK in string format
// to avoid errors from the use of the JavaScript Number data structure.
// the decimal. They are represented in the SDK in string format to avoid
// errors from the use of the JavaScript Number data structure.
amount: '350.1234567',
}))
// Make this transaction valid for the next 30 seconds only
.setTimeout(30)
// Uncomment to add a memo (https://developers.stellar.org/docs/glossary/transactions/)
// .addMemo(StellarSdk.Memo.text('Hello world!'))
// .addMemo(Memo.text('Hello world!'))
.build();

// Sign this transaction with the secret key
Expand Down Expand Up @@ -106,8 +109,8 @@ const server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
Let's say you want to look at an account's transaction history. You can use the `transactions()` command and pass in the account address to `forAccount` as the resource you're interested in.

```javascript
const StellarSdk = require('stellar-sdk')
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
const StellarSdk = require('@stellar/stellar-sdk')
const server = new StellarSdk.Horizon.Server('https://horizon-testnet.stellar.org');
const accountId = 'GBBORXCY3PQRRDLJ7G7DWHQBXPCJVFGJ4RGMJQVAX6ORAUH6RWSPP6FM';

server.transactions()
Expand All @@ -129,12 +132,13 @@ server.transactions()

## Streaming payment events

js-stellar-sdk provides streaming support for Horizon endpoints using `EventSource`. You can pass a function to handle any events that occur on the stream.
The SDK provides streaming support for Horizon endpoints using `EventSource`. You can pass a function to handle any events that occur on the stream.

Try submitting a transaction (via the guide above) while running the following code example.

```javascript
const StellarSdk = require('stellar-sdk')
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
const StellarSdk = require('@stellar/stellar-sdk')
const server = new StellarSdk.Horizon.Server('https://horizon-testnet.stellar.org');

// Get a message any time a payment occurs. Cursor is set to "now" to be notified
// of payments happening starting from when this script runs (as opposed to from
Expand Down
38 changes: 15 additions & 23 deletions docs/reference/readme.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
---
title: Overview
---
The JavaScript Stellar SDK facilitates integration with the [Stellar Horizon API server](https://github.com/stellar/go/tree/master/services/horizon) and submission of Stellar transactions, either on Node.js or in the browser. It has two main uses: [querying Horizon](#querying-horizon) and [building, signing, and submitting transactions to the Stellar network](#building-transactions).
The JavaScript Stellar SDK facilitates integration with the Stellar [Horizon API server](https://developers.stellar.org/api/), the Stellar [Soroban RPC server](https://soroban.stellar.org/docs/reference/rpc) and submission of Stellar transactions, either on Node.js or in the browser. It has three main uses: [querying Horizon](#querying-horizon), [interacting with Soroban RPC](), and [building, signing, and submitting transactions to the Stellar network](#building-transactions).

[Building and installing js-stellar-sdk](https://github.com/stellar/js-stellar-sdk)<br>
[Examples of using js-stellar-sdk](./examples.md)
* [Building and installing the SDK](https://github.com/stellar/js-stellar-sdk)
* [Examples of using the SDK](./examples.md)

# Querying Horizon
js-stellar-sdk gives you access to all the endpoints exposed by Horizon.
The Stellar SDK gives you access to all the endpoints exposed by Horizon.

## Building requests
js-stellar-sdk uses the [Builder pattern](https://en.wikipedia.org/wiki/Builder_pattern) to create the requests to send
to Horizon. Starting with a [server](https://stellar.github.io/js-stellar-sdk/Server.html) object, you can chain methods together to generate a query.
(See the [Horizon reference](https://developers.stellar.org/api/) documentation for what methods are possible.)
js-stellar-sdk uses the [Builder pattern](https://en.wikipedia.org/wiki/Builder_pattern) to create the requests to send to Horizon. Starting with a [server](https://stellar.github.io/js-stellar-sdk/Server.html) object, you can chain methods together to generate a query. (See the [Horizon reference](https://developers.stellar.org/api/) documentation for what methods are possible.)

```js
var StellarSdk = require('stellar-sdk');
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
var StellarSdk = require('@stellar/stellar-sdk');
var server = new StellarSdk.Horizon.Server('https://horizon-testnet.stellar.org');
// get a list of transactions that occurred in ledger 1400
server.transactions()
.forLedger(1400)
Expand All @@ -27,19 +26,16 @@ server.transactions()
.call().then(function(r){ console.log(r); });
```

Once the request is built, it can be invoked with `.call()` or with `.stream()`. `call()` will return a
[promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to the response given by Horizon.
Once the request is built, it can be invoked with `.call()` or with `.stream()`. `call()` will return a [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to the response given by Horizon.

## Streaming requests
Many requests can be invoked with `stream()`. Instead of returning a promise like `call()` does, `.stream()` will return an `EventSource`.
Horizon will start sending responses from either the beginning of time or from the point specified with `.cursor()`.
(See the [Horizon reference](https://developers.stellar.org/api/introduction/streaming/) documentation to learn which endpoints support streaming.)
Many requests can be invoked with `stream()`. Instead of returning a promise like `call()` does, `.stream()` will return an `EventSource`. Horizon will start sending responses from either the beginning of time or from the point specified with `.cursor()`. (See the [Horizon reference](https://developers.stellar.org/api/introduction/streaming/) documentation to learn which endpoints support streaming.)

For example, to log instances of transactions from a particular account:

```javascript
var StellarSdk = require('stellar-sdk')
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
var StellarSdk = require('@stellar/stellar-sdk')
var server = new StellarSdk.Horizon.Server('https://horizon-testnet.stellar.org');
var lastCursor=0; // or load where you left off

var txHandler = function (txResponse) {
Expand Down Expand Up @@ -98,17 +94,13 @@ See the [Building Transactions](https://github.com/stellar/js-stellar-base/blob/
## Submitting transactions
Once you have built your transaction, you can submit it to the Stellar network with `Server.submitTransaction()`.
```js
const StellarSdk = require('stellar-sdk')
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
const StellarSdk = require('@stellar/stellar-sdk')
const server = new StellarSdk.Horizon.Server('https://horizon-testnet.stellar.org');

(async function main() {
const account = await server.loadAccount(publicKey);

/*
Right now, we have one function that fetches the base fee.
In the future, we'll have functions that are smarter about suggesting fees,
e.g.: `fetchCheapFee`, `fetchAverageFee`, `fetchPriorityFee`, etc.
*/
// Right now, we have one function that fetches the base fee.
const fee = await server.fetchBaseFee();

const transaction = new StellarSdk.TransactionBuilder(account, { fee, networkPassphrase: StellarSdk.Networks.TESTNET })
Expand Down

0 comments on commit ca72e86

Please sign in to comment.