Skip to content

Commit

Permalink
When a leveraged loan gets created, set the leverage mode in the brow…
Browse files Browse the repository at this point in the history
…ser (#618)
  • Loading branch information
bpierre authored Nov 26, 2024
1 parent 210fe44 commit 2792384
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
16 changes: 11 additions & 5 deletions frontend/app/src/services/TransactionFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { LOCAL_STORAGE_PREFIX } from "@/src/constants";
import { getContracts } from "@/src/contracts";
import { jsonParseWithDnum, jsonStringifyWithDnum } from "@/src/dnum-utils";
import { useAccount, useWagmiConfig } from "@/src/services/Ethereum";
import { useStoredState } from "@/src/services/StoredState";
import { claimCollateralSurplus } from "@/src/tx-flows/claimCollateralSurplus";
import { closeLoanPosition } from "@/src/tx-flows/closeLoanPosition";
import { earnClaimRewards } from "@/src/tx-flows/earnClaimRewards";
Expand Down Expand Up @@ -225,6 +226,7 @@ type FlowArgs<FR extends FlowRequest> = {
contracts: Contracts;
request: FR;
steps: FlowSteps | null;
storedState: ReturnType<typeof useStoredState>;
wagmiConfig: ReturnType<typeof useWagmiConfig>;
};

Expand Down Expand Up @@ -368,6 +370,7 @@ function useSteps<FR extends FlowRequest>({
onSteps: (steps: string[]) => void;
}) {
const contracts = getContracts();
const storedState = useStoredState();

const steps = useQuery({
enabled,
Expand All @@ -387,6 +390,7 @@ function useSteps<FR extends FlowRequest>({
contracts,
request: flow.request,
steps: flow.steps,
storedState,
wagmiConfig,
});
},
Expand Down Expand Up @@ -502,6 +506,7 @@ function useTransactionExecution({
const account = useAccount();
const contracts = getContracts();
const wagmiConfig = useWagmiConfig();
const storedState = useStoredState();

// step status updates
const setStepToAwaitingSignature = () => {
Expand Down Expand Up @@ -582,6 +587,7 @@ function useTransactionExecution({
contracts,
request: flow.request,
steps: flow.steps,
storedState,
wagmiConfig,
});
// check passed
Expand All @@ -597,6 +603,7 @@ function useTransactionExecution({
contracts,
flow,
flowDeclaration,
storedState,
wagmiConfig,
]);

Expand Down Expand Up @@ -650,6 +657,7 @@ function useTransactionExecution({
contracts,
request: flow.request,
steps: flow.steps,
storedState,
wagmiConfig,
});

Expand Down Expand Up @@ -680,13 +688,11 @@ function useResetQueriesOnPathChange(condition: boolean) {
useEffect(() => {
// when the condition changes, set a flag to invalidate
if (pathName === "/transactions" && condition) {
console.log("SETTING INVALIDATE ON PATH CHANGE");
invalidateOnPathChange.current = true;
return;
}
// when the path changes, invalidate if the flag is set
if (pathName !== "/transactions" && invalidateOnPathChange.current) {
console.log("CLEARING INVALIDATE ON PATH CHANGE");
queryClient.resetQueries();
invalidateOnPathChange.current = false;
}
Expand All @@ -699,10 +705,10 @@ const FlowContextStorage = {
},
get(): FlowContext<FlowRequest> | null {
try {
const storedState = (localStorage.getItem(TRANSACTION_FLOW_KEY) ?? "").trim();
if (!storedState) return null;
const storedFlowState = (localStorage.getItem(TRANSACTION_FLOW_KEY) ?? "").trim();
if (!storedFlowState) return null;

const { request, steps, account } = v.parse(FlowStateSchema, jsonParseWithDnum(storedState));
const { request, steps, account } = v.parse(FlowStateSchema, jsonParseWithDnum(storedFlowState));
const declaration = getFlowDeclaration(request.flowId);
const parsedRequest = declaration.parseRequest(request);
let parsedSteps = v.parse(FlowStepsSchema, steps);
Expand Down
14 changes: 12 additions & 2 deletions frontend/app/src/tx-flows/openLeveragePosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export const openLeveragePosition: FlowDeclaration<Request, Step> = {
throw new Error("Not implemented");
},

async postFlowCheck({ request, steps }) {
async postFlowCheck({ request, steps, storedState }) {
const lastStep = steps?.at(-1);

if (lastStep?.txStatus !== "post-check" || !isTroveId(lastStep.txReceiptData)) {
Expand All @@ -315,7 +315,17 @@ export const openLeveragePosition: FlowDeclaration<Request, Step> = {
const prefixedTroveId = getPrefixedTroveId(request.loan.collIndex, lastStep.txReceiptData);
while (true) {
const { trove } = await graphQuery(TroveByIdQuery, { id: prefixedTroveId });
if (trove !== null) return;
if (trove !== null) {
storedState.setState(({ loanModes }) => {
return {
loanModes: {
...loanModes,
[prefixedTroveId]: "leverage",
},
};
});
return;
}
}
},
};

0 comments on commit 2792384

Please sign in to comment.