【问题标题】:Uniswap: How to read token price with APIUniswap:如何使用 API 读取代币价格
【发布时间】:2021-02-18 12:34:52
【问题描述】:

在我的 PHP 代码(或 Javascript)中,我想从 Uniswap 读取 erc20(以太坊)代币价格 - 它正在那里交易。我找不到任何 API 调用来返回价格。

我正在寻找此令牌的价格: 处女令牌:0x1381F369D9D5df87a1A04Ed856C9dbc90f5DB2fA

我该怎么做?

【问题讨论】:

标签: php blockchain ethereum erc20


【解决方案1】:

您可以使用GraphQL查询The Graph上的Uniswap data

一种方法是直接查询token

{
  token(id: "0x1381f369d9d5df87a1a04ed856c9dbc90f5db2fa") {
    derivedETH
  }
}

...derivedETH 是 ETH 价格。

另一个是查询pair(通过pair id,或者,在本例中,使用令牌id's):

{
  pairs(where: { token0: "0x1381f369d9d5df87a1a04ed856c9dbc90f5db2fa" token1: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" }) {
    token0Price
    token1Price
  }
}

... 其中token0Pricetoken1Price 是代币的相对价格(VRGN\WETH)。

您可以在sandbox 中使用这些,或者您可能需要client

或者,为了简单起见,您可以直接请求,如下所示:

curl -X POST -H "Content-Type: application/json" -d '{"query": "{ token(id: \"0x1381f369d9d5df87a1a04ed856c9dbc90f5db2fa\") { derivedETH } }"}' https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2

...得到:

{"data":{"token":{"derivedETH":"0.0004465905539042863338157407540331524"}}}

【讨论】:

    【解决方案2】:

    你看https://uniswap.org/docs/v2/API/queries/#pair-data了吗?

    他们的 API 文档似乎展示了如何做到这一点

    【讨论】:

    • 我做到了,但我不明白。他们的文档和 API 是愚蠢的。沙盒示例总是返回错误。
    猜你喜欢
    • 2021-04-15
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    • 2021-11-12
    • 2021-06-28
    • 2019-12-19
    • 2021-07-30
    相关资源
    最近更新 更多