Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit number of orders in withdraw script #72

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/tasks/selfSell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import {
} from "../ts/api";

import {
APP_DATA,
assertNotBuyingNativeAsset,
getQuote,
computeValidTo,
APP_DATA,
getQuote,
MAX_ORDER_VALIDITY_SECONDS,
Quote,
} from "./dump";
Expand All @@ -47,13 +47,13 @@ import { Align, displayTable } from "./ts/table";
import { Erc20Token, erc20Token } from "./ts/tokens";
import { prompt } from "./ts/tui";
import {
formatGasCost,
formatTokenValue,
formatUsdValue,
REFERENCE_TOKEN,
ReferenceToken,
usdValue,
usdValueOfEth,
formatGasCost,
} from "./ts/value";
import { BalanceOutput, getAmounts } from "./withdraw";
import { ignoredTokenMessage } from "./withdraw/messages";
Expand Down Expand Up @@ -522,6 +522,7 @@ interface SelfSellInput {
domainSeparator: TypedDataDomain;
solverIsSafe: boolean;
notifySlackChannel?: string;
maxOrders: number;
}

async function prepareOrders({
Expand All @@ -545,6 +546,7 @@ async function prepareOrders({
dryRun,
gasEstimator,
domainSeparator,
maxOrders,
}: SelfSellInput): Promise<{
orders: OrderDetails[];
finalSettlement: EncodedSettlement | null;
Expand Down Expand Up @@ -668,6 +670,18 @@ async function prepareOrders({
console.log("No tokens to sell.");
return { orders: [], finalSettlement: null };
}
if (orders.length > maxOrders) {
console.log(`Truncating total of ${orders.length} order to ${maxOrders}`);
// Remove the least profitable orders
orders = orders
.sort((o1, o2) => {
const first_proceeds = o1.sellAmountUsd.sub(o1.feeUsd);
const second_proceeds = o2.sellAmountUsd.sub(o2.feeUsd);
// We want to sort in descending order
return first_proceeds.lt(second_proceeds) ? 1 : -1;
})
.slice(0, maxOrders);
}
displayOrders(orders, usdReference, toToken);

const { finalSettlement, transactionEthCost, transactionUsdCost, soldValue } =
Expand Down Expand Up @@ -897,6 +911,12 @@ const setupSelfSellTask: () => void = () =>
"notifySlackChannel",
"The slack channel id to send the proposed transaction to (requires SLACK_TOKEN env variable to be set)",
)
.addOptionalParam(
"maxOrders",
"Limit the amount of orders per settlement to this value.",
50,
types.int,
)
.setAction(
async (
{
Expand All @@ -915,6 +935,7 @@ const setupSelfSellTask: () => void = () =>
safe,
notifySlackChannel,
doNotPrompt,
maxOrders,
},
hre: HardhatRuntimeEnvironment,
) => {
Expand Down Expand Up @@ -981,6 +1002,7 @@ const setupSelfSellTask: () => void = () =>
solverIsSafe: safe,
notifySlackChannel,
doNotPrompt,
maxOrders,
});
},
);
Expand Down
Loading