This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(angle): Add vaults on Arbitrum (#3158)
- Loading branch information
Showing
7 changed files
with
173 additions
and
137 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
86 changes: 86 additions & 0 deletions
86
src/apps/angle/arbitrum/angle.vault.contract-position-fetcher.ts
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,86 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { BigNumberish } from 'ethers'; | ||
import { sum } from 'lodash'; | ||
|
||
import { IAppToolkit, APP_TOOLKIT } from '~app-toolkit/app-toolkit.interface'; | ||
import { PositionTemplate } from '~app-toolkit/decorators/position-template.decorator'; | ||
import { getLabelFromToken } from '~app-toolkit/helpers/presentation/image.present'; | ||
import { MetaType } from '~position/position.interface'; | ||
import { ContractPositionTemplatePositionFetcher } from '~position/template/contract-position.template.position-fetcher'; | ||
import { | ||
DefaultContractPositionDefinition, | ||
GetDisplayPropsParams, | ||
GetTokenBalancesParams, | ||
GetTokenDefinitionsParams, | ||
} from '~position/template/contract-position.template.types'; | ||
|
||
import { AnglePositionResolver } from '../common/angle.position-resolver'; | ||
import { AngleViemContractFactory } from '../contracts'; | ||
import { AngleVaultManager } from '../contracts/viem'; | ||
|
||
@PositionTemplate() | ||
export class ArbitrumAngleVaultsContractPositionFetcher extends ContractPositionTemplatePositionFetcher<AngleVaultManager> { | ||
groupLabel = 'Vaults'; | ||
|
||
constructor( | ||
@Inject(APP_TOOLKIT) protected readonly appToolkit: IAppToolkit, | ||
@Inject(AngleViemContractFactory) protected readonly contractFactory: AngleViemContractFactory, | ||
@Inject(AnglePositionResolver) protected readonly anglePositionResolver: AnglePositionResolver, | ||
) { | ||
super(appToolkit); | ||
} | ||
|
||
getContract(address: string) { | ||
return this.contractFactory.angleVaultManager({ address, network: this.network }); | ||
} | ||
|
||
async getDefinitions(): Promise<DefaultContractPositionDefinition[]> { | ||
const vaultAddresses = await this.anglePositionResolver.getVaultManagers(this.network); | ||
return vaultAddresses.map(address => { | ||
return { | ||
address, | ||
}; | ||
}); | ||
} | ||
|
||
async getTokenDefinitions({ contract }: GetTokenDefinitionsParams<AngleVaultManager>) { | ||
return [ | ||
{ metaType: MetaType.SUPPLIED, address: await contract.read.collateral(), network: this.network }, | ||
{ metaType: MetaType.BORROWED, address: await contract.read.stablecoin(), network: this.network }, | ||
]; | ||
} | ||
|
||
async getLabel({ contractPosition }: GetDisplayPropsParams<AngleVaultManager>): Promise<string> { | ||
return `${getLabelFromToken(contractPosition.tokens[0])} - ${getLabelFromToken(contractPosition.tokens[1])}`; | ||
} | ||
|
||
async getTokenBalancesPerPosition({ | ||
address, | ||
contract, | ||
}: GetTokenBalancesParams<AngleVaultManager>): Promise<BigNumberish[]> { | ||
const userBalanceCount = await contract.read.balanceOf([address]); | ||
|
||
if (Number(userBalanceCount) < 1) return [0, 0]; | ||
|
||
const vaultIds = Object.values(await this.anglePositionResolver.getVaultIds(address, this.network)); | ||
|
||
const balances = await Promise.all( | ||
vaultIds.map(async vaultId => { | ||
const [collateralData, vaultDebt] = await Promise.all([ | ||
contract.read.vaultData([BigInt(vaultId)]), | ||
contract.read.getVaultDebt([BigInt(vaultId)]), | ||
]); | ||
|
||
return { | ||
supplied: collateralData[0], | ||
borrowed: vaultDebt, | ||
}; | ||
}), | ||
); | ||
|
||
const supplied = sum(balances.map(x => Number(x.supplied))); | ||
const borrowed = sum(balances.map(x => Number(x.borrowed))); | ||
|
||
return [supplied, borrowed]; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,66 @@ | ||
import querystring from 'node:querystring'; | ||
|
||
import { Injectable } from '@nestjs/common'; | ||
import Axios from 'axios'; | ||
|
||
import { Cache } from '~cache/cache.decorator'; | ||
import { NETWORK_IDS, Network } from '~types/network.interface'; | ||
|
||
const BASE_URL = 'https://api.angle.money/v1'; | ||
|
||
type TVaultManager = { | ||
address: string; | ||
}; | ||
|
||
type TVault = { | ||
address: string; | ||
id: number; | ||
}; | ||
|
||
@Injectable() | ||
export class AnglePositionResolver { | ||
private async callAngleApi<T>(endpoint: string, params?: Record<string, any>): Promise<T> { | ||
let url = `${BASE_URL}/${endpoint}`; | ||
if (params) { | ||
url += `?${querystring.stringify(params)}`; | ||
} | ||
const data = await Axios.get<T>(url).then(v => v.data); | ||
return data; | ||
} | ||
|
||
@Cache({ | ||
key: (network: Network) => `studio:angle:vaultmanagers:${network}:angle`, | ||
ttl: 15 * 60, | ||
}) | ||
async getVaultManagers(network: Network) { | ||
const vaultManagers = await this.callAngleApi<Record<string, TVaultManager>>('vaultManagers', { | ||
chainId: NETWORK_IDS[network], | ||
}); | ||
|
||
return Object.values(vaultManagers).map(x => x.address); | ||
} | ||
|
||
@Cache({ | ||
key: (network: Network) => `studio:angle:vaults:${network}:angle`, | ||
ttl: 15 * 60, | ||
}) | ||
async getVaultIds(address: string, network: Network) { | ||
const userVaultDataRaw = await this.callAngleApi<Record<string, TVault>>('vaults', { | ||
chainId: NETWORK_IDS[network], | ||
user: address, | ||
}); | ||
|
||
return Object.values(userVaultDataRaw).map(userVault => userVault.id); | ||
} | ||
|
||
@Cache({ | ||
key: (network: Network) => `studio:angle:rewardsdata:${network}:angle`, | ||
ttl: 30 * 60, | ||
}) | ||
async getRewardsData(address: string, network: Network) { | ||
return this.callAngleApi<{ rewardsData: { totalClaimable: number } }>('dao', { | ||
chainId: NETWORK_IDS[network], | ||
user: address, | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.