Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
fix(notional-finance-v3): Remove fCash app token and move logic into …
Browse files Browse the repository at this point in the history
…supply and borrow positions (#3089)
  • Loading branch information
wpoulin authored Nov 29, 2023
1 parent a25a5ff commit ffb1616
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ export type NotionalBorrowingDataProps = {
currencyId: number;
tokenId: string;
maturity: number;
fCashPV: number;
positionKey: string;
type: string;
};

const NOTIONAL_CONSTANT = 10 ** 7;
const SECONDS_IN_YEAR = 31104000; // 360 days

@PositionTemplate()
export class ArbitrumNotionalFinanceV3BorrowContractPositionFetcher extends ContractPositionTemplatePositionFetcher<
NotionalView,
Expand Down Expand Up @@ -79,11 +84,19 @@ export class ArbitrumNotionalFinanceV3BorrowContractPositionFetcher extends Cont
.read.encodeToId([currencyId, maturity, 0])
.then(v => v.toString());

const apy = Number(activeMarket.lastImpliedRate ?? 0) / NOTIONAL_CONSTANT;

const dateNowEpoch = Date.now() / 1000;
const timeToMaturity = (maturity - dateNowEpoch) / SECONDS_IN_YEAR;
const lastImpliedRate = apy / 100;
const fCashPV = 1 / Math.exp(lastImpliedRate * timeToMaturity);

return {
address: this.notionalViewContractAddress,
currencyId,
underlyingTokenAddress,
maturity,
fCashPV,
tokenId,
type,
};
Expand All @@ -107,7 +120,7 @@ export class ArbitrumNotionalFinanceV3BorrowContractPositionFetcher extends Cont
params: GetDataPropsParams<NotionalView, NotionalBorrowingDataProps, NotionalBorrowingDefinition>,
) {
const defaultDataProps = await super.getDataProps(params);
const props = pick(params.definition, ['currencyId', 'tokenId', 'maturity', 'type']);
const props = pick(params.definition, ['currencyId', 'tokenId', 'maturity', 'fCashPV', 'type']);
return { ...defaultDataProps, ...props, positionKey: Object.values(props).join(':') };
}

Expand All @@ -120,7 +133,7 @@ export class ArbitrumNotionalFinanceV3BorrowContractPositionFetcher extends Cont
contractPosition,
contract,
}: GetTokenBalancesParams<NotionalView, NotionalBorrowingDataProps>) {
const { maturity, currencyId } = contractPosition.dataProps;
const { maturity, currencyId, fCashPV } = contractPosition.dataProps;
const portfolio = await contract.read.getAccountPortfolio([address]);
const debtPositions = portfolio.filter(v => v.notional < 0);
const position = debtPositions.find(v => Number(v.maturity) === maturity && Number(v.currencyId) === currencyId);
Expand All @@ -130,6 +143,8 @@ export class ArbitrumNotionalFinanceV3BorrowContractPositionFetcher extends Cont
.times(10 ** contractPosition.tokens[0].decimals)
.div(10 ** 8);

return [fcashAmountAtMaturity.abs().toString()];
const presentValue = fcashAmountAtMaturity.times(new BigNumber(fCashPV));

return [presentValue.toString()];
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ export type NotionalFinanceLendingDataProps = {
currencyId: number;
tokenId: string;
maturity: number;
fCashPV: number;
positionKey: string;
type: string;
};

const NOTIONAL_CONSTANT = 10 ** 7;
const SECONDS_IN_YEAR = 31104000; // 360 days

@PositionTemplate()
export class ArbitrumNotionalFinanceV3SupplyContractPositionFetcher extends ContractPositionTemplatePositionFetcher<
NotionalView,
Expand Down Expand Up @@ -81,11 +85,19 @@ export class ArbitrumNotionalFinanceV3SupplyContractPositionFetcher extends Cont
.read.encodeToId([currencyId, maturity, 0])
.then(v => v.toString());

const apy = Number(activeMarket.lastImpliedRate ?? 0) / NOTIONAL_CONSTANT;

const dateNowEpoch = Date.now() / 1000;
const timeToMaturity = (maturity - dateNowEpoch) / SECONDS_IN_YEAR;
const lastImpliedRate = apy / 100;
const fCashPV = 1 / Math.exp(lastImpliedRate * timeToMaturity);

return {
address: this.notionalViewContractAddress,
currencyId,
underlyingTokenAddress,
maturity,
fCashPV,
tokenId,
type,
};
Expand All @@ -100,16 +112,14 @@ export class ArbitrumNotionalFinanceV3SupplyContractPositionFetcher extends Cont
}

async getTokenDefinitions({ definition }: GetTokenDefinitionsParams<NotionalView, NotionalFinanceLendingDefinition>) {
return [
{ metaType: MetaType.SUPPLIED, address: definition.address, network: this.network, tokenId: definition.tokenId },
];
return [{ metaType: MetaType.SUPPLIED, address: definition.underlyingTokenAddress, network: this.network }];
}

async getDataProps(
params: GetDataPropsParams<NotionalView, NotionalFinanceLendingDataProps, NotionalFinanceLendingDefinition>,
) {
const defaultDataProps = await super.getDataProps(params);
const props = pick(params.definition, ['currencyId', 'tokenId', 'maturity', 'type']);
const props = pick(params.definition, ['currencyId', 'tokenId', 'maturity', 'fCashPV', 'type']);
return { ...defaultDataProps, ...props, positionKey: Object.values(props).join(':') };
}

Expand All @@ -122,7 +132,7 @@ export class ArbitrumNotionalFinanceV3SupplyContractPositionFetcher extends Cont
contractPosition,
contract,
}: GetTokenBalancesParams<NotionalView, NotionalFinanceLendingDataProps>): Promise<BigNumberish[]> {
const { maturity, currencyId } = contractPosition.dataProps;
const { maturity, currencyId, fCashPV } = contractPosition.dataProps;
const portfolio = await contract.read.getAccountPortfolio([address]);
const supplyPositions = portfolio.filter(v => v.notional >= 0);
const position = supplyPositions.find(v => Number(v.maturity) === maturity && Number(v.currencyId) === currencyId);
Expand All @@ -132,6 +142,8 @@ export class ArbitrumNotionalFinanceV3SupplyContractPositionFetcher extends Cont
.times(10 ** contractPosition.tokens[0].decimals)
.div(10 ** 8);

return [fcashAmountAtMaturity.abs().toString()];
const presentValue = fcashAmountAtMaturity.times(new BigNumber(fCashPV));

return [presentValue.toString()];
}
}
2 changes: 0 additions & 2 deletions src/apps/notional-finance-v3/notional-finance-v3.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Module } from '@nestjs/common';
import { AbstractApp } from '~app/app.dynamic-module';

import { ArbitrumNotionalFinanceV3BorrowContractPositionFetcher } from './arbitrum/notional-finance-v3.borrow.contract-position-fetcher';
import { ArbitrumNotionalFinanceV3FCashTokenFetcher } from './arbitrum/notional-finance-v3.f-cash.token-fetcher';
import { ArbitrumNotionalFinanceV3NTokenTokenFetcher } from './arbitrum/notional-finance-v3.n-token.token-fetcher';
import { ArbitrumNotionalFinanceV3PCashTokenFetcher } from './arbitrum/notional-finance-v3.p-cash.token-fetcher';
import { ArbitrumNotionalFinanceV3PDebtTokenFetcher } from './arbitrum/notional-finance-v3.p-debt.token-fetcher';
Expand All @@ -17,7 +16,6 @@ import { NotionalFinanceV3ViemContractFactory } from './contracts';
ArbitrumNotionalFinanceV3PCashTokenFetcher,
ArbitrumNotionalFinanceV3PDebtTokenFetcher,
ArbitrumNotionalFinanceV3NTokenTokenFetcher,
ArbitrumNotionalFinanceV3FCashTokenFetcher,
ArbitrumNotionalFinanceV3SupplyContractPositionFetcher,
ArbitrumNotionalFinanceV3BorrowContractPositionFetcher,
],
Expand Down

0 comments on commit ffb1616

Please sign in to comment.