Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

90% limit clm, cNote in circulation #91

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion app/lending/components/modal/modal.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved

Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ export const LendingModal = (props: Props) => {
convertToBigNumber(amount, cToken.underlying.decimals).data ?? "0"
).toString();
const amountCheck = transaction.validateAmount(bnAmount, actionType);

// limits
const needLimit =
actionType === CTokenLendingTxTypes.BORROW ||
(actionType === CTokenLendingTxTypes.WITHDRAW &&
Number(position.totalBorrow) !== 0);
const maxAmount = maxAmountForLendingTx(
actionType,
cToken,
position,
needLimit ? 90 : 100
);
const maxLabel =
!needLimit ||
(actionType === CTokenLendingTxTypes.WITHDRAW &&
maxAmount === cToken.userDetails?.supplyBalanceInUnderlying)
? undefined
: "90% limit";

return (
<div className={styles.content}>
<Spacer height="20px" />
Expand All @@ -162,10 +181,11 @@ export const LendingModal = (props: Props) => {
}}
IconUrl={cToken.underlying.logoURI}
title={cToken.underlying.symbol}
max={maxAmountForLendingTx(actionType, cToken, position)}
max={maxAmount}
symbol={cToken.underlying.symbol}
error={!amountCheck.isValid && Number(amount) !== 0}
errorMessage={amountCheck.errorMessage}
limitName={maxLabel}
/>
<Spacer height="40px" />

Expand Down
4 changes: 4 additions & 0 deletions app/lending/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ export default function LendingPage() {
value={displayAmount(lendingStats.circulatingNote, 18)}
postChild={<NoteIcon />}
/>
<Item
name="CNote in circulation"
value={displayAmount(lendingStats.circulatingCNote, 18)}
/>
<Item
name="Value of rwas on canto"
value={displayAmount(lendingStats.valueOfAllRWA, 18)}
Expand Down
23 changes: 15 additions & 8 deletions app/lending/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { CTokenWithUserData } from "@/hooks/lending/interfaces/tokens";
import { UserLMPosition } from "@/hooks/lending/interfaces/userPositions";
import useLending from "@/hooks/lending/useLending";
import { listIncludesAddress } from "@/utils/address.utils";
import { getCirculatingNote } from "@/utils/clm/noteStats.utils";
import {
getCirculatingCNote,
getCirculatingNote,
} from "@/utils/clm/noteStats.utils";
import {
addTokenBalances,
convertTokenAmountToNote,
Expand Down Expand Up @@ -41,6 +44,7 @@ interface LendingComboReturn {
setSelectedCToken: (address: string | null) => void;
};
lendingStats: {
circulatingCNote: string;
circulatingNote: string;
valueOfAllRWA: string;
cNotePrice: string;
Expand Down Expand Up @@ -97,18 +101,20 @@ export function useLendingCombo(props: LendingComboProps): LendingComboReturn {
}, [rwas]);
// circulating note
const [circulatingNote, setCirculatingNote] = useState("0");
const [circulatingCNote, setCirculatingCNote] = useState("0");
useEffect(() => {
async function getStats() {
if (cNote?.underlying.address) {
const { data: circulatingNote, error } = await getCirculatingNote(
chainId,
cNote.underlying.address
);
if (error) {
console.log(error);
const [circulatingNote, circulatingCNote] = await Promise.all([
getCirculatingNote(chainId, cNote.underlying.address),
getCirculatingCNote(chainId, cNote.address),
]);
if (circulatingNote.error || circulatingCNote.error) {
console.log(circulatingNote.error ?? circulatingCNote.error);
return;
}
setCirculatingNote(circulatingNote);
setCirculatingNote(circulatingNote.data);
setCirculatingCNote(circulatingCNote.data);
}
}
getStats();
Expand Down Expand Up @@ -169,6 +175,7 @@ export function useLendingCombo(props: LendingComboProps): LendingComboReturn {
},
selection,
lendingStats: {
circulatingCNote: circulatingCNote,
circulatingNote: circulatingNote,
valueOfAllRWA: valueOfAllRWA,
cNotePrice: cNote?.exchangeRate ?? "0",
Expand Down
7 changes: 4 additions & 3 deletions components/amount/amount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import Spacer from "../layout/spacer";
interface Props {
IconUrl: string;
title: string;
max: string;
symbol: string;
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
decimals: number;
value: string;
max: string;
limitName?: string;
error?: boolean;
errorMessage?: string;
}
Expand Down Expand Up @@ -91,7 +92,7 @@ const Amount = (props: Props) => {
</Container>
<Container direction="row" gap={6} className={styles.balance}>
<Text size="xx-sm" theme="secondary-dark">
Balance :{" "}
{props.limitName ? "Limit: " : "Balance: "}
{formatBalance(props.max, props.decimals, {
commify: true,
})}{" "}
Expand All @@ -111,7 +112,7 @@ const Amount = (props: Props) => {
}}
>
<Text size="xx-sm" weight="bold">
(max)
{`(${props.limitName ?? "max"})`}
</Text>
</span>
</Container>
Expand Down
2 changes: 2 additions & 0 deletions config/consts/addresses.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CANTO_MAINNET_EVM, CANTO_TESTNET_EVM } from "../networks";

export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
export const MAX_UINT256 = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";

export const GRAVITY_BRIDGE_ETH_ADDRESS =
"0xa4108aA1Ec4967F8b52220a4f7e94A8201F2D906";
export const WETH_MAINNET_ADDRESS =
Expand Down
31 changes: 27 additions & 4 deletions utils/clm/noteStats.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
} from "../evm/helpers.utils";
import { ERC20_ABI } from "@/config/abis";
import { Contract } from "web3";
import { getCantoCoreAddress } from "@/config/consts/addresses";
import {
MAX_UINT256,
ZERO_ADDRESS,
getCantoCoreAddress,
} from "@/config/consts/addresses";
import BigNumber from "bignumber.js";

export async function getCirculatingNote(
Expand All @@ -36,11 +40,30 @@ export async function getCirculatingNote(
const accountantBalance = await noteContract.methods
.balanceOf(accountantAddress)
.call();
const circulatingNote = new BigNumber(
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
).minus(accountantBalance as number);
const circulatingNote = new BigNumber(MAX_UINT256).minus(
accountantBalance as number
);
return NO_ERROR(circulatingNote.toString());
} catch (err) {
return NEW_ERROR("getCirculatingNote: " + errMsg(err));
}
}

export async function getCirculatingCNote(
chainId: number,
cNoteAddress: string
): PromiseWithError<string> {
// get rpc
const { data: rpcUrl, error } = getRpcUrlFromChainId(chainId);
if (error) return NEW_ERROR("getCirculatingNote" + errMsg(error));

// get contract
const cNoteContract = new Contract(
ERC20_ABI,
cNoteAddress,
getProviderWithoutSigner(rpcUrl)
);
// get total supply
const circulatingCNote = await cNoteContract.methods.totalSupply().call();
return NO_ERROR(circulatingCNote.toString());
}