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");
 
/** Create a new wallet for a new user. */
export function createWallet() {
  const wallet = ethers.Wallet.createRandom(provider);
  return {
    wallet,
    address: wallet.address,
    seedPhrase: wallet.mnemonic!.phrase, // show to the user once for backup
  };
}
 
/** Restore a wallet from a seed phrase (returning user). */
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",
    });
}
 
/** Create a new wallet for a new user. */
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, // show to the user once for backup
  };
}
 
/** Restore a wallet from a seed phrase (returning user). */
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 作为 gas。在测试网上,可从水龙头请求:

open https://faucet.stable.xyz

粘贴地址并选择按钮即可接收 1 个测试网 USDT0(足够进行数千次原生转账)。对于主网,可从任何受支持的交易所或桥转入 USDT0;参见跨链桥接到 Stable

查询余额

原生 USDT0 使用 18 位小数。原生余额即为支付 gas 所用的余额。

// 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

推荐的后续步骤