Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

创建钱包

Stable 钱包是符合以太坊标准的密钥对。任何生成 EVM 账户的钱包库都可以在 Stable 上运行,无需修改。本指南介绍了两种方法:对于大多数应用程序,可以使用 ethers.js;对于需要为代理和支付提供一站式自托管层的集成,可以使用 Tether 的 WDK(钱包开发工具包)

前提条件

  • Node.js 20 或更高版本。

选项 1: ethers.js

安装库并生成密钥对。

npm install ethers
// wallet.ts
import { ethers } from "ethers";
 
const provider = new ethers.JsonRpcProvider("https://rpc.testnet.stable.xyz");
 
/** 为新用户创建新钱包。 */
export function createWallet() {
  const wallet = ethers.Wallet.createRandom(provider);
  return {
    wallet,
    address: wallet.address,
    seedPhrase: wallet.mnemonic!.phrase, // 一次性向用户显示以便备份
  };
}
 
/** 从助记词恢复钱包(回访用户)。 */
export function restoreWallet(seedPhrase: string) {
  const wallet = ethers.Wallet.fromPhrase(seedPhrase, provider);
  return { wallet, address: wallet.address };
}
 
if (import.meta.url === `file://${process.argv[1]}`) {
  const { address, seedPhrase } = createWallet();
  console.log("Address:    ", address);
  console.log("Seed phrase:", seedPhrase);
}
npx tsx wallet.ts
Address:     0xAlice...1234
Seed phrase: liberty shoot ... (12 words)

选项 2: Tether WDK

WDK 将密钥派生、签名和交易提交包装在一个接口中。当你希望自托管而无需重新实现常见的账户流时,这是一个正确的选择,并且它直接与 x402 集成,用于代理支付。

npm install @tetherto/wdk @tetherto/wdk-wallet-evm
// wallet-wdk.ts
import WDK from "@tetherto/wdk";
import WalletManagerEvm from "@tetherto/wdk-wallet-evm";
 
function initWdk(seedPhrase: string) {
  return new WDK(seedPhrase)
    .registerWallet("stable", WalletManagerEvm, {
      provider: "https://rpc.testnet.stable.xyz",
    });
}
 
/** 为新用户创建新钱包。 */
export async function createWallet() {
  const seedPhrase = WDK.getRandomSeedPhrase();
  const wdk = initWdk(seedPhrase);
  const account = await wdk.getAccount("stable", 0);
  return {
    account,
    address: await account.getAddress(),
    seedPhrase, // 一次性向用户显示以便备份
  };
}
 
/** 从助记词恢复钱包(回访用户)。 */
export async function restoreWallet(seedPhrase: string) {
  const wdk = initWdk(seedPhrase);
  const account = await wdk.getAccount("stable", 0);
  return { account, address: await account.getAddress() };
}
npx tsx wallet-wdk.ts
Address:     0xAlice...1234
Seed phrase: liberty shoot ... (12 words)

充值钱包

在钱包可以进行交易之前,它需要 USDT0 作为燃料费。在测试网上,从水龙头请求:

open https://faucet.stable.xyz

粘贴地址并选择按钮以接收 1 个测试网 USDT0(足以用于数千次原生转账)。对于主网,从任何受支持的交易所或网桥发送 USDT0;请参阅将 USDT0 桥接到 Stable

检查余额

原生 USDT0 使用 18 位小数。原生余额是支付燃料费的余额。

// balance.ts
import { ethers } from "ethers";
 
const provider = new ethers.JsonRpcProvider("https://rpc.testnet.stable.xyz");
const balance = await provider.getBalance("0xYourAddress");
console.log("Balance:", ethers.formatEther(balance), "USDT0");
npx tsx balance.ts
Balance: 1.0 USDT0

接下来推荐