简体   繁体   English

Uniswap:如何使用 API 读取代币价格

[英]Uniswap: How to read token price with API

In my PHP code (or Javascript) I would like to read erc20 (Ethereum) token price from Uniswap - it is trading there.在我的 PHP 代码(或 Javascript)中,我想从 Uniswap 读取 erc20(以太坊)令牌价格 - 它在那里交易。 I cannot find any API call to return me the price.我找不到任何 API 调用来返回价格。

I am looking to get price for this token: VIRGIN TOKEN: 0x1381F369D9D5df87a1A04Ed856C9dbc90f5DB2fA我希望获得此代币的价格:VIRGIN TOKEN: 0x1381F369D9D5df87a1A04Ed856C9dbc90f5DB2fA

How can I do it?我该怎么做?

You can query Uniswap data on The Graph using GraphQL .您可以使用GraphQLThe Graph上查询Uniswap 数据

One way is to query token directly:一种方法是直接查询token

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

... where derivedETH is ETH price. ...其中derivedETH ETH是ETH价格。

Another is to query pair (by pair id or, in this example, using token id 's):另一个是查询对(按对id或在本例中使用令牌id ):

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

... where token0Price and token1Price are prices of tokens relative to each other (VRGN\\WETH). ... 其中token0Pricetoken1Price是代币相对于彼此的价格 (VRGN\\WETH)。

You can play with these in sandbox or you might need a client .您可以在沙箱中使用这些,或者您可能需要一个客户端

Alternatively, to keep things simple, you can do request directly, like this:或者,为了简单起见,您可以直接进行请求,如下所示:

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

... to get: ... 要得到:

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

Did you look at https://uniswap.org/docs/v2/API/queries/#pair-data ?你看过https://uniswap.org/docs/v2/API/queries/#pair-data吗?

Their API documentation seems to show how to do that他们的 API 文档似乎展示了如何做到这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM