このページの翻訳は LLM によって生成されています。誤りに気づいた場合は GitHub で issue を開いてお知らせください。
オラクルAPIリファレンス
トークン価格フィードとメタデータを取得・購読します。
IDでトークンの詳細を取得します。
シグネチャ:
token(id: Int!): Token!パラメーター:
id:整数のトークンID。
戻り値: Token。
例:
query GetToken { oracle { token(id: 1) { id symbol name logoUrl tradingViewSymbol permissionGroup } }}用途:
- メタデータ取得。
- トークン情報表示。
- IDからシンボルを取得。
tokens
Section titled “tokens”フィルターと並べ替え付きの全トークン一覧。
シグネチャ:
tokens( where: TokensFilter limit: Int order_by: TokensOrder order_desc: Boolean): [Token!]!パラメーター:
where:任意フィルター。name:名前の文字列一致。permissionGroup:権限グループ。
limit:最大件数。order_by:id、name、permission_group。order_desc:降順。
戻り値: Token の配列。
例:全トークン
query ListAllTokens { oracle { tokens(order_by: name) { id symbol name logoUrl tradingViewSymbol } }}例:名前で検索
query SearchToken($searchTerm: String!) { oracle { tokens(where: { name: $searchTerm }) { id symbol name } }}例:権限グループで絞り込み
query GetPermittedTokens { oracle { tokens(where: { permissionGroup: 1 }) { id symbol name } }}用途:
- トークン選択UI。
- 名前検索。
- 対応資産一覧。
- 権限による絞り込み。
tokenPricesUsdクエリ
Section titled “tokenPricesUsdクエリ”現在のUSD価格を取得します。
シグネチャ:
tokenPricesUsd( where: TokenPriceUsdFilter limit: Int order_by: TokenPriceUsdOrder order_desc: Boolean): [TokenPricesUsd!]!パラメーター:
where:任意フィルター。tokenId:トークンID。
limit:最大件数。order_by:oracle_token_id。order_desc:降順。
戻り値: TokenPricesUsd の配列。
例:全価格
query GetAllPrices { oracle { tokenPricesUsd { token { id symbol name } priceUsd lastUpdatedBlock { block block_ts } } }}例:特定トークンの価格
query GetBTCPrice { oracle { tokenPricesUsd(where: { tokenId: 1 }) { token { symbol } priceUsd lastUpdatedBlock { block block_ts } } }}例:複数の価格
query GetPrices { btc: oracle { tokenPricesUsd(where: { tokenId: 1 }) { priceUsd lastUpdatedBlock { block_ts } } } eth: oracle { tokenPricesUsd(where: { tokenId: 2 }) { priceUsd lastUpdatedBlock { block_ts } } }}主なフィールド:
priceUsd:現在のUSD価格、浮動小数点。lastUpdatedBlock:直近更新のブロック情報。
用途:
- 価格表示。
- ポジション価値計算。
- 価格変化監視。
- 価格鮮度確認。
サブスクリプション
Section titled “サブスクリプション”リアルタイム価格を購読します。
tokenPricesUsd
Section titled “tokenPricesUsd”特定・全トークンの価格更新。
シグネチャ:
subscription { tokenPricesUsd(where: SubTokenPriceUsdFilter): [TokenPricesUsd!]!}例:全価格を購読
subscription WatchAllPrices { tokenPricesUsd { token { id symbol } priceUsd lastUpdatedBlock { block block_ts } }}例:特定トークンを購読
subscription WatchBTCPrice { tokenPricesUsd(where: { tokenId: 1 }) { token { symbol } priceUsd lastUpdatedBlock { block_ts } }}用途:
- リアルタイム価格ティッカー。
- 取引ダッシュボード。
- 価格通知。
- チャート更新。
userBalances
Section titled “userBalances”全トークンの利用者残高更新を購読します。
シグネチャ:
subscription { userBalances(where: SubUserBalancesFilter!): [Balance!]!}例:
subscription WatchUserBalances($user: String!) { userBalances(where: { user: $user }) { amount token_info { symbol decimals bank_denom erc20_contract_address logo name type } }}用途:
- ウォレット残高追跡。
- アカウント変更監視。
- ポートフォリオ更新。
- 利用可能資金の表示。
型リファレンス
Section titled “型リファレンス”type Token { id: Int! symbol: String! name: String! logoUrl: String tradingViewSymbol: String permissionGroup: Int!}フィールド詳細:
id:固有ID。symbol:BTC、ETHなどのシンボル。name:Bitcoin、Ethereumなど正式名。logoUrl:ロゴ画像URL。tradingViewSymbol:TradingViewのシンボル。permissionGroup:アクセス・取引権限。
TokenPricesUsd
Section titled “TokenPricesUsd”type TokenPricesUsd { token: Token priceUsd: Float! lastUpdatedBlock: Block!}フィールド詳細:
token:関連するトークン情報。priceUsd:現在のUSD価格。lastUpdatedBlock:直近更新。block:ブロック高。block_ts:RFC3339時刻。
Balance
Section titled “Balance”type Balance { amount: String! token_info: TokenInfo!}TokenInfo
Section titled “TokenInfo”残高用の詳しいトークン情報。
type TokenInfo { bank_denom: String! symbol: String! name: String! decimals: Int! logo: String type: TokenType! erc20_contract_address: String!}TokenType列挙型:
bank:Cosmos SDKネイティブコイン。erc20:EVMのERC20。
必ず直近の更新時刻を確認してください。
const now = Date.now()const lastUpdate = new Date(lastUpdatedBlock.block_ts).getTime()const ageSeconds = (now - lastUpdate) / 1000
if (ageSeconds > 60) { console.warn('Price may be stale')}価格の表示形式
Section titled “価格の表示形式”// Format price with appropriate decimalsconst formatPrice = (priceUsd) => { if (priceUsd >= 1000) { return priceUsd.toFixed(2) } else if (priceUsd >= 1) { return priceUsd.toFixed(4) } else { return priceUsd.toFixed(6) }}ポジション価値の計算
Section titled “ポジション価値の計算”// Calculate position value in USDconst positionValueUsd = (collateralAmount, priceUsd, decimals) => { const actualAmount = collateralAmount / (10 ** decimals) return actualAmount * priceUsd}// Convert balance amount string to numberconst getBalanceValue = (balance) => { const amount = parseFloat(balance.amount) return amount / (10 ** balance.token_info.decimals)}価格のキャッシュ
Section titled “価格のキャッシュ”// Cache prices for short periods to reduce queriesconst priceCache = new Map()const CACHE_TTL = 5000 // 5 seconds
const getCachedPrice = async (tokenId) => { const cached = priceCache.get(tokenId) if (cached && Date.now() - cached.timestamp < CACHE_TTL) { return cached.price }
const price = await fetchPrice(tokenId) priceCache.set(tokenId, { price, timestamp: Date.now() }) return price}複数トークンの扱い
Section titled “複数トークンの扱い”// Batch query multiple prices efficientlyconst query = ` query GetMultiplePrices($tokenIds: [Int!]!) { oracle { prices: tokenPricesUsd(limit: 100) { token { id symbol } priceUsd } } }`
// Filter client-side for needed tokensconst relevantPrices = data.oracle.prices.filter(p => tokenIds.includes(p.token.id))// Manage price subscription lifecyclelet priceSubscription
const subscribeToPrices = (tokenIds) => { // Unsubscribe from previous if (priceSubscription) { priceSubscription.unsubscribe() }
// Subscribe to new set priceSubscription = client.subscribe({ query: PRICE_SUBSCRIPTION, variables: { tokenIds } }).subscribe({ next: (data) => updatePrices(data), error: (err) => console.error(err) })}