Skip to content

Commit

Permalink
fuel depositsfor erc20
Browse files Browse the repository at this point in the history
  • Loading branch information
killroy192 committed Jul 27, 2024
1 parent e9df93f commit bfb925d
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 24 deletions.
5 changes: 3 additions & 2 deletions .apprc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"cli": {
"cluster_id": "fuelDust2",
"cluster_id": "fuelDust",
"funding": {
"chain": "eth",
"filters": [
Expand All @@ -20,7 +20,8 @@
"miller": "0x0000000000000000000000000000000000000000"
},
"fuel": {
"minDeposit": 30
"minDeposit": 30,
"token": "0x8c9532a60E0E7C6BbD2B2c1303F63aCE1c3E9811"
},
"scroll": {
"kelp": {
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ decrypt :; export FILE=$(in) && export OUTPUT=$(out) && npx tsx src/cli/decrypt.

funding :; npx tsx src/cli/funding.ts

fuel :; npx tsx src/cli/fuel.ts
fuelNative :; npx tsx src/cli/fuelNative.ts

fuelERC :; npx tsx src/cli/fuelNative.ts

scroll-kelp :; npx tsx src/cli/scroll.kelp.ts

Expand Down
10 changes: 0 additions & 10 deletions src/cli/fuel.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/cli/fuelERC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { initERCFuelDeposits } from "src/core/fuel";
import { getAppConf } from "src/libs/configs";
import { logger } from "src/logger";

(async function main() {
logger.info("initiated", { label: "CLI::fuelERC" });
const conf = await getAppConf();
await initERCFuelDeposits(conf.cli.fuel.token);
logger.info("finished", { label: "CLI::fuelERC" });
})();
10 changes: 10 additions & 0 deletions src/cli/fuelNative.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { initNativeFuelDeposits } from "src/core/fuel";
import { getAppConf } from "src/libs/configs";
import { logger } from "src/logger";

(async function main() {
logger.info("initiated", { label: "CLI::fuelNative" });
const conf = await getAppConf();
await initNativeFuelDeposits(conf.cli.fuel.minDeposit);
logger.info("finished", { label: "CLI::fuelNative" });
})();
73 changes: 73 additions & 0 deletions src/core/fuel/depositERC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Hex } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import * as chains from "viem/chains";
import { FUEL_POINTS_CONTRACT_ABI, FUEL_POINTS_CONTRACT } from "src/constants/fuel";
import { getClient, getPublicClient } from "src/libs/clients";
import Clock from "src/libs/clock";
import { getEVMWallets } from "src/libs/configs";
import { getBalance, approve } from "src/libs/erc20";
import { getRandomArbitrary, loopUntil } from "src/libs/shared";
import { logger } from "src/logger";
import { EVMWallet } from "src/types/configs";

const chain = chains.mainnet;
const localClock = new Clock();

const approveAndDepositERC = async (wallet: EVMWallet, token: Hex, amount: bigint) => {
await approve({
wallet,
tokenAddress: token,
spenderAddress: FUEL_POINTS_CONTRACT,
amount,
chain,
});
const walletClient = await getClient(chain);
const account = privateKeyToAccount(wallet.pkᵻ);
const { request } = await walletClient.simulateContract({
address: FUEL_POINTS_CONTRACT,
abi: FUEL_POINTS_CONTRACT_ABI,
functionName: "deposit",
args: [token, amount, 0x0],
value: amount,
account,
});
const txHash = await walletClient.writeContract(request);
const receipt = await walletClient.waitForTransactionReceipt({
hash: txHash,
});
logger.info(`tx hash: ${receipt.transactionHash}`, { label: "fuel::depositERC" });
return receipt;
};

const sleepForProperGasPrice = async () =>
await loopUntil(
async () => {
const gasPrice = await (await getPublicClient(chains.mainnet)).getGasPrice();
return gasPrice < 8000000000n; // 8 gwei
},
5 * 60 * 1000,
);

export async function initERCFuelDeposits(token: Hex) {
const EVMWallets = await getEVMWallets();

for (const wallet of EVMWallets) {
if (wallet === EVMWallets[0]) {
continue;
}
const [, balance] = await getBalance({
walletAddress: wallet.address,
tokenAddress: token,
chain: chain,
});

if (balance === 0n) {
continue;
}
localClock.markTime();
await sleepForProperGasPrice();
await approveAndDepositERC(wallet, token, balance);

await localClock.sleepMax(getRandomArbitrary(1 * 3_600_000, 2 * 3_600_000));
}
}
17 changes: 7 additions & 10 deletions src/core/fuel/deposit.fuel.ts → src/core/fuel/depositNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FUEL_POINTS_CONTRACT_ABI, FUEL_POINTS_CONTRACT } from "src/constants/fu
import { getPrice } from "src/libs/chainlink";
import { getClient, getPublicClient } from "src/libs/clients";
import Clock from "src/libs/clock";
import { getProfiles } from "src/libs/configs";
import { getEVMWallets } from "src/libs/configs";
import { refreshProxy } from "src/libs/proxify";
import { getRandomArbitrary, loopUntil } from "src/libs/shared";
import { logger } from "src/logger";
Expand All @@ -15,7 +15,7 @@ import { EVMWallet } from "src/types/configs";
const chain = chains.mainnet;
const localClock = new Clock();

const deposit = async (wallet: EVMWallet, toDeposit: bigint) => {
const depositNative = async (wallet: EVMWallet, toDeposit: bigint) => {
const axiosInstance = await refreshProxy();
const walletClient = await getClient(chain, axiosInstance);
const account = privateKeyToAccount(wallet.pkᵻ);
Expand All @@ -31,7 +31,7 @@ const deposit = async (wallet: EVMWallet, toDeposit: bigint) => {
const receipt = await walletClient.waitForTransactionReceipt({
hash: txHash,
});
logger.info(`tx hash: ${receipt.transactionHash}`, { label: "fuel::deposit" });
logger.info(`tx hash: ${receipt.transactionHash}`, { label: "fuel::depositNative" });
return receipt;
};

Expand Down Expand Up @@ -103,20 +103,17 @@ const getAccountToDeposit = async (decodedEVMAccounts: EVMWallet[], minDeposit:
const eligibleAccounts = await Promise.all(
decodedEVMAccounts.map(prepare({ expenses, ethPrice, minDeposit: parseEther(String(minDeposit)) })),
).then((accounts) => accounts.filter(({ isEligible }) => isEligible));
logger.info(`accounts to deposit ${eligibleAccounts.length}`, { label: "fuel::deposit" });
logger.info(`accounts to deposit ${eligibleAccounts.length}`, { label: "fuel::depositNative" });
return eligibleAccounts[0];
};

export async function initFuelDeposits(minDeposit: number) {
const profiles = await getProfiles();
const decodedEVMAccounts = Object.values(profiles).map(({ wallets }) => ({
...(wallets.evm as EVMWallet),
}));
export async function initNativeFuelDeposits(minDeposit: number) {
const decodedEVMAccounts = await getEVMWallets();

let accountToDeposit = await getAccountToDeposit(decodedEVMAccounts, minDeposit);

while (accountToDeposit !== undefined) {
await deposit(accountToDeposit.wallet, accountToDeposit.toDeposit);
await depositNative(accountToDeposit.wallet, accountToDeposit.toDeposit);
localClock.markTime();
accountToDeposit = await getAccountToDeposit(decodedEVMAccounts, minDeposit);
await localClock.sleepMax(getRandomArbitrary(1 * 3600000, 2 * 3600000));
Expand Down
3 changes: 2 additions & 1 deletion src/core/fuel/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { initFuelDeposits } from "./deposit.fuel";
export { initNativeFuelDeposits } from "./depositNative";
export { initERCFuelDeposits } from "./depositERC";
1 change: 1 addition & 0 deletions src/types/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export type AppConfig = {
};
readonly fuel: {
readonly minDeposit: number;
readonly token: Hex;
};
readonly scroll: {
readonly kelp: {
Expand Down

0 comments on commit bfb925d

Please sign in to comment.