Skip to content

Commit b350f82

Browse files
authored
Merge pull request #154 from williamsvedinoe-crypto/feat/92-93-trustline-payment-stream
feat: add trustline management (#93) and account payment stream (#92) examples
2 parents f1e8cac + 869157d commit b350f82

4 files changed

Lines changed: 954 additions & 2 deletions

File tree

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ The repository currently includes the following runnable examples:
201201
65. **`107-contract-spec-introspection`**: Retrieving on-chain WASM, parsing Soroban ScSpec metadata, and displaying functions, arguments, return types, user-defined types, and documentation with dynamic function selection.
202202
65. **`81-transaction-preflight`**: Running the full Soroban preflight workflow — simulating an invocation, extracting the footprint/authorization/resource-fee data, assembling, signing, submitting, and confirming the final transaction.
203203
65. **`83-multi-contract-transaction`**: Composing a single orchestrator contract invocation that touches multiple downstream contracts, simulating and submitting it, and explaining atomicity and execution order across contracts within one Soroban host invocation.
204+
66. **`93-trustline-management`**: Creating, inspecting, updating, and removing asset trustlines — demonstrating changeTrust operations, trust limit configuration, authorization status inspection, and the 0.5 XLM reserve cost of each subentry.
205+
67. **`92-account-payment-stream`**: Subscribing to a Horizon account payment stream, displaying incoming and outgoing payments in real time, handling stream errors with automatic reconnection, and explaining when streaming should be preferred over polling.
204206

205207
## Installation
206208

@@ -748,6 +750,39 @@ CONTRACT_ID=<orchestrator-id> CONTRACT_ID_A=<contract-a-id> CONTRACT_ID_B=<contr
748750

749751
Soroban only allows a single host-function (contract invocation) operation per transaction, so "multiple contract invocations in one transaction" is achieved by invoking one orchestrator/router contract whose method internally makes cross-contract calls into other contracts, rather than by adding several top-level `contract.call(...)` operations. The example builds that single orchestrator invocation with two downstream contract IDs as arguments, simulates it to display the combined resource footprint and authorization entries spanning every contract touched, signs and submits it, and explains why a failure anywhere in the call chain — including a downstream cross-contract call — rolls back the entire transaction atomically, and why execution order follows the orchestrator's own code path rather than the order arguments are listed.
750752

753+
Run the trustline management lifecycle example:
754+
755+
```bash
756+
npm run run-example 93-trustline-management
757+
```
758+
759+
Use a custom asset code:
760+
761+
```bash
762+
npm run run-example -- 93-trustline-management MYTOKEN
763+
```
764+
765+
The example creates two ephemeral Testnet accounts (a holder and a simulated issuer), then walks through the complete trustline lifecycle: creating a trustline for a custom asset with an initial limit, inspecting all trustline details (asset code, issuer, balance, limit, authorization status, and buying/selling liabilities), updating the trust limit, demonstrating invalid asset handling, and finally removing the trustline by setting the limit to zero — recovering the 0.5 XLM reserve subentry cost.
766+
767+
Each non-native Stellar asset requires an explicit opt-in from the receiving account before any payment of that asset can land. The changeTrust operation creates or updates a trustline when `limit > "0"` and removes it when `limit = "0"` (provided the balance is already zero). Each trustline consumes one account subentry, raising the minimum reserve by 0.5 XLM. The asset code can also be supplied through `ASSET_CODE`.
768+
769+
Stream real-time payment events for an account:
770+
771+
```bash
772+
npm run run-example 92-account-payment-stream
773+
```
774+
775+
Monitor a specific account with optional direction filtering:
776+
777+
```bash
778+
npm run run-example -- 92-account-payment-stream <account-id>
779+
ACCOUNT_ID=<account-id> PAYMENT_FILTER=incoming npm run run-example 92-account-payment-stream
780+
```
781+
782+
The example connects to a Horizon server, resolves or discovers an account to monitor, opens a Server-Sent Events stream via `server.payments().forAccount(...).cursor('now').stream(...)`, and displays every new payment record in real time — including the transaction hash, payment type, source and destination accounts, asset information, amount, and timestamp. Each record is labelled as incoming, outgoing, self, or related relative to the monitored account.
783+
784+
The `PAYMENT_FILTER` variable accepts `all` (default), `incoming`, or `outgoing`. Stream errors are logged with a reconnection notice rather than aborting — the SDK reconnects automatically after 15 seconds. Setting `STREAM_MAX_EVENTS` or `STREAM_DURATION_SECONDS` limits the run time, which is useful in CI. Leaving `ACCOUNT_ID` blank makes the example discover a recently active account, so it runs without any setup. Press Ctrl+C to shut down cleanly.
785+
751786
_Note: You can configure custom environment variables in a local `.env` file, including `HORIZON_URL`, `SOROBAN_RPC_URL`, `NETWORK_PASSPHRASE`, and `TRANSACTION_HASH`._
752787

753788
## Automated Example Validation

0 commit comments

Comments
 (0)