This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sturdy): Adds Ethereum version of Sturdy Finance (#574)
- Loading branch information
1 parent
59b4cb0
commit 291a50e
Showing
7 changed files
with
215 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Inject } from '@nestjs/common'; | ||
|
||
import { TokenBalanceHelper } from '~app-toolkit'; | ||
import { Register } from '~app-toolkit/decorators'; | ||
import { presentBalanceFetcherResponse } from '~app-toolkit/helpers/presentation/balance-fetcher-response.present'; | ||
import { BalanceFetcher } from '~balance/balance-fetcher.interface'; | ||
import { Network } from '~types/network.interface'; | ||
|
||
import { STURDY_DEFINITION } from '../sturdy.definition'; | ||
|
||
const network = Network.ETHEREUM_MAINNET; | ||
const appId = STURDY_DEFINITION.id; | ||
|
||
@Register.BalanceFetcher(STURDY_DEFINITION.id, network) | ||
export class EthereumSturdyBalanceFetcher implements BalanceFetcher { | ||
constructor(@Inject(TokenBalanceHelper) private readonly tokenBalanceHelper: TokenBalanceHelper) {} | ||
|
||
private async getLendingTokenBalances(address: string) { | ||
return this.tokenBalanceHelper.getTokenBalances({ | ||
address, | ||
appId, | ||
groupId: STURDY_DEFINITION.groups.lending.id, | ||
network, | ||
}); | ||
} | ||
|
||
async getBalances(address: string) { | ||
const [lendingTokenBalances] = await Promise.all([this.getLendingTokenBalances(address)]); | ||
|
||
return presentBalanceFetcherResponse([ | ||
{ | ||
label: 'Lending', | ||
assets: lendingTokenBalances, | ||
}, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import axios from 'axios'; | ||
|
||
import { IAppToolkit, APP_TOOLKIT } from '~app-toolkit/app-toolkit.interface'; | ||
import { ZERO_ADDRESS } from '~app-toolkit/constants/address'; | ||
import { Register } from '~app-toolkit/decorators'; | ||
import { | ||
buildNumberDisplayItem, | ||
buildPercentageDisplayItem, | ||
} from '~app-toolkit/helpers/presentation/display-item.present'; | ||
import { getImagesFromToken } from '~app-toolkit/helpers/presentation/image.present'; | ||
import { CacheOnInterval } from '~cache/cache-on-interval.decorator'; | ||
import { ContractType } from '~position/contract.interface'; | ||
import { PositionFetcher } from '~position/position-fetcher.interface'; | ||
import { AppTokenPosition } from '~position/position.interface'; | ||
import { BaseToken } from '~position/token.interface'; | ||
import { Network } from '~types/network.interface'; | ||
|
||
import { SturdyContractFactory } from '../contracts'; | ||
import { VaultMonitoringResponse, cacheOnIntervalKeyCreationHelper, TIMEOUT_DURATION } from '../helpers/constants'; | ||
import { STURDY_DEFINITION } from '../sturdy.definition'; | ||
|
||
const appId = STURDY_DEFINITION.id; | ||
const groupId = STURDY_DEFINITION.groups.lending.id; | ||
const network = Network.ETHEREUM_MAINNET; | ||
|
||
@Register.TokenPositionFetcher({ appId, groupId, network }) | ||
export class EthereumSturdyLendingTokenFetcher implements PositionFetcher<AppTokenPosition> { | ||
constructor( | ||
@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit, | ||
@Inject(SturdyContractFactory) private readonly sturdyContractFactory: SturdyContractFactory, | ||
) {} | ||
|
||
@CacheOnInterval({ | ||
key: cacheOnIntervalKeyCreationHelper(appId, groupId, network), | ||
timeout: TIMEOUT_DURATION, | ||
}) | ||
private async getVaultMonitoringData() { | ||
const endpoint = 'https://us-central1-stu-dashboard-a0ba2.cloudfunctions.net/getVaultMonitoring?chain=ethereum'; | ||
const data = await axios.get<VaultMonitoringResponse>(endpoint).then(res => res.data); | ||
return data; | ||
} | ||
|
||
async getPositions() { | ||
const multicall = this.appToolkit.getMulticall(network); | ||
const baseTokens = await this.appToolkit.getBaseTokenPrices(network); | ||
const ethToken = baseTokens.find(t => t.address === ZERO_ADDRESS); | ||
if (!ethToken) return []; | ||
|
||
const tokenData = await this.getVaultMonitoringData(); | ||
|
||
const tokens = tokenData.map(async data => { | ||
const symbol = data.tokens; | ||
const underlyingTokens: BaseToken[] = []; | ||
|
||
const contract = this.sturdyContractFactory.sturdyToken({ address: data.address, network }); | ||
const underlyingTokenAddress = await multicall | ||
.wrap(contract) | ||
.UNDERLYING_ASSET_ADDRESS() | ||
.then(v => v.toLowerCase()); | ||
const underlyingToken = baseTokens.find(t => t.address === underlyingTokenAddress); | ||
if (underlyingToken) underlyingTokens.push(underlyingToken); | ||
|
||
const token: AppTokenPosition = { | ||
type: ContractType.APP_TOKEN, | ||
appId, | ||
groupId, | ||
address: data.address, | ||
network, | ||
symbol, | ||
decimals: data.decimals, | ||
supply: data.supply, | ||
pricePerShare: 1, | ||
price: underlyingTokens[0].price, | ||
tokens: underlyingTokens, | ||
dataProps: { | ||
apy: data.base, | ||
tvl: data.tvl, | ||
}, | ||
displayProps: { | ||
label: symbol, | ||
images: getImagesFromToken(underlyingTokens[0]), | ||
statsItems: [ | ||
{ | ||
label: 'APY', | ||
value: buildPercentageDisplayItem(data.base), | ||
}, | ||
{ | ||
label: 'Liquidity', | ||
value: buildNumberDisplayItem(data.tvl), | ||
}, | ||
], | ||
}, | ||
}; | ||
return token; | ||
}); | ||
return Promise.all(tokens); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { sumBy } from 'lodash'; | ||
|
||
import { IAppToolkit, APP_TOOLKIT } from '~app-toolkit/app-toolkit.interface'; | ||
import { Register } from '~app-toolkit/decorators'; | ||
import { TvlFetcher } from '~stats/tvl/tvl-fetcher.interface'; | ||
import { Network } from '~types/network.interface'; | ||
|
||
import { STURDY_DEFINITION } from '../sturdy.definition'; | ||
|
||
const appId = STURDY_DEFINITION.id; | ||
const network = Network.ETHEREUM_MAINNET; | ||
|
||
@Register.TvlFetcher({ appId, network }) | ||
export class EthereumSturdyTvlFetcher implements TvlFetcher { | ||
constructor(@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit) {} | ||
|
||
async getTvl() { | ||
const lendingTokens = await this.appToolkit.getAppTokenPositions({ | ||
appId, | ||
groupIds: [STURDY_DEFINITION.groups.lending.id], | ||
network, | ||
}); | ||
return sumBy(lendingTokens, v => v.supply * v.price); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Network } from '~types'; | ||
|
||
export type VaultMonitoringResponse = { | ||
chain: string; | ||
tokens: string; | ||
decimals: number; | ||
address: string; | ||
supply: number; | ||
price: number; | ||
base: number; | ||
reward: number; | ||
rewards: { | ||
CRV: number; | ||
CVX: number; | ||
}; | ||
url: number; | ||
tvl: number; | ||
active: boolean; | ||
}[]; | ||
|
||
export const TIMEOUT_DURATION = 15 * 60 * 1000; | ||
|
||
/** | ||
* Creates a unique primary key used for caching API responses | ||
* @param appId The application's unique identifier | ||
* @param groupId The application's unique group identifier | ||
* @param network Network where the application is deployed | ||
* @returns string | ||
*/ | ||
export function cacheOnIntervalKeyCreationHelper(appId: string, groupId: string, network: Network): string { | ||
return `studio:${appId}:${groupId}:${network}:addresses`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters