|
| 1 | +import { |
| 2 | + BailoutPool, |
| 3 | + contracts, |
| 4 | + AssetDefinition, |
| 5 | + createEngine, |
| 6 | + Storage, |
| 7 | + StorageKey, |
| 8 | + StorageKeyReturnType, |
| 9 | + UnifiedStaking, |
| 10 | + mainnetTokens, |
| 11 | + mainnetNetworkConstants |
| 12 | +} from '@temple-wallet/youves-sdk'; |
| 13 | +import BigNumber from 'bignumber.js'; |
| 14 | + |
| 15 | +import SingleQueryDataProvider from './SingleQueryDataProvider'; |
| 16 | +import { tezosToolkit as tezos } from './tezos'; |
| 17 | +import { getExchangeRates } from './tokens'; |
| 18 | + |
| 19 | +const YOUVES_INDEXER_URL = 'https://indexer.youves.com/v1/graphql'; |
| 20 | +const INDEXER_CONFIG = { url: YOUVES_INDEXER_URL, headCheckUrl: '' }; |
| 21 | +const MULTIPLIER = 100; |
| 22 | +const ubtcToken = contracts.mainnet.find(token => token.id === 'uBTC')!; |
| 23 | +const uusdToken = contracts.mainnet.find(token => token.id === 'uUSD')!; |
| 24 | +const youToken = mainnetTokens.youToken; |
| 25 | + |
| 26 | +class MemoryStorage implements Storage { |
| 27 | + public storage: Map<string, any>; |
| 28 | + |
| 29 | + constructor() { |
| 30 | + this.storage = new Map(); |
| 31 | + } |
| 32 | + public async set<K extends StorageKey>(key: K, value: StorageKeyReturnType[K]): Promise<void> { |
| 33 | + this.storage.set(key, value); |
| 34 | + } |
| 35 | + public async get<K extends StorageKey>(key: K): Promise<StorageKeyReturnType[K]> { |
| 36 | + return this.storage.get(key); |
| 37 | + } |
| 38 | + public async delete<K extends StorageKey>(key: K): Promise<void> { |
| 39 | + this.storage.delete(key); |
| 40 | + } |
| 41 | + public async clear() { |
| 42 | + this.storage.clear(); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +const unifiedStaking = new UnifiedStaking(tezos, INDEXER_CONFIG, mainnetNetworkConstants); |
| 47 | +const bailoutPool = new BailoutPool(tezos, INDEXER_CONFIG, mainnetNetworkConstants); |
| 48 | + |
| 49 | +const createLocalEngine = (token: AssetDefinition) => |
| 50 | + createEngine({ |
| 51 | + tezos, |
| 52 | + contracts: token, |
| 53 | + storage: new MemoryStorage(), |
| 54 | + indexerConfig: INDEXER_CONFIG, |
| 55 | + tokens: mainnetTokens, |
| 56 | + activeCollateral: token.collateralOptions[0], |
| 57 | + networkConstants: mainnetNetworkConstants |
| 58 | + }); |
| 59 | + |
| 60 | +const withToPercentage = |
| 61 | + <A extends unknown[]>(fn: (...args: A) => Promise<BigNumber>) => |
| 62 | + async (...args: A) => { |
| 63 | + const rawApr = await fn(...args); |
| 64 | + |
| 65 | + return Number(rawApr.multipliedBy(MULTIPLIER)); |
| 66 | + }; |
| 67 | + |
| 68 | +const getV2YOUTokenApr = withToPercentage((assetToUsdExchangeRate: BigNumber, governanceToUsdExchangeRate: BigNumber) => |
| 69 | + unifiedStaking.getAPR(assetToUsdExchangeRate, governanceToUsdExchangeRate) |
| 70 | +); |
| 71 | + |
| 72 | +const getV3YOUTokenApr = withToPercentage(() => bailoutPool.getAPR()); |
| 73 | + |
| 74 | +const getYouvesTokenApr = withToPercentage((token: AssetDefinition) => |
| 75 | + createLocalEngine(token).getSavingsPoolV3YearlyInterestRate() |
| 76 | +); |
| 77 | + |
| 78 | +export const youvesStatsProvider = new SingleQueryDataProvider(60000, async () => { |
| 79 | + const exchangeRates = await getExchangeRates(); |
| 80 | + const youToUsdExchangeRate = exchangeRates.find( |
| 81 | + rate => rate.tokenAddress === youToken.contractAddress && rate.tokenId === youToken.tokenId |
| 82 | + )!.exchangeRate; |
| 83 | + |
| 84 | + const aprResults = await Promise.all([ |
| 85 | + Promise.all([ |
| 86 | + getV2YOUTokenApr(youToUsdExchangeRate, youToUsdExchangeRate).then(value => ({ v2: value })), |
| 87 | + getV3YOUTokenApr().then(value => ({ v3: value })) |
| 88 | + ]), |
| 89 | + getYouvesTokenApr(ubtcToken), |
| 90 | + getYouvesTokenApr(uusdToken) |
| 91 | + ]); |
| 92 | + |
| 93 | + return { |
| 94 | + apr: Object.fromEntries( |
| 95 | + [youToken, ubtcToken, uusdToken].map((asset, index) => { |
| 96 | + const rawResult = aprResults[index]; |
| 97 | + const { contractAddress, tokenId } = 'token' in asset ? asset.token : asset; |
| 98 | + |
| 99 | + return [ |
| 100 | + `${contractAddress}_${tokenId}`, |
| 101 | + typeof rawResult === 'number' ? rawResult : Object.assign(...rawResult) |
| 102 | + ]; |
| 103 | + }) |
| 104 | + ) |
| 105 | + }; |
| 106 | +}); |
0 commit comments