Skip to content

Commit

Permalink
adjust function
Browse files Browse the repository at this point in the history
  • Loading branch information
xieqiancaosissi committed Dec 24, 2023
1 parent af861a4 commit 1dc34a0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 52 deletions.
8 changes: 0 additions & 8 deletions components/Modal/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ export default function Controls({
dispatch(updateAmount({ isMax: false, amount: value }));
};

const handleMaxClick = () => {
dispatch(updateAmount({ isMax: true, amount: available }));
};

const handleFocus = (e) => {
e.target.select();
};

const handleSliderChange = (percent) => {
const p = percent < 1 ? 0 : percent > 99 ? 100 : percent;
const value = new Decimal(available).mul(p).div(100).toFixed();
Expand Down
12 changes: 9 additions & 3 deletions redux/selectors/getCollateralAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ export const getCollateralAmount = (tokenId: string) =>
(state: RootState) => state.account,
(assets, account) => {
if (!hasAssets(assets)) return "0";
const collateral = account.portfolio.collateral[tokenId];
const { metadata, config, isLpToken } = assets.data[tokenId];
const collateral = isLpToken
? account.portfolio.positions[tokenId].collateral[tokenId]
: account.portfolio.collateral[tokenId];
if (!collateral) return "0";
const { metadata, config } = assets.data[tokenId];
return toDecimal(
shrinkToken(collateral.balance, metadata.decimals + config.extra_decimals, PERCENT_DIGITS),
shrinkToken(
collateral.balance || 0,
metadata.decimals + config.extra_decimals,
PERCENT_DIGITS,
),
);
},
);
1 change: 1 addition & 0 deletions redux/selectors/getPortfolioAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const getPortfolioAssets = createSelector(
let portfolioLpAssets = {};
Object.keys(lpPositions).forEach((shadow_id: string) => {
portfolioLpAssets = {
...portfolioLpAssets,
...lpPositions[shadow_id].collateral,
};
});
Expand Down
12 changes: 9 additions & 3 deletions redux/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,20 @@ export const transformAsset = (
// TODO: refactor this without conditional
if (account.accountId) {
const decimals = asset.metadata.decimals + asset.config.extra_decimals;

const supplied = Number(
shrinkToken(account.portfolio.supplied[tokenId]?.balance || 0, decimals),
);
const collateral = Number(
shrinkToken(account.portfolio.collateral[tokenId]?.balance || 0, decimals),
shrinkToken(
asset.isLpToken
? account.portfolio.positions[tokenId].collateral[tokenId]?.balance || 0
: account.portfolio.collateral[tokenId]?.balance || 0,
decimals,
),
);
const borrowed = account.portfolio.borrowed[tokenId]?.balance || 0;
const borrowed = asset.isLpToken
? account.portfolio.positions[tokenId].borrowed[tokenId]?.balance || 0
: account.portfolio.borrowed[tokenId]?.balance || 0;
const available = account.balances[tokenId] || 0;
const availableNEAR = account.balances["near"] || 0;

Expand Down
96 changes: 58 additions & 38 deletions store/actions/adjustCollateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { ChangeMethodsLogic, ChangeMethodsOracle } from "../../interfaces";
import { Transaction } from "../wallet";
import { getMetadata, prepareAndExecuteTransactions } from "../tokens";
import { getAccountDetailed } from "../accounts";
import getAssets from "../../api/get-assets";
import { transformAssets } from "../../transformers/asstets";
import getAccount from "../../api/get-account";
import { transformAccount } from "../../transformers/account";

export async function adjustCollateral({
tokenId,
Expand All @@ -19,16 +23,18 @@ export async function adjustCollateral({
amount: string;
isMax: boolean;
}) {
const { oracleContract, logicContract, account, call } = await getBurrow();
const { decimals } = (await getMetadata(tokenId))!;
const detailedAccount = (await getAccountDetailed(account.accountId))!;

const suppliedBalance = new Decimal(
detailedAccount.supplied.find((a) => a.token_id === tokenId)?.balance || 0,
);
const { oracleContract, logicContract } = await getBurrow();
const assets = await getAssets().then(transformAssets);
const asset = assets[tokenId];
const { decimals } = asset.metadata;
const account = await getAccount().then(transformAccount);
if (!account) return;

const suppliedBalance = new Decimal(account.portfolio?.supplied[tokenId]?.balance || 0);
const collateralBalance = new Decimal(
detailedAccount.collateral.find((a) => a.token_id === tokenId)?.balance || 0,
asset.isLpToken
? account.portfolio?.positions[tokenId].collateral[tokenId]?.balance || 0
: account.portfolio?.collateral[tokenId]?.balance || 0,
);

const totalBalance = suppliedBalance.add(collateralBalance);
Expand All @@ -38,16 +44,25 @@ export async function adjustCollateral({
: decimalMin(totalBalance, expandTokenDecimal(amount, decimals + extraDecimals));

if (expandedAmount.gt(collateralBalance)) {
// await call(logicContract, ChangeMethodsLogic[ChangeMethodsLogic.execute], {
// actions: [
// {
// IncreaseCollateral: {
// token_id: tokenId,
// max_amount: !isMax ? expandedAmount.sub(collateralBalance).toFixed(0) : undefined,
// },
// },
// ],
// });
let increaseCollateralTemplate;
if (asset.isLpToken) {
increaseCollateralTemplate = {
PositionIncreaseCollateral: {
position: tokenId,
asset_amount: {
token_id: tokenId,
amount: !isMax ? expandedAmount.sub(collateralBalance).toFixed(0) : undefined,
},
},
};
} else {
increaseCollateralTemplate = {
IncreaseCollateral: {
token_id: tokenId,
max_amount: !isMax ? expandedAmount.sub(collateralBalance).toFixed(0) : undefined,
},
};
}
await prepareAndExecuteTransactions([
{
receiverId: logicContract.contractId,
Expand All @@ -56,22 +71,36 @@ export async function adjustCollateral({
methodName: ChangeMethodsLogic[ChangeMethodsLogic.execute],
gas: new BN("100000000000000"),
args: {
actions: [
{
IncreaseCollateral: {
token_id: tokenId,
max_amount: !isMax
? expandedAmount.sub(collateralBalance).toFixed(0)
: undefined,
},
},
],
actions: [increaseCollateralTemplate],
},
},
],
} as Transaction,
]);
} else if (expandedAmount.lt(collateralBalance)) {
let decreaseCollateralTemplate;
if (asset.isLpToken) {
decreaseCollateralTemplate = {
PositionDecreaseCollateral: {
position: tokenId,
asset_amount: {
token_id: tokenId,
amount: expandedAmount.gt(0)
? collateralBalance.sub(expandedAmount).toFixed(0)
: undefined, // TODO is can undefined?
},
},
};
} else {
decreaseCollateralTemplate = {
DecreaseCollateral: {
token_id: tokenId,
max_amount: expandedAmount.gt(0)
? collateralBalance.sub(expandedAmount).toFixed(0)
: undefined,
},
};
}
await prepareAndExecuteTransactions([
{
receiverId: oracleContract.contractId,
Expand All @@ -83,16 +112,7 @@ export async function adjustCollateral({
receiver_id: logicContract.contractId,
msg: JSON.stringify({
Execute: {
actions: [
{
DecreaseCollateral: {
token_id: tokenId,
max_amount: expandedAmount.gt(0)
? collateralBalance.sub(expandedAmount).toFixed(0)
: undefined,
},
},
],
actions: [decreaseCollateralTemplate],
},
}),
},
Expand Down

0 comments on commit 1dc34a0

Please sign in to comment.