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

viem과 함께 SDK 사용하기

@stablechain/sdk는 viem 기반으로 만들어졌습니다. createStable은 세 가지 서명 모드를 지원하며, 코드가 실행되는 위치에 따라 하나를 선택합니다: 프라이빗 키를 사용하는 서버 측, 사용자 지갑을 사용하는 브라우저 측, 또는 이미 구성한 WalletClient(예: wagmi 앱에서)를 사용하는 방식입니다.

이 가이드에서는 각 모드를 처음부터 끝까지 보여줍니다.

서버 측: 프라이빗 키 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를 구성하고 프로바이더에서 서명자 주소를 읽어옵니다.

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 또는 커스텀 서명자에서), 직접 전달하세요. 이는 accounttransport보다 우선합니다.

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사용자가 MetaMask 또는 wagmi 없는 커스텀 플로우로 서명하는 브라우저 앱.
walletClient이미 구성된 WalletClient가 있는 경우(wagmi, RainbowKit, ConnectKit).

다음 권장 사항