Skip to content

Commit

Permalink
Merge pull request #34 from Parifi/fix-gelato-gaslimit
Browse files Browse the repository at this point in the history
Add Gelato gas limit for transactions
  • Loading branch information
sudeepb02 committed Mar 26, 2024
2 parents ea73e2d + ed75100 commit 0a27bef
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@parifi/sdk",
"version": "0.1.1",
"version": "0.1.2",
"description": "Parifi SDK with common utility functions",
"files": [
"dist",
Expand Down
4 changes: 4 additions & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const DECIMAL_10 = new Decimal(10);
export const DECIMAL_ZERO = new Decimal(0);
export const DEFAULT_BATCH_COUNT = 10;

export const GAS_LIMIT_SETTLEMENT = 1000000 // 1M gas
export const GAS_LIMIT_LIQUIDATION = 6000000 // 6M gas


// Constants for Pyth price ids of collaterals
export const PYTH_ETH_USD_PRICE_ID_BETA = '0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6';
export const PYTH_ETH_USD_PRICE_ID_STABLE = '0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace';
Expand Down
29 changes: 25 additions & 4 deletions src/core/order-manager/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Market, Order, Position } from '../../interfaces/subgraphTypes';
import { Decimal } from 'decimal.js';
import { DECIMAL_ZERO, DEVIATION_PRECISION_MULTIPLIER, MAX_FEE, PRECISION_MULTIPLIER } from '../../common/constants';
import {
DECIMAL_ZERO,
DEVIATION_PRECISION_MULTIPLIER,
GAS_LIMIT_LIQUIDATION,
GAS_LIMIT_SETTLEMENT,
MAX_FEE,
PRECISION_MULTIPLIER,
} from '../../common/constants';
import { getAccruedBorrowFeesInMarket, getMarketUtilization } from '../data-fabric';
import { convertMarketAmountToCollateral } from '../price-feed';
import { Chain } from '@parifi/references';
Expand Down Expand Up @@ -329,14 +336,20 @@ export const liquidatePositionUsingGelato = async (
const orderManager = getOrderManagerInstance(chainId);
const { data: encodedTxData } = await orderManager.liquidatePosition.populateTransaction(positionId, priceUpdateData);

taskId = await executeTxUsingGelato(parifiContracts[chainId].OrderManager.address, chainId, gelatoKey, encodedTxData);
const gelatoGasLimit = BigInt(GAS_LIMIT_LIQUIDATION);
taskId = await executeTxUsingGelato(
parifiContracts[chainId].OrderManager.address,
chainId,
gelatoKey,
encodedTxData,
gelatoGasLimit,
);

// We need these console logs for feedback to Tenderly actions and other scripts
console.log('Task ID:', taskId);
return { gelatoTaskId: taskId };
};


// Settles an order using Gelato as the relayer
export const settleOrderUsingGelato = async (
chainId: Chain,
Expand All @@ -358,7 +371,15 @@ export const settleOrderUsingGelato = async (
const orderManager = getOrderManagerInstance(chainId);
const { data: encodedTxData } = await orderManager.settleOrder.populateTransaction(orderId, priceUpdateData);

taskId = await executeTxUsingGelato(parifiContracts[chainId].OrderManager.address, chainId, gelatoKey, encodedTxData);
const gelatoGasLimit = BigInt(GAS_LIMIT_SETTLEMENT);

taskId = await executeTxUsingGelato(
parifiContracts[chainId].OrderManager.address,
chainId,
gelatoKey,
encodedTxData,
gelatoGasLimit,
);

// We need these console logs for feedback to Tenderly actions and other scripts
console.log('Task ID:', taskId);
Expand Down
20 changes: 18 additions & 2 deletions src/core/parifi-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { AxiosInstance } from 'axios';
import { executeTxUsingGelato } from '../../gelato/gelato-function';
import { getAllPendingOrders, getPythPriceIdsForPositionIds } from '../../subgraph';
import { BatchExecute } from '../../interfaces/subgraphTypes';
import { DEFAULT_BATCH_COUNT, getPriceIdsForCollaterals } from '../../common';
import {
DEFAULT_BATCH_COUNT,
GAS_LIMIT_LIQUIDATION,
GAS_LIMIT_SETTLEMENT,
getPriceIdsForCollaterals,
} from '../../common';
import { checkIfOrderCanBeSettled } from '../order-manager';

// Returns an Order Manager contract instance without signer
Expand Down Expand Up @@ -83,11 +88,14 @@ export const batchSettlePendingOrdersUsingGelato = async (
const parifiUtils = getParifiUtilsInstance(chainId);
const { data: encodedTxData } = await parifiUtils.batchSettleOrders.populateTransaction(batchedOrders);

const gelatoGasLimit = BigInt(batchedOrders.length * GAS_LIMIT_SETTLEMENT);

taskId = await executeTxUsingGelato(
parifiContracts[chainId].ParifiUtils.address,
chainId,
gelatoKey,
encodedTxData,
gelatoGasLimit,
);
// We need these console logs for feedback to Tenderly actions and other scripts
console.log('Task ID:', taskId);
Expand Down Expand Up @@ -129,11 +137,14 @@ export const batchLiquidatePostionsUsingGelato = async (
const parifiUtils = getParifiUtilsInstance(chainId);
const { data: encodedTxData } = await parifiUtils.batchLiquidatePositions.populateTransaction(batchedPositions);

const gelatoGasLimit = BigInt(batchedPositions.length * GAS_LIMIT_LIQUIDATION);

taskId = await executeTxUsingGelato(
parifiContracts[chainId].ParifiUtils.address,
chainId,
gelatoKey,
encodedTxData,
gelatoGasLimit,
);
// We need these console logs for feedback to Tenderly actions and other scripts
console.log('Task ID:', taskId);
Expand Down Expand Up @@ -168,11 +179,14 @@ export const batchSettleOrdersUsingGelato = async (
const parifiUtils = getParifiUtilsInstance(chainId);
const { data: encodedTxData } = await parifiUtils.batchSettleOrders.populateTransaction(batchedOrders);

const gelatoGasLimit = BigInt(batchedOrders.length * GAS_LIMIT_SETTLEMENT);

taskId = await executeTxUsingGelato(
parifiContracts[chainId].ParifiUtils.address,
chainId,
gelatoKey,
encodedTxData,
gelatoGasLimit,
);
// We need these console logs for feedback to Tenderly actions and other scripts
console.log('Task ID:', taskId);
Expand Down Expand Up @@ -209,7 +223,9 @@ export const batchSettleOrdersUsingWallet = async (
wallet,
);

const tx = await parifiUtilsContract.batchSettleOrders(batchedOrders);
const txGasLimit = BigInt(batchedOrders.length * GAS_LIMIT_SETTLEMENT);

const tx = await parifiUtilsContract.batchSettleOrders(batchedOrders, { gasLimit: txGasLimit });
await tx.wait();
return { txHash: tx.hash };
}
Expand Down
14 changes: 12 additions & 2 deletions src/gelato/gelato-function.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { GelatoRelay, SponsoredCallRequest, TransactionStatusResponse } from '@gelatonetwork/relay-sdk';
import {
GelatoRelay,
RelayRequestOptions,
SponsoredCallRequest,
TransactionStatusResponse,
} from '@gelatonetwork/relay-sdk';
import { Chain } from '@parifi/references';

export const executeTxUsingGelato = async (
targetContractAddress: string,
chainId: Chain,
gelatoKey: string | undefined,
encodedTxData: string,
gelatoGasLimit?: bigint,
): Promise<string> => {
const request: SponsoredCallRequest = {
chainId: BigInt(chainId.toString()),
Expand All @@ -14,7 +20,11 @@ export const executeTxUsingGelato = async (
};

const relay = new GelatoRelay();
const { taskId } = await relay.sponsoredCall(request, gelatoKey || '');
const relayOptions: RelayRequestOptions = {
gasLimit: gelatoGasLimit || BigInt(5000000),
};

const { taskId } = await relay.sponsoredCall(request, gelatoKey || '', relayOptions);
return taskId;
};

Expand Down
7 changes: 6 additions & 1 deletion src/gelato/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ export class Gelato {
private rpcConfig: RpcConfig,
) {}

public async executeTxUsingGelato(targetContractAddress: string, encodedTxData: string): Promise<string> {
public async executeTxUsingGelato(
targetContractAddress: string,
encodedTxData: string,
gelatoGasLimit?: bigint,
): Promise<string> {
return await executeTxUsingGelato(
targetContractAddress,
this.rpcConfig.chainId,
this.gelatoConfig?.apiKey,
encodedTxData,
gelatoGasLimit,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/subgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class Subgraph {
}

////////////////////////////////////////////////////////////////
////////////////////// MARKET ////////////////////////////
////////////////////// ACCOUNT ///////////////////////////
////////////////////////////////////////////////////////////////
public async getRealizedPnlForUser(userAddress: string): Promise<{
totalRealizedPnlPositions: Decimal;
Expand Down

0 comments on commit 0a27bef

Please sign in to comment.