home / skills / raintree-technology / claude-starter / dapp-integration

dapp-integration skill

/skills/aptos/dapp-integration

This skill helps you build Aptos dApps with frontend integration, wallet support, TypeScript SDK usage, and secure transaction flows across React apps.

npx playbooks add skill raintree-technology/claude-starter --skill dapp-integration

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
4.9 KB
---
name: aptos-dapp-integration
description: Expert on building Aptos dApps with frontend integration. Covers wallet connectivity (Petra, Martian, Pontem), wallet adapter patterns, TypeScript SDK, transaction building and submission, account management, and React/Next.js integration.
allowed-tools: Read, Write, Edit, Grep, Glob, Bash
model: sonnet
license: MIT
metadata:
  author: raintree
  version: "1.0"
---

# Aptos DApp Integration Expert

Expert on integrating Aptos blockchain with frontend applications.

## Triggers

- wallet connect, petra, martian, pontem
- typescript sdk, aptos sdk, @aptos-labs/ts-sdk
- dapp, frontend integration
- wallet adapter, transaction, sign

## Wallet Adapter Setup

### Installation

```bash
npm install @aptos-labs/wallet-adapter-react \
            petra-plugin-wallet-adapter \
            @martianwallet/aptos-wallet-adapter
```

### React Provider

```typescript
import { AptosWalletAdapterProvider } from "@aptos-labs/wallet-adapter-react";
import { PetraWallet } from "petra-plugin-wallet-adapter";
import { MartianWallet } from "@martianwallet/aptos-wallet-adapter";

function App() {
  const wallets = [new PetraWallet(), new MartianWallet()];

  return (
    <AptosWalletAdapterProvider plugins={wallets} autoConnect={true}>
      <YourApp />
    </AptosWalletAdapterProvider>
  );
}
```

### Using Wallet Hooks

```typescript
import { useWallet } from "@aptos-labs/wallet-adapter-react";

function WalletButton() {
  const { connect, disconnect, account, connected, signAndSubmitTransaction } = useWallet();

  if (connected) {
    return (
      <div>
        <p>Connected: {account?.address}</p>
        <button onClick={disconnect}>Disconnect</button>
      </div>
    );
  }

  return <button onClick={() => connect("Petra")}>Connect</button>;
}
```

## TypeScript SDK

### Initialize Client

```typescript
import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";

const config = new AptosConfig({ network: Network.MAINNET });
const aptos = new Aptos(config);
```

### Read Data

```typescript
// Account info
const account = await aptos.getAccountInfo({ accountAddress: "0x..." });

// Resources
const resources = await aptos.getAccountResources({ accountAddress: "0x..." });

// Specific resource
const coin = await aptos.getAccountResource({
  accountAddress: "0x...",
  resourceType: "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>"
});

// View function
const result = await aptos.view({
  payload: {
    function: "0x1234::module::get_balance",
    functionArguments: ["0xabcd..."],
  },
});
```

### Submit Transactions

```typescript
const { signAndSubmitTransaction } = useWallet();

const handleTransfer = async () => {
  const response = await signAndSubmitTransaction({
    data: {
      function: "0x1::coin::transfer",
      typeArguments: ["0x1::aptos_coin::AptosCoin"],
      functionArguments: ["0xrecipient...", "100000000"],
    },
  });

  await aptos.waitForTransaction({ transactionHash: response.hash });
};
```

### Transaction with SDK (More Control)

```typescript
// Build
const transaction = await aptos.transaction.build.simple({
  sender: sender.accountAddress,
  data: {
    function: "0x1234::module::my_function",
    functionArguments: [arg1, arg2],
  },
});

// Sign
const senderAuth = aptos.transaction.sign({ signer: sender, transaction });

// Submit
const response = await aptos.transaction.submit.simple({
  transaction,
  senderAuthenticator: senderAuth,
});

// Wait
await aptos.waitForTransaction({ transactionHash: response.hash });
```

## Common Patterns

### Check Balance

```typescript
async function getAptBalance(address: string): Promise<number> {
  const resource = await aptos.getAccountResource({
    accountAddress: address,
    resourceType: "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
  });
  return Number(resource.coin.value);
}
```

### Display NFTs

```typescript
const nfts = await aptos.getAccountOwnedTokens({ accountAddress: address });
```

### Transaction Status Hook

```typescript
function useTransactionStatus() {
  const [status, setStatus] = useState<"idle" | "pending" | "success" | "error">("idle");

  const submitTransaction = async (txn) => {
    setStatus("pending");
    try {
      const response = await signAndSubmitTransaction(txn);
      await aptos.waitForTransaction({ transactionHash: response.hash });
      setStatus("success");
    } catch {
      setStatus("error");
    }
  };

  return { status, submitTransaction };
}
```

## Error Handling

```typescript
try {
  await signAndSubmitTransaction(txn);
} catch (error: any) {
  if (error.code === 4001) {
    console.error("User rejected");
  } else if (error.message.includes("INSUFFICIENT_BALANCE")) {
    console.error("Insufficient balance");
  }
}
```

## Best Practices

- Always validate input before building transactions
- Show transaction details before signing
- Handle wallet disconnection gracefully
- Check network before submitting
- Use HTTPS for all API calls
- Never ask for private keys

Overview

This skill is an expert guide for building Aptos dApps and integrating them with modern frontends like React and Next.js. It focuses on wallet connectivity (Petra, Martian, Pontem), wallet adapter patterns, TypeScript SDK usage, transaction lifecycle, and account management. The content is practical, production-ready, and aimed at speeding up integration and reducing common mistakes.

How this skill works

It explains how to set up the Aptos wallet adapter provider, register wallet plugins, and use wallet hooks for connect, disconnect, and sign-and-submit operations. It shows how to initialize and call the @aptos-labs/ts-sdk for reading on-chain data, building, signing and submitting transactions, and waiting for confirmations. The skill includes reusable patterns and small hooks for balance checks, NFT display, transaction status tracking, and common error handling.

When to use it

  • Building a React or Next.js frontend that requires Aptos wallet connectivity
  • Implementing transaction flows that require signing and on-chain confirmation
  • Reading account data, resources, tokens, or calling view functions via TypeScript SDK
  • Creating reusable hooks for transaction status and wallet state
  • Integrating multiple wallet plugins (Petra, Martian, Pontem) in a single app

Best practices

  • Always validate user input before building transactions
  • Show a clear transaction summary and ask for explicit user confirmation before signing
  • Handle wallet connect/disconnect and account changes gracefully in UI
  • Verify network and chain configuration before submitting transactions
  • Use HTTPS endpoints for any API calls and never request private keys

Example use cases

  • Wallet connect button and account management component using wallet-adapter hooks
  • Transfer flow: build transaction, prompt user to sign, wait for confirmation, and update UI
  • Balance and resource viewer that reads CoinStore and tokens using ts-sdk
  • NFT gallery fetching owned tokens and rendering metadata
  • Custom transaction hook that manages pending/success/error states for UX feedback

FAQ

Which wallets are supported and how do I register them?

Supported wallets include Petra, Martian, and Pontem via their adapter plugins. Instantiate each wallet plugin and pass them to AptosWalletAdapterProvider.plugins in your React app.

When should I use wallet signAndSubmitTransaction vs building with the SDK?

Use signAndSubmitTransaction for quick frontend-initiated actions where the wallet handles signing. Use the SDK build/sign/submit flow when you need more control over transaction construction, custom signing workflows, or server-side signing.