viem과 함께 SDK 사용하기
@stablechain/sdk는 viem 위에 구축되었습니다. createStable은 세 가지 서명 모드를 허용하며, 코드가 실행되는 위치(서버 측에서 개인 키 사용, 브라우저 측에서 사용자 지갑 사용 또는 이미 구성한 WalletClient 사용)에 따라 하나를 선택합니다.
이 가이드에서는 각 모드를 처음부터 끝까지 보여줍니다.
서버 측: 개인 키 Account
viem의 privateKeyToAccount를 사용하여 백엔드에 저장된 개인 키로 서명합니다.
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.Mainnet,
account,
});
const { txHash } = await stable.transfer({
from: account.address,
to: "0xRecipient",
amount: 5,
});
console.log(txHash);0x8f3a...2d41브라우저 측: 지갑의 Transport
custom(window.ethereum) (또는 EIP-1193 제공자)을 transport로 전달합니다. SDK는 트랜스포트에서 WalletClient를 구축하고 transfer에 전달한 from 주소로 서명합니다.
import { createStable, Network } from "@stablechain/sdk";
import { custom } from "viem";
const stable = createStable({
network: Network.Mainnet,
transport: custom(window.ethereum),
});
const [from] = await window.ethereum.request({ method: "eth_requestAccounts" });
const { txHash } = await stable.transfer({
from,
to: "0xRecipient",
amount: 5,
});0x8f3a...2d41자신만의 WalletClient 가져오기
이미 WalletClient가 있는 경우(예: wagmi 또는 사용자 지정 서명자), 이를 직접 전달합니다. 이는 account 및 transport보다 우선합니다.
import { createStable, Network } from "@stablechain/sdk";
import { createWalletClient, custom } from "viem";
import { stable as stableChain } from "viem/chains";
const walletClient = createWalletClient({
chain: stableChain,
transport: custom(window.ethereum),
});
const [from] = await walletClient.requestAddresses();
const stable = createStable({
network: Network.Mainnet,
walletClient,
});
const { txHash } = await stable.transfer({ from, to: "0xRecipient", amount: 5 });0x8f3a...2d41모드 선택
| 모드 | 사용 시점 |
|---|---|
account | 백엔드 서비스, 스크립트, 에이전트, 키를 보유하고 있는 모든 곳. |
transport | 명시적인 from을 사용하여 transfer만 호출하는 브라우저 앱. |
walletClient | 이미 구성된 WalletClient가 있는 경우(wagmi, RainbowKit, ConnectKit). |
다음 권장 사항
- wagmi와 함께 사용: wagmi 훅을 통해 SDK를 React 앱에 연결합니다.
- SDK 참조: 모든 구성 필드, 메서드, 열거형 및 오류 클래스.
- SDK 빠른 시작: 테스트넷에서 첫 번째 전송, 브릿지 및 스왑을 실행합니다.

