Skip to content

Commit

Permalink
Merge pull request #83 from nick134-bit/reset_ignore_addresses
Browse files Browse the repository at this point in the history
feat: Reset Ignore Addresses after some time. Try without Skip.
  • Loading branch information
SirTLB authored May 31, 2023
2 parents e9abdbf + 996cbf8 commit fbd8070
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .env.injective.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ USE_SKIP="1"
SKIP_URL="https://injective-1-api.skip.money"
SKIP_BID_WALLET="inj17yqtnk08ly94lgz3fzagfu2twsws33z7sfsv46"
SKIP_BID_RATE="0.1" #e.g. 20% of the profit is used as a bid to win the auction
TRY_WITHOUT_SKIP="0" # Try without skip if the next Validator is not using skip. Send a standard Tx through the Mempool. More Risk! 0 == false

# Addresses to Blacklist. Needed against Spam Txs.
# For more Info Discord Channel Developers/Bot-Support
IGNORE_ADDRESSES='[""]'
TIMEOUT_DURATION="100" # Timeout Duration for Addresses in Blocks

## INJECTIVE SETTINGS
BASE_DENOM="peggy0xdAC17F958D2ee523a2206206994597C13D831ec7"
Expand Down
5 changes: 4 additions & 1 deletion .env.juno.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ SIGN_OF_LIFE="30" #Sign of Life in Minutes. E.g. "30"
##SKIP SPECIFIC ENVIRONMENT VARIABLES FOR CURRENT CHAIN
USE_SKIP= "1"
SKIP_URL= "http://juno-1-api.skip.money"
SKIP_BID_WALLET= "juno10g0l3hd9sau3vnjrayjhergcpxemucxcspgnn4"
SKIP_BID_WALLET= "juno1qan7ffv9kqpp704ywevq26hw53997ppdkwzs74"
SKIP_BID_RATE="0.31" #e.g. 31% of the profit is used as a bid to win the auction
TRY_WITHOUT_SKIP="0" # Try without skip if the next Validator is not using skip. Send a standard Tx through the Mempool. More Risk! 0 == false


# Addresses to Blacklist. Needed against Spam Txs.
# For more Info Discord Channel Developers/Bot-Support
IGNORE_ADDRESSES='[""]'
TIMEOUT_DURATION="100" # Timeout Duration for Addresses in Blocks

##JUNO SETTINGS
BASE_DENOM="ujuno" # The asset denom to be used as base asset. This should be the denom of a Native Token only.
Expand Down
4 changes: 3 additions & 1 deletion .env.terra.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ SIGN_OF_LIFE="30" #Sign of Life in Minutes. E.g. "30"
##SKIP SPECIFIC ENVIRONMENT VARIABLES FOR CURRENT CHAIN
USE_SKIP = "1"
SKIP_URL= "http://phoenix-1-api.skip.money"
SKIP_BID_WALLET= "terra1d5fzv2y8fpdax4u2nnzrn5uf9ghyu5sxr865uy"
SKIP_BID_WALLET= "terra1kdx075ghexr2l6mx4mgn37deshu9fn59r9zq9v"
SKIP_BID_RATE="0.1" #e.g. 10% of the profit is used as a bid to win the auction
TRY_WITHOUT_SKIP="0" # Try without skip if the next Validator is not using skip. Send a standard Tx through the Mempool. More Risk! 0 == false

# Addresses to Blacklist. Needed against Spam Txs.
# For more Info Discord Channel Developers/Bot-Support
IGNORE_ADDRESSES='[""]'
TIMEOUT_DURATION="100" # Timeout Duration for Addresses in Blocks

##TERRA SETTINGS
BASE_DENOM="uluna"
Expand Down
13 changes: 11 additions & 2 deletions src/core/chainOperator/chainoperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,17 @@ export class ChainOperator {
*
*/
async queryContractSmart(address: string, queryMsg: Record<string, unknown>): Promise<JsonObject> {
//Error handled in getPoolState.
return await this.client.queryContractSmart(address, queryMsg);
try {
return await this.client.queryContractSmart(address, queryMsg);
} catch (e: any) {
//custom error for initPools JunoSwapPoolState
if (e.message.includes("Query failed with (18):")) {
throw new Error(e.message);
}
console.log(e);
await this.client.getNewClients();
await this.client.queryContractSmart(address, queryMsg);
}
}

/**
Expand Down
30 changes: 25 additions & 5 deletions src/core/types/arbitrageloops/mempoolLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ChainOperator } from "../../chainOperator/chainoperator";
import { Logger } from "../../logging";
import { BotConfig } from "../base/botConfig";
import { LogType } from "../base/logging";
import { decodeMempool, flushTxMemory, Mempool, MempoolTx } from "../base/mempool";
import { decodeMempool, flushTxMemory, IgnoredAddresses, Mempool, MempoolTx } from "../base/mempool";
import { Path } from "../base/path";
import { applyMempoolMessagesOnPools, Pool } from "../base/pool";
/**
Expand All @@ -20,7 +20,7 @@ export class MempoolLoop {
pathlib: Array<Path>; //holds all known paths
CDpaths: Map<string, [number, number, number]>; //holds all cooldowned paths' identifiers
chainOperator: ChainOperator;
ignoreAddresses: { [index: string]: boolean };
ignoreAddresses: IgnoredAddresses;
botConfig: BotConfig;
logger: Logger | undefined;
// CACHE VALUES
Expand Down Expand Up @@ -56,11 +56,10 @@ export class MempoolLoop {
botConfig: BotConfig,
logger: Logger | undefined,
pathlib: Array<Path>,
ignoreAddresses: { [index: string]: boolean },
ignoreAddresses: IgnoredAddresses,
) {
this.pools = pools;
this.CDpaths = new Map<string, [number, number, number]>();

this.paths = paths;
this.arbitrageFunction = arbitrage;
this.updateStateFunction = updateState;
Expand Down Expand Up @@ -96,8 +95,14 @@ export class MempoolLoop {
this.totalBytes = +this.mempool.total_bytes;
}

const mempoolTxs: Array<MempoolTx> = decodeMempool(this.mempool, this.ignoreAddresses);
const mempoolTxs: Array<MempoolTx> = decodeMempool(
this.mempool,
this.ignoreAddresses,
this.botConfig.timeoutDuration,
this.iterations,
);

// Checks if there is a SendMsg from a blacklisted Address, if so add the reciever to the timeouted addresses
if (mempoolTxs.length === 0) {
continue;
} else {
Expand Down Expand Up @@ -198,6 +203,21 @@ export class MempoolLoop {
}
});
}

/**
*
*/
public clearIgnoreAddresses() {
const keys = Object.keys(this.ignoreAddresses);
for (let i = 0; i < keys.length; i++) {
if (
this.ignoreAddresses[keys[i]].timeoutAt > 0 &&
this.ignoreAddresses[keys[i]].timeoutAt + this.ignoreAddresses[keys[i]].duration <= this.iterations
) {
delete this.ignoreAddresses[keys[i]];
}
}
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/core/types/arbitrageloops/nomempoolLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ export class NoMempoolLoop {
}
});
}

/**
*
*/
public clearIgnoreAddresses() {
return;
}
}

/**
Expand Down
35 changes: 25 additions & 10 deletions src/core/types/arbitrageloops/skipLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { SkipResult } from "../../chainOperator/skipclients";
import { Logger } from "../../logging";
import { BotConfig } from "../base/botConfig";
import { LogType } from "../base/logging";
import { decodeMempool, MempoolTx } from "../base/mempool";
import { decodeMempool, IgnoredAddresses, MempoolTx } from "../base/mempool";
import { Path } from "../base/path";
import { applyMempoolMessagesOnPools, Pool } from "../base/pool";
import { MempoolLoop } from "./mempoolLoop";
Expand Down Expand Up @@ -47,7 +47,7 @@ export class SkipLoop extends MempoolLoop {
logger: Logger | undefined,

pathlib: Array<Path>,
ignoreAddresses: { [index: string]: boolean },
ignoreAddresses: IgnoredAddresses,
) {
super(
pools,
Expand Down Expand Up @@ -88,7 +88,12 @@ export class SkipLoop extends MempoolLoop {
this.totalBytes = +this.mempool.total_bytes;
}

const mempoolTxs: Array<MempoolTx> = decodeMempool(this.mempool, this.ignoreAddresses);
const mempoolTxs: Array<MempoolTx> = decodeMempool(
this.mempool,
this.ignoreAddresses,
this.botConfig.timeoutDuration,
this.iterations,
);

if (mempoolTxs.length === 0) {
continue;
Expand Down Expand Up @@ -122,8 +127,11 @@ export class SkipLoop extends MempoolLoop {
);
return;
}

const skipFee = Math.max(Math.round(arbTrade.profit * this.botConfig.skipConfig.skipBidRate), 651);

const bidMsgEncoded = getSendMessage(
String(Math.max(Math.round(arbTrade.profit * this.botConfig.skipConfig.skipBidRate), 651)),
String(skipFee),
this.botConfig.gasDenom,
this.chainOperator.client.publicAddress,
this.botConfig.skipConfig.skipBidWallet,
Expand Down Expand Up @@ -153,13 +161,15 @@ export class SkipLoop extends MempoolLoop {
console.log(inspect(res, { depth: null }));

let logItem = "";
let logMessage = `**wallet:** ${this.chainOperator.client.publicAddress}\t **block:** ${res.result.desired_height}\t **profit:** ${arbTrade.profit}`;
let logMessage = `**wallet:** ${this.chainOperator.client.publicAddress}\t **block:** ${
res.result.desired_height
}\t **profit:** ${arbTrade.profit - skipFee}`;

if (res.result.code !== 0) {
logMessage += `\t **error code:** ${res.result.code}\n**error:** ${res.result.error}\n`;
}
if (res.result.code === 4) {
console.log("no skip validator up, trying default broadcast");
if (this.botConfig.skipConfig.tryWithoutSkip && res.result.code === 4) {
await this.logger?.sendMessage("no skip validator up, trying default broadcast", LogType.Console);
await this.trade(arbTrade);
}

Expand All @@ -171,7 +181,10 @@ export class SkipLoop extends MempoolLoop {
const logMessageCheckTx = `**CheckTx Error:** index: ${idx}\t ${String(item.log)}\n`;
logMessage = logMessage.concat(logMessageCheckTx);
if (toArbTrade?.message.sender && idx == 0 && item["code"] == "5") {
this.ignoreAddresses[toArbTrade.message.sender] = true;
this.ignoreAddresses[toArbTrade.message.sender] = {
timeoutAt: this.iterations,
duration: this.botConfig.timeoutDuration,
};
await this.logger?.sendMessage(
"Error on Trade from Address: " + toArbTrade.message.sender,
LogType.Console,
Expand All @@ -189,7 +202,10 @@ export class SkipLoop extends MempoolLoop {
logMessage = logMessage.concat(logMessageDeliverTx);
if (idx == 0 && (item["code"] == 10 || item["code"] == 5)) {
if (toArbTrade?.message.sender) {
this.ignoreAddresses[toArbTrade.message.sender] = true;
this.ignoreAddresses[toArbTrade.message.sender] = {
timeoutAt: this.iterations,
duration: this.botConfig.timeoutDuration,
};
await this.logger?.sendMessage(
"Error on Trade from Address: " + toArbTrade.message.sender,
LogType.Console,
Expand All @@ -209,7 +225,6 @@ export class SkipLoop extends MempoolLoop {
if (res.result.code != 4) {
this.cdPaths(arbTrade.path);
}

if (res.result.code === 0) {
this.chainOperator.client.sequence = this.chainOperator.client.sequence + 1;
}
Expand Down
13 changes: 9 additions & 4 deletions src/core/types/base/botConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import axios from "axios";
import { assert } from "console";

import { NativeAssetInfo } from "./asset";
import { IgnoredAddresses } from "./mempool";

interface SkipConfig {
useSkip: boolean;
skipRpcUrl: string;
skipBidWallet: string;
skipBidRate: number;
tryWithoutSkip: boolean;
}

interface LoggerConfig {
Expand All @@ -26,7 +28,7 @@ export interface BotConfig {
grpcUrl: string;
restUrl: string;
useRpcUrlScraper: boolean;
ignoreAddresses: { [index: string]: boolean };
ignoreAddresses: IgnoredAddresses;
poolEnvs: Array<{ pool: string; inputfee: number; outputfee: number; LPratio: number }>;
maxPathPools: number;
mappingFactoryRouter: Array<{ factory: string; router: string }>;
Expand All @@ -35,6 +37,7 @@ export interface BotConfig {
offerAssetInfo: NativeAssetInfo;
mnemonic: string;
useMempool: boolean;
timeoutDuration: number;
baseDenom: string;
gasDenom: string;
signOfLife: number;
Expand Down Expand Up @@ -87,12 +90,12 @@ export async function setBotConfig(envs: NodeJS.ProcessEnv): Promise<BotConfig>

const GAS_USAGE_PER_HOP = +envs.GAS_USAGE_PER_HOP;
const MAX_PATH_HOPS = +envs.MAX_PATH_HOPS; //required gas units per trade (hop)

const IGNORE_ADDRS: { [index: string]: boolean } = {};
const timeoutDuration = envs.TIMEOUT_DURATION === undefined ? 100 : Number(envs.TIMEOUT_DURATION);
const IGNORE_ADDRS: IgnoredAddresses = {};
// set ignored Addresses
if (envs.IGNORE_ADDRESSES) {
const addrs = JSON.parse(envs.IGNORE_ADDRESSES);
addrs.forEach((element: string) => (IGNORE_ADDRS[element] = true));
addrs.forEach((element: string) => (IGNORE_ADDRS[element] = { timeoutAt: 0, duration: timeoutDuration }));
}
// setup skipconfig if present
let skipConfig;
Expand All @@ -103,6 +106,7 @@ export async function setBotConfig(envs: NodeJS.ProcessEnv): Promise<BotConfig>
skipRpcUrl: envs.SKIP_URL ?? "",
skipBidWallet: envs.SKIP_BID_WALLET ?? "",
skipBidRate: envs.SKIP_BID_RATE === undefined ? 0 : +envs.SKIP_BID_RATE,
tryWithoutSkip: envs.TRY_WITHOUT_SKIP === "1" ? true : false,
};
}

Expand Down Expand Up @@ -159,6 +163,7 @@ export async function setBotConfig(envs: NodeJS.ProcessEnv): Promise<BotConfig>
gasPrice: envs.GAS_UNIT_PRICE,
profitThresholds: PROFIT_THRESHOLDS,
txFees: TX_FEES,
timeoutDuration: timeoutDuration,
skipConfig: skipConfig,
loggerConfig: loggerConfig,
signOfLife: SIGN_OF_LIFE,
Expand Down
Loading

0 comments on commit fbd8070

Please sign in to comment.