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

SDK 快速入门

你将安装 @stablechain/sdk,使用私钥创建客户端,在 Stable 测试网上发送 USDT0 转账,并获取桥接和互换报价。总耗时:大约五分钟。

前提条件

1. 安装

mkdir stable-sdk-quickstart && cd stable-sdk-quickstart
npm init -y && npm install @stablechain/sdk viem
added 2 packages, audited 3 packages in 2s

保存你的测试密钥:

echo "PRIVATE_KEY=0xYOUR_TEST_KEY" > .env

2. 创建客户端

创建 index.ts

import "dotenv/config";
import { createStable, Network } from "@stablechain/sdk";
import { privateKeyToAccount } from "viem/accounts";
 
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
 
const stable = createStable({
  network: Network.Testnet,
  account,
});
 
console.log("Signer:", account.address);
Signer: 0xYourAddress

createStable 接受三种签名模式:account(服务器端,如上所示)、transport(通过 custom(window.ethereum) 进行浏览器钱包签名)或 walletClient(预构建的 viem WalletClient)。有关所有三种模式的详细信息,请参阅将 SDK 与 viem 结合使用

3. 发送 USDT0 转账

追加到 index.ts

const { txHash } = await stable.transfer({
  from: account.address,
  to: "0x000000000000000000000000000000000000dEaD",
  amount: 0.001,
});
 
console.log("Transfer:", txHash);

运行:

npx tsx index.ts
Signer: 0xYourAddress
Transfer: 0x8f3a...2d41

testnet explorer 上打开哈希以确认。

4. 报价桥接

将 USDT0 从 Ethereum Sepolia 桥接到 Stable Testnet。quoteBridge 是一个只读调用,无需签名和 Gas 费用:

import { Chain } from "@stablechain/sdk";
 
const bridgeQuote = await stable.quoteBridge({
  fromChain: Chain.Sepolia,
  toChain: Chain.StableTestnet,
  fromToken: "0xc4DCC311c028e341fd8602D8eB89c5de94625927",
  toToken: "0x78Cf24370174180738C5B8E352B6D14c83a6c9A9",
  amount: 1,
});
 
console.log("Bridge quote:", bridgeQuote);
Bridge quote: { toAmount: 0.999812 }

将报价传递给 stable.bridge({ ...params, quote }) 以执行。SDK 会为 USDT0 → USDT0 路由选择 LayerZero,并为其他所有路由选择 LI.FI。

5. 报价互换

互换在 Stable 上通过 LI.FI 运行。报价返回预期输出和预构建的交易:

const swapQuote = await stable.quoteSwap({
  fromToken: "0x8a2B28364102Bea189D99A475C494330Ef2bDD0B",
  toToken: "0x78Cf24370174180738C5B8E352B6D14c83a6c9A9",
  amount: 1,
  fromDecimals: 6,
});
 
console.log("You'll receive:", swapQuote.toAmount, "USDT0");
You'll receive: 0.998 USDT0

调用 stable.swap({ ...params, quote: swapQuote }) 以执行。ERC-20 源的批准是在内部处理的。

接下来推荐