Skip to content

Commit

Permalink
[fiat rates] add CNY, JPY, KRW and AUD rates (#89)
Browse files Browse the repository at this point in the history
* Refactor rewards calculation

* [fiat rates] add CNY, JPY, KRW and AUD rates

---------

Co-authored-by: Dmitri Tsumak <tsumak.dmitri@gmail.com>
  • Loading branch information
mike-diamond and tsudmi authored Dec 18, 2024
1 parent a1a68ed commit c562842
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "v3-subgraph",
"version": "3.0.0",
"version": "3.0.1",
"description": "Subgraph for the StakeWise Protocol",
"repository": "https://github.com/stakewise/v3-subgraph",
"license": "AGPL-3.0-only",
Expand Down
4 changes: 4 additions & 0 deletions src/config/chiado.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"usdcUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"eurUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"gbpUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"cnyUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"jpyUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"krwUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"audUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"assetToken": "0x0000000000000000000000000000000000000000",
"swiseAssetUniPool": "0x0000000000000000000000000000000000000000",
"usdcToken": "0x0000000000000000000000000000000000000000",
Expand Down
4 changes: 4 additions & 0 deletions src/config/gnosis.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"usdcUsdPriceFeed": "0x26C31ac71010aF62E6B486D1132E266D6298857D",
"eurUsdPriceFeed": "0xab70BCB260073d036d1660201e9d5405F5829b7a",
"gbpUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"cnyUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"jpyUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"krwUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"audUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"assetToken": "0x0000000000000000000000000000000000000000",
"swiseAssetUniPool": "0x0000000000000000000000000000000000000000",
"usdcToken": "0xDDAfbb505ad214D7b80b1f830fcCc89B60fb7A83",
Expand Down
4 changes: 4 additions & 0 deletions src/config/holesky.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"usdcUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"eurUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"gbpUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"cnyUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"jpyUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"krwUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"audUsdPriceFeed": "0x0000000000000000000000000000000000000000",
"assetToken": "0x94373a4919B3240D86eA41593D5eBa789FEF3848",
"swiseAssetUniPool": "0x0000000000000000000000000000000000000000",
"usdcToken": "0x0000000000000000000000000000000000000000",
Expand Down
4 changes: 4 additions & 0 deletions src/config/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"usdcUsdPriceFeed": "0x8fFfFfd4AfB6115b954Bd326cbe7B4BA576818f6",
"eurUsdPriceFeed": "0xb49f677943BC038e9857d61E7d053CaA2C1734C1",
"gbpUsdPriceFeed": "0x5c0Ab2d9b5a7ed9f470386e82BB36A3613cDd4b5",
"cnyUsdPriceFeed": "0xeF8A4aF35cd47424672E3C590aBD37FBB7A7759a",
"jpyUsdPriceFeed": "0xBcE206caE7f0ec07b545EddE332A47C2F75bbeb3",
"krwUsdPriceFeed": "0x01435677FB11763550905594A16B645847C1d0F3",
"audUsdPriceFeed": "0x77F9710E7d0A19669A13c055F62cd80d313dF022",
"assetToken": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"swiseAssetUniPool": "0x2aF41D838763E3c5cad6AdC111af9c5dA19B9AFa",
"usdcToken": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
Expand Down
4 changes: 4 additions & 0 deletions src/entities/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export function createOrLoadNetwork(): Network {
network.usdcUsdRate = BigDecimal.zero()
network.usdToEurRate = BigDecimal.zero()
network.usdToGbpRate = BigDecimal.zero()
network.usdToCnyRate = BigDecimal.zero()
network.usdToJpyRate = BigDecimal.zero()
network.usdToKrwRate = BigDecimal.zero()
network.usdToAudRate = BigDecimal.zero()
network.save()
}

Expand Down
54 changes: 53 additions & 1 deletion src/mappings/exchangeRates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {
ASSETS_USD_PRICE_FEED,
EUR_USD_PRICE_FEED,
GBP_USD_PRICE_FEED,
CNY_USD_PRICE_FEED,
JPY_USD_PRICE_FEED,
KRW_USD_PRICE_FEED,
AUD_USD_PRICE_FEED,
DAI_USD_PRICE_FEED,
USDC_USD_PRICE_FEED,
SWISE_ASSET_UNI_POOL,
Expand All @@ -20,6 +24,10 @@ export function handleExchangeRates(block: ethereum.Block): void {
let usdcUsdRate = BigDecimal.zero()
let eurToUsdRate = BigDecimal.zero()
let gbpToUsdRate = BigDecimal.zero()
let cnyToUsdRate = BigDecimal.zero()
let jpyToUsdRate = BigDecimal.zero()
let krwToUsdRate = BigDecimal.zero()
let audToUsdRate = BigDecimal.zero()
let response: BigInt
let priceFeedContract: PriceFeedContract

Expand All @@ -44,6 +52,34 @@ export function handleExchangeRates(block: ethereum.Block): void {
gbpToUsdRate = new BigDecimal(response).div(decimals)
}

const cnyUsdPriceFeed = Address.fromString(CNY_USD_PRICE_FEED)
if (cnyUsdPriceFeed.notEqual(Address.zero())) {
priceFeedContract = PriceFeedContract.bind(cnyUsdPriceFeed)
response = priceFeedContract.latestAnswer()
cnyToUsdRate = new BigDecimal(response).div(decimals)
}

const jpyUsdPriceFeed = Address.fromString(JPY_USD_PRICE_FEED)
if (jpyUsdPriceFeed.notEqual(Address.zero())) {
priceFeedContract = PriceFeedContract.bind(jpyUsdPriceFeed)
response = priceFeedContract.latestAnswer()
jpyToUsdRate = new BigDecimal(response).div(decimals)
}

const krwUsdPriceFeed = Address.fromString(KRW_USD_PRICE_FEED)
if (krwUsdPriceFeed.notEqual(Address.zero())) {
priceFeedContract = PriceFeedContract.bind(krwUsdPriceFeed)
response = priceFeedContract.latestAnswer()
krwToUsdRate = new BigDecimal(response).div(decimals)
}

const audUsdPriceFeed = Address.fromString(AUD_USD_PRICE_FEED)
if (audUsdPriceFeed.notEqual(Address.zero())) {
priceFeedContract = PriceFeedContract.bind(audUsdPriceFeed)
response = priceFeedContract.latestAnswer()
audToUsdRate = new BigDecimal(response).div(decimals)
}

const daiUsdPriceFeed = Address.fromString(DAI_USD_PRICE_FEED)
if (daiUsdPriceFeed.notEqual(Address.zero())) {
priceFeedContract = PriceFeedContract.bind(daiUsdPriceFeed)
Expand Down Expand Up @@ -71,12 +107,20 @@ export function handleExchangeRates(block: ethereum.Block): void {
const one = BigDecimal.fromString('1')
const usdToEurRate = eurToUsdRate.gt(zero) ? one.div(eurToUsdRate) : zero
const usdToGbpRate = gbpToUsdRate.gt(zero) ? one.div(gbpToUsdRate) : zero
const usdToCnyRate = cnyToUsdRate.gt(zero) ? one.div(cnyToUsdRate) : zero
const usdToJpyRate = jpyToUsdRate.gt(zero) ? one.div(jpyToUsdRate) : zero
const usdToKrwRate = krwToUsdRate.gt(zero) ? one.div(krwToUsdRate) : zero
const usdToAudRate = audToUsdRate.gt(zero) ? one.div(audToUsdRate) : zero

const network = loadNetwork()!
network.assetsUsdRate = assetsUsdRate
network.swiseUsdRate = swiseUsdRate
network.usdToEurRate = usdToEurRate
network.usdToGbpRate = usdToGbpRate
network.usdToCnyRate = usdToCnyRate
network.usdToJpyRate = usdToJpyRate
network.usdToKrwRate = usdToKrwRate
network.usdToAudRate = usdToAudRate
network.daiUsdRate = daiUsdRate
network.usdcUsdRate = usdcUsdRate
network.save()
Expand All @@ -89,14 +133,22 @@ export function handleExchangeRates(block: ethereum.Block): void {
exchangeRateSnapshot.usdcUsdRate = usdcUsdRate
exchangeRateSnapshot.usdToEurRate = usdToEurRate
exchangeRateSnapshot.usdToGbpRate = usdToGbpRate
exchangeRateSnapshot.usdToCnyRate = usdToCnyRate
exchangeRateSnapshot.usdToJpyRate = usdToJpyRate
exchangeRateSnapshot.usdToKrwRate = usdToKrwRate
exchangeRateSnapshot.usdToAudRate = usdToAudRate
exchangeRateSnapshot.save()

log.info(
'[ExchangeRates] assetsUsdRate={} usdToEurRate={} usdToGbpRate={} daiUsdRate={} usdcUsdRate={} swiseUsdRate={}',
'[ExchangeRates] assetsUsdRate={} usdToEurRate={} usdToGbpRate={} usdToCnyRate={} usdToJpyRate={} usdToKrwRate={} usdToAudRate={} daiUsdRate={} usdcUsdRate={} swiseUsdRate={}',
[
assetsUsdRate.toString(),
usdToEurRate.toString(),
usdToGbpRate.toString(),
usdToCnyRate.toString(),
usdToJpyRate.toString(),
usdToKrwRate.toString(),
usdToAudRate.toString(),
daiUsdRate.toString(),
usdcUsdRate.toString(),
swiseUsdRate.toString(),
Expand Down
20 changes: 20 additions & 0 deletions src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,18 @@ type Network @entity {
"The USD to GBP rate"
usdToGbpRate: BigDecimal!

"The USD to CNY rate"
usdToCnyRate: BigDecimal!

"The USD to JPY rate"
usdToJpyRate: BigDecimal!

"The USD to KRW rate"
usdToKrwRate: BigDecimal!

"The USD to AUD rate"
usdToAudRate: BigDecimal!

"The non repeated addresses of all the vaults"
vaultIds: [String!]!

Expand Down Expand Up @@ -914,6 +926,10 @@ type ExchangeRateSnapshot @entity(timeseries: true) {
usdcUsdRate: BigDecimal!
usdToEurRate: BigDecimal!
usdToGbpRate: BigDecimal!
usdToCnyRate: BigDecimal!
usdToJpyRate: BigDecimal!
usdToKrwRate: BigDecimal!
usdToAudRate: BigDecimal!
}

"""
Expand All @@ -928,6 +944,10 @@ type ExchangeRateStats @aggregation(intervals: ["day"], source: "ExchangeRateSna
usdcUsdRate: BigDecimal! @aggregate(fn: "last", arg: "usdcUsdRate")
usdToEurRate: BigDecimal! @aggregate(fn: "last", arg: "usdToEurRate")
usdToGbpRate: BigDecimal! @aggregate(fn: "last", arg: "usdToGbpRate")
usdToCnyRate: BigDecimal! @aggregate(fn: "last", arg: "usdToCnyRate")
usdToJpyRate: BigDecimal! @aggregate(fn: "last", arg: "usdToJpyRate")
usdToKrwRate: BigDecimal! @aggregate(fn: "last", arg: "usdToKrwRate")
usdToAudRate: BigDecimal! @aggregate(fn: "last", arg: "usdToAudRate")
}

"""
Expand Down

0 comments on commit c562842

Please sign in to comment.