diff --git a/ts/config.ts b/ts/config.ts index b437de4..600afab 100644 --- a/ts/config.ts +++ b/ts/config.ts @@ -104,4 +104,5 @@ export interface IConfig { tokenListStrategy: "explorer" | "chain"; lookbackRange: number; confirmDrip: boolean; + leaveDust: BigNumber; } diff --git a/ts/drip/dripItAll.ts b/ts/drip/dripItAll.ts index aab0d9c..9b3aa38 100644 --- a/ts/drip/dripItAll.ts +++ b/ts/drip/dripItAll.ts @@ -25,7 +25,7 @@ export async function dripItAll( tokensToSwap.map((token) => ({ symbol: token.symbol, address: token.address, - balance: formatUnits(token.balance, token.decimals), + balance: formatUnits(token.adjustedBalance, token.decimals), buyAmount: formatUnits(token.buyAmount, WETH_DECIMALS), needsApproval: token.needsApproval, })) diff --git a/ts/drip/getTokensToSwap.ts b/ts/drip/getTokensToSwap.ts index 92e3001..5ded59c 100644 --- a/ts/drip/getTokensToSwap.ts +++ b/ts/drip/getTokensToSwap.ts @@ -12,7 +12,7 @@ import { getAllowances } from "./getAllowances"; export interface GetTokensToSwapResult { buyAmount: BigNumber; - balance: BigNumber; + adjustedBalance: BigNumber; allowance: BigNumber; needsApproval: boolean; address: string; @@ -45,7 +45,7 @@ export async function getTokensToSwap( // minValue filter again with _real_ balance const unfilteredWithBalanceAndAllowance = unfiltered.map((token, idx) => ({ ...token, - balance: balances[idx], + adjustedBalance: balances[idx].sub(config.leaveDust), allowance: allowances[idx], needsApproval: allowances[idx].lt(balances[idx]), })); @@ -56,7 +56,7 @@ export async function getTokensToSwap( unfilteredWithBalanceAndAllowance.map((token) => orderBookApi.getQuote({ sellToken: token.address, - sellAmountBeforeFee: token.balance.toString(), + sellAmountBeforeFee: token.adjustedBalance.toString(), kind: OrderQuoteSideKindSell.SELL, buyToken: config.wrappedNativeToken, from: config.gpv2Settlement, diff --git a/ts/drip/postOrders.ts b/ts/drip/postOrders.ts index 2268782..6a12e2d 100644 --- a/ts/drip/postOrders.ts +++ b/ts/drip/postOrders.ts @@ -22,7 +22,7 @@ export async function postOrders( orderBookApi.sendOrder({ sellToken: token.address, buyToken: config.wrappedNativeToken, - sellAmount: token.balance.toString(), + sellAmount: token.adjustedBalance.toString(), buyAmount: token.buyAmount.toString(), validTo: nextValidTo, appData: appDataContent, diff --git a/ts/drip/swapTokens.ts b/ts/drip/swapTokens.ts index f3bb878..780e156 100644 --- a/ts/drip/swapTokens.ts +++ b/ts/drip/swapTokens.ts @@ -50,7 +50,7 @@ export const swapTokens = async ( const toDrip = toActuallySwap.map((token) => ({ token: token.address, - sellAmount: token.balance, + sellAmount: token.adjustedBalance, buyAmount: token.buyAmount, })); diff --git a/ts/utils/readConfig.ts b/ts/utils/readConfig.ts index 89c6d4d..0c9a707 100644 --- a/ts/utils/readConfig.ts +++ b/ts/utils/readConfig.ts @@ -72,6 +72,14 @@ export async function readConfig(): Promise< "-c, --confirm-drip", "Ask for confirmation before dripping" ).default(false) + ) + .addOption( + new Option( + "--leave-dust ", + "Amount of each token (in wei) to leave in the contract after swapping" + ) + .default(BigNumber.from(10)) + .argParser((x) => BigNumber.from(x)) ); program.parse(); @@ -88,6 +96,7 @@ export async function readConfig(): Promise< lookbackRange, tokenListStrategy, confirmDrip, + leaveDust, } = options; const network = selectedNetwork || "mainnet"; @@ -147,6 +156,7 @@ export async function readConfig(): Promise< lookbackRange, targetSafe, confirmDrip, + leaveDust, }, provider, ];