Chuyển đến nội dung
Skillpedia
API và máy chủ (backend)7.0k · #13687

starknet-js

Skill này làm gì

Skill này là tài liệu tham khảo cho thư viện starknet.js phiên bản 9, dùng để kết nối ứng dụng với blockchain Starknet. Nó dành cho nhà phát triển viết mã front end hoặc back end để đọc hoặc gửi giao dịch trên Starknet.

Khi nào nên dùng

  • ✓Khi bạn đang xây ứng dụng Starknet để đọc dữ liệu hợp đồng hoặc gửi giao dịch.
  • ✓Khi bạn cần kết nối ví hoặc thiết lập luồng paymaster trong ứng dụng Starknet.
  • ✓Khi bạn cần ước tính phí hoặc xử lý giao dịch trong starknet.js.

Liên kết skill

https://github.com/internet-court/internet-court-skill/blob/main/vendored/starknet/starknet-js/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/internet-court/internet-court-skill/blob/main/vendored/starknet/starknet-js/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Bạn có thể chỉ tôi cách đọc số dư từ một hợp đồng Starknet trong ứng dụng TypeScript bằng starknet.js không?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent thiết lập một provider cho mạng Starknet, rồi tạo đối tượng hợp đồng để đọc dữ liệu hoặc đối tượng tài khoản để ký giao dịch. Nó tuân theo cách thư viện tách vai trò chỉ đọc và vai trò ký.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

starknet.js v9.x SDK

Related modules: skills catalog.

When to Use

  • Building Starknet apps with provider, account, contract, wallet, or paymaster flows.

When NOT to Use

  • Cairo contract authoring, deployment-only runbooks, or security audits.

Quick Start

npm install starknet

Minimal setup to read from Starknet:

import { RpcProvider, Contract } from 'starknet';

const provider = await RpcProvider.create({ nodeUrl: 'https://rpc.starknet.lava.build' });
const contract = new Contract(abi, contractAddress, provider);
const result = await contract.get_balance();

Core Architecture

Provider -> Account -> Contract
   |          |          |
Network   Identity   Interaction
  • Provider: Read-only network connection (RpcProvider)
  • Account: Extends Provider with signing and transaction capabilities
  • Contract: Type-safe interface to deployed contracts

Use Provider for read operations, Account for write operations.

Provider Setup

import { RpcProvider } from 'starknet';

// Recommended: Auto-detect RPC spec version
const provider = await RpcProvider.create({
  nodeUrl: 'https://rpc.starknet.lava.build'
});

Networks:

  • Mainnet: https://rpc.starknet.lava.build
  • Sepolia: https://rpc.starknet-testnet.lava.build

Key Methods:

const chainId = await provider.getChainId();
const block = await provider.getBlock('latest');
const nonce = await provider.getNonceForAddress(accountAddress);
await provider.waitForTransaction(txHash);

// Read storage directly
const value = await provider.getStorageAt(contractAddress, storageKey);

Account Management

Account Creation (4 Steps)

Step 1: Compute address

import { hash, ec, encode, CallData } from 'starknet';

// IMPORTANT: `stark.randomAddress()` returns an address-like random felt and is NOT a private key.
// Use a real stark curve private key generator.
const privateKey = '0x' + encode.buf2hex(ec.starkCurve.utils.randomPrivateKey());
const publicKey = ec.starkCurve.getStarkKey(privateKey);

// NOTE: account class hashes are network/account-type dependent.
// Treat this as an example only (verify the correct class hash for your setup).
const classHash = '0x540d7f5ec7ecf317e68d48564934cb99259781b1ee3cedbbc37ec5337f8e688'; // example

const constructorCalldata = CallData.compile({ publicKey });
const address = hash.calculateContractAddressFromHash(publicKey, classHash, constructorCalldata, 0);

Step 2: Fund the address with STRK before deployment.

Step 3: Deploy

import { Account } from 'starknet';

// NOTE: Account constructor signature varies across starknet.js versions.
// If this doesn't typecheck for your version, refer to the official docs.
const account = new Account({ provider, address, signer: privateKey, cairoVersion: '1' });
const { transaction_hash } = await account.deployAccount({
  classHash,
  constructorCalldata,
  addressSalt: publicKey
});
await provider.waitForTransaction(transaction_hash);

Step 4: Use the account for transactions.

Connect to Existing Account

const account = new Account({
  provider,
  address: '0x123...',
  signer: privateKey,
  cairoVersion: '1'  // Optional, auto-detected if omitted
});

Contract Interaction

Connect to Contract

import { Contract } from 'starknet';

const contract = new Contract(abi, contractAddress, provider);  // Read-only
const writeContract = new Contract(abi, contractAddress, account);   // Read-write

Typed Contract (Type-Safe)

// Get full TypeScript autocomplete and type checking from ABI
const typedContract = contract.typedv2(abi);
const balance = await typedContract.balanceOf(userAddress);

Read State

const balance = await contract.get_balance();
const userBalance = await contract.balanceOf(userAddress);

Write (Execute)

const tx = await contract.increase_balance(100);
await provider.waitForTransaction(tx.transaction_hash);

Multicall (Batch Transactions)

import { CallData, cairo } from 'starknet';

const calls = [
  {
    contractAddress: tokenAddress,
    entrypoint: 'approve',
    calldata: CallData.compile({ spender: bridgeAddress, amount: cairo.uint256(1000n) })
  },
  {
    contractAddress: bridgeAddress,
    entrypoint: 'deposit',
    calldata: CallData.compile({ amount: cairo.uint256(1000
Câu hỏi thường gặp

Provider và account khác nhau thế nào?

Provider là quyền truy cập mạng chỉ đọc, còn account có thể ký và gửi giao dịch.

Skill bao gồm những mạng nào?

Skill liệt kê địa chỉ RPC của mainnet Starknet và testnet Sepolia.

Skill có bao gồm viết hợp đồng không?

Không. Viết hợp đồng và kiểm toán bảo mật nằm ngoài skill này.

Chi tiết

Mô tả gốc của tác giả

Reference for building Starknet applications using starknet.js v9.x SDK, including contract interaction, account management, transaction handling, fee estimation, wallet integration, and paymaster flows.

repo: internet-court/internet-court-skill

Độ phổ biến

Sao GitHub
7.0k
Vị trí xếp hạng
#13687
Giấy phép
—
Cập nhật
26/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự