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 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(amp): Fix staking balances (#2269)
* fix(amp): Fix staking balances * fix(gamma-strategies): Linting
- Loading branch information
Showing
4 changed files
with
93 additions
and
81 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
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,72 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import Axios, { AxiosError } from 'axios'; | ||
|
||
import { Cache } from '~cache/cache.decorator'; | ||
|
||
interface AmpStakingApiData { | ||
apps: { | ||
displayName: string; | ||
ampPartition: string; | ||
apy: string; | ||
}[]; | ||
} | ||
|
||
interface AmpStakingBalanceData { | ||
address: string; | ||
supplyTotal: string; | ||
rewardTotal: string; | ||
} | ||
|
||
@Injectable() | ||
export class AmpStakingResolver { | ||
@Cache({ | ||
key: `studio:amp:staking:staking-data`, | ||
ttl: 15 * 60, // 15 minutes | ||
}) | ||
private async getPoolApyData() { | ||
const url = 'https://api.capacity.production.flexa.network/apps'; | ||
const { data } = await Axios.get<AmpStakingApiData>(url, { | ||
headers: { Accept: 'application/vnd.flexa.capacity.v1+json' }, | ||
}); | ||
|
||
return data.apps; | ||
} | ||
|
||
@Cache({ | ||
key: (address: string) => `studio:amp:staking:balance-data:${address}`, | ||
ttl: 5 * 60, // 15 minutes | ||
}) | ||
private async getBalanceData(address: string) { | ||
const url = `https://api.capacity.production.flexa.network/accounts/${address}/totals`; | ||
const data = await Axios.get<AmpStakingBalanceData>(url, { | ||
headers: { Accept: 'application/vnd.flexa.capacity.v1+json' }, | ||
}) | ||
.then(({ data }) => data) | ||
.catch(err => { | ||
if ((err as AxiosError).response?.data.error === 'Address not found') | ||
return { address, supplyTotal: '0', rewardTotal: '0' } as AmpStakingBalanceData; | ||
throw err; | ||
}); | ||
|
||
return data; | ||
} | ||
|
||
async getBalance(address: string) { | ||
const balance = await this.getBalanceData(address); | ||
|
||
return { | ||
address: balance.address, | ||
supplyTotal: Number(balance.supplyTotal), | ||
rewardTotal: Number(balance.rewardTotal), | ||
}; | ||
} | ||
|
||
async getPoolApys(poolName: string) { | ||
const poolApys = await this.getPoolApyData(); | ||
|
||
const poolApy = poolApys.find(x => x.displayName == poolName); | ||
if (!poolApy) return 0; | ||
|
||
return poolApy.apy; | ||
} | ||
} |
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