Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gas padding everywhere #39

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/knockout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber } from "ethers";
import { BigNumber, BigNumberish } from "ethers";
import { TransactionResponse } from '@ethersproject/providers';
import { CrocContext } from './context';
import { CrocEthView, CrocTokenView, sortBaseQuoteViews, TokenQty } from './tokens';
Expand All @@ -7,6 +7,7 @@ import { KnockoutEncoder } from "./encoding/knockout";
import { ChainSpec } from "./constants";
import { CrocSurplusFlags, decodeSurplusFlag, encodeSurplusArg } from "./encoding/flags";
import { baseTokenForQuoteConc, bigNumToFloat, floatToBigNum, quoteTokenForBaseConc, roundForConcLiq } from "./utils";
import { GAS_PADDING } from "./utils";


export class CrocKnockoutHandle {
Expand Down Expand Up @@ -36,7 +37,7 @@ export class CrocKnockoutHandle {

const cmd = encoder.encodeKnockoutMint(await this.qty, lowerTick, upperTick,
this.sellBase, surplus);
return (await this.context).dex.userCmd(KNOCKOUT_PATH, cmd, { value: this.msgVal(surplus) })
return this.sendCmd(cmd, { value: await this.msgVal(surplus) })
}

async burn (opts?: CrocKnockoutOpts): Promise<TransactionResponse> {
Expand All @@ -48,7 +49,7 @@ export class CrocKnockoutHandle {

const cmd = encoder.encodeKnockoutBurnQty(await this.qty, lowerTick, upperTick,
this.sellBase, surplus);
return (await this.context).dex.userCmd(KNOCKOUT_PATH, cmd)
return this.sendCmd(cmd)
}

async burnLiq (liq: BigNumber, opts?: CrocKnockoutOpts): Promise<TransactionResponse> {
Expand All @@ -60,7 +61,7 @@ export class CrocKnockoutHandle {

const cmd = encoder.encodeKnockoutBurnLiq(roundForConcLiq(liq), lowerTick, upperTick,
this.sellBase, surplus);
return (await this.context).dex.userCmd(KNOCKOUT_PATH, cmd)
return this.sendCmd(cmd)
}

async recoverPost (pivotTime: number, opts?: CrocKnockoutOpts): Promise<TransactionResponse> {
Expand All @@ -72,7 +73,7 @@ export class CrocKnockoutHandle {

const cmd = encoder.encodeKnockoutRecover(pivotTime, lowerTick, upperTick,
this.sellBase, surplus);
return (await this.context).dex.userCmd(KNOCKOUT_PATH, cmd)
return this.sendCmd(cmd)
}

async willMintFail(): Promise<boolean> {
Expand All @@ -84,6 +85,14 @@ export class CrocKnockoutHandle {
(this.knockoutTick - await gridSize <= await marketTick)
}

private async sendCmd (calldata: string, txArgs?: { value?: BigNumberish }):
Promise<TransactionResponse> {
let cntx = await this.context
if (txArgs === undefined) { txArgs = {} }
const gasEst = await cntx.dex.estimateGas.userCmd(KNOCKOUT_PATH, calldata, txArgs)
Object.assign(txArgs, { gasLimit: gasEst.add(GAS_PADDING)})
return cntx.dex.userCmd(KNOCKOUT_PATH, calldata, txArgs);
}

private maskSurplusFlags (opts?: CrocKnockoutOpts): number {
if (!opts || !opts.surplus) { return encodeSurplusArg(false) }
Expand Down
12 changes: 8 additions & 4 deletions src/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BigNumber, BigNumberish } from 'ethers';
import { AddressZero } from '@ethersproject/constants';
import { PoolInitEncoder } from "./encoding/init";
import { CrocSurplusFlags, decodeSurplusFlag, encodeSurplusArg } from "./encoding/flags";
import { GAS_PADDING } from "./utils";

type PriceRange = [number, number]
type TickRange = [number, number]
Expand Down Expand Up @@ -134,6 +135,8 @@ export class CrocPoolView {
let calldata = encoder.encodeInitialize(await spotPrice)

let cntx = await this.context
const gasEst = await cntx.dex.estimateGas.userCmd(cntx.chain.proxyPaths.cold, calldata, txArgs)
Object.assign(txArgs, { gasLimit: gasEst.add(GAS_PADDING)})
return cntx.dex.userCmd(cntx.chain.proxyPaths.cold, calldata, txArgs)
}

Expand Down Expand Up @@ -189,12 +192,13 @@ export class CrocPoolView {
return this.sendCmd(calldata)
}

private async sendCmd (calldata: string, txArgs?: { value?: BigNumberish}):
private async sendCmd (calldata: string, txArgs?: { value?: BigNumberish }):
Promise<TransactionResponse> {
let cntx = await this.context
return txArgs ?
cntx.dex.userCmd(cntx.chain.proxyPaths.liq, calldata, txArgs) :
cntx.dex.userCmd(cntx.chain.proxyPaths.liq, calldata)
if (txArgs === undefined) { txArgs = {} }
const gasEst = await cntx.dex.estimateGas.userCmd(cntx.chain.proxyPaths.liq, calldata, txArgs)
Object.assign(txArgs, { gasLimit: gasEst.add(GAS_PADDING)})
return cntx.dex.userCmd(cntx.chain.proxyPaths.liq, calldata, txArgs);
}

private async mintAmbient (qty: TokenQty, isQtyBase: boolean,
Expand Down
7 changes: 5 additions & 2 deletions src/recipes/reposition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CrocSwapPlan } from "../swap";
import { CrocTokenView } from "../tokens";
import { encodeCrocPrice, tickToPrice } from "../utils";
import { baseTokenForConcLiq, concDepositBalance, quoteTokenForConcLiq } from "../utils/liquidity";
import { GAS_PADDING } from "../utils";


interface RepositionTarget {
Expand Down Expand Up @@ -34,8 +35,10 @@ export class CrocReposition {

async rebal(): Promise<TransactionResponse> {
const directive = await this.formatDirective()
const path = (await this.pool.context).chain.proxyPaths.long
return (await this.pool.context).dex.userCmd(path, directive.encodeBytes())
let cntx = await this.pool.context
const path = cntx.chain.proxyPaths.long
const gasEst = await cntx.dex.estimateGas.userCmd(path, directive.encodeBytes())
return cntx.dex.userCmd(path, directive.encodeBytes(), { gasLimit: gasEst.add(GAS_PADDING)})
}

async simStatic() {
Expand Down
2 changes: 1 addition & 1 deletion src/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CrocSurplusFlags, decodeSurplusFlag, encodeSurplusArg } from "./encodin
import { MAX_SQRT_PRICE, MIN_SQRT_PRICE } from "./constants";
import { AbiCoder } from "ethers/lib/utils";
import { CrocSlotReader } from "./slots";
import { GAS_PADDING } from "./utils";

/* Describes the predicted impact of a given swap.
* @property sellQty The total quantity of tokens predicted to be sold by the swapper to the dex.
Expand Down Expand Up @@ -202,7 +203,6 @@ export class CrocSwapPlan {
let txArgs = await this.attachEthMsg(surplusArg)

if (gasEst) {
const GAS_PADDING = 30000
Object.assign(txArgs, { gasLimit: gasEst.add(GAS_PADDING)})
}

Expand Down
4 changes: 3 additions & 1 deletion src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AddressZero, MaxUint256 } from "@ethersproject/constants";
import { MAX_LIQ } from "./constants";
import { toDisplayQty, fromDisplayQty } from "./utils/token";
import { BlockTag } from "./position";
import { GAS_PADDING } from "./utils";

/* Type representing specified token quantities. This type can either represent the raw non-decimalized
* on-chain value in wei, if passed as a BigNuber. Or it can represent the decimalized value if passed
Expand Down Expand Up @@ -173,8 +174,9 @@ export class CrocTokenView {

const txArgs = useMsgVal ? { value: await weiQty } : { }
let cntx = await this.context
const gasEst = await cntx.dex.estimateGas.userCmd(cntx.chain.proxyPaths.cold, cmd, txArgs)
Object.assign(txArgs, { gasLimit: gasEst.add(GAS_PADDING)})
return cntx.dex.userCmd(cntx.chain.proxyPaths.cold, cmd, txArgs)

}

readonly tokenAddr: string;
Expand Down
3 changes: 3 additions & 0 deletions src/utils/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { BigNumber, Contract, PopulatedTransaction, Transaction, utils } from "e
import { CrocEnv } from "../croc";
import { L1_GAS_PRICE_ORACLE_ABI } from "../abis/external/L1GasPriceOracle";

// Applied to all gas estimates.
export const GAS_PADDING = 30000;

/**
* Compute the raw transaction data for a given transaction.
*
Expand Down
Loading