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

[PFT-798] [PFT-779] Add Pimlico relayer functionalities to the SDK #45

Merged
merged 5 commits into from
Apr 17, 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
93 changes: 76 additions & 17 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@parifi/sdk",
"version": "0.1.13",
"version": "0.1.14",
"description": "Parifi SDK with common utility functions",
"files": [
"dist",
Expand Down Expand Up @@ -52,6 +52,8 @@
"dotenv": "^16.4.1",
"ethers": "^6.10.0",
"graphql": "^16.8.1",
"graphql-request": "^6.1.0"
"graphql-request": "^6.1.0",
"permissionless": "^0.1.16",
"viem": "^2.9.17"
}
}
3 changes: 3 additions & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ export const PYTH_ETH_USD_PRICE_ID_STABLE = '0xff61491a931112ddf1bd8147cd1b64137

export const PYTH_USDC_USD_PRICE_ID_BETA = '0x41f3625971ca2ed2263e78573fe5ce23e13d2558ed3f2e47ab0f84fb9e7ae722';
export const PYTH_USDC_USD_PRICE_ID_STABLE = '0xeaa020c61cc479712813461ce153894a96a6c00b21ed0cfc2798d1f9a9e9c94a';

// Constants for Pimlico relayer
export const FACTORY_ADDRESS_SIMPLE_ACCOUNT = '0x91E60e0613810449d098b0b5Ec8b51A0FE8c8985'
2 changes: 1 addition & 1 deletion src/core/order-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { AxiosInstance } from 'axios';
import { getPythPriceIdsForOrderIds, getPythPriceIdsForPositionIds } from '../../subgraph';
import { getVaaPriceUpdateData } from '../../pyth/pyth';
import { getPriceIdsForCollaterals } from '../../common';
import { executeTxUsingGelato } from '../../gelato/gelato-function';
import { executeTxUsingGelato } from '../../relayers/gelato/gelato-function';

// Returns an Order Manager contract instance without signer
export const getOrderManagerInstance = (chain: Chain): Contract => {
Expand Down
79 changes: 77 additions & 2 deletions src/core/parifi-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import {
// normalizePythPriceForParifi
} from '../../pyth/pyth';
import { AxiosInstance } from 'axios';
import { executeTxUsingGelato } from '../../gelato/gelato-function';
import { getAllPendingOrders, getPythPriceIdsForPositionIds } from '../../subgraph';
import { executeTxUsingGelato } from '../../relayers/gelato/gelato-function';
import { getAllPendingOrders, getPythPriceIdsForOrderIds, getPythPriceIdsForPositionIds } from '../../subgraph';
import { BatchExecute } from '../../interfaces/subgraphTypes';
import {
DEFAULT_BATCH_COUNT,
GAS_LIMIT_LIQUIDATION,
GAS_LIMIT_SETTLEMENT,
getPriceIdsForCollaterals,
} from '../../common';
import { executeTxUsingPimlico } from '../../relayers/pimlico/utils';
// import { checkIfOrderCanBeSettled } from '../order-manager';

// Returns an Order Manager contract instance without signer
Expand Down Expand Up @@ -248,3 +249,77 @@ export const batchSettleOrdersUsingWallet = async (
}
return { txHash: '0x' };
};

// Returns encoded tx data to batch settle multiple orders
export const getBatchSettleTxData = async (
chainId: Chain,
subgraphEndpoint: string,
pythClient: AxiosInstance,
orderIds: string[],
): Promise<{ txData: string }> => {
if (orderIds.length == 0) {
console.log('Orders not available for settlement');
return { txData: '0x' };
}

const priceIds = await getPythPriceIdsForOrderIds(subgraphEndpoint, orderIds);

// Get Price IDs of collateral tokens
const priceIdsForCollaterals = getPriceIdsForCollaterals(true);

// Get Price update data and latest prices from Pyth
const priceUpdateData = await getVaaPriceUpdateData(priceIds.concat(priceIdsForCollaterals), pythClient);

// Populate batched orders for settlement for orders that can be settled
const batchedOrders: BatchExecute[] = [];

orderIds.forEach((orderId) => {
batchedOrders.push({
id: orderId,
priceUpdateData: priceUpdateData,
});
});

if (batchedOrders.length != 0) {
const parifiUtils = getParifiUtilsInstance(chainId);
const { data: txData } = await parifiUtils.batchSettleOrders.populateTransaction(batchedOrders);
return { txData };
}
return { txData: '0x' };
};

// Returns encoded tx data to batch liquidate multiple positions
export const getBatchLiquidateTxData = async (
chainId: Chain,
subgraphEndpoint: string,
pythClient: AxiosInstance,
positionIds: string[],
): Promise<{ txData: string }> => {
if (positionIds.length == 0) return { txData: '0x' };

// Get unique price ids for all the positions
const priceIds = await getPythPriceIdsForPositionIds(subgraphEndpoint, positionIds);

// Get Price IDs of collateral tokens
const priceIdsForCollaterals = getPriceIdsForCollaterals(true);

// Get Price update data from Pyth
const priceUpdateData = await getVaaPriceUpdateData(priceIds.concat(priceIdsForCollaterals), pythClient);

// Populate batched positions for positions that can be liquidated
const batchedPositions: BatchExecute[] = [];
positionIds.forEach((positionId) => {
batchedPositions.push({
id: positionId,
priceUpdateData: priceUpdateData,
});
});

// Encode transaction data
if (batchedPositions.length != 0) {
const parifiUtils = getParifiUtilsInstance(chainId);
const { data: txData } = await parifiUtils.batchLiquidatePositions.populateTransaction(batchedPositions);
return { txData };
}
return { txData: '0x' };
};
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Pyth } from './pyth';
import { Subgraph } from './subgraph';
import { PythConfig, RelayerConfig, RpcConfig, SubgraphConfig } from './interfaces/classConfigs';
import { Core } from './core';
import { Gelato } from './gelato';
import { Gelato } from './relayers/gelato';
import { relayerRepository } from './interfaces/repositories/relayer';
import { ParifiRelayer } from './relayers/parifi';
import { Pimlico } from './relayers/pimlico';

export * from './common';
export * from './core';
export * from './gelato/gelato-function';
export * from './relayers/gelato/gelato-function';
export * from './interfaces';
export * from './pyth';
export * from './subgraph';
Expand All @@ -17,9 +18,10 @@ export class ParifiSdk {
subgraph: Subgraph;
pyth: Pyth;
core: Core;
gelato: Gelato;
relayer: {
gelato: Gelato;
parifi: relayerRepository;
pimlico: Pimlico;
};

constructor(
Expand All @@ -31,13 +33,15 @@ export class ParifiSdk {
this.subgraph = new Subgraph(rpcConfig, subgraphConfig, pythConfig);
this.pyth = new Pyth(pythConfig);
this.core = new Core(rpcConfig, subgraphConfig, relayerConfig, pythConfig);
this.gelato = new Gelato(relayerConfig['gelatoConfig'], rpcConfig);
this.relayer = {
gelato: new Gelato(relayerConfig['gelatoConfig'], rpcConfig),
parifi: new ParifiRelayer(relayerConfig['parifiRealyerConfig'], rpcConfig.chainId),
pimlico: new Pimlico(relayerConfig['pimlicoConfig'], rpcConfig, subgraphConfig),
};
}

async init() {
await this.pyth.initPyth();
await this.relayer.pimlico.initPimlico();
}
}
6 changes: 6 additions & 0 deletions src/interfaces/classConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Chain } from '@parifi/references';

// Interface to configure chain specific details of the sdk
export interface RpcConfig {
chainId: Chain;
rpcEndpointUrl?: string;
Expand All @@ -8,18 +9,22 @@ export interface RpcConfig {
apiKey?: string;
}

// Interface to configure subgraph details
export interface SubgraphConfig {
subgraphEndpoint?: string;
username?: string;
password?: string;
apiKey?: string;
}

// Interface to configure relayers on the sdk
export interface RelayerConfig {
gelatoConfig?: RelayerI;
parifiRealyerConfig?: RelayerI;
pimlicoConfig?: RelayerI;
}

// Common relayer config to configure relayers
export interface RelayerI {
relayerEndpoint?: string;
username?: string;
Expand All @@ -28,6 +33,7 @@ export interface RelayerI {
jwtToken?: string;
}

// Pyth price feed config
export interface PythConfig {
pythEndpoint?: string;
username?: string;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/gelato/index.ts → src/relayers/gelato/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransactionStatusResponse } from '@gelatonetwork/relay-sdk';
import { RelayerConfig, RpcConfig } from '../interfaces';
import { RelayerConfig, RpcConfig } from '../../interfaces';
import { checkGelatoTaskStatus, executeTxUsingGelato } from './gelato-function';

export class Gelato {
Expand Down
3 changes: 3 additions & 0 deletions src/relayers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './gelato';
export * from './parifi';
export * from './pimlico';
Loading
Loading