Skip to content

Commit

Permalink
Merge pull request #74 from nick134-bit/ignorev2_fix+
Browse files Browse the repository at this point in the history
Ignore Spam Txs, Switch RPC if Error, Query RPCs
  • Loading branch information
SirTLB authored Apr 14, 2023
2 parents 321afcd + 440734e commit d3ec8fb
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 81 deletions.
3 changes: 2 additions & 1 deletion .env.injective.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ 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
CHAIN_PREFIX="inj"
RPC_URL="https://ww-juno-rpc.polkachu.com" #irrelevant overwritten by k8s frominjective
USE_RPC_URL_SCRAPER="0"
RPC_URL="" #change this
GAS_UNIT_PRICE="0.0004"
FLASHLOAN_ROUTER_ADDRESS="inj1hyja4uyjktpeh0fxzuw2fmjudr85rk2qxgqkvu"
FLASHLOAN_FEE="0.15"
7 changes: 6 additions & 1 deletion .env.juno.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ SKIP_URL= "http://juno-1-api.skip.money"
SKIP_BID_WALLET= "juno10g0l3hd9sau3vnjrayjhergcpxemucxcspgnn4"
SKIP_BID_RATE="0.31" #e.g. 31% of the profit is used as a bid to win the auction

# Addresses to Blacklist. Needed against Spam Txs.
# For more Info Discord Channel Developers/Bot-Support
IGNORE_ADDRESSES='[""]'

##JUNO SETTINGS
BASE_DENOM="ujuno" # The asset denom to be used as base asset. This should be the denom of a Native Token only.
GAS_DENOM="ujuno"
CHAIN_PREFIX="juno"
RPC_URL="https://juno-rpc.reece.sh" #change this
USE_RPC_URL_SCRAPER="0"
RPC_URL=["https://juno-rpc.reece.sh"] #change this
GAS_UNIT_PRICE="0.0025"
FLASHLOAN_ROUTER_ADDRESS="juno1qa7vdlm6zgq3radal5sltyl4t4qd32feug9qs50kcxda46q230pqzny48s"
FACTORIES_TO_ROUTERS_MAPPING ='{"factory":"juno14m9rd2trjytvxvu4ldmqvru50ffxsafs8kequmfky7jh97uyqrxqs5xrnx","router":"juno128lewlw6kv223uw4yzdffl8rnh3k9qs8vrf6kef28579w8ygccyq7m90n2"},
Expand Down
3 changes: 2 additions & 1 deletion .env.terra.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ SKIP_BID_RATE="0.1" #e.g. 10% of the profit is used as a bid to win the auction
BASE_DENOM="uluna"
GAS_DENOM="uluna"
CHAIN_PREFIX="terra"
RPC_URL="" ##change this
USE_RPC_URL_SCRAPER="0"
RPC_URL=[""] ##change this
GAS_UNIT_PRICE="0.015"
FLASHLOAN_ROUTER_ADDRESS="terra1c8tpvta3umr4mpufvxmq39gyuw2knpyxyfgq8thpaeyw2r6a80qsg5wa00"
FLASHLOAN_FEE="0.15" #in %
Expand Down
14 changes: 11 additions & 3 deletions src/chains/defaults/queries/getPoolsFromFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ export async function getPoolsFromFactory(
const factorypairs: Array<{ pool: string; factory: string; router: string }> = [];
await Promise.all(
factoryMapping.map(async (factorymap) => {
let res: FactoryState = await chainOperator.queryContractSmart(factorymap.factory, {
pairs: { limit: 30 },
});
let res: FactoryState = { pairs: [] };
try {
res = await chainOperator.queryContractSmart(factorymap.factory, {
pairs: { limit: 30 },
});
} catch {
console.log("RPC err on getPoolsfromFactory ... switching RPC");
await chainOperator.client.getNewClients();
await getPoolsFromFactory(chainOperator, factoryMapping);
return;
}

res.pairs.map((factorypair) => {
factorypairs.push({
Expand Down
106 changes: 90 additions & 16 deletions src/core/chainOperator/chainAdapters/cosmjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,36 @@ import { ChainOperatorInterface, TxResponse } from "../chainOperatorInterface";
class CosmjsAdapter implements ChainOperatorInterface {
private _signingCWClient!: SigningCosmWasmClient; //used to sign transactions
private _tmClient!: Tendermint34Client; //used to broadcast transactions
private _httpClient: HttpBatchClient | HttpClient; //used to query rpc methods (unconfirmed_txs, account)
private _httpClient!: HttpBatchClient | HttpClient; //used to query rpc methods (unconfirmed_txs, account)
private _wasmQueryClient!: QueryClient & WasmExtension; //used to query wasm methods (contract states)
private _account!: AccountData;
private _publicAddress!: string;
private _accountNumber = 0;
private _sequence = 0;

private _chainPrefix: string;
private _chainId!: string;

private _signer!: DirectSecp256k1HdWallet;
private _skipBundleClient?: SkipBundleClient;
private _timeoutRPCs: Map<string, number>;

private _rpcUrls!: Array<string>;
private _denom!: string;
private _gasPrice!: string;
private _currRpcUrl: string;

/**
*
*/
constructor(botConfig: BotConfig) {
this._httpClient = new HttpBatchClient(botConfig.rpcUrl);
this._chainPrefix = botConfig.chainPrefix;
this._timeoutRPCs = new Map<string, number>();
this._currRpcUrl = botConfig.rpcUrls[0];
if (botConfig.skipConfig) {
this._skipBundleClient = new SkipBundleClient(botConfig.skipConfig.skipRpcUrl);
}
this._rpcUrls = botConfig.rpcUrls;
this._denom = botConfig.baseDenom;
this._gasPrice = botConfig.gasPrice;
}
/**
*
Expand All @@ -57,6 +67,12 @@ class CosmjsAdapter implements ChainOperatorInterface {
public get publicAddress(): string {
return this._publicAddress;
}
/**
*
*/
public set publicAddress(value) {
this._publicAddress = value;
}
/**
*
*/
Expand All @@ -68,24 +84,30 @@ class CosmjsAdapter implements ChainOperatorInterface {
*/
async init(botConfig: BotConfig) {
// derive signing wallet
const signer = await DirectSecp256k1HdWallet.fromMnemonic(botConfig.mnemonic, {
this._signer = await DirectSecp256k1HdWallet.fromMnemonic(botConfig.mnemonic, {
prefix: botConfig.chainPrefix,
});
this._signer = signer;

// connect to client and querier
this._signingCWClient = await SigningCosmWasmClient.connectWithSigner(botConfig.rpcUrl, signer, {
prefix: botConfig.chainPrefix,
gasPrice: GasPrice.fromString(botConfig.gasPrice + botConfig.baseDenom),
});
this._tmClient = await Tendermint34Client.create(this._httpClient);
this._wasmQueryClient = QueryClient.withExtensions(this._tmClient, setupWasmExtension, setupAuthExtension);
this._account = (await signer.getAccounts())[0];
await this.setClients(botConfig.rpcUrls[0]);
this._account = (await this._signer.getAccounts())[0];
const { accountNumber, sequence } = await this._signingCWClient.getSequence(this._account.address);
this._chainId = await this._signingCWClient.getChainId();
this._accountNumber = accountNumber;
this._sequence = sequence;
this._publicAddress = this._account.address;
this.sequence = sequence;
this.publicAddress = this._account.address;
}

/**
*
*/
async setClients(rpcUrl: string) {
this._httpClient = new HttpBatchClient(rpcUrl);
this._tmClient = await Tendermint34Client.create(this._httpClient);
this._wasmQueryClient = QueryClient.withExtensions(this._tmClient, setupWasmExtension, setupAuthExtension);
this._signingCWClient = await SigningCosmWasmClient.connectWithSigner(rpcUrl, this._signer, {
prefix: this._chainPrefix,
gasPrice: GasPrice.fromString(this._gasPrice + this._denom),
});
}
/**
*
Expand Down Expand Up @@ -155,6 +177,52 @@ class CosmjsAdapter implements ChainOperatorInterface {
return mempoolResult.result;
}

/**
* Sets new Clients for Mempoolloop.
*
*/
public async getNewClients(): Promise<string | void> {
let out: string;
const TIMEOUTDUR = 60000; // 10 Min timeout if error
let n = 0;
let urlString: string | undefined;
this._timeoutRPCs.set(this._currRpcUrl, Date.now());
while (!urlString && n < this._rpcUrls.length) {
const currTime: number = Date.now();

if (!this._timeoutRPCs.has(this._rpcUrls[n])) {
urlString = this._rpcUrls[n];
} else {
const errTime = this._timeoutRPCs.get(this._rpcUrls[n]);
if (errTime && errTime + TIMEOUTDUR <= currTime) {
this._timeoutRPCs.delete(this._rpcUrls[n]);
urlString = this._rpcUrls[n];
}
}
n++;
}
if (!urlString) {
console.log("All RPC's Timeouted");
let n: number = Date.now();
let nextUrl: string = this._currRpcUrl;
for (const [url, timeouted] of this._timeoutRPCs.entries()) {
if (timeouted < n) {
n = timeouted;
nextUrl = url;
}
}
await delay(TIMEOUTDUR + n - Date.now());
await this.setClients(nextUrl);
out = nextUrl;
} else {
console.log("Updating Clients to: " + urlString);
await this.setClients(urlString);
out = urlString;
}
console.log("Continue...");
this._currRpcUrl = out;
}

/**
*
*/
Expand All @@ -164,5 +232,11 @@ class CosmjsAdapter implements ChainOperatorInterface {
this._sequence = sequence;
}
}
/**
*
*/
function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export default CosmjsAdapter;
25 changes: 18 additions & 7 deletions src/core/chainOperator/chainAdapters/injective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { SkipBundleClient } from "@skip-mev/skipjs";
import { MsgSend as CosmJSMsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx";
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { MsgExecuteContract as CosmJSMsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx";
import { inspect } from "util";

import { BotConfig } from "../../types/base/botConfig";
import { Mempool } from "../../types/base/mempool";
Expand All @@ -42,7 +41,7 @@ class InjectiveAdapter implements ChainOperatorInterface {

private _network: Network;
private _publicKey: PublicKey;
private _publicAddress: string;
private _publicAddress!: string;

private _signer!: DirectSecp256k1HdWallet;
private _accountNumber = 0;
Expand All @@ -63,16 +62,16 @@ class InjectiveAdapter implements ChainOperatorInterface {
});
this._spotQueryClient = new IndexerGrpcSpotApi(endpoints.indexer);
this._wasmQueryClient = new ChainGrpcWasmApi(endpoints.grpc);
this._httpClient = new HttpBatchClient(botConfig.rpcUrl);
this._httpClient = new HttpBatchClient(botConfig.rpcUrls[0]);
this._chainId = network === Network.TestnetK8s ? ChainId.Testnet : ChainId.Mainnet;
this._network = network;
this._publicKey = privateKey.toPublicKey();
this._publicAddress = privateKey.toPublicKey().toAddress().address;
this.publicAddress = privateKey.toPublicKey().toAddress().address;
}
/**
*
*/
public get sequence() {
public get sequence(): number {
return this._sequence;
}
/**
Expand All @@ -87,6 +86,12 @@ class InjectiveAdapter implements ChainOperatorInterface {
public get publicAddress(): string {
return this._publicAddress;
}
/**
*
*/
public set publicAddress(value) {
this._publicAddress = value;
}
/**
*
*/
Expand All @@ -104,7 +109,7 @@ class InjectiveAdapter implements ChainOperatorInterface {
const baseAccount = BaseAccount.fromRestApi(accountDetailsResponse);
const accountDetails = baseAccount.toAccountDetails();
this._accountNumber = accountDetails.accountNumber;
this._sequence = accountDetails.sequence;
this.sequence = accountDetails.sequence;

const hdPath = stringToPath("m/44'/60'/0'/0/0");
this._signer = await DirectSecp256k1HdWallet.fromMnemonic(botConfig.mnemonic, {
Expand Down Expand Up @@ -241,7 +246,6 @@ class InjectiveAdapter implements ChainOperatorInterface {
} else {
signed = await this._skipBundleClient.signBundle([cosmTxRaw], this._signer, this._skipSigningAddress);
}
console.log(inspect(signed, { depth: null }));
const res = await this._skipBundleClient.sendBundle(signed, 0, true);
return res;
}
Expand Down Expand Up @@ -306,6 +310,13 @@ class InjectiveAdapter implements ChainOperatorInterface {
console.log(error);
}
}
/**
* Sets new Clients for Mempoolloop.
* TODO!!!
*/
public async getNewClients() {
throw new Error("Change Clients not implemented for Injective yet");
}
}

export default InjectiveAdapter;
27 changes: 23 additions & 4 deletions src/core/chainOperator/chainoperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,28 @@ export class ChainOperator {
const cosmjsClient = new CosmjsAdapter(botConfig);
await cosmjsClient.init(botConfig);
return new Promise((resolve, reject) => {
resolve(new ChainOperator(cosmjsClient, botConfig.rpcUrl));
resolve(new ChainOperator(cosmjsClient, botConfig.rpcUrls[0]));
});
}
/**
*
*/
async queryContractSmart(address: string, queryMsg: Record<string, unknown>): Promise<JsonObject> {
//Error handled in getPoolState.
return await this.client.queryContractSmart(address, queryMsg);
}

/**
*
*/
async queryMempool() {
return await this.client.queryMempool();
try {
return await this.client.queryMempool();
} catch (e) {
console.log(e);
await this.client.getNewClients();
return await this.client.queryMempool();
}
}
/**
*
Expand All @@ -61,7 +68,13 @@ export class ChainOperator {
fee?: StdFee | "auto",
memo?: string | undefined,
): Promise<TxResponse> {
return await this.client.signAndBroadcast(msgs, fee, memo);
try {
return await this.client.signAndBroadcast(msgs, fee, memo);
} catch (e) {
console.log(e);
await this.client.getNewClients();
return await this.client.signAndBroadcast(msgs, fee, memo);
}
}

/**
Expand All @@ -74,6 +87,12 @@ export class ChainOperator {
*
*/
async signAndBroadcastSkipBundle(messages: Array<EncodeObject>, fee: StdFee, memo?: string, otherTx?: TxRaw) {
return await this.client.signAndBroadcastSkipBundle(messages, fee, memo, otherTx);
try {
return await this.client.signAndBroadcastSkipBundle(messages, fee, memo, otherTx);
} catch (e) {
console.log(e);
await this.client.getNewClients();
return await this.client.signAndBroadcastSkipBundle(messages, fee, memo, otherTx);
}
}
}
Loading

0 comments on commit d3ec8fb

Please sign in to comment.