使用 Python 腳本在 AAVE 上檢索 Matic 獎勵率
我正在嘗試建構一個 Python 腳本,以在 Polygon 區塊鏈上以程式方式檢索不同的 AAVE 費率(借款、貸款和獎勵)(對於乙太坊來說將非常相似)。我正在使用 Python。
到目前為止,我設法檢索了 AAVE 上可用貨幣(DAI、USDC、MATIC、WETH、WBTC、USDT)的借貸利率。請參閱下面的程式碼。我還想自動檢索 WMATIC APR 獎勵費率。我還沒有找到辦法做到這一點。有人知道該怎麼做嗎?
為了清楚起見,我的意思是檢索此處突出顯示的數字:
在以下連結https://docs.aave.com/developers/guides/apy-and-apr中,他們提供了一種計算“percentDepositAPR”和“percentBorrowAPR”的方法,但是當我實現它時它給出了明顯錯誤的值,並且所以我認為這與其他事情有關,而不是 MATIC 代幣獎勵分配。
這裡還有有趣的資訊:
Polygon 分兩個階段發放 MATIC 獎勵:從今天到 6 月 14 日,佔總供應量的 0.5%,在 2022 年 6 月 14 日至 4 月 13 日期間再發放 0.5%。
但這還不足以計算獎勵率。
提前致謝。
import json import requests import urllib3 from gql import gql, Client from gql.transport.requests import RequestsHTTPTransport token_ticker = 'USDC' reward_token_ticker = 'WMATIC' URL_API = 'https://api.thegraph.com/subgraphs/name/aave/aave-v2-matic' query = gql(''' query { reserves (where: { usageAsCollateralEnabled: true }) { id name price { id priceInEth } liquidityRate variableBorrowRate stableBorrowRate aEmissionPerSecond vEmissionPerSecond decimals totalATokenSupply totalCurrentVariableDebt symbol } } ''') sample_transport=RequestsHTTPTransport( url=URL_API, verify=True, retries=3, ) client = Client( transport=sample_transport ) response = client.execute(query) for token in response['reserves']: if token_ticker in token['symbol']: variableBorrowRate = float(token['variableBorrowRate']) liquidityRate = float(token['liquidityRate']) TOKEN_DECIMALS = float(token['decimals']) TOKEN_PRICE_ETH = float(token['price']['priceInEth'])/10.0**18 aEmissionPerSecond = float(token['aEmissionPerSecond']) vEmissionPerSecond = float(token['vEmissionPerSecond']) totalCurrentVariableDebt = float(token['totalCurrentVariableDebt']) totalATokenSupply = float(token['totalATokenSupply']) if reward_token_ticker in token['symbol']: REWARD_PRICE_ETH = float(token['price']['priceInEth'])/10.0**18 REWARD_DECIMALS = float(token['decimals']) RAY = 10.0**27 WAD = 10.0**18 SECONDS_PER_YEAR = 31556926.0 # deposit and borrow calculation percentDepositAPY = 100.0 * liquidityRate/RAY percentVariableBorrowAPY = 100.0 * variableBorrowRate/RAY percentStableBorrowAPY = 100.0 * variableBorrowRate/RAY print(f"{token_ticker} deposit APY: {percentDepositAPY:.2f}%") print(f"{token_ticker} borrow APY: {percentVariableBorrowAPY:.2f}%")
**編輯:**下面是嘗試計算 WMATIC 獎勵 APR。但結果值與 AAVE 網站不一致,除了 WMATIC 作為 token_ticker。當我將 WMATIC 設置為主要代幣和獎勵時,我得到了我需要的值。但我仍然無法得到它的其餘部分:USDC、DAI 等……
percentDepositAPR = 100 * (aEmissionPerSecond*SECONDS_PER_YEAR * REWARD_PRICE_ETH * TOKEN_DECIMALS) / (totalATokenSupply * TOKEN_PRICE_ETH * REWARD_DECIMALS) print(f"{percentDepositAPR:.2f}") percentBorrowAPR = 100 * (vEmissionPerSecond*SECONDS_PER_YEAR * REWARD_PRICE_ETH * TOKEN_DECIMALS) / (totalCurrentVariableDebt * TOKEN_PRICE_ETH * REWARD_DECIMALS) print(f"{percentBorrowAPR:.2f}")
所以我解決了這個問題。有兩個錯誤:
- 對於“REWARD_DECIMAL”和“TOKEN_DECIMAL”,我應該使用 10 的數字次方,而不是數字本身。
- 由於某種原因,從呼叫 API api.thegraph.com 中檢索到的 ETH 價格是錯誤的。所以我通過呼叫 Binance API 來獲取 ETH 的價格。
Bellow 是給出正確結果的 Python 程式碼:
import json import requests import urllib3 from gql import gql, Client from gql.transport.requests import RequestsHTTPTransport token_ticker = 'USDC' reward_token_ticker = 'WMATIC' RAY = 10.0**27 WAD = 10.0**18 SECONDS_PER_YEAR = 31556926.0 ################################################# def get_token_price_in_eth(token_ticker): if token_ticker!='USDT': while True: r = requests.get( f'https://api.binance.com/api/v3/ticker/price?symbol={token_ticker}USDT') if r.status_code == 200: break data = r.json() token_in_usdt= float(data['price']) else: token_in_usdt = 1.0 while True: r = requests.get( 'https://api.binance.com/api/v3/ticker/price?symbol=ETHUSDT') if r.status_code == 200: break data = r.json() eth_in_usdt= float(data['price']) return token_in_usdt/eth_in_usdt ################################################# URL_API = 'https://api.thegraph.com/subgraphs/name/aave/aave-v2-matic' query = gql(''' query { reserves (where: { usageAsCollateralEnabled: true }) { id name price { id priceInEth } liquidityRate variableBorrowRate stableBorrowRate aEmissionPerSecond vEmissionPerSecond decimals totalATokenSupply totalCurrentVariableDebt symbol } } ''') sample_transport=RequestsHTTPTransport( url=URL_API, verify=True, retries=3, ) client = Client( transport=sample_transport ) response = client.execute(query) for token in response['reserves']: if token_ticker in token['symbol']: variableBorrowRate = float(token['variableBorrowRate']) liquidityRate = float(token['liquidityRate']) TOKEN_DECIMALS = 10**float(token['decimals']) aEmissionPerSecond = float(token['aEmissionPerSecond']) vEmissionPerSecond = float(token['vEmissionPerSecond']) totalCurrentVariableDebt = float(token['totalCurrentVariableDebt']) totalATokenSupply = float(token['totalATokenSupply']) if reward_token_ticker in token['symbol']: REWARD_DECIMALS = 10**float(token['decimals']) REWARD_PRICE_ETH = get_token_price_in_eth('MATIC') TOKEN_PRICE_ETH = get_token_price_in_eth(token_ticker.replace('W', '')) # deposit and borrow calculation percentDepositAPY = 100.0 * liquidityRate/RAY percentVariableBorrowAPY = 100.0 * variableBorrowRate/RAY percentStableBorrowAPY = 100.0 * variableBorrowRate/RAY print(f"{token_ticker} deposit APY: {percentDepositAPY:.2f}%") print(f"{token_ticker} borrow APY: {percentVariableBorrowAPY:.2f}%") percentDepositAPR = 100 * (aEmissionPerSecond*SECONDS_PER_YEAR * REWARD_PRICE_ETH * TOKEN_DECIMALS) / (totalATokenSupply * TOKEN_PRICE_ETH * REWARD_DECIMALS) percentBorrowAPR = 100 * (vEmissionPerSecond*SECONDS_PER_YEAR * REWARD_PRICE_ETH * TOKEN_DECIMALS) / (totalCurrentVariableDebt * TOKEN_PRICE_ETH * REWARD_DECIMALS) print(f"{token_ticker} WMATIC reward deposit APR: {percentDepositAPR:.2f}%") print(f"{token_ticker} WMATIC reward borrow APR: {percentBorrowAPR:.2f}%")
它給出了以下結果(2021 年 9 月 12 日 @ 21:05 UTC):
USDC deposit APY: 3.18% USDC borrow APY: 3.96% USDC WMATIC reward deposit APR: 1.60% USDC WMATIC reward borrow APR: 2.71%
這與 AAVE 應用程序網站上給出的內容足夠接近: