Skip to content

Commit

Permalink
feat: error messages, toast fixes & more UI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
borcherd committed Dec 16, 2024
1 parent 1f938be commit cff3174
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const AmountInput = ({
inputMode="decimal"
value={amount}
onChange={(e) => handleAmountChange(e.target.value)}
disabled={maxAmount === 0}
placeholder="0"
className="bg-transparent shadow-none min-w-[130px] h-auto py-0 pr-0 text-right outline-none focus-visible:outline-none focus-visible:ring-0 border-none text-base font-medium"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function useTradeSimulation({
} else if (simulationResult.simulationResult) {
return { simulationResult: simulationResult.simulationResult, actionMessage: null };
} else {
const errorMessage = DYNAMIC_SIMULATION_ERRORS.REPAY_COLLAT_FAILED_CHECK(props.bank.meta.tokenSymbol); // TODO: update
const errorMessage = DYNAMIC_SIMULATION_ERRORS.TRADE_FAILED_CHECK();
return { simulationResult: null, actionMessage: errorMessage };
}
} else {
Expand All @@ -125,20 +125,18 @@ export function useTradeSimulation({
props: CalculateLoopingProps
): Promise<{ actionTxns: TradeActionTxns | null; actionMessage: ActionMessageType | null }> => {
try {
const loopingResult = await generateTradeTx({
const tradingResult = await generateTradeTx({
...props,
});

if (loopingResult && "actionQuote" in loopingResult) {
return { actionTxns: loopingResult, actionMessage: null };
if (tradingResult && "actionQuote" in tradingResult) {
return { actionTxns: tradingResult, actionMessage: null };
} else {
const errorMessage =
loopingResult ?? DYNAMIC_SIMULATION_ERRORS.REPAY_COLLAT_FAILED_CHECK(props.borrowBank.meta.tokenSymbol);
// TODO: update
const errorMessage = tradingResult ?? DYNAMIC_SIMULATION_ERRORS.TRADE_FAILED_CHECK();
return { actionTxns: null, actionMessage: errorMessage };
}
} catch (error) {
return { actionTxns: null, actionMessage: STATIC_SIMULATION_ERRORS.REPAY_COLLAT_FAILED }; // TODO: update
return { actionTxns: null, actionMessage: STATIC_SIMULATION_ERRORS.TRADE_FAILED };
}
};

Expand Down Expand Up @@ -172,7 +170,7 @@ export function useTradeSimulation({
});

if (tradeActionTxns.actionMessage || tradeActionTxns.actionTxns === null) {
handleError(tradeActionTxns.actionMessage ?? STATIC_SIMULATION_ERRORS.REPAY_COLLAT_FAILED, {
handleError(tradeActionTxns.actionMessage ?? STATIC_SIMULATION_ERRORS.TRADE_FAILED, {
// TODO: update error message
setErrorMessage,
setSimulationResult,
Expand All @@ -198,7 +196,7 @@ export function useTradeSimulation({
});

if (simulationResult.actionMessage || simulationResult.simulationResult === null) {
handleError(simulationResult.actionMessage ?? STATIC_SIMULATION_ERRORS.REPAY_COLLAT_FAILED, {
handleError(simulationResult.actionMessage ?? STATIC_SIMULATION_ERRORS.TRADE_FAILED, {
// TODO: update
setErrorMessage,
setSimulationResult,
Expand Down Expand Up @@ -246,9 +244,7 @@ export function useTradeSimulation({
const { maxLeverage, ltv } = computeMaxLeverage(selectedBank.info.rawBank, selectedSecondaryBank.info.rawBank);

if (!maxLeverage) {
const errorMessage = DYNAMIC_SIMULATION_ERRORS.REPAY_COLLAT_FAILED_CHECK(
selectedSecondaryBank.meta.tokenSymbol
);
const errorMessage = DYNAMIC_SIMULATION_ERRORS.TRADE_FAILED_CHECK();
setErrorMessage(errorMessage);
} else {
setMaxLeverage(maxLeverage);
Expand Down
2 changes: 2 additions & 0 deletions apps/marginfi-v2-trading/src/context/TradeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const TradePovider: React.FC<{
state.initialized,
state.hydrationComplete,
]);

const [fetchPriorityFee] = useUiStore((state) => [state.fetchPriorityFee]);
const [isLoggedIn, setIsLoggedIn] = React.useState(false);

React.useEffect(() => {
Expand Down
8 changes: 7 additions & 1 deletion packages/marginfi-client-v2/src/models/account/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ExtendedV0Transaction,
getTxSize,
getAccountKeys,
MRGN_TX_TYPES,
} from "@mrgnlabs/mrgn-common";
import * as sb from "@switchboard-xyz/on-demand";
import { Address, BorshCoder, Idl, translateAddress } from "@coral-xyz/anchor";
Expand Down Expand Up @@ -727,7 +728,11 @@ class MarginfiAccountWrapper {
instructions: setupIxs,
}).compileToLegacyMessage();

additionalTxs.push(new VersionedTransaction(message));
additionalTxs.push(
addTransactionMetadata(new VersionedTransaction(message), {
type: "ATAS" as MRGN_TX_TYPES,
})
);
}

// if crank is needed, add it
Expand All @@ -741,6 +746,7 @@ class MarginfiAccountWrapper {
additionalTxs.push(
addTransactionMetadata(new VersionedTransaction(message), {
addressLookupTables: feedLuts,
type: "CRANK" as MRGN_TX_TYPES,
})
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { VersionedTransaction, Transaction, Signer, AddressLookupTableAccount } from "@solana/web3.js";

export type MRGN_TX_TYPES = "CRANK" | "SETUP" | "BUNDLE_TIP" | "MRGN_ACCOUNT_CREATION";
export type MRGN_TX_TYPES = "CRANK" | "SETUP" | "BUNDLE_TIP" | "MRGN_ACCOUNT_CREATION" | "ATAS";

export const MRGN_TX_TYPE_TOAST_MAP: Record<MRGN_TX_TYPES, string> = {
CRANK: "Updating latest prices",
SETUP: "Setting up token accounts",
BUNDLE_TIP: "Sending bundle tip",
MRGN_ACCOUNT_CREATION: "Creating marginfi account",
ATAS: "Creating associated token account",
};

export type ExtendedTransaction = Transaction & {
Expand Down
21 changes: 1 addition & 20 deletions packages/mrgn-utils/src/actions/individualFlows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,28 +640,9 @@ export async function trade({

try {
let sigs: string[] = [];

if (actionTxns?.actionTxn) {
const txns: SolanaTransaction[] = [...actionTxns.additionalTxns, actionTxns.actionTxn];
if (actionTxns.accountCreationTx) {
if (processOpts.broadcastType !== "RPC") {
await marginfiClient.processTransaction(actionTxns.accountCreationTx, {
...processOpts,
callback: (index, success, sig, stepsToAdvance) =>
success &&
multiStepToast.setSuccessAndNext(
stepsToAdvance,
sig,
composeExplorerUrl(sig, processOpts?.broadcastType)
),
}); // TODO: add sig saving & displaying
} else {
txns.push(actionTxns.accountCreationTx);
}
}

sigs = await marginfiClient.processTransactions(
txns,
[...actionTxns.additionalTxns, actionTxns.actionTxn],
{
...processOpts,
callback: (index, success, sig, stepsToAdvance) =>
Expand Down
12 changes: 12 additions & 0 deletions packages/mrgn-utils/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ export const STATIC_SIMULATION_ERRORS: { [key: string]: ActionMessageType } = {
actionMethod: "WARNING",
code: 124,
},
TRADE_FAILED: {
description: `Unable to execute trade, please try again.`,
isEnabled: false,
code: 142,
},
};

const createRepayCollatFailedCheck = (tokenSymbol?: string): ActionMessageType => ({
Expand All @@ -170,6 +175,12 @@ const createRepayCollatFailedCheck = (tokenSymbol?: string): ActionMessageType =
code: 141,
});

const createTradeFailedCheck = (): ActionMessageType => ({
description: `Unable to execute trade, please try again.`,
isEnabled: false,
code: 142,
});

const createInsufficientBalanceCheck = (tokenSymbol?: string): ActionMessageType => ({
description: `Insufficient ${tokenSymbol} in wallet.`,
isEnabled: false,
Expand Down Expand Up @@ -326,6 +337,7 @@ export const DYNAMIC_SIMULATION_ERRORS = {
EXISTING_ISO_BORROW_CHECK: createExistingIsolatedBorrowCheck,
INSUFFICIENT_BALANCE_CHECK: createInsufficientBalanceCheck,
REPAY_COLLAT_FAILED_CHECK: createRepayCollatFailedCheck,
TRADE_FAILED_CHECK: createTradeFailedCheck,
};

const createCustomError = (description: string): ActionMessageType => ({
Expand Down
6 changes: 6 additions & 0 deletions packages/mrgn-utils/src/toasts/toastUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ export class MultiStepToastHandle {
this._stepsWithStatus[this._stepIndex].status = "error";
this._stepsWithStatus[this._stepIndex].message = message;

for (let i = 0; i < this._stepsWithStatus.length; i++) {
if (this._stepsWithStatus[i].status === "pending") {
this._stepsWithStatus[i].status = "error";
}
}

for (let i = this._stepIndex + 1; i < this._stepsWithStatus.length; i++) {
this._stepsWithStatus[i].status = "canceled";
}
Expand Down

0 comments on commit cff3174

Please sign in to comment.