The GRAPH and Uniswap v2 Queries

Solidity Programming Language
7 min readMay 18, 2021

--

https://thegraph.com/

The Graph is an indexing protocol for querying networks like Ethereum and AVAX. Anyone can build and publish open APIs, called subgraphs, making data easily accessible.

We go to the Uniswap v2 playground.

https://thegraph.com/explorer/subgraph/uniswap/uniswap-v2

Code 1:

I want to see uniswap v2 general liquidity situation

{

uniswapFactories
{
# factory address
id

# pair info
pairCount

# total volume from genesis to today
totalVolumeUSD
totalVolumeETH

# total liquidity
totalLiquidityUSD
totalLiquidityETH

# transactions
txCount
}
}

Result 1:

uniswapFactories

“id” is uniswap v2 factory, “pairCount” is the number of pairs in uniswap v2, “TotalLiquidityETH” is TVL amount in the uniswap v2 as ETH and “TotalLiquidityUSD” is TVL amount in the uniswap v2 as USD.

https://defipulse.com/uniswap (22.05.2021)

Code 2:

I want to see most traded tokens.

{
tokens (first:5, orderBy:tradeVolumeUSD, orderDirection:desc)

{
# token address
id

# mirrored from the smart contract
symbol
name
decimals

# used for other stats like marketcap
totalSupply

# token specific volume
tradeVolume
tradeVolumeUSD

# transactions
txCount

# liquidity
totalLiquidity

}
}

Result 2:

total liquidity 882.128 ETHER for Wrapped Ether

882.128*2.721$=2.4 b

txCount: 43.171.995 transactions are realized with this token.

Code 3:

I want to see spesific pair

{
pair(id:”0x94b0a3d511b6ecdb17ebf877278ab030acb0a878")

{

reserve0,
reserve1,
reserveUSD,
trackedReserveETH

}

}

Result 3:

“0x94b0a3d511b6ecdb17ebf877278ab030acb0a878” is FEI/ETH pool, “reserve0” is FEI amount, “reserve1” is ETH amount, “reserveUSD” is total liquidity amount of the pool as $ and “trackedreserveETH” is total liquidity amount of the pool as ETHER.

Cross check !

Extra code:

{
pair(id:”0x94b0a3d511b6ecdb17ebf877278ab030acb0a878")
{
token0{name},
token1{name},
reserve0,
reserve1,
reserveUSD,
trackedReserveETH

}
}

Code 4:

I want to see most reserved pairs

{
pairs(first:5, orderBy:trackedReserveETH, orderDirection:desc)
{
# pair address
id

# mirrored from the smart contract
token0{symbol}
token1{symbol}
reserve0
reserve1
totalSupply

# derived liquidity
reserveETH
reserveUSD
trackedReserveETH
# Price in terms of the asset pair
token0Price
token1Price

# lifetime volume stats
volumeToken0
volumeToken1
volumeUSD
txCount

# Fields used to help derived relationship
liquidityProviderCount


}
}

Results 4:

token0 is FEI; token1 is WETH,

reserve0 is amount of FEI, reserve1 is amount of WETH

reserveUSD is all liquidity amount as $

cross check !

https://v2.info.uniswap.org/pair/0x94b0a3d511b6ecdb17ebf877278ab030acb0a878

Little problem, this code can not hide untracked pairs.

Code 5:

I want to see swaps transactions of spesific pool

{
swaps(orderBy: timestamp, orderDirection: desc, where:{ pair: “0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852” } )

{
pair
{
token0 {symbol},
token1 {symbol}
},

amount0In,
amount0Out,
amount1In,
amount1Out,
amountUSD ,
transaction {id}

}
}

Result 5:

https://etherscan.io/tx/0x903f7fe4be16416cd261bbd2e58ee27c21d004f1764d0093336c1b27aedc2704/

Token0 is WETH and Token1 is USDT

User “Swap USDT for ETH”, takes 1.01 WETH and gives 2.358 Tether.

Cross check!

https://etherscan.io/tx/0x903f7fe4be16416cd261bbd2e58ee27c21d004f1764d0093336c1b27aedc2704/

Code 6 :

I want to see mints transactions of spesific pool

{
mints(orderBy: timestamp, orderDirection: desc, where:
{ pair: “0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852” }
)

{
pair {token0 {symbol},token1 {symbol}},
amount0,
amount1,
liquidity
}

}

Results 6:

https://etherscan.io/tx/0x4101900c3fd20e6f103da748de0b83c3860b824810a7fad9ebec05621b4e2f6f/

liquidity” variable is the minted amount as UNI .

Cross check!

https://etherscan.io/tx/0x4101900c3fd20e6f103da748de0b83c3860b824810a7fad9ebec05621b4e2f6f/
https://etherscan.io/tx/0x4101900c3fd20e6f103da748de0b83c3860b824810a7fad9ebec05621b4e2f6f/

Code 7 :

I want to see burns transactions of spesific pool

{
burns(orderBy: timestamp, orderDirection: desc, where:
{ pair: “0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852” }
)

{
pair {token0 {symbol},token1 {symbol}},
amount0,
amount1,
liquidity
}

}

Results 7:

https://etherscan.io/tx/0x4018a063cc5fa5b7b79433fd0afdee2257fa13079b02cad4ea21e7677583125b/

The liquidity is the burned amount as UNI.

Cross check:

https://etherscan.io/tx/0x4018a063cc5fa5b7b79433fd0afdee2257fa13079b02cad4ea21e7677583125b/
https://etherscan.io/tx/0x4018a063cc5fa5b7b79433fd0afdee2257fa13079b02cad4ea21e7677583125b/

Code 8 :

I want to see swaps transactions of spesific user

{

swaps(first:5, where:{to:“0xee1F07F88934C2811E3DcAbdf438d975C3d62cd3"}, orderBy:timestamp,orderDirection:desc)
{

pair
{
token0{name},
token1{name}

},
amount0In,
amount0Out,
amount1In,
amount1Out,
amountUSD,

}
}

Results 8:

https://etherscan.io/address/0xee1f07f88934c2811e3dcabdf438d975c3d62cd3

Code 9:

I want to see mints transactions of spesific user

{
mints(first:5, where:{to:“0x5acedba6c402e2682d312a7b4982eda0ccf2d2e3"}, orderBy:timestamp,orderDirection:desc)
{

pair
{
token0{name},
token1{name}

},
amount0,

amount1,
amountUSD,
liquidity
transaction {
id
}

}
}

Results 9:

https://etherscan.io/tx/0xa187c311191ca0d8f88a6bc1f4ba1a2353dca32ea08e819326ce81dc2e588d76/

Code 10

I want to see burns transactions of spesific user

{
burns (first:1, where:{sender:“0x1ae4e247eb47bf8a2f126b34e67c4df2aab5a024"}, orderBy:timestamp,orderDirection:desc)
{

pair
{
token0{name},
token1{name}

},
amount0,
amount1,
amountUSD
transaction {
id
}

}
}

Result 10:

https://etherscan.io/tx/0x23d6c0400bba01328022c82046fb37ca5d5db7caa71f5ccc96d890b297ed2cf9/

Code 11:

I want to see daily transactions of spesific token

{
tokenDayDatas(orderBy: date, orderDirection: desc,
where: {
token: “0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9”
}
)

{
id
date
priceUSD
totalLiquidityToken
totalLiquidityUSD
totalLiquidityETH
dailyVolumeToken
dailyVolumeUSD
}
}

Result 11:

totalLiquidityUSD: 17.557.960

Aave Token (AAVE) ($357.8)

totalLiquidityToken: 49.071 =(17.557.960/357.8)

ETH= 2.604 USD

totalLiquidityETH: 6.742 = (17.557.960/2.604)

dailyVolumeUSD: 3.654.158

Aave Token (AAVE) ($357.8)

dailyVolumeToken: 10.212=(3.654.158/357.8)

Code 12:

I want to see daily mints transactions of spesific user

{
users(where:{id:”0x9ad4a47e223312b4d8b15d514bd1f7fb7ee6ab8b”})
{
id
}
mints(first: 3, orderBy: timestamp, orderDirection: desc)
{
transaction {
id
timestamp
}
to
liquidity
amount0
amount1
amountUSD
}

}

Results 12:

https://etherscan.io/tx/0xba43fbb894bd34416dec56c81d5af0a9c28775e31c7481f221be59563c74032c

Code 13:

I want to see daily all transactions of spesific user

{
users(where:{id:”0x9ad4a47e223312b4d8b15d514bd1f7fb7ee6ab8b”})
{
id
}
mints(first: 3, orderBy: timestamp, orderDirection: desc)
{
transaction {
id
timestamp

}
to
liquidity
amount0
amount1
amountUSD
}
burns(first: 3, orderBy: timestamp, orderDirection: desc) {
transaction {
id
timestamp
}
to
liquidity
amount0
amount1
amountUSD
}

swaps(first: 3, orderBy: timestamp, orderDirection: desc) {
transaction {
id
timestamp
}
amount0In
amount0Out
amount1In
amount1Out
amountUSD
to
}

}

Result 13:

0x9ad4a47e223312b4d8b15d514bd1f7fb7ee6ab8b
0x9ad4a47e223312b4d8b15d514bd1f7fb7ee6ab8b
0x9ad4a47e223312b4d8b15d514bd1f7fb7ee6ab8b

Note : 13 extra

{
users(where:{id:”0x9ad4a47e223312b4d8b15d514bd1f7fb7ee6ab8b”})
{
id

}
mints(first: 3, orderBy: timestamp, orderDirection: desc)
{
transaction {
id
timestamp

}
pair{token0{symbol},token1{symbol}}
to
liquidity
amount0
amount1
amountUSD
}
burns(first: 3, orderBy: timestamp, orderDirection: desc) {
transaction {
id
timestamp
}
pair{token0{symbol},token1{symbol}}
to
liquidity
amount0
amount1
amountUSD
}

swaps(first: 3, orderBy: timestamp, orderDirection: desc) {
transaction {
id
timestamp
}
pair{token0{symbol},token1{symbol}}
amount0In
amount0Out
amount1In
amount1Out
amountUSD
to
}

}

Code 14:

I want to see daily transactions amount in uniswap

{
uniswapDayDatas(first:50,orderBy:date,orderDirection:desc)

{
id # timestamp rounded to current day by dividing by 86400
date

dailyVolumeETH
dailyVolumeUSD
dailyVolumeUntracked

totalVolumeETH
totalLiquidityETH
totalVolumeUSD # Accumulate at each trade, not just calculated off whatever totalVolume is. making it more accurate as it is a live conversion
totalLiquidityUSD

txCount
}}

Result 14:

Cross Check

Code 15:

I want to see most reserved pools

{
pairs(first: 10, orderBy: trackedReserveETH, orderDirection: desc)
{
id
token0{name},
token1{name}
trackedReserveETH
}
}

Result 15:

Code 16:

I want to see pools daily amounts

{
pairDayDatas(first: 10, orderBy: date, orderDirection: desc,
where: {pairAddress: “0x94b0a3d511b6ecdb17ebf877278ab030acb0a878”})
{
token0{name}
token1{name}
dailyVolumeUSD
reserveUSD
}
}

Result 16:

https://v2.info.uniswap.org/pair/0x94b0a3d511b6ecdb17ebf877278ab030acb0a878

cross check!

https://v2.info.uniswap.org/pair/0x94b0a3d511b6ecdb17ebf877278ab030acb0a878

Code 17:

I want to see hourly pool datas

{
pairHourDatas(first: 10, orderBy:hourStartUnix orderDirection: desc,
where: {pair:”0x94b0a3d511b6ecdb17ebf877278ab030acb0a878"})
{
pair
{

token0{name}
token1{name}
},
hourlyVolumeToken0
hourlyVolumeToken1
hourlyVolumeUSD
reserve0
reserve1
reserveUSD
}
}

Result 17:

Dr. Engin YILMAZ

27.05.2021

Ankara

--

--

Solidity Programming Language

Solidity basics for beginners: Learn the fundamentals of smart contract development and build your first DApp! #Solidity #Foundry #Ethereum #Opcodes #DApps