Skip to content

Commit

Permalink
Merge pull request #943 from hirosystems/develop
Browse files Browse the repository at this point in the history
v2.5.3
  • Loading branch information
ryanwaits authored Jan 23, 2025
2 parents 14b2ead + 02f90ad commit fc81b5e
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 4 deletions.
5 changes: 1 addition & 4 deletions app/cookbook/components/snippet-result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,7 @@ export function SnippetResult({
</Button> */}
{type === "clarity" && (
<Button variant="link" className="gap-2 self-end" size="sm" asChild>
<Link
href={`https://play.hiro.so/?epoch=3.0&snippet=KGRlZmluZS1yZWFkLW9ubHkgKGdldC10ZW51cmUtaGVpZ2h0IChibG9jayB1aW50KSkKICAob2sKICAgIChhdC1ibG9jawogICAgICAodW53cmFwIQogICAgICAgIChnZXQtc3RhY2tzLWJsb2NrLWluZm8_IGlkLWhlYWRlci1oYXNoIGJsb2NrKQogICAgICAgIChlcnIgdTQwNCkogICAgICApCiAgICAgIHRlbnVyZS1oZWlnaHQKICAgICkKICApCik=`}
target="_blank"
>
<Link href={recipe?.external_url || ""} target="_blank">
Open in Clarity Playground <ArrowUpRight className="w-4 h-4" />
</Link>
</Button>
Expand Down
49 changes: 49 additions & 0 deletions content/_recipes/build-a-stx-pc.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Build a STX post-condition

A helper function that creates a post-condition for STX token transfers using the `Pc` builder class, ensuring exact amounts are transferred as expected.

## Use cases

- Securing STX token transfers with transfer amount validation

## Example usage

```typescript -cn
import {
AnchorMode,
broadcastTransaction,
makeSTXTokenTransfer,
Pc,
PostConditionMode,
} from "@stacks/transactions";

// !hover post-conditions
const pc = Pc.principal("ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM")
// !hover post-conditions
.willSendEq(10000000)
// !hover post-conditions
.ustx();

const txOptions = {
recipient: "ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5",
amount: 10000000,
senderKey: "753b7cc01a1a2e86221266a154af739463fce51219d97e4f856cd7200c3bd2a601",
network: STACKS_TESTNET,
// !hover post-conditions
postConditions: [pc],
postConditionMode: PostConditionMode.Deny,
anchorMode: AnchorMode.Any,
};

const transaction = await makeSTXTokenTransfer(txOptions);
const broadcastResponse = await broadcastTransaction({
transaction,
});
```

This example passes in our <HoverLink href="hover:post-condition" className="text-[var(--ch-11)]">post-conditions</HoverLink> as part of the transaction options, ensuring that exactly 10 STX will be transferred from the `ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM` principal.

## Resources

- [Stacks.js / Transactions](/stacks/stacks.js/packages/transactions)
- [Post-conditions](/stacks/stacks.js/guides/post-conditions)
12 changes: 12 additions & 0 deletions content/_recipes/build-an-ft-pc.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Build a fungible token post-condition

A helper function that creates a post-condition for SIP-010 fungible token transfers using the `Pc` builder class, ensuring exact amounts of specific tokens are transferred as expected.

## Use cases

- Securing token swaps with transfer amount validation

## Resources

- [Stacks.js / Transactions](/stacks/stacks.js/packages/transactions)
- [Post-conditions](/stacks/stacks.js/guides/post-conditions)
12 changes: 12 additions & 0 deletions content/_recipes/build-an-nft-pc.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Build an NFT post-condition

A helper function that creates a post-condition for NFT transfers using the `Pc` builder class, ensuring specific NFTs are handled as expected.

## Use cases

- Securing NFT transfers with ownership validation

## Resources

- [Stacks.js / Transactions](/stacks/stacks.js/packages/transactions)
- [Post-conditions](/stacks/stacks.js/guides/post-conditions)
50 changes: 50 additions & 0 deletions content/_recipes/create-a-sponsored-tx.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Create a sponsored transaction

A script to create sponsored transactions where one party (the sponsor) pays the transaction fees for another party's transaction.

## Use cases

- Building onboarding flows where a service pays user fees
- Creating DAO operations where treasury pays member fees

## Example usage

```typescript -cn
import {
broadcastTransaction,
deserializeTransaction,
sponsorTransaction,
} from "@stacks/transactions";
// !hover serialized-tx
import { serializedTx } from "./create-a-sponsored-tx";

const deserializedTx = deserializeTransaction(serializedTx);
const sponsorOptions = {
// !hover tx-options
transaction: deserializedTx,
// !hover tx-options
sponsorPrivateKey: "<sponsor-private-key>",
// !hover tx-options
fee: 1000n;
// !hover tx-options
sponsorNonce: 0,
// !hover tx-options
network: STACKS_TESTNET,
};
// !hover sponsor-tx
const sponsoredTx = await sponsorTransaction(sponsorOptions);

const broadcastResponse = await broadcastTransaction({
transaction: sponsoredTx,
});
const txId = broadcastResponse.txid;
```

This example takes the <HoverLink href="hover:serialized-tx">pre-built transaction</HoverLink> and adds the sponsorship details to it. The <HoverLink href="hover:tx-options">sponsorship options</HoverLink> include the transaction itself, the sponsor's credentials, their next valid nonce, and network details.

Once configured, the <HoverLink href="hover:sponsor-tx" className="text-[var(--ch-9)]">sponsorTransaction</HoverLink> function creates the sponsored version, which can then be broadcast to the network.

## Resources

- [Stacks.js / Transactions](/stacks/stacks.js/packages/transactions)
- [Transaction Broadcasting](/stacks/stacks.js/guides/broadcast-transactions)
28 changes: 28 additions & 0 deletions content/_recipes/deploy-a-contract.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Deploy a contract

A script that deploys a Clarity smart contract to the Stacks blockchain using Stacks.js.

## Example usage

```typescript
import { STACKS_TESTNET } from "@stacks/network";
import { makeContractDeploy, broadcastTransaction } from "@stacks/transactions";
import { readFileSync } from 'fs';

const txOptions = {
contractName: "super-cool-contract",
codeBody: readFileSync("./contract.clar", "utf8"),
senderKey: "<your-private-key>",
network: STACKS_TESTNET,
};

const transaction = await makeContractDeploy(txOptions);
const broadcastResponse = await broadcastTransaction({transaction});
const txId = broadcastResponse.txid;
```

This example reads the contract code from a file and uses it to deploy a contract to the Stacks blockchain.

## Resources

- [Stacks.js / Transactions](/stacks/stacks.js/packages/transactions)
13 changes: 13 additions & 0 deletions content/_recipes/generate-a-secret-key.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generate a secret key

A script that generates BIP39 mnemonic phrases for use as wallet secret keys.

## Use cases

- Creating new wallets
- Generating backup phrases
- Testing wallet functionality

## Resources

- [Stacks.js](/stacks/stacks.js)
40 changes: 40 additions & 0 deletions content/_recipes/generate-a-wallet.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generate a wallet from a seed phrase or private key

A script that creates a Stacks wallet from a seed phrase or private key, with support for multiple accounts.

## Use cases

- Creating new wallets from existing seed phrases
- Restoring wallets from backups
- Managing multiple accounts within a single wallet

## Example usage

```typescript
import { generateWallet, generateNewAccount } from '@stacks/wallet-sdk';

const mnemonic = "twice kind fence tip hidden tilt action fragile skin nothing glory cousin green tomorrow spring wrist shed math olympic multiply hip blue scout claw"
const privateKey = "753b7cc01a1a2e86221266a154af739463fce51219d97e4f856cd7200c3bd2a601"

let wallet = generateWallet({
// !diff -
secretKey: mnemonic,
// !diff +
secretKey: privateKey,
password: "secret",
});

// !hover generate-new-account
wallet = generateNewAccount(wallet);

const { account1, account2 } = wallet;
```

This example demonstrates:

1. You can use either a seed phrase or private key to create a wallet.
2. The <HoverLink href="hover:generate-new-account" className="text-[var(--ch-9)]">generateNewAccount</HoverLink> function adds a new account to the wallet.

## Resources

- [Stacks.js](/stacks/stacks.js)
13 changes: 13 additions & 0 deletions content/_recipes/get-account-details-from-wallet.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Get account details from a wallet

A script that extracts important account details like the STX address and private key from a wallet.

## Use cases

- Retrieving wallet addresses for constructing transactions
- Getting private keys for signing operations
- Managing multiple wallet accounts

## Resources

- [Stacks.js](/stacks/stacks.js)
13 changes: 13 additions & 0 deletions content/_recipes/transfer-stx.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Transfer STX tokens

A script that creates and broadcasts an STX token transfer with <HoverLink href="hover:post-conditions">post-conditions</HoverLink> for secure transfers.

## Use cases

- Sending STX tokens between addresses
- Building payment functionality

## Resources

- [Stacks.js / Transactions](/stacks/stacks.js/packages/transactions)
- [Post-conditions](/stacks/stacks.js/guides/post-conditions)
1 change: 1 addition & 0 deletions types/recipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface RecipeMetadata {
categories: RecipeCategory[];
tags: SubTagsForCategory[RecipeCategory][];
dependencies?: Record<string, string>;
external_url?: string;
}

// Code blocks extracted from markdown content
Expand Down

0 comments on commit fc81b5e

Please sign in to comment.