Skip to content

Commit

Permalink
add filters support for funding
Browse files Browse the repository at this point in the history
  • Loading branch information
killroy192 committed Jul 15, 2024
1 parent 64596a8 commit e69adf0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
36 changes: 33 additions & 3 deletions src/cli/okxFunding.ts
Original file line number Diff line number Diff line change
@@ -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: [
Expand Down Expand Up @@ -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]);
})();
2 changes: 2 additions & 0 deletions src/packages/funding/filters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { noFuel } from "./noFuel";
export { onlyZero } from "./onlyZero";
12 changes: 12 additions & 0 deletions src/packages/funding/filters/noFuel.ts
Original file line number Diff line number Diff line change
@@ -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;
};
8 changes: 8 additions & 0 deletions src/packages/funding/filters/onlyZero.ts
Original file line number Diff line number Diff line change
@@ -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;
};
20 changes: 13 additions & 7 deletions src/packages/funding/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -16,7 +17,7 @@ const OKXChainToViem = {
};

export const initFunding = async (
onlyZero: boolean,
filters: FundingFilter[],
minBalance: number,
maxBalance: number,
withdrawChain: WithdrawChain,
Expand All @@ -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;
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/packages/funding/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PublicClient } from "viem";

export type FundingFilter = (client: PublicClient, address: `0x${string}`) => Promise<boolean>;

0 comments on commit e69adf0

Please sign in to comment.