Skip to content

Commit

Permalink
Merge pull request #14 from curvefi/fix/v1.15.1
Browse files Browse the repository at this point in the history
Fix/v1.15.1
  • Loading branch information
Macket authored Jan 12, 2022
2 parents 303cd1e + 14968a9 commit cc55afc
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
- name: Changelog
id: Changelog
uses: scottbrenner/generate-changelog-action@master
with:
allow-unknown: true
- name: Create Release
id: create_release
uses: actions/create-release@v1
Expand Down
69 changes: 49 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,36 @@ const WalletProvider: FunctionComponent = ({ children }) => {
...
```
## Available pools
```ts
import curve from "@curvefi/api";

(async () => {
await curve.init('JsonRpc', {}, {gasPrice: 0, maxFeePerGas: 0, maxPriorityFeePerGas: 0});

console.log(curve.getPoolList());
// [
// 'compound', 'usdt', 'y', 'busd',
// 'susd', 'pax', 'ren', 'sbtc',
// 'hbtc', '3pool', 'gusd', 'husd',
// 'usdk', 'usdn', 'musd', 'rsv',
// 'tbtc', 'dusd', 'pbtc', 'bbtc',
// 'obtc', 'seth', 'eurs', 'ust',
// 'aave', 'steth', 'saave', 'ankreth',
// 'usdp', 'ib', 'link', 'tusd',
// 'frax', 'lusd', 'busdv2', 'reth',
// 'alusd', 'mim', 'tricrypto2', 'eurt',
// 'eurtusd', 'crveth', 'cvxeth'
// ]
})()
````

## Balances
```ts
import curve from "@curvefi/api";
(async () => {
await curve.init('JsonRpc', {}, { gasPrice: 0, maxFeePerGas: 0, maxPriorityFeePerGas: 0, chainId: 1 });
await curve.init('JsonRpc', {}, { gasPrice: 0, maxFeePerGas: 0, maxPriorityFeePerGas: 0 });
console.log(await curve.getBalances(['DAI', 'sUSD']));
// OR console.log(await curve.getBalances(['0x6B175474E89094C44Da98b954EedeAC495271d0F', '0x57Ab1ec28D129707052df4dF418D58a2D46d5f51']));
Expand Down Expand Up @@ -218,41 +242,46 @@ import curve from "@curvefi/api";
(async () => {
await curve.init('JsonRpc', {}, {gasPrice: 0, maxFeePerGas: 0, maxPriorityFeePerGas: 0});
const saave = new curve.Pool('aave');
const aave = new curve.Pool('aave');
console.log(await saave.stats.getParameters());
console.log(await aave.stats.getParameters());
// {
// virtualPrice: '1.051888073134291314',
// virtualPrice: '1.082056814810440924',
// fee: '0.04',
// adminFee: '0.02',
// A: '100',
// A: '2000',
// future_A: '2000',
// initial_A: '200',
// future_A_time: 1628525830000,
// initial_A_time: 1627923611000,
// gamma: undefined
// }
console.log(await aave.stats.getPoolBalances());
// [ '19619514.600802512613372364', '18740372.790339', '16065974.167437' ]
console.log(await saave.stats.getPoolBalances());
// [ '56379002.278506498342855456', '40931955.428972956435172989' ]

console.log(await saave.stats.getPoolWrappedBalances());
// [ '56379002.278506498342855456', '40931955.428972956435172989' ]
console.log(await aave.stats.getPoolWrappedBalances());
// [ '19619514.600802512613372364', '18740372.790339', '16065974.167437' ]
console.log(await saave.stats.getTotalLiquidity());
// 97172772.77289483
console.log(await aave.stats.getTotalLiquidity());
// 54425861.55857851
console.log(await saave.stats.getVolume());
// 1022328.1797568246
console.log(await aave.stats.getVolume());
// 175647.68180084194
console.log(await saave.stats.getBaseApy());
// [ '3.2200', '3.5690', '3.0858', '5.9629' ]
console.log(await aave.stats.getBaseApy());
// { day: '3.2015', week: '3.1185', month: '3.1318', total: '7.7286' }
console.log(await saave.stats.getTokenApy());
// [ '0.0167', '0.0417' ]
console.log(await aave.stats.getTokenApy());
// [ '0.4093', '1.0233' ]
console.log(await saave.stats.getRewardsApy());
console.log(await aave.stats.getRewardsApy());
// [
// {
// token: '0x4da27a545c0c5B758a6BA100e3a049001de870f5',
// symbol: 'stkAAVE',
// apy: '0.5807714739298449'
// apy: '0.4978306501849664'
// }
// ]
})()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/api",
"version": "1.15.0",
"version": "1.15.1",
"description": "JavaScript library for curve.fi",
"main": "lib/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/constants/aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const ALIASES_ETHEREUM = {

export const ALIASES_POLYGON = {
"crv": "0x172370d5cd63279efa6d502dab29171933a610af",
"minter": "",
"minter": "0xd061D61a4d941c39E5453435B6345Dc261C2fcE0",
"voting_escrow": "0x5f3b5DfEb7B28CDbD7FAba78963EE202a494e2A2",
"gauge_controller": "0x2F50D538606Fa9EDD2B11E2446BEb18C9D5846bB",
"address_provider": "0x0000000022d53366457f9d5e68ec105046fc4383",
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
withdrawLockedCrvEstimateGas,
withdrawLockedCrv,
} from "./boosting";
import { getBalances, getAllowance, hasAllowance, ensureAllowanceEstimateGas, ensureAllowance } from "./utils";
import { getBalances, getAllowance, hasAllowance, ensureAllowanceEstimateGas, ensureAllowance, getPoolList } from "./utils";

async function init (
providerType: 'JsonRpc' | 'Web3' | 'Infura' | 'Alchemy',
Expand All @@ -56,6 +56,7 @@ function setCustomFeeData (customFeeData: { gasPrice?: number, maxFeePerGas?: nu

const curve = {
init,
getPoolList,
setCustomFeeData,
signerAddress: '',
chainId: 0,
Expand Down
62 changes: 49 additions & 13 deletions src/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class Pool {
getPoolWrappedBalances: () => Promise<string[]>,
getTotalLiquidity: () => Promise<string>,
getVolume: () => Promise<string>,
getBaseApy: () => Promise<[daily: string, weekly: string, monthly: string, total: string]>,
getBaseApy: () => Promise<{day: string, week: string, month: string, total: string}>,
getTokenApy: () => Promise<[baseApy: string, boostedApy: string]>,
getRewardsApy: () => Promise<RewardsApyInterface[]>,
}
Expand Down Expand Up @@ -184,15 +184,35 @@ export class Pool {
return ethers.utils.formatUnits(_expected);
}

private getParameters = async (): Promise<{ virtualPrice: string, fee: string, adminFee: string, A: string, gamma?: string }> => {
private getParameters = async (): Promise<{
virtualPrice: string,
fee: string,
adminFee: string,
A: string,
future_A?: string,
initial_A?: string,
future_A_time?: number,
initial_A_time?: number,
gamma?: string,
}> => {
const multicallContract = curve.contracts[this.swap].multicallContract;

const calls = [
curve.contracts[this.swap].multicallContract.get_virtual_price(),
curve.contracts[this.swap].multicallContract.fee(),
curve.contracts[this.swap].multicallContract.admin_fee(),
curve.contracts[this.swap].multicallContract.A(),
multicallContract.get_virtual_price(),
multicallContract.fee(),
multicallContract.admin_fee(),
multicallContract.A(),
]

if (this.isCrypto) calls.push(curve.contracts[this.swap].multicallContract.gamma())
if (this.isCrypto) calls.push(multicallContract.gamma())

const additionalCalls = this.isCrypto ? [] : [multicallContract.future_A()];
if ('initial_A' in multicallContract) {
additionalCalls.push(
multicallContract.initial_A(),
multicallContract.future_A_time(),
multicallContract.initial_A_time()
);
}

const [_virtualPrice, _fee, _adminFee, _A, _gamma] = await curve.multicallProvider.all(calls) as ethers.BigNumber[];
const [virtualPrice, fee, adminFee, A, gamma] = [
Expand All @@ -204,7 +224,16 @@ export class Pool {

]

return { virtualPrice, fee, adminFee, A, gamma };
const A_PRECISION = curve.chainId === 1 && ['compound', 'usdt', 'y', 'busd', 'susd', 'pax', 'ren', 'sbtc', 'hbtc', '3pool'].includes(this.name) ? 1 : 100;
const [_future_A, _initial_A, _future_A_time, _initial_A_time] = await curve.multicallProvider.all(additionalCalls) as ethers.BigNumber[]
const [future_A, initial_A, future_A_time, initial_A_time] = [
_future_A ? String(Number(ethers.utils.formatUnits(_future_A, 0)) / A_PRECISION) : undefined,
_initial_A ? String(Number(ethers.utils.formatUnits(_initial_A, 0)) / A_PRECISION) : undefined,
_future_A_time ? Number(ethers.utils.formatUnits(_future_A_time, 0)) * 1000 : undefined,
_initial_A_time ? Number(ethers.utils.formatUnits(_initial_A_time, 0)) * 1000 : undefined,
]

return { virtualPrice, fee, adminFee, A, future_A, initial_A, future_A_time, initial_A_time, gamma };
}

private getPoolBalances = async (): Promise<string[]> => {
Expand Down Expand Up @@ -244,10 +273,10 @@ export class Pool {
}

private getTotalLiquidity = async (): Promise<string> => {
const balances = await this.getPoolWrappedBalances();
const balances = await this.getPoolBalances();

const promises = [];
for (const addr of this.coinAddresses) {
for (const addr of this.underlyingCoinAddresses) {
promises.push(_getUsdRate(addr))
}

Expand All @@ -268,14 +297,21 @@ export class Pool {
return String(volume * usdRate)
}

private getBaseApy = async (): Promise<[daily: string, weekly: string, monthly: string, total: string]> => {
private getBaseApy = async (): Promise<{day: string, week: string, month: string, total: string}> => {
const name = (this.name === 'ren' && curve.chainId === 1) ? 'ren2' : this.name === 'sbtc' ? 'rens' : this.name;
const statsUrl = _getStatsUrl(this.isCrypto);
const apy = (await axios.get(statsUrl)).data.apy;

return [apy.day[name], apy.week[name], apy.month[name], apy.total[name]].map(
const formattedApy = [apy.day[name], apy.week[name], apy.month[name], apy.total[name]].map(
(x: number) => (x * 100).toFixed(4)
) as [daily: string, weekly: string, monthly: string, total: string]

return {
day: formattedApy[0],
week: formattedApy[1],
month: formattedApy[2],
total: formattedApy[3],
}
}

private getTokenApy = async (): Promise<[baseApy: string, boostedApy: string]> => {
Expand Down
6 changes: 4 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,12 @@ export const _getUsdRate = async (assetId: string): Promise<number> => {

export const _getStatsUrl = (isCrypto = false): string => {
if (curve.chainId === 1 || curve.chainId === 1337) {
return isCrypto ? "http://stats.curve.fi/raw-stats-crypto/apys.json" : "http://stats.curve.fi/raw-stats/apys.json";
return isCrypto ? "https://stats.curve.fi/raw-stats-crypto/apys.json" : "https://stats.curve.fi/raw-stats/apys.json";
} else if (curve.chainId === 137) {
return "http://stats.curve.fi/raw-stats-polygon/apys.json"
return "https://stats.curve.fi/raw-stats-polygon/apys.json"
} else {
throw Error(`Unsupported network id${curve.chainId}`)
}
}

export const getPoolList = (): string[] => Object.keys(POOLS_DATA);
20 changes: 10 additions & 10 deletions test/readme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ const balancesTest = async () => {
const statsTest = async () => {
await curve.init('JsonRpc', {}, { gasPrice: 0 });

const saave = new curve.Pool('aave');

console.log(await saave.stats.getParameters());
console.log(await saave.stats.getPoolBalances());
console.log(await saave.stats.getPoolWrappedBalances());
console.log(await saave.stats.getTotalLiquidity());
console.log(await saave.stats.getVolume());
console.log(await saave.stats.getBaseApy());
console.log(await saave.stats.getTokenApy());
console.log(await saave.stats.getRewardsApy());
const aave = new curve.Pool('aave');

console.log(await aave.stats.getParameters());
console.log(await aave.stats.getPoolBalances());
console.log(await aave.stats.getPoolWrappedBalances());
console.log(await aave.stats.getTotalLiquidity());
console.log(await aave.stats.getVolume());
console.log(await aave.stats.getBaseApy());
console.log(await aave.stats.getTokenApy());
console.log(await aave.stats.getRewardsApy());
}

const poolTest = async () => {
Expand Down

0 comments on commit cc55afc

Please sign in to comment.