Skip to content

Commit

Permalink
refactor: error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Aug 26, 2024
1 parent 0eb6b07 commit 7045f4e
Showing 1 changed file with 21 additions and 34 deletions.
55 changes: 21 additions & 34 deletions packages/cli/source/commands/convert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export default function Convert({ options }: Props) {
vTokenAddress,
amountOut,
);

if (+underlyingUsdValue > minTradeUsd) {
if (+underlyingUsdValue > maxTradeUsd) {
amountOut = parseUnits((maxTradeUsd / +underlyingPriceUsd.toString()).toString(), underlyingDecimals);
Expand All @@ -200,49 +201,35 @@ export default function Convert({ options }: Props) {

const maxMinIncome = ((amount * BigInt(10000 + minIncomeBp)) / 10000n - amount) * -1n;

if (trade && ((profitable && minIncome > 0n) || !profitable)) {
const context = {
converter: t.tokenConverter,
tokenToReceiveFromConverter: t.assetOut.address,
tokenToSendToConverter: t.assetIn.address,
amount,
minIncome,
percentage: Number(minIncome) && Number(amount) && Number((minIncome * 10000000n) / amount) / 10000000,
maxMinIncome,
}
if (t.accountBalanceAssetOut < minIncome * -1n && !profitable) {
dispatch({
error: "Insufficient wallet balance to pay min income",
type: "ExecuteTrade",
context: {
converter: t.tokenConverter,
tokenToReceiveFromConverter: t.assetOut.address,
tokenToSendToConverter: t.assetIn.address,
amount,
minIncome,
percentage: Number((minIncome * 10000000n) / amount) / 10000000,
maxMinIncome,
},
context,
});

await tokenConverter.arbitrage(t.tokenConverter, trade, amount, minIncome);
} else if (t.accountBalanceAssetOut < minIncome * -1n) {
} else if (minIncome < 1 && minIncome * -1n > maxMinIncome * -1n) {
dispatch({
type: "ExecuteTrade",
error: "Insufficient wallet balance to pay min income",
context: {
converter: t.tokenConverter,
tokenToReceiveFromConverter: t.assetOut.address,
tokenToSendToConverter: t.assetIn.address,
amount,
minIncome,
percentage: Number((minIncome * 10000000n) / amount) / 10000000,
maxMinIncome,
},
error: "Min income too high",
context,
});
} else if (minIncome < 1 && minIncome * -1n > maxMinIncome * -1n) {
} else if (profitable && minIncome < 0) {
dispatch({
error: "Conversion is not profitable",
type: "ExecuteTrade",
error: "Min income too high",
context: {
converter: t.tokenConverter,
tokenToReceiveFromConverter: t.assetOut.address,
tokenToSendToConverter: t.assetIn.address,
amount,
minIncome,
percentage: Number((minIncome * 10000000n) / amount) / 10000000,
maxMinIncome,
},
context,
});
} else if (trade) {
await tokenConverter.arbitrage(t.tokenConverter, trade, amount, minIncome);
}
}
}
Expand Down

0 comments on commit 7045f4e

Please sign in to comment.