Skip to content
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

Pulling in new OpenAPI /api into our new new docs site #2

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default async function depositAsset(e: Event) {

if (!popup) {
this.loading = { ...this.loading, deposit: false };
throw 'Popups are blocked. You\'ll need to enable popups for this demo to work';
throw "Popups are blocked. You'll need to enable popups for this demo to work";
}

window.onmessage = ({ data: { transaction } }) => {
Expand Down Expand Up @@ -401,7 +401,7 @@ const popup = open(urlBuilder.toString(), "popup", "width=500,height=800");

if (!popup) {
this.loading = { ...this.loading, deposit: false };
throw 'Popups are blocked. You\'ll need to enable popups for this demo to work';
throw "Popups are blocked. You'll need to enable popups for this demo to work";
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export default async function withdrawAsset(e: Event) {

if (!popup) {
this.loading = { ...this.loading, withdraw: false };
throw 'Popups are blocked. You\'ll need to enable popups for this demo to work';
throw "Popups are blocked. You'll need to enable popups for this demo to work";
}

await new Promise((resolve, reject) => {
Expand Down
4 changes: 2 additions & 2 deletions docs/building-apps/first-deposit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The flow with Claimable Balances looks like this:
1. The wallet initiates a deposit on behalf of a user.
1. The anchor provides deposit instructions to the wallet.
1. The user transfers money from a bank account to the anchor’s account.
1. The anchor creates and funds the user's Stellar account plus the amount required for trustlines and transaction fees. Again, we suggest 2 XLM to start.
1. The anchor creates and funds the user's Stellar account plus the amount required for trustlines and transaction fees. Again, we suggest 2 XLM to start.
1. The anchor creates a Claimable Balance.
1. The wallet detects the Claimable Balance for the account, claims the funds, and posts it in the wallet.

Expand All @@ -57,4 +57,4 @@ The flow looks like this:

![wallet creates account flow](/assets/first-deposit-wallet-flow.png)

**Note**: In the examples above, we suggest having the anchor or wallet cover minimum balance and trustline XLM requirements by depositing funds directly into a user's account. We made that suggestion for the sake of simplicity, but in all cases, the anchor or wallet could instead use [Sponsored Reserves](../glossary/sponsored-reserves.mdx) to ensure that when a user closes a trustline or merges their account, the reserve reverts to the sponsoring account rather than to the user's account.
**Note**: In the examples above, we suggest having the anchor or wallet cover minimum balance and trustline XLM requirements by depositing funds directly into a user's account. We made that suggestion for the sake of simplicity, but in all cases, the anchor or wallet could instead use [Sponsored Reserves](../glossary/sponsored-reserves.mdx) to ensure that when a user closes a trustline or merges their account, the reserve reverts to the sponsoring account rather than to the user's account.
91 changes: 53 additions & 38 deletions docs/glossary/claimable-balance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,45 +72,54 @@ const sdk = require("stellar-sdk");
async function main() {
let server = new sdk.Server("https://horizon-testnet.stellar.org");

let A = sdk.Keypair.fromSecret("SAQLZCQA6AYUXK6JSKVPJ2MZ5K5IIABJOEQIG4RVBHX4PG2KMRKWXCHJ");
let B = sdk.Keypair.fromPublicKey("GAS4V4O2B7DW5T7IQRPEEVCRXMDZESKISR7DVIGKZQYYV3OSQ5SH5LVP");
let A = sdk.Keypair.fromSecret(
"SAQLZCQA6AYUXK6JSKVPJ2MZ5K5IIABJOEQIG4RVBHX4PG2KMRKWXCHJ",
);
let B = sdk.Keypair.fromPublicKey(
"GAS4V4O2B7DW5T7IQRPEEVCRXMDZESKISR7DVIGKZQYYV3OSQ5SH5LVP",
);

// NOTE: Proper error checks are omitted for brevity; always validate things!

let aAccount = await server.loadAccount(A.publicKey()).catch(function (err) {
console.error(`Failed to load ${A.publicKey()}: ${err}`)
})
if (!aAccount) { return }
console.error(`Failed to load ${A.publicKey()}: ${err}`);
});
if (!aAccount) {
return;
}

// Create a claimable balance with our two above-described conditions.
let soon = Math.ceil((Date.now() / 1000) + 60); // .now() is in ms
let soon = Math.ceil(Date.now() / 1000 + 60); // .now() is in ms
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do linters think they know better than humans

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/shrug I'm not a linter fan but the only thing I hate worse than linting is configuring linters

let bCanClaim = sdk.Claimant.predicateBeforeRelativeTime("60");
let aCanReclaim = sdk.Claimant.predicateNot(
sdk.Claimant.predicateBeforeAbsoluteTime(soon.toString())
sdk.Claimant.predicateBeforeAbsoluteTime(soon.toString()),
);

// Create the operation and submit it in a transaction.
let claimableBalanceEntry = sdk.Operation.createClaimableBalance({
claimants: [
new sdk.Claimant(B.publicKey(), bCanClaim),
new sdk.Claimant(A.publicKey(), aCanReclaim)
new sdk.Claimant(A.publicKey(), aCanReclaim),
],
asset: sdk.Asset.native(),
amount: "420",
});

let tx = new sdk.TransactionBuilder(aAccount, {fee: sdk.BASE_FEE})
let tx = new sdk.TransactionBuilder(aAccount, { fee: sdk.BASE_FEE })
.addOperation(claimableBalanceEntry)
.setNetworkPassphrase(sdk.Networks.TESTNET)
.setTimeout(180)
.build();

tx.sign(A);
let txResponse = await server.submitTransaction(tx).then(function() {
console.log("Claimable balance created!");
}).catch(function (err) {
console.error(`Tx submission failed: ${err}`)
});
let txResponse = await server
.submitTransaction(tx)
.then(function () {
console.log("Claimable balance created!");
})
.catch(function (err) {
console.error(`Tx submission failed: ${err}`);
});
}
```

Expand Down Expand Up @@ -188,16 +197,16 @@ import time
from stellar_sdk.xdr import TransactionResult, OperationType
from stellar_sdk.exceptions import NotFoundError, BadResponseError, BadRequestError
from stellar_sdk import (
Keypair,
Network,
Keypair,
Network,
Server,
TransactionBuilder,
Transaction,
Asset,
TransactionBuilder,
Transaction,
Asset,
Operation,
Claimant,
ClaimPredicate,
CreateClaimableBalance,
Claimant,
ClaimPredicate,
CreateClaimableBalance,
ClaimClaimableBalance
)

Expand Down Expand Up @@ -241,7 +250,7 @@ tx = (
.build()
)

tx.sign(A)
tx.sign(A)
try:
txResponse = server.submit_transaction(tx)
print("Claimable balance created!")
Expand All @@ -267,7 +276,9 @@ Either party could also check the `/effects` of the transaction, query `/claimab
// Method 2: Suppose `txResponse` comes from the transaction submission
// above.
let txResult = sdk.xdr.TransactionResult.fromXDR(
txResponse.result_xdr, "base64");
txResponse.result_xdr,
"base64",
);
let results = txResult.result().results();

// We look at the first result since our first (and only) operation
Expand All @@ -278,15 +289,17 @@ console.log("Balance ID (2):", balanceId);

// Method 3: Account B could alternatively do something like:
let balances = await server
.claimableBalances()
.claimant(B.publicKey())
.limit(1) // there may be many in general
.order("desc") // so always get the latest one
.call()
.catch(function(err) {
console.error(`Claimable balance retrieval failed: ${err}`)
});
if (!balances) { return; }
.claimableBalances()
.claimant(B.publicKey())
.limit(1) // there may be many in general
.order("desc") // so always get the latest one
.call()
.catch(function (err) {
console.error(`Claimable balance retrieval failed: ${err}`);
});
if (!balances) {
return;
}

balanceId = balances.records[0].id;
console.log("Balance ID (3):", balanceId);
Expand Down Expand Up @@ -320,7 +333,7 @@ balanceId := balances.Embedded.Records[0].BalanceID

```python
# Method 1: Not available in the Python SDK yet.

# Method 2: Suppose `txResponse` comes from the transaction submission
# above.
txResult = TransactionResult.from_xdr(txResponse["result_xdr"])
Expand All @@ -330,7 +343,7 @@ results = txResult.result.results
# in the transaction was the CreateClaimableBalanceOp.
operationResult = results[0].tr.create_claimable_balance_result
balanceId = operationResult.balance_id.to_xdr_bytes().hex()
print(f"Balance ID (2): {balanceId}")
print(f"Balance ID (2): {balanceId}")

# Method 3: Account B could alternatively do something like:
try:
Expand All @@ -356,18 +369,20 @@ With the claimable balance ID acquired, either Account B or A can actually submi
<CodeExample>

```js
let claimBalance = sdk.Operation.claimClaimableBalance({ balanceId: balanceId });
let claimBalance = sdk.Operation.claimClaimableBalance({
balanceId: balanceId,
});
console.log(A.publicKey(), "claiming", balanceId);

let tx = new sdk.TransactionBuilder(aAccount, {fee: sdk.BASE_FEE})
let tx = new sdk.TransactionBuilder(aAccount, { fee: sdk.BASE_FEE })
.addOperation(claimBalance)
.setNetworkPassphrase(sdk.Networks.TESTNET)
.setTimeout(180)
.build();

tx.sign(A);
await server.submitTransaction(tx).catch(function (err) {
console.error(`Tx submission failed: ${err}`)
console.error(`Tx submission failed: ${err}`);
});
```

Expand Down
Loading