/
readme.md
128 lines (99 loc) · 5.46 KB
/
readme.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
---
title: Overview
---
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 the SDK](https://github.com/stellar/js-stellar-sdk)
* [Examples of using the SDK](./examples.md)
# Querying 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
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)
.call().then(function(r){ console.log(r); });
// get a list of transactions submitted by a particular account
server.transactions()
.forAccount('GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW')
.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.
## 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.)
For example, to log instances of transactions from a particular account:
```javascript
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) {
console.log(txResponse);
};
var es = server.transactions()
.forAccount(accountAddress)
.cursor(lastCursor)
.stream({
onmessage: txHandler
})
```
## Handling responses
### XDR
The transaction endpoints will return some fields in raw [XDR](https://developers.stellar.org/api/introduction/xdr/)
form. You can convert this XDR to JSON using the `.fromXDR()` method.
An example of re-writing the txHandler from above to print the XDR fields as JSON:
```javascript
var txHandler = function (txResponse) {
console.log( JSON.stringify(StellarSdk.xdr.TransactionEnvelope.fromXDR(txResponse.envelope_xdr, 'base64')) );
console.log( JSON.stringify(StellarSdk.xdr.TransactionResult.fromXDR(txResponse.result_xdr, 'base64')) );
console.log( JSON.stringify(StellarSdk.xdr.TransactionMeta.fromXDR(txResponse.result_meta_xdr, 'base64')) );
};
```
### Following links
The [HAL format](https://developers.stellar.org/api/introduction/response-format/) links returned with the Horizon response are converted into functions you can call on the returned object.
This allows you to simply use `.next()` to page through results. It also makes fetching additional info, as in the following example, easy:
```js
server.payments()
.limit(1)
.call()
.then(function(response){
// will follow the transactions link returned by Horizon
response.records[0].transaction().then(function(txs){
console.log(txs);
});
});
```
# Transactions
## Building transactions
See the [Building Transactions](https://github.com/stellar/js-stellar-base/blob/master/docs/reference/building-transactions.md) guide for information about assembling a transaction.
## Submitting transactions
Once you have built your transaction, you can submit it to the Stellar network with `Server.submitTransaction()`.
```js
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.
const fee = await server.fetchBaseFee();
const transaction = new StellarSdk.TransactionBuilder(account, { fee, networkPassphrase: StellarSdk.Networks.TESTNET })
.addOperation(
// this operation funds the new account with XLM
StellarSdk.Operation.payment({
destination: "GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW",
asset: StellarSdk.Asset.native(),
amount: "2"
})
)
.setTimeout(30)
.build();
// sign the transaction
transaction.sign(StellarSdk.Keypair.fromSecret(secretString));
try {
const transactionResult = await server.submitTransaction(transaction);
console.log(transactionResult);
} catch (err) {
console.error(err);
}
})()
```