Skip to content

Conversation

@serayd61
Copy link

Summary

Added practical DeFi examples to help developers get started with stacks.js:

  • contract-deployment.ts: Deploy Clarity contracts programmatically
  • contract-interaction.ts: Call read-only and public functions
  • token-operations.ts: SIP-010 token balance, transfer, metadata

Motivation

The current documentation is great but could benefit from real-world examples.
These examples use actual deployed contracts on mainnet to show practical usage.

Changes

  • Created examples/defi/ directory
  • Added 3 TypeScript examples with full documentation
  • Added README with setup instructions

Testing

All examples have been tested against mainnet contracts:

  • SP2PEBKJ2W1ZDDF2QQ6Y4FXKZEDPT9J9R2NKD9WJB.sentinel-token
  • SP2PEBKJ2W1ZDDF2QQ6Y4FXKZEDPT9J9R2NKD9WJB.voting

Related

These examples complement my open-source DeFi projects:

Copy link
Contributor

@friedger friedger left a comment

Choose a reason for hiding this comment

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

Looks like AI generated code based on historical stuff not being progressive.

Make it a real world example. Like https://github.com/friedger/clarity-ccip-026 or something similar.

} from '@stacks/transactions';
import { StacksMainnet, StacksTestnet } from '@stacks/network';

// Contract source code (simple counter example)
Copy link
Contributor

Choose a reason for hiding this comment

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

Simple counter is not defi and not real world example as mentioned in the readme.

import { StacksMainnet, StacksTestnet } from '@stacks/network';

// Contract source code (simple counter example)
const contractSource = `
Copy link
Contributor

Choose a reason for hiding this comment

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

In real world the contract is either generated or read from file.

async function deployContract() {
// Configuration
const network = new StacksTestnet(); // Use StacksMainnet() for mainnet
const senderKey = 'YOUR_PRIVATE_KEY_HERE'; // Never commit real keys!
Copy link
Contributor

Choose a reason for hiding this comment

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

How to never commit keys in real world?

callReadOnlyFunction,
makeContractCall,
broadcastTransaction,
uintCV,
Copy link
Contributor

Choose a reason for hiding this comment

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

Using Cl is more recommended nowadays because it requires less imports

const CONTRACT_ADDRESS = 'SP2PEBKJ2W1ZDDF2QQ6Y4FXKZEDPT9J9R2NKD9WJB';
const CONTRACT_NAME = 'voting';

const network = new StacksMainnet();
Copy link
Contributor

Choose a reason for hiding this comment

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

Only name is required

postConditionMode: PostConditionMode.Deny,
// Add post-condition to protect user
postConditions: [
makeStandardFungiblePostCondition(
Copy link
Contributor

Choose a reason for hiding this comment

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

We have better builders for post conditions nows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants