Skip to content

Commit

Permalink
get config infos from contrct
Browse files Browse the repository at this point in the history
  • Loading branch information
xieqian committed Feb 4, 2024
1 parent 1176bc3 commit 74071d3
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 171 deletions.
21 changes: 10 additions & 11 deletions components/Modal/Action.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { useState, useMemo, useEffect } from "react";
import { Box, Typography, Switch, Tooltip, Alert, useTheme } from "@mui/material";
import LoadingButton from "@mui/lab/LoadingButton";
import Decimal from "decimal.js";
import { FcInfo } from "@react-icons/all-files/fc/FcInfo";
import { nearTokenId } from "../../utils";
import { toggleUseAsCollateral, hideModal } from "../../redux/appSlice";
import { getModalData } from "./utils";
Expand All @@ -15,22 +11,22 @@ import { withdraw } from "../../store/actions/withdraw";
import { shadow_action_supply } from "../../store/actions/shadow";
import { adjustCollateral } from "../../store/actions/adjustCollateral";
import { useAppSelector, useAppDispatch } from "../../redux/hooks";
import { getSelectedValues, getAssetData } from "../../redux/appSelectors";
import { trackActionButton, trackUseAsCollateral } from "../../utils/telemetry";
import { getSelectedValues, getAssetData, getConfig } from "../../redux/appSelectors";
import { trackActionButton } from "../../utils/telemetry";
import { useDegenMode } from "../../hooks/hooks";
import { SubmitButton, AlertWarning } from "./components";
import { getAccountPortfolio } from "../../redux/accountSelectors";
import { SubmitButton } from "./components";
import getShadowRecords from "../../api/get-shadows";
import { expandToken } from "../../store";
import getFrontEndConfig from "../../utils/config";

export default function Action({ maxBorrowAmount, healthFactor, collateralType }) {
const [loading, setLoading] = useState(false);
const { amount, useAsCollateral, isMax } = useAppSelector(getSelectedValues);
// const enable_pyth_oracle = getFrontEndConfig().PRICE_SWITCH === "pyth";
const { enable_pyth_oracle } = useAppSelector(getConfig);
const dispatch = useAppDispatch();
const asset = useAppSelector(getAssetData);
const { action = "Deposit", tokenId, isLpToken, decimals } = asset;
const { isRepayFromDeposits } = useDegenMode();

const { available, canUseAsCollateral, extraDecimals, collateral, disabled } = getModalData({
...asset,
maxBorrowAmount,
Expand Down Expand Up @@ -83,7 +79,7 @@ export default function Action({ maxBorrowAmount, healthFactor, collateralType }
}
break;
case "Borrow": {
await borrow({ tokenId, extraDecimals, amount, collateralType });
await borrow({ tokenId, extraDecimals, amount, collateralType, enable_pyth_oracle });
break;
}
case "Withdraw": {
Expand All @@ -92,6 +88,7 @@ export default function Action({ maxBorrowAmount, healthFactor, collateralType }
extraDecimals,
amount,
isMax,
enable_pyth_oracle,
});
break;
}
Expand All @@ -101,6 +98,7 @@ export default function Action({ maxBorrowAmount, healthFactor, collateralType }
extraDecimals,
amount,
isMax,
enable_pyth_oracle,
});
break;
case "Repay": {
Expand All @@ -111,6 +109,7 @@ export default function Action({ maxBorrowAmount, healthFactor, collateralType }
extraDecimals,
position: collateralType,
isMax,
enable_pyth_oracle,
});
} else {
await repay({
Expand Down
2 changes: 2 additions & 0 deletions interfaces/burrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export interface IConfig {
ref_exchange_id: string;
owner_id: string;
x_booster_multiplier_at_maximum_staking_duration: number;
enable_price_oracle: boolean;
enable_pyth_oracle: boolean;
}

export interface IBurrow {
Expand Down
1 change: 1 addition & 0 deletions interfaces/contract-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum ViewMethodsLogic {
get_asset_farms_paged,
storage_balance_of,
check_registration,
get_all_token_pyth_infos,
}

// Change methods can modify the state. But you don't receive the returned value when called.
Expand Down
2 changes: 2 additions & 0 deletions redux/appSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ export const initialState: AppState = {
ref_exchange_id: "",
owner_id: "",
x_booster_multiplier_at_maximum_staking_duration: 0,
enable_price_oracle: false,
enable_pyth_oracle: true,
},
};

Expand Down
6 changes: 5 additions & 1 deletion redux/assetsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export const assetSlice = createSlice({
builder.addCase(fetchRefPrices.fulfilled, (state, action) => {
missingPriceTokens.forEach((missingToken) => {
const missingTokenId = missingToken[defaultNetwork];
if (missingTokenId && state.data[missingTokenId] && !state.data[missingTokenId]["price"]) {
if (
missingTokenId &&
state.data[missingTokenId] &&
!+(state.data[missingTokenId]["price"]?.multiplier || "0")
) {
state.data[missingTokenId]["price"] = {
decimals: action.payload[missingToken.mainnet].decimal,
usd: Number(action.payload[missingToken.mainnet].price),
Expand Down
41 changes: 22 additions & 19 deletions store/actions/adjustCollateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { decimalMin, getBurrow } from "../../utils";
import { expandTokenDecimal } from "../helper";
import { ChangeMethodsLogic, ChangeMethodsOracle } from "../../interfaces";
import { Transaction } from "../wallet";
import { getMetadata, prepareAndExecuteTransactions } from "../tokens";
import { getAccountDetailed } from "../accounts";
import { prepareAndExecuteTransactions } from "../tokens";
import getAssets from "../../api/get-assets";
import { transformAssets } from "../../transformers/asstets";
import getAccount from "../../api/get-account";
Expand All @@ -17,19 +16,20 @@ export async function adjustCollateral({
extraDecimals,
amount,
isMax,
enable_pyth_oracle,
}: {
tokenId: string;
extraDecimals: number;
amount: string;
isMax: boolean;
enable_pyth_oracle: boolean;
}) {
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(
asset.isLpToken
Expand Down Expand Up @@ -68,7 +68,9 @@ export async function adjustCollateral({
receiverId: logicContract.contractId,
functionCalls: [
{
methodName: ChangeMethodsLogic[ChangeMethodsLogic.execute_with_pyth],
methodName: enable_pyth_oracle
? ChangeMethodsLogic[ChangeMethodsLogic.execute_with_pyth]
: ChangeMethodsLogic[ChangeMethodsLogic.execute],
gas: new BN("100000000000000"),
args: {
actions: [increaseCollateralTemplate],
Expand Down Expand Up @@ -103,24 +105,25 @@ export async function adjustCollateral({
}
await prepareAndExecuteTransactions([
{
// receiverId: oracleContract.contractId,
receiverId: logicContract.contractId,
receiverId: enable_pyth_oracle ? logicContract.contractId : oracleContract.contractId,
functionCalls: [
{
// methodName: ChangeMethodsOracle[ChangeMethodsOracle.oracle_call],
methodName: ChangeMethodsLogic[ChangeMethodsLogic.execute_with_pyth],
methodName: enable_pyth_oracle
? ChangeMethodsLogic[ChangeMethodsLogic.execute_with_pyth]
: ChangeMethodsOracle[ChangeMethodsOracle.oracle_call],
gas: new BN("100000000000000"),
// args: {
// receiver_id: logicContract.contractId,
// msg: JSON.stringify({
// Execute: {
// actions: [decreaseCollateralTemplate],
// },
// }),
// },
args: {
actions: [decreaseCollateralTemplate],
},
args: enable_pyth_oracle
? {
actions: [decreaseCollateralTemplate],
}
: {
receiver_id: logicContract.contractId,
msg: JSON.stringify({
Execute: {
actions: [decreaseCollateralTemplate],
},
}),
},
},
],
} as Transaction,
Expand Down
25 changes: 14 additions & 11 deletions store/actions/borrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ export async function borrow({
extraDecimals,
amount,
collateralType,
enable_pyth_oracle,
}: {
tokenId: string;
extraDecimals: number;
amount: string;
collateralType: string;
enable_pyth_oracle: boolean;
}) {
const { oracleContract, logicContract, account } = await getBurrow();
const { decimals } = (await getMetadata(tokenId))!;
Expand Down Expand Up @@ -121,20 +123,21 @@ export async function borrow({
}

transactions.push({
// receiverId: oracleContract.contractId,
receiverId: logicContract.contractId,
receiverId: enable_pyth_oracle ? logicContract.contractId : oracleContract.contractId,
functionCalls: [
{
// methodName: ChangeMethodsOracle[ChangeMethodsOracle.oracle_call],
methodName: ChangeMethodsLogic[ChangeMethodsLogic.execute_with_pyth],
methodName: enable_pyth_oracle
? ChangeMethodsLogic[ChangeMethodsLogic.execute_with_pyth]
: ChangeMethodsOracle[ChangeMethodsOracle.oracle_call],
gas: new BN("100000000000000"),
// args: {
// receiver_id: logicContract.contractId,
// msg: JSON.stringify(borrowTemplate),
// },
args: {
actions: borrowTemplate.Execute.actions,
},
args: enable_pyth_oracle
? {
actions: borrowTemplate.Execute.actions,
}
: {
receiver_id: logicContract.contractId,
msg: JSON.stringify(borrowTemplate),
},
},
],
});
Expand Down
81 changes: 42 additions & 39 deletions store/actions/repayFromDeposits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ export async function repayFromDeposits({
extraDecimals,
position,
isMax,
enable_pyth_oracle,
}: {
tokenId: string;
amount: string;
extraDecimals: number;
position: string;
isMax: boolean;
enable_pyth_oracle: boolean;
}) {
// TODO repay from supplied
const { logicContract, oracleContract } = await getBurrow();
Expand Down Expand Up @@ -66,47 +68,48 @@ export async function repayFromDeposits({
},
};
transactions.push({
// receiverId: oracleContract.contractId,
receiverId: logicContract.contractId,
receiverId: enable_pyth_oracle ? logicContract.contractId : oracleContract.contractId,
functionCalls: [
{
// methodName: ChangeMethodsOracle[ChangeMethodsOracle.oracle_call],
methodName: ChangeMethodsLogic[ChangeMethodsLogic.execute_with_pyth],
// args: {
// receiver_id: logicContract.contractId,
// msg: JSON.stringify({
// Execute: {
// actions: [
// ...(decreaseCollateralAmount.gt(0)
// ? [
// {
// DecreaseCollateral: {
// token_id: tokenId,
// amount: decreaseCollateralAmount.toFixed(0),
// },
// },
// ]
// : []),
// repayTemplate,
// ],
// },
// }),
// },
args: {
actions: [
...(decreaseCollateralAmount.gt(0)
? [
{
DecreaseCollateral: {
token_id: tokenId,
amount: decreaseCollateralAmount.toFixed(0),
},
},
]
: []),
repayTemplate,
],
},
methodName: enable_pyth_oracle
? ChangeMethodsLogic[ChangeMethodsLogic.execute_with_pyth]
: ChangeMethodsOracle[ChangeMethodsOracle.oracle_call],
args: enable_pyth_oracle
? {
actions: [
...(decreaseCollateralAmount.gt(0)
? [
{
DecreaseCollateral: {
token_id: tokenId,
amount: decreaseCollateralAmount.toFixed(0),
},
},
]
: []),
repayTemplate,
],
}
: {
receiver_id: logicContract.contractId,
msg: JSON.stringify({
Execute: {
actions: [
...(decreaseCollateralAmount.gt(0)
? [
{
DecreaseCollateral: {
token_id: tokenId,
amount: decreaseCollateralAmount.toFixed(0),
},
},
]
: []),
repayTemplate,
],
},
}),
},
},
],
});
Expand Down
Loading

0 comments on commit 74071d3

Please sign in to comment.