Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
samlior committed May 20, 2024
1 parent 6f2f1ef commit 954c9f5
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 165 deletions.
2 changes: 2 additions & 0 deletions src/amm/amm.requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ export interface PerpPositionResponse extends PerpPosition {
latency: number;
base: string;
quote: string;
balance?: string;
liquidationPrice?: string;
}

export interface PerpAvailablePairsResponse {
Expand Down
4 changes: 2 additions & 2 deletions src/connectors/synfutures/synfutures.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
PerpBalanceResponse,
PerpBalanceRequest,
} from '../../amm/amm.requests';
import { PerpPosition } from '../perp/perp';
import { SynFuturesPosition } from './synfutures';

async function getWallet(ethereumish: Ethereumish, address: string) {
let wallet: Wallet;
Expand Down Expand Up @@ -127,7 +127,7 @@ export async function getPosition(
latency: latency(startTimestamp, Date.now()),
base: req.base,
quote: req.quote,
...(position as PerpPosition),
...(position as SynFuturesPosition),
};
}

Expand Down
28 changes: 23 additions & 5 deletions src/connectors/synfutures/synfutures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
encodeTradeParam,
NULL_DDL,
PairModel,
wdiv,
} from '@synfutures/oyster-sdk';
import { Token } from '@uniswap/sdk';
import { Transaction, Wallet, ethers } from 'ethers';
Expand All @@ -29,13 +30,18 @@ function toTickerSymbol(symbol: string) {
throw new Error('invalid symbol: ' + symbol);
}

return symbol.substring(0, index - 1);
return symbol.substring(0, index);
}

function formatSlippage(slippage: number) {
return Math.floor(slippage * 10000);
}

export interface SynFuturesPosition extends PerpPosition {
balance: string;
liquidationPrice: string;
}

export class SynFutures implements SynFuturesish {
private static _instances: { [name: string]: SynFutures };
private ethereum: Ethereum;
Expand Down Expand Up @@ -202,7 +208,7 @@ export class SynFutures implements SynFuturesish {
async isMarketActive(tickerSymbol: string): Promise<boolean> {
const pair = this.pairs[tickerSymbol];

return (
return !!(
pair &&
pair.rootInstrument.state.condition === InstrumentCondition.NORMAL &&
(pair.amm.status === AMMStatus.TRADING ||
Expand All @@ -219,7 +225,7 @@ export class SynFutures implements SynFuturesish {
async getPositions(
address: string,
tickerSymbol: string,
): Promise<PerpPosition | undefined> {
): Promise<SynFuturesPosition | undefined> {
const pair = this.pairs[tickerSymbol];

if (!pair) {
Expand All @@ -237,11 +243,15 @@ export class SynFutures implements SynFuturesish {
return {
positionAmt: position.size.abs().toString(),
positionSide: position.size.gt(0) ? 'LONG' : 'SHORT',
unrealizedProfit: position.unrealizedPnl.toString(),
unrealizedProfit: position.unrealizedPnl
.sub(position.unrealizedFundingFee)
.toString(),
leverage: ethers.utils.formatUnits(position.leverageWad),
entryPrice: position.entryNotional.div(position.size.abs()).toString(),
entryPrice: wdiv(position.entryNotional, position.size.abs()).toString(),
tickerSymbol,
pendingFundingPayment: position.unrealizedFundingFee.toString(),
balance: position.balance.toString(),
liquidationPrice: position.liquidationPrice.toString(),
};
}

Expand Down Expand Up @@ -275,6 +285,10 @@ export class SynFutures implements SynFuturesish {

const slippage = formatSlippage(this.getAllowedSlippage(allowedSlippage));

await this.synfutures.syncVaultCache(wallet.address, [
pair.rootInstrument.info.quote.address,
]);

const account = await this.synfutures.updatePairLevelAccount(
wallet.address,
pair.rootInstrument.info.addr,
Expand Down Expand Up @@ -338,6 +352,10 @@ export class SynFutures implements SynFuturesish {
throw new Error('invalid ticker symbol: ' + tickerSymbol);
}

await this.synfutures.syncVaultCache(wallet.address, [
pair.rootInstrument.info.quote.address,
]);

const account = await this.synfutures.updatePairLevelAccount(
wallet.address,
pair.rootInstrument.info.addr,
Expand Down
3 changes: 2 additions & 1 deletion src/services/common-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import {
Fraction as XsswapFraction,
} from 'xsswap-sdk';
import { PerpPosition } from '../connectors/perp/perp';
import { SynFuturesPosition } from "../connectors/synfutures/synfutures";
import { XdcBase } from '../chains/xdc/xdc.base';
import { NearBase } from '../chains/near/near.base';
import { TezosBase } from '../chains/tezos/tezos.base';
Expand Down Expand Up @@ -704,7 +705,7 @@ export interface SynFuturesish {
getPositions(
address: string,
tickerSymbol: string,
): Promise<PerpPosition | undefined>;
): Promise<SynFuturesPosition | undefined>;

/**
* Attempts to return balance of a connected acct
Expand Down
Loading

0 comments on commit 954c9f5

Please sign in to comment.