2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Moralis フリー archive node アクセスサービスで QuickSwap の USDC/JPYC 流動性ペア残高の履歴を取得するメモ

Last updated at Posted at 2021-10-03

背景

税金計算とか裁定取引とかで Matic チェーンのアーカイブノードにアクセスしたい...
Infra とか QuickNode の archive お高い... $250/month
(quicknode は 10 回/month まで archive node call 無料ではるが, 少なすぎ)

自身で archive node 建てるのもめんどいし...

Moralis というサービスが出てました.

今の所(2021/10/03) 500 万回/day まで無料とあります. いずれ有料化されるかなとは思います.

使う

エンドポイント変えるだけなので, 他は Infra みたいな感じで使えます.

Python web3 で Uniswap の流動性ペアの残高を取得するメモ(最新ブロックのみ)
https://qiita.com/syoyo/items/d4db214903bd0105247c

を参考にして, QuickSwap の USDC/JPYC 流動性ペア残高の履歴を見てみます.


import json
from decimal import *

from web3.auto import w3
from web3 import Web3,HTTPProvider
#import web3.eth

from eth_account.messages import encode_defunct

web3 = Web3(HTTPProvider('https://speedy-nodes-nyc.moralis.io/<your_id>/polygon/mainnet/archive'))
blockNumber=web3.eth.blockNumber
print(blockNumber)

lp_addr=Web3.toChecksumAddress('0x205995421c72dc223f36bbfad78b66eea72d2677') # QuickSwap USDC/JPYC

# https://polygonscan.com/address/0x205995421c72dc223f36bbfad78b66eea72d2677#code
abi = json.loads(open('abi.json').read())

contract = web3.eth.contract(address=lp_addr, abi=abi) #, bytecode=router_bytecode)

print("decimals", contract.functions.decimals().call())
print("totalSupply", contract.functions.totalSupply().call())
print("token0 addr", contract.functions.token0().call())
print("token1 addr", contract.functions.token1().call())

# Recent one
print("reserves(token0, token1, blocktime): ", 
contract.functions.getReserves().call())

# At specific block #
blocknum=15796396
print("reserves(token0, token1, blocktime): ", contract.functions.getReserves().call(block_identifier=blocknum))

19796932
decimals 18
totalSupply 1965833802783266745
token0 addr 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174
token1 addr 0x6AE7Dfc73E0dDE2aa99ac063DcF7e8A63265108c
reserves(token0, token1, blocktime):  [202515937252, 23942787504421634745737625, 1633266720]
reserves(token0, token1, blocktime):  [23924006905, 3045627439797363538382766, 1623877364]

Voila :tada:

とりあえず blockid 変えて 15796396 https://polygonscan.com/block/15796396 = この記事を書いているときから 108 日前にしても残高変化あるので, アーカイブノードアクセスできているのがわかります!
(帰ってくる blocktime は unix timestamp)

happy 税金計算 and 裁定取引!

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?