Skip to content

Commit

Permalink
Merge pull request #28 from curvefi/feat/add-userLoss
Browse files Browse the repository at this point in the history
feat: add userLoss
  • Loading branch information
fedorovdg authored Aug 5, 2024
2 parents 12627f6 + f3a940a commit 044dc7b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/lending-api",
"version": "2.1.1",
"version": "2.1.2",
"description": "JavaScript library for Curve Lending",
"main": "lib/index.js",
"author": "Macket",
Expand Down
4 changes: 2 additions & 2 deletions src/external-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ export const _getUsdPricesFromApi = async (): Promise<IDict<number>> => {
}

export const _getUserCollateral = memoize(
async (network: INetworkName, controller: string, user: string, collateralDecimals = 18): Promise<string> => {
async (network: INetworkName, controller: string, user: string): Promise<string> => {
const url = `https://prices.curve.fi/v1/lending/collateral_events/${network}/${controller}/${user}`;
const response = await axios.get(url, { validateStatus: () => true });
return lending.formatUnits(lending.parseUnits(response.data.total_collateral ?? "0.0", 0), collateralDecimals);
return response.data.total_deposit;
},
{
promise: true,
Expand Down
28 changes: 27 additions & 1 deletion src/markets/OneWayMarketTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
smartNumber,
} from "../utils.js";
import {IDict, TGas, TAmount, IReward, I1inchRoute, I1inchSwapData} from "../interfaces.js";
import { _getExpected1inch, _getSwapData1inch, _getSpotPrice1inch } from "../external-api.js";
import { _getExpected1inch, _getSwapData1inch, _getSpotPrice1inch, _getUserCollateral } from "../external-api.js";
import ERC20Abi from '../constants/abis/ERC20.json' assert { type: 'json' };
import {cacheKey, cacheStats} from "../cache/index.js";

Expand Down Expand Up @@ -1252,6 +1252,32 @@ export class OneWayMarketTemplate {
return _prices.map((_p) => formatUnits(_p)).reverse();
}

public async userLoss(userAddress = ""): Promise<{ deposited_collateral: string, current_collateral_estimation: string, loss: string, loss_pct: string }> {
userAddress = _getAddress(userAddress);
const [deposited_collateral, _current_collateral_estimation] = await Promise.all([
_getUserCollateral(lending.constants.NETWORK_NAME, this.addresses.controller, userAddress),
lending.contracts[this.addresses.amm].contract.get_y_up(userAddress),
]);
const current_collateral_estimation = lending.formatUnits(_current_collateral_estimation, this.collateral_token.decimals);
if (BN(deposited_collateral).lte(0)) {
return {
deposited_collateral,
current_collateral_estimation,
loss: "0.0",
loss_pct: "0.0",
};
}
const loss = BN(deposited_collateral).minus(current_collateral_estimation).toString()
const loss_pct = BN(loss).div(deposited_collateral).times(100).toString();

return {
deposited_collateral,
current_collateral_estimation,
loss,
loss_pct,
};
}

public async userBandsBalances(address = ""): Promise<IDict<{ collateral: string, borrowed: string }>> {
const [n2, n1] = await this.userBands(address);
if (n1 == 0 && n2 == 0) return {};
Expand Down

0 comments on commit 044dc7b

Please sign in to comment.