From caccad38d951d008a849b7c396a6d46393b4a465 Mon Sep 17 00:00:00 2001 From: Sudeep Biswas Date: Tue, 26 Mar 2024 00:13:29 +0530 Subject: [PATCH 1/2] Add Gelato gas limit for transactions --- src/common/constants.ts | 4 ++++ src/core/order-manager/index.ts | 29 +++++++++++++++++++++++++---- src/core/parifi-utils/index.ts | 20 ++++++++++++++++++-- src/gelato/gelato-function.ts | 14 ++++++++++++-- src/gelato/index.ts | 7 ++++++- src/subgraph/index.ts | 2 +- 6 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/common/constants.ts b/src/common/constants.ts index 27fab8e..b7729e5 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -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'; diff --git a/src/core/order-manager/index.ts b/src/core/order-manager/index.ts index 33b936b..6de74f3 100644 --- a/src/core/order-manager/index.ts +++ b/src/core/order-manager/index.ts @@ -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'; @@ -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, @@ -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); diff --git a/src/core/parifi-utils/index.ts b/src/core/parifi-utils/index.ts index e7cc087..e53dfd2 100644 --- a/src/core/parifi-utils/index.ts +++ b/src/core/parifi-utils/index.ts @@ -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 @@ -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); @@ -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); @@ -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); @@ -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 }; } diff --git a/src/gelato/gelato-function.ts b/src/gelato/gelato-function.ts index d1b3750..f151046 100644 --- a/src/gelato/gelato-function.ts +++ b/src/gelato/gelato-function.ts @@ -1,4 +1,9 @@ -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 ( @@ -6,6 +11,7 @@ export const executeTxUsingGelato = async ( chainId: Chain, gelatoKey: string | undefined, encodedTxData: string, + gelatoGasLimit?: bigint, ): Promise => { const request: SponsoredCallRequest = { chainId: BigInt(chainId.toString()), @@ -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; }; diff --git a/src/gelato/index.ts b/src/gelato/index.ts index 4a52fac..e51a3d6 100644 --- a/src/gelato/index.ts +++ b/src/gelato/index.ts @@ -8,12 +8,17 @@ export class Gelato { private rpcConfig: RpcConfig, ) {} - public async executeTxUsingGelato(targetContractAddress: string, encodedTxData: string): Promise { + public async executeTxUsingGelato( + targetContractAddress: string, + encodedTxData: string, + gelatoGasLimit?: bigint, + ): Promise { return await executeTxUsingGelato( targetContractAddress, this.rpcConfig.chainId, this.gelatoConfig?.apiKey, encodedTxData, + gelatoGasLimit, ); } diff --git a/src/subgraph/index.ts b/src/subgraph/index.ts index 39d0ebe..64d7d4d 100644 --- a/src/subgraph/index.ts +++ b/src/subgraph/index.ts @@ -78,7 +78,7 @@ export class Subgraph { } //////////////////////////////////////////////////////////////// - ////////////////////// MARKET //////////////////////////// + ////////////////////// ACCOUNT /////////////////////////// //////////////////////////////////////////////////////////////// public async getRealizedPnlForUser(userAddress: string): Promise<{ totalRealizedPnlPositions: Decimal; From ed751004df17b4016996fe77aacde33e8ac1652b Mon Sep 17 00:00:00 2001 From: Sudeep Biswas Date: Tue, 26 Mar 2024 00:15:53 +0530 Subject: [PATCH 2/2] 0.1.2 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 54e2844..dc5ad07 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@parifi/sdk", - "version": "0.1.1", + "version": "0.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@parifi/sdk", - "version": "0.1.1", + "version": "0.1.2", "license": "ISC", "dependencies": { "@gelatonetwork/relay-sdk": "^5.5.5", diff --git a/package.json b/package.json index 300279a..037f195 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@parifi/sdk", - "version": "0.1.1", + "version": "0.1.2", "description": "Parifi SDK with common utility functions", "files": [ "dist",