Skip to content

Commit

Permalink
feat(heartbeat): Add heartbeat as string to logs, improve greedy gase…
Browse files Browse the repository at this point in the history
…stimation (#89)

feat(heartbeat): Add heartbeat as string to logs, improve greedy gas estimation
  • Loading branch information
lnist authored Jun 29, 2023
1 parent 4a2a768 commit 0581027
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
13 changes: 8 additions & 5 deletions packages/bot-arbitrage/src/ArbBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ export class ArbBot {
askTransaction: ethers.ContractTransaction;
bidTransaction: ethers.ContractTransaction;
}> {
logger.info("Checking whether Mangrove market can be arb'ed...", {
base: market.base.name,
quote: market.quote.name,
contextInfo,
});
logger.info(
"Heartbeat - Checking whether Mangrove market can be arb'ed...",
{
base: market.base.name,
quote: market.quote.name,
contextInfo,
}
);
this.#latestMarketActivity.latestBlock =
this.mgv.reliableProvider.blockManager.getLastBlock();
this.#latestMarketActivity.lastActive = new Date().toISOString();
Expand Down
2 changes: 1 addition & 1 deletion packages/bot-cleaning/src/MarketCleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class MarketCleaner {
try {
this.#isCleaning = true;

logger.info("Cleaning market", {
logger.info("Heartbeat - Cleaning market", {
base: this.#market.base.name,
quote: this.#market.quote.name,
contextInfo,
Expand Down
74 changes: 42 additions & 32 deletions packages/bot-taker-greedy/src/OfferTaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,22 @@ export class OfferTaker {
}

async #tradeIfPricesAreBetterThanExternalSignal(): Promise<void> {
const block = this.#market.mgv.reliableProvider.blockManager.getLastBlock();
const contextInfo = `block#=${block.number}`;

// const baseTokenBalancePromise = this.#market.base.contract.balanceOf(
// this.#takerAddress
// );
// const quoteTokenBalancePromise = this.#market.quote.contract.balanceOf(
// this.#takerAddress
// );
const externalPrice = await this.#getExternalPrice();
const externalPrice = await this.#getExternalPrice(contextInfo);

if (externalPrice === undefined) {
logger.info(
"No external price found, so not buying anything at this time",
"Heartbeat - No external price found, so not buying anything at this time",
{
contextInfo: "taker",
contextInfo,
base: this.#market.base.name,
quote: this.#market.quote.name,
}
Expand All @@ -136,21 +139,23 @@ export class OfferTaker {
const asksTradePromise =
this.#tradeOnSemibookIfPricesAreBetterThanExternalSignal(
"asks",
externalPrice
externalPrice,
contextInfo
);
const bidsTradePromise =
this.#tradeOnSemibookIfPricesAreBetterThanExternalSignal(
"bids",
externalPrice
externalPrice,
contextInfo
);
await asksTradePromise;
await bidsTradePromise;
}

async #getExternalPrice(): Promise<Big | undefined> {
async #getExternalPrice(contextInfo: string): Promise<Big | undefined> {
try {
logger.debug("Fetching external price", {
contextInfo: "taker",
contextInfo,
base: this.#market.base.name,
quote: this.#market.quote.name,
data: {
Expand All @@ -162,7 +167,7 @@ export class OfferTaker {
if (json[this.#market.quote.name] !== undefined) {
const externalPrice = new Big(json[this.#market.quote.name]);
logger.debug("Received external price", {
contextInfo: "taker",
contextInfo,
base: this.#market.base.name,
quote: this.#market.quote.name,
data: {
Expand All @@ -176,7 +181,7 @@ export class OfferTaker {
logger.warn(
`Response did not contain a ${this.#market.quote.name} field`,
{
contextInfo: "taker",
contextInfo,
base: this.#market.base.name,
quote: this.#market.quote.name,
data: {
Expand All @@ -189,7 +194,7 @@ export class OfferTaker {
return;
} catch (e) {
logger.error(`Error encountered while fetching external price`, {
contextInfo: "taker",
contextInfo,
base: this.#market.base.name,
quote: this.#market.quote.name,
data: {
Expand All @@ -205,7 +210,8 @@ export class OfferTaker {

async #tradeOnSemibookIfPricesAreBetterThanExternalSignal(
ba: BA,
externalPrice: Big
externalPrice: Big,
contextInfo: string
): Promise<void> {
const semibook = this.#market.getSemibook(ba);

Expand All @@ -222,31 +228,29 @@ export class OfferTaker {
this.#market.trade.isPriceBetter(o.price, externalPrice, ba)
);
if (offersWithBetterThanExternalPrice.length <= 0) {
if (logger.getLevel() <= logger.levels.DEBUG) {
const block =
this.#market.mgv.reliableProvider.blockManager.getLastBlock();
logger.debug("No offer better than external price", {
contextInfo: "taker",
base: this.#market.base.name,
quote: this.#market.quote.name,
ba,
data: {
bestFetchedPrice: offers[0]?.price,
externalPrice: externalPrice,
blockNumber: block.number,
blockHash: block.hash,
},
});
}
const block =
this.#market.mgv.reliableProvider.blockManager.getLastBlock();
logger.info("Heartbeat - No offer better than external price", {
contextInfo,
base: this.#market.base.name,
quote: this.#market.quote.name,
ba,
data: {
bestFetchedPrice: offers[0]?.price,
externalPrice: externalPrice,
blockNumber: block.number,
blockHash: block.hash,
},
});
return;
}

const total = offersWithBetterThanExternalPrice
.slice(0, this.#takerConfig.offerCountCap - 1)
.reduce((v, o) => v.add(o[quoteSideOfOffers]), Big(0));

logger.debug(`Posting ${buyOrSell} market order`, {
contextInfo: "taker",
logger.info(`Heartbeat - Posting ${buyOrSell} market order`, {
contextInfo,
base: this.#market.base.name,
quote: this.#market.quote.name,
ba,
Expand All @@ -258,13 +262,19 @@ export class OfferTaker {
},
});
try {
const tradeParams = { total: total, price: externalPrice };
const gasLowerBound = await this.#market.trade.estimateGas(
buyOrSell,
tradeParams,
this.#market
);
const buyOrSellPromise = await this.#market[buyOrSell](
{ total: total, price: externalPrice },
{ ...tradeParams, gasLowerBound },
{}
);
const result = await buyOrSellPromise.result;
logger.info(`Successfully completed ${buyOrSell} order`, {
contextInfo: "taker",
contextInfo,
base: this.#market.base.name,
quote: this.#market.quote.name,
ba,
Expand All @@ -282,7 +292,7 @@ export class OfferTaker {
});
} catch (e) {
logger.error(`Error occurred while ${buyOrSell}ing`, {
contextInfo: "taker",
contextInfo,
base: this.#market.base.name,
quote: this.#market.quote.name,
ba,
Expand Down
9 changes: 6 additions & 3 deletions packages/bot-updategas/src/GasUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,12 @@ export class GasUpdater {
public async checkSetGasprice(contextInfo?: string): Promise<void> {
//NOTE: Possibly suitable protection against reentrancy

logger.info(`Checking whether Mangrove gas price needs updating...`, {
contextInfo,
});
logger.info(
`Heartbeat - Checking whether Mangrove gas price needs updating...`,
{
contextInfo,
}
);

const globalConfig = await this.#mangrove.config();

Expand Down

0 comments on commit 0581027

Please sign in to comment.