-
Notifications
You must be signed in to change notification settings - Fork 43
feat: add Claimable Balance support across lib, AgentClient, and Lang… #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nishant-uxs
wants to merge
2
commits into
Stellar-Tools:main
Choose a base branch
from
nishant-uxs:feat/claimable-balances-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,113 @@ | ||
| /** | ||
| * Claimable Balance Usage Example | ||
| * | ||
| * Demonstrates the three claimable-balance flows exposed by AgentKit: | ||
| * 1. create — lock funds with a (possibly composite) claim predicate | ||
| * 2. list — discover open claimable balances for a recipient | ||
| * 3. claim — release the locked funds to the recipient | ||
| * | ||
| * Use cases this enables for AI agents: | ||
| * - Conditional payouts (only pay if X happens before deadline) | ||
| * - Vesting / scheduled payments | ||
| * - Two-party escrow (either party can claim before / after deadline) | ||
| * | ||
| * ⚠️ TESTNET ONLY. Never use mainnet secrets in examples. | ||
| * | ||
| * Run with: ts-node examples/claimable-balance-example.ts | ||
| */ | ||
|
|
||
| import { AgentClient } from "../agent"; | ||
| import { Keypair } from "@stellar/stellar-sdk"; | ||
|
|
||
| async function exampleClaimableBalances() { | ||
| console.log("🔒 Claimable Balance Example"); | ||
| console.log("=".repeat(60)); | ||
|
|
||
| const agent = new AgentClient({ network: "testnet" }); | ||
|
|
||
| // In real usage, source/recipient would be funded testnet accounts. | ||
| const source = Keypair.random(); | ||
| const recipient = Keypair.random(); | ||
|
|
||
| console.log("\nGenerated test accounts:"); | ||
| console.log(` Source: ${source.publicKey()}`); | ||
| console.log(` Recipient: ${recipient.publicKey()}`); | ||
| console.log( | ||
| "\n⚠️ Fund these accounts on testnet before running for real:\n" + | ||
| " https://laboratory.stellar.org/#account-creator?network=test" | ||
| ); | ||
|
|
||
| // ─── 1. Create ───────────────────────────────────────────────────────── | ||
| // Lock 50 XLM for the recipient. Two layered conditions, expressed as a | ||
| // composite predicate: | ||
| // - claimable for the next 24h (beforeRelativeTime: 86400s) | ||
| // - AND only after the configured "release time" has passed | ||
| // (i.e. NOT before that absolute timestamp) | ||
| const releaseAtUnix = Math.floor(Date.now() / 1000) + 60; // 60s from now | ||
|
|
||
| console.log("\nCreating claimable balance with composite predicate:"); | ||
| console.log(` Amount: 50 XLM`); | ||
| console.log(` Window: next 24h`); | ||
| console.log(` Earliest: ${new Date(releaseAtUnix * 1000).toISOString()}`); | ||
|
|
||
| const created = await agent.claimable.create({ | ||
| sourceSecret: source.secret(), | ||
| asset: { code: "XLM" }, | ||
| amount: "50", | ||
| claimants: [ | ||
| { | ||
| destination: recipient.publicKey(), | ||
| predicate: { | ||
| type: "and", | ||
| predicates: [ | ||
| { type: "beforeRelativeTime", seconds: 86400 }, | ||
| { | ||
| type: "not", | ||
| predicate: { | ||
| type: "beforeAbsoluteTime", | ||
| epochSeconds: releaseAtUnix, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| console.log("\n✅ Created!"); | ||
| console.log(` Tx hash: ${created.transactionHash}`); | ||
| console.log(` Balance id: ${created.balanceIds[0]}`); | ||
|
|
||
| // ─── 2. List ─────────────────────────────────────────────────────────── | ||
| console.log("\nListing open claimable balances for recipient..."); | ||
| const open = await agent.claimable.list({ | ||
| claimant: recipient.publicKey(), | ||
| }); | ||
| console.log(` Found ${open.length} balance(s).`); | ||
| open.forEach((b) => | ||
| console.log(` - ${b.id} amount=${b.amount} asset=${b.asset}`) | ||
| ); | ||
|
|
||
| // ─── 3. Claim ────────────────────────────────────────────────────────── | ||
| // In a real flow you'd wait until the predicate is satisfied. Here we | ||
| // demonstrate the call shape — Horizon will reject the claim with a | ||
| // descriptive error if the predicate is not yet satisfied. | ||
| console.log("\nAttempting to claim (will fail if predicate not yet satisfied)..."); | ||
| try { | ||
| const claimed = await agent.claimable.claim({ | ||
| claimerSecret: recipient.secret(), | ||
| balanceId: created.balanceIds[0], | ||
| }); | ||
| console.log(`✅ Claimed! Tx hash: ${claimed.transactionHash}`); | ||
| } catch (err) { | ||
| console.log( | ||
| `⏳ Not yet claimable (expected): ${(err as Error).message}\n` + | ||
| ` Wait until ${new Date(releaseAtUnix * 1000).toISOString()} and retry.` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| exampleClaimableBalances().catch((err) => { | ||
| console.error("Example failed:", err); | ||
| process.exit(1); | ||
| }); | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.