Skip to content

Commit

Permalink
Merge pull request #95 from Premian-Labs/fix/transaction-api
Browse files Browse the repository at this point in the history
Add function to get optionPS transactions and fix transaction fragments
  • Loading branch information
froggiedev committed Dec 21, 2023
2 parents 45df057 + 476d21c commit 4b6908b
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@premia/v3-sdk",
"version": "2.3.3",
"version": "2.3.4",
"description": "The official SDK for building applications on Premia V3.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
56 changes: 55 additions & 1 deletion src/api/transactionAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Transaction, VaultTransaction } from '../entities'
import { Transaction, VaultTransaction, OptionPSTransaction } from '../entities'
import { BaseAPI } from './baseAPI'

/**
Expand Down Expand Up @@ -133,4 +133,58 @@ export class TransactionAPI extends BaseAPI {
searchInput
)
}

/**
* Get a single optionPS transaction given its hash.
*
* @param {string} hash - The hash of the optionPS transaction to retrieve.
*
* @returns {Promise<OptionPSTransaction>} A promise that resolves to the requested optionPS transaction.
*
* @remark Uses caching with a one-minute time-to-live.
*/
async getOptionPSTransaction(hash: string): Promise<OptionPSTransaction> {
return this.premia.subgraph.getOptionPSTransaction(hash)
}

/**
* Get a list of optionPS transactions with optional filter, search, order, and pagination parameters.
*
* @param {string} search - The search query.
* @param {string} [orderBy='timestamp'] - The attribute by which to order the optionPS transactions.
* @param {string} [order='asc'] - The order in which to return the vault transactions (asc, desc).
* @param {number} [first=100] - The maximum number of optionPS transactions to return.
* @param {number} [skip=0] - The number of optionPS transactions to skip.
* @param {string} [account] - The account associated with the optionPS transactions to return.
* @param {number} [startTime] - The start time for the optionPS transactions to return.
* @param {number} [endTime] - The end time for the optionPS transactions to return.
* @param {string} [searchInput] - An additional search input.
*
* @returns {Promise<OptionPSTransaction[]>} A promise that resolves to a list of optionPS transactions.
*
* @remark Uses caching with a one-minute time-to-live.
*/
async getOptionPSTransactions(
search: string,
orderBy: string = 'timestamp',
order: string = 'asc',
first = 100,
skip = 0,
account?: string,
startTime?: number,
endTime?: number,
searchInput?: string
): Promise<OptionPSTransaction[]> {
return this.premia.subgraph.getOptionPSTransactions(
search,
orderBy,
order,
first,
skip,
account,
startTime,
endTime,
searchInput
)
}
}
1 change: 1 addition & 0 deletions src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './vaultTransaction'
export * from './volatilitySurface'
export * from './vxPremia'
export * from './referral'
export * from './optionPSTransaction'
38 changes: 38 additions & 0 deletions src/entities/optionPSTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { BigNumberish } from 'ethers'

import { Token } from './token'
import { OptionPhysicallySettled } from './option'

export enum OptionPSTransactionType {
OPTION_PS_EXERCISE = 'OPTION_PS_EXERCISE',
OPTION_PS_CANCEL_EXERCISE = 'OPTION_PS_CANCEL_EXERCISE',
OPTION_PS_SETTLE_LONG = 'OPTION_PS_SETTLE_LONG',
OPTION_PS_SETTLE_SHORT = 'OPTION_PS_SETTLE_SHORT',
OPTION_PS_ANNIHILATE = 'OPTION_PS_ANNIHILATE',
OPTION_REWARD_UNDERWRITE = 'OPTION_REWARD_UNDERWRITE',
SHORT_OPTION_TRANSFER = 'SHORT_OPTION_TRANSFER',
SHORT_OPTION_RECEIVE = 'SHORT_OPTION_RECEIVE',
LONG_OPTION_PS_TRANSFER = 'LONG_OPTION_PS_TRANSFER',
LONG_OPTION_PS_RECEIVE = 'LONG_OPTION_PS_RECEIVE',
}

export interface OptionPSTransaction {
id: string
option: OptionPhysicallySettled
tokenSymbol: string
token: Token
origin: string
gasUsed: BigNumberish
gasPrice: BigNumberish
timestamp: BigNumberish
block: BigNumberish
logIndex: BigNumberish

type: OptionPSTransactionType
action: string
description: string
size: BigNumberish
sizeETH: BigNumberish
sizeUSD: BigNumberish
user: string
}
3 changes: 3 additions & 0 deletions src/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { VaultPosition, VaultPositionExtended } from './vaultPosition'
import { Transaction } from './transaction'
import { VaultTransaction } from './vaultTransaction'
import { Referral } from './referral'
import { OptionPSTransaction } from './optionPSTransaction'

export interface ActionAuthorization {
actions: bigint[]
Expand Down Expand Up @@ -95,6 +96,7 @@ export interface UserPortfolioHistory extends UserPortfolio {
snapshots: UserSnapshot[]
poolTransactions: Transaction[]
vaultTransactions: VaultTransaction[]
optionPSTransactions: OptionPSTransaction[]
}

export interface UserPortfolioExtended extends UserPortfolio {
Expand All @@ -105,6 +107,7 @@ export interface UserPortfolioExtended extends UserPortfolio {
snapshots: UserSnapshotExtended[]
poolTransactions: Transaction[]
vaultTransactions: VaultTransaction[]
optionPSTransactions: OptionPSTransaction[]
}

export interface UserSnapshot {
Expand Down
1 change: 1 addition & 0 deletions src/services/subgraph/graphql/fragments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from './vaultRegistry'
export * from './vaultTransaction'
export * from './volatilitySurface'
export * from './vxPremia'
export * from './optionPSTransaction'
33 changes: 33 additions & 0 deletions src/services/subgraph/graphql/fragments/optionPSTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { gql } from '@apollo/client/core'

import { OptionPhysicallySettledFragment } from './option'
import { TokenFragment } from './token'

export const OptionPSTransactionFragment = gql`
${OptionPhysicallySettledFragment}
${TokenFragment}
fragment OptionPSTransaction on OptionPSTransaction {
id
option {
...OptionPhysicallySettled
}
tokenSymbol
token {
...Token
}
origin
gasUsed
gasPrice
timestamp
block
logIndex
type
action
description
size
sizeETH
sizeUSD
}
`
6 changes: 4 additions & 2 deletions src/services/subgraph/graphql/fragments/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { gql } from '@apollo/client/core'

import { PoolFragment } from './pool'
import { TokenFragment } from './token'
import { TokenPairFragment } from './tokenPair'
import { VaultFragment } from './vault'
import { TokenFragment } from './token'

export const TransactionFragment = gql`
${PoolFragment}
Expand All @@ -22,6 +22,9 @@ export const TransactionFragment = gql`
pair {
...TokenPair
}
token {
...Token
}
origin
gasUsed
gasPrice
Expand All @@ -35,6 +38,5 @@ export const TransactionFragment = gql`
size
sizeETH
sizeUSD
user
}
`
5 changes: 5 additions & 0 deletions src/services/subgraph/graphql/fragments/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { TransactionFragment } from './transaction'
import { VaultTransactionFragment } from './vaultTransaction'
import { ReferralFragment } from './referral'
import { OptionPSTransactionFragment } from './optionPSTransaction'

export const UserFragment = gql`
fragment User on User {
Expand Down Expand Up @@ -216,6 +217,7 @@ export const UserPortfolioExtendedFragment = gql`
${UserSnapshotExtendedFragment}
${TransactionFragment}
${VaultTransactionFragment}
${OptionPSTransactionFragment}
fragment UserPortfolioExtended on User {
...UserExtended
Expand All @@ -239,6 +241,9 @@ export const UserPortfolioExtendedFragment = gql`
vaultTransactions {
...VaultTransaction
}
optionPSTransactions {
...OptionPSTransaction
}
}
`

Expand Down
7 changes: 3 additions & 4 deletions src/services/subgraph/graphql/fragments/vaultTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { gql } from '@apollo/client/core'

import { VaultFragment } from './vault'
import { TokenFragment } from './token'
import { TokenPairFragment } from './tokenPair'

export const VaultTransactionFragment = gql`
${VaultFragment}
${TokenFragment}
${TokenPairFragment}
fragment VaultTransaction on VaultTransaction {
id
registry
vaultName
vault {
...Vault
}
token {
...Token
}
origin
gasUsed
gasPrice
Expand All @@ -29,6 +29,5 @@ export const VaultTransactionFragment = gql`
size
sizeETH
sizeUSD
user
}
`
1 change: 1 addition & 0 deletions src/services/subgraph/graphql/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * from './vaultTransaction'
export * from './volatilitySurface'
export * from './vxPremia'
export * from './referral'
export * from './optionPSTransaction'
74 changes: 74 additions & 0 deletions src/services/subgraph/graphql/queries/optionPSTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { DocumentNode, gql } from '@apollo/client/core'
import { BigNumberish } from 'ethers'
import { OptionPSTransactionFragment } from '../fragments'
import { addFields } from '../../../../utils/subgraph'
import PremiaSubgraph from '../../index'

export class OptionPSTransactionQuery {
static optionPSTransactionId(hash: string): string {
return hash
}

@addFields
static GetOptionPSTransaction(
subgraph: PremiaSubgraph,
hash: string
): DocumentNode {
return gql`
${OptionPSTransactionFragment}
{
optionPSTransaction(id: "${this.optionPSTransactionId(hash)}") {
...OptionPSTransaction
}
}
`
}

@addFields
static GetOptionPSTransactions(
subgraph: PremiaSubgraph,
search: string,
orderBy: string,
order: string,
first = 100,
skip = 0,
account?: string,
startTime?: BigNumberish,
endTime?: BigNumberish,
searchInput?: string
): DocumentNode {
const startFilter = startTime ? `timestamp_gte: ${Number(startTime)},` : ''
const endFilter = endTime ? `timestamp_lte: ${Number(endTime)},` : ''
const searchFilter = searchInput
? `description_contains: "${searchInput.toUpperCase()}",`
: ''
const accountFilter = account ? `user: "${account.toLowerCase()}",` : ''

const containsName = 'tokenSymbol_contains'
const containsFilter = `${containsName}: "${search}",`

return gql`
${OptionPSTransactionFragment}
query OptionPSTransaction {
optionPSTransactions(
first: ${first}
skip: ${skip}
orderBy: "${orderBy}"
orderDirection: "${order}"
where: {
sizeUSD_gt: 0,
${startFilter}
${endFilter}
${searchFilter}
${accountFilter}
${containsFilter}
}
) {
...OptionPSTransaction
}
}
`
}
}
6 changes: 4 additions & 2 deletions src/services/subgraph/graphql/queries/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ export class TransactionQuery {
${
filter === 'all'
? ''
: `, type${filter === 'options' ? '_not_in' : '_in'}: ${
: `, type_in: ${
filter === 'add'
? '["POOL_DEPOSIT"]'
: filter === 'remove'
? '["POOL_WITHDRAW"]'
: '["POOL_DEPOSIT", "POOL_WITHDRAW"]'
: filter === 'pools'
? '["POOL_DEPOSIT", "POOL_TRADE", "POOL_CLAIM_FEES", "POOL_FILL_QUOTE", "POOL_WRITE_FROM", "POOL_SETTLE_POSITION", "POOL_LIQUIDITY_TRANSFER", "POOL_LIQUIDITY_RECEIVE", "POOL_WITHDRAW"]'
: '["SHORT_OPTION_SETTLE", "SHORT_OPTION_TRANSFER", "SHORT_OPTION_RECEIVE", "LONG_OPTION_EXERCISE", "LONG_OPTION_TRANSFER", "LONG_OPTION_RECEIVE", "DUAL_OPTION_ANNIHILATE"]'
}`
}
}
Expand Down

0 comments on commit 4b6908b

Please sign in to comment.