diff --git a/src/cli/okxFunding.ts b/src/cli/okxFunding.ts index 54a14d2..ee78e94 100644 --- a/src/cli/okxFunding.ts +++ b/src/cli/okxFunding.ts @@ -1,10 +1,39 @@ -import { confirm, input, select } from "@inquirer/prompts"; +import { input, select, confirm } from "@inquirer/prompts"; import { OKX_WITHDRAW_CHAINS, SupportedChains } from "src/libs/okx"; import { validatePositiveNumber } from "src/libs/validations"; import { initFunding } from "src/packages/funding"; +import { noFuel, onlyZero } from "src/packages/funding/filters"; + +const selectFilters = async () => { + const filters = []; + let choices = [ + { + name: "No fuel deposits", + value: noFuel, + }, + { + name: "Only zero balances", + value: onlyZero, + }, + ]; + let addFilter = await confirm({ message: "Add account selection criteria?", default: false }); + while (addFilter) { + const filter = await select({ + message: "Select criteria", + choices: choices, + }); + filters.push(filter); + choices = choices.filter(({ value }) => value !== filter); + if (choices.length > 0) { + addFilter = await confirm({ message: "Add more account selection criteria?", default: false }); + } else { + addFilter = false; + } + } + return filters; +}; (async function main() { - const onlyZero = await confirm({ message: "Fund only zero balances", default: true }); const fundingChain: SupportedChains = await select({ message: "Select withdrawal chain", choices: [ @@ -38,11 +67,12 @@ import { initFunding } from "src/packages/funding"; }, ], }); + const filters = await selectFilters(); const minAmount = await input({ message: "enter minimal wallet balance after top up in usd" }).then( validatePositiveNumber, ); const maxAmount = await input({ message: "enter maximum wallet balance after top up in usd" }).then( validatePositiveNumber, ); - return initFunding(onlyZero, minAmount, maxAmount, OKX_WITHDRAW_CHAINS[fundingChain]); + return initFunding(filters, minAmount, maxAmount, OKX_WITHDRAW_CHAINS[fundingChain]); })(); diff --git a/src/packages/funding/filters/index.ts b/src/packages/funding/filters/index.ts new file mode 100644 index 0000000..3854d68 --- /dev/null +++ b/src/packages/funding/filters/index.ts @@ -0,0 +1,2 @@ +export { noFuel } from "./noFuel"; +export { onlyZero } from "./onlyZero"; diff --git a/src/packages/funding/filters/noFuel.ts b/src/packages/funding/filters/noFuel.ts new file mode 100644 index 0000000..ca42985 --- /dev/null +++ b/src/packages/funding/filters/noFuel.ts @@ -0,0 +1,12 @@ +import { zeroAddress, PublicClient } from "viem"; +import { FUEL_POINTS_CONTRACT_ABI, FUEL_POINTS_CONTRACT } from "src/packages/fuel/constants"; + +export const noFuel = async (client: PublicClient, address: `0x${string}`) => { + const userBalanceInFuel = await client.readContract({ + address: FUEL_POINTS_CONTRACT, + abi: FUEL_POINTS_CONTRACT_ABI, + functionName: "getBalance", + args: [address, zeroAddress], + }); + return userBalanceInFuel === 0n; +}; diff --git a/src/packages/funding/filters/onlyZero.ts b/src/packages/funding/filters/onlyZero.ts new file mode 100644 index 0000000..6492f14 --- /dev/null +++ b/src/packages/funding/filters/onlyZero.ts @@ -0,0 +1,8 @@ +import { PublicClient } from "viem"; + +export const onlyZero = async (client: PublicClient, address: `0x${string}`) => { + const balance = await client.getBalance({ + address, + }); + return balance === 0n; +}; diff --git a/src/packages/funding/index.ts b/src/packages/funding/index.ts index aced820..57fce8f 100644 --- a/src/packages/funding/index.ts +++ b/src/packages/funding/index.ts @@ -1,5 +1,6 @@ import { formatEther, parseEther } from "viem"; import * as chains from "viem/chains"; +import { FundingFilter } from "./types"; import { getPrice, chainLinkAddresses } from "src/libs/chainlink"; import { getPublicClient } from "src/libs/clients"; import { withdrawETH, consolidateETH, WithdrawChain, OKX_WITHDRAW_CHAINS } from "src/libs/okx"; @@ -16,7 +17,7 @@ const OKXChainToViem = { }; export const initFunding = async ( - onlyZero: boolean, + filters: FundingFilter[], minBalance: number, maxBalance: number, withdrawChain: WithdrawChain, @@ -33,12 +34,15 @@ export const initFunding = async ( const balance = await publicClient.getBalance({ address, }); - if (onlyZero && balance > 0n) { - return { - address, - amount: "0", - withdrawChain, - }; + for (let filter of filters) { + const filterPassed = await filter(publicClient, address); + if (!filterPassed) { + return { + address, + amount: "0", + withdrawChain, + }; + } } const usdTargetBalance = String(getRandomArbitrary(minBalance, maxBalance)); const expectedBalance = (10n ** 18n * parseEther(usdTargetBalance)) / ethPrice; @@ -52,6 +56,8 @@ export const initFunding = async ( const config = rawConfig.filter((config) => parseFloat(config.amount) > 0); + console.log(config); + if (config.length > 0) { await consolidateETH(); const data = await withdrawETH(config, 4 * 3600000, 8 * 3600000); diff --git a/src/packages/funding/types.ts b/src/packages/funding/types.ts new file mode 100644 index 0000000..fb52664 --- /dev/null +++ b/src/packages/funding/types.ts @@ -0,0 +1,3 @@ +import { PublicClient } from "viem"; + +export type FundingFilter = (client: PublicClient, address: `0x${string}`) => Promise;