Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nftperp/sdk

Package Overview
Dependencies
Maintainers
2
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nftperp/sdk - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

dist/config/index.d.ts

105

dist/sdk.d.ts
import { Wallet } from "ethers";
import { AddMarginParams, Asset, AssetInfo, ClosePositionParams, GetMarginRatioParams, GetPositionParams, OpenPositionParams, PartialCloseParams, PositionDisplay, RemoveMarginParams } from "./types/types";
import { Amm, AmmInfo, Instance, PositionDisplay, Side } from "./types/types";
export declare class SDK {
private readonly _wallet;
private readonly _instance;
private readonly _ch;

@@ -10,5 +11,10 @@ private readonly _chv;

/**
* @param wallet_ wallet signer used for making transactions
* @param params params for initing sdk
* @param params.wallet ethers wallet class for signing txs
* @param params.instance instance
*/
constructor(wallet_: Wallet);
constructor(params: {
wallet: Wallet;
instance: Instance;
});
/**

@@ -22,4 +28,4 @@ * Get mock weth from faucet for paper trading on beta

* @param params params for opening position
* @param params.asset the asset to trade eg bayc
* @param params.direction long or short
* @param params.amm the amm to trade eg bayc
* @param params.side buy or sell
* @param params.margin collateral amount

@@ -29,64 +35,89 @@ * @param params.leverage leverage

*/
openPosition(params: OpenPositionParams): Promise<string>;
openPosition(params: {
amm: Amm;
side: Side;
margin: number;
leverage: number;
slippagePercent?: number;
}): Promise<string>;
/**
* Close position
* @param params params for closing position
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @returns tx hash
*/
closePosition(params: ClosePositionParams): Promise<string>;
closePosition(params: {
amm: Amm;
slippagePercent?: number;
}): Promise<string>;
/**
* Partially close position
* @param params params for partially closing position
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @param params.partialClosePercent percentage of position to close
* @returns tx hash
*/
partialClose(params: PartialCloseParams): Promise<string>;
partialClose(params: {
amm: Amm;
partialClosePercent: number;
slippagePercent?: number;
}): Promise<string>;
/**
* Add margin to position. increases margin ratio and position health
* @param params params for adding margin
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @param params.marginToAdd margin to add
* @returns tx hash
*/
addMargin(params: AddMarginParams): Promise<string>;
addMargin(params: {
amm: Amm;
marginToAdd: number;
}): Promise<string>;
/**
* Remove margin from position. decreases margin ratio and increases liq price
* @param params params for removing margin
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @param params.marginToRemove margin to remove
* @returns
*/
removeMargin(params: RemoveMarginParams): Promise<string>;
removeMargin(params: {
amm: Amm;
marginToRemove: number;
}): Promise<string>;
/**
* Get position
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @returns position
*/
getPosition(params: GetPositionParams): Promise<PositionDisplay>;
getPosition(params: {
amm: Amm;
trader?: string;
}): Promise<PositionDisplay>;
/**
* Get margin ratio. margin ratio = active margin / active notional
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @returns margin ratio
*/
getMarginRatio(params: GetMarginRatioParams): Promise<number>;
getMarginRatio(params: {
amm: Amm;
trader?: string;
}): Promise<number>;
/**
* Get mark price (nftperp price)
* @param asset asset eg bayc
* @param amm amm eg bayc
* @returns mark price
*/
getMarkPrice(asset: Asset): Promise<number>;
getMarkPrice(amm: Amm): Promise<number>;
/**
* Get index price (real price - as per marketplaces)
* @param asset asset eg bayc
* @param amm amm eg bayc
* @returns index price
*/
getIndexPrice(asset: Asset): Promise<number>;
getIndexPrice(amm: Amm): Promise<number>;
/**
* Get funding details
* @param asset asset eg bayc
* @param amm amm eg bayc
* @returns funding period and next funding time, prev funding rate (`wei`)
*/
getFundingInfo(asset: Asset): Promise<{
getFundingInfo(amm: Amm): Promise<{
fundingPeriod: number;

@@ -98,14 +129,13 @@ nextFundingTime: number;

* Get asset info
* @param asset asset eg bayc
* @returns Asset Info
* @param amm amm eg bayc
* @returns Amm Info
*/
getAssetInfo(asset: Asset): Promise<AssetInfo>;
getAmmInfo(amm: Amm): Promise<AmmInfo>;
/**
* Get supported assets
* @returns assets
* Get supported Amms
* @returns Amms
*/
getSupportedAssets(): readonly ["bayc"];
getSupportedAmms(): (keyof typeof Amm)[];
/**
* get notional longs and shorts
* @param asset
* @returns open interest info in `wei`

@@ -240,10 +270,13 @@ */

/**
* get side based on direction.
* @returns side
* get amm instance to interact with amm
*/
private _getSide;
private _getAmmInstance;
/**
* get amm instance to interact with amm
* get amm address
*/
private _getAssetInstance;
private _getAmmAddress;
/**
* validate rpc and instance match
*/
private _validateWalletAndInstance;
}

@@ -12,5 +12,7 @@ "use strict";

const mathUtil_1 = require("./utils/math/mathUtil");
const helperUtil_1 = require("./utils/helpers/helperUtil");
const commonUtil_1 = require("./utils/common/commonUtil");
const configDao_1 = require("./utils/dao/configDao");
class SDK {
_wallet;
_instance;
_ch;

@@ -21,10 +23,16 @@ _chv;

/**
* @param wallet_ wallet signer used for making transactions
* @param params params for initing sdk
* @param params.wallet ethers wallet class for signing txs
* @param params.instance instance
*/
constructor(wallet_) {
this._wallet = wallet_;
this._ch = new ethers_1.Contract((0, helperUtil_1.getChAddress)(), abis_1.default.chAbi, wallet_);
this._chv = new ethers_1.Contract((0, helperUtil_1.getChvAddress)(), abis_1.default.chvAbi, wallet_);
this._if = new ethers_1.Contract((0, helperUtil_1.getIfAddress)(), abis_1.default.ifAbi, wallet_);
this._weth = new ethers_1.Contract((0, helperUtil_1.getWethAddress)(), abis_1.default.mockWethAbi, wallet_);
constructor(params) {
const { wallet, instance } = params;
void this._validateWalletAndInstance(wallet, instance);
const { ch, chv, iF, weth } = (0, configDao_1.getInstanceConfig)(instance);
this._ch = new ethers_1.Contract(ch, abis_1.default.chAbi, wallet);
this._chv = new ethers_1.Contract(chv, abis_1.default.chvAbi, wallet);
this._if = new ethers_1.Contract(iF, abis_1.default.ifAbi, wallet);
this._weth = new ethers_1.Contract(weth, abis_1.default.mockWethAbi, wallet);
this._wallet = wallet;
this._instance = instance;
}

@@ -36,6 +44,2 @@ /**

async useFaucet() {
const { chainId } = await this._wallet.provider.getNetwork();
if (chainId !== 42161) {
(0, helperUtil_1._throw)("faucet only available on beta version of protocol (arb mainnet)");
}
const tx = await this._weth.mint();

@@ -47,4 +51,4 @@ return tx.hash;

* @param params params for opening position
* @param params.asset the asset to trade eg bayc
* @param params.direction long or short
* @param params.amm the amm to trade eg bayc
* @param params.side buy or sell
* @param params.margin collateral amount

@@ -55,11 +59,10 @@ * @param params.leverage leverage

async openPosition(params) {
const { asset, direction, margin, leverage, slippagePercent } = params;
const { amm, side, margin, leverage, slippagePercent } = params;
const notional = (0, mathUtil_1.toBig)(margin).mul(leverage);
const side = this._getSide(direction);
const fees = await this._calcFee(asset, (0, mathUtil_1.toWei)(notional), side);
const fees = await this._calcFee(amm, (0, mathUtil_1.toWei)(notional), side);
const totalCost = (0, mathUtil_1.toWei)(margin).add(fees);
await this._checkBalance(totalCost);
await this._checkAllowance(totalCost);
const slippageAmount = await this._getSlippageBaseAssetAmount(asset, side, notional, slippagePercent);
return await this._openPosition((0, helperUtil_1.getAssetAddress)(asset), side, (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(margin)), (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(leverage)), (0, mathUtil_1.toDecimal)(slippageAmount));
const slippageAmount = await this._getSlippageBaseAssetAmount(amm, side, notional, slippagePercent);
return await this._openPosition(this._getAmmAddress(amm), side, (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(margin)), (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(leverage)), (0, mathUtil_1.toDecimal)(slippageAmount));
}

@@ -69,15 +72,15 @@ /**

* @param params params for closing position
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @returns tx hash
*/
async closePosition(params) {
const { asset, slippagePercent } = params;
const { size } = await this._getPosition(asset);
const { amm, slippagePercent } = params;
const { size } = await this._getPosition(amm);
if (size.eq(0)) {
(0, helperUtil_1._throw)("no such position");
(0, commonUtil_1._throw)("no such position");
}
// closing long is equivalent of shorting
const side = size.gt(0) ? types_1.Side.SELL : types_1.Side.BUY;
const slippageAmount = await this._getSlippageQuoteAssetAmount(asset, side, size, slippagePercent);
return await this._closePosition((0, helperUtil_1.getAssetAddress)(asset), (0, mathUtil_1.toDecimal)(slippageAmount));
const slippageAmount = await this._getSlippageQuoteAssetAmount(amm, side, size, slippagePercent);
return await this._closePosition(this._getAmmAddress(amm), (0, mathUtil_1.toDecimal)(slippageAmount));
}

@@ -87,3 +90,3 @@ /**

* @param params params for partially closing position
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @param params.partialClosePercent percentage of position to close

@@ -93,6 +96,6 @@ * @returns tx hash

async partialClose(params) {
const { asset, partialClosePercent, slippagePercent } = params;
const { size } = await this._getPosition(asset);
const { amm, partialClosePercent, slippagePercent } = params;
const { size } = await this._getPosition(amm);
if (size.eq(0)) {
(0, helperUtil_1._throw)("no such position");
(0, commonUtil_1._throw)("no such position");
}

@@ -102,4 +105,4 @@ const side = size.gt(0) ? types_1.Side.SELL : types_1.Side.BUY;

const sizeToClose = size.mul(partialCloseFraction).round(0, 0);
const slippageAmount = await this._getSlippageQuoteAssetAmount(asset, side, sizeToClose, slippagePercent);
return await this._partialClose((0, helperUtil_1.getAssetAddress)(asset), (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(partialCloseFraction)), (0, mathUtil_1.toDecimal)(slippageAmount));
const slippageAmount = await this._getSlippageQuoteAssetAmount(amm, side, sizeToClose, slippagePercent);
return await this._partialClose(this._getAmmAddress(amm), (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(partialCloseFraction)), (0, mathUtil_1.toDecimal)(slippageAmount));
}

@@ -109,3 +112,3 @@ /**

* @param params params for adding margin
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @param params.marginToAdd margin to add

@@ -115,6 +118,6 @@ * @returns tx hash

async addMargin(params) {
const { asset, marginToAdd } = params;
const { amm, marginToAdd } = params;
await this._checkBalance((0, mathUtil_1.toWei)(marginToAdd));
await this._checkAllowance((0, mathUtil_1.toWei)(marginToAdd));
return await this._addMargin((0, helperUtil_1.getAssetAddress)(asset), (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(marginToAdd)));
return await this._addMargin(this._getAmmAddress(amm), (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(marginToAdd)));
}

@@ -124,3 +127,3 @@ /**

* @param params params for removing margin
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @param params.marginToRemove margin to remove

@@ -130,23 +133,23 @@ * @returns

async removeMargin(params) {
const { asset, marginToRemove } = params;
const { margin } = await this._getPositionWithFundingPayment(asset);
const { positionNotional, upnl } = await this._getPositionNotionalAndUpnl(asset, this._wallet.address);
const { mmr } = await this._getRatios(asset);
const { amm, marginToRemove } = params;
const { margin } = await this._getPositionWithFundingPayment(amm);
const { positionNotional, upnl } = await this._getPositionNotionalAndUpnl(amm, this._wallet.address);
const { mmr } = await this._getRatios(amm);
const newMarginRatio = margin.add(upnl).sub((0, mathUtil_1.toWei)(marginToRemove)).div(positionNotional);
if (newMarginRatio.lt(mmr)) {
(0, helperUtil_1._throw)("position goes below mmr after removal. try removing a small amount to keep above mmr");
(0, commonUtil_1._throw)("position goes below mmr after removal. try removing a small amount to keep above mmr");
}
return await this._removeMargin((0, helperUtil_1.getAssetAddress)(asset), (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(marginToRemove)));
return await this._removeMargin(this._getAmmAddress(amm), (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(marginToRemove)));
}
/**
* Get position
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @returns position
*/
async getPosition(params) {
const { asset, trader } = params;
const { size, margin, openNotional } = await this._getPosition(asset, trader);
const liquidationPrice = await this._getLiquidationPrice(asset, trader);
const { upnl } = await this._getPositionNotionalAndUpnl(asset, trader);
const { margin: marginWithFunding } = await this._getPositionWithFundingPayment(asset, trader);
const { amm, trader } = params;
const { size, margin, openNotional } = await this._getPosition(amm, trader);
const liquidationPrice = await this._getLiquidationPrice(amm, trader);
const { upnl } = await this._getPositionNotionalAndUpnl(amm, trader);
const { margin: marginWithFunding } = await this._getPositionWithFundingPayment(amm, trader);
const pnl = (0, mathUtil_1.format)((0, mathUtil_1.fromWei)(upnl), 4);

@@ -166,8 +169,8 @@ const funding = (0, mathUtil_1.format)((0, mathUtil_1.fromWei)(marginWithFunding.sub(margin)), 4);

* Get margin ratio. margin ratio = active margin / active notional
* @param params.asset asset eg bayc
* @param params.amm amm eg bayc
* @returns margin ratio
*/
async getMarginRatio(params) {
const { asset, trader } = params;
const mr = await this._getMarginRatio(asset, trader);
const { amm, trader } = params;
const mr = await this._getMarginRatio(amm, trader);
return (0, mathUtil_1.format)((0, mathUtil_1.fromWei)(mr), undefined, true);

@@ -177,7 +180,7 @@ }

* Get mark price (nftperp price)
* @param asset asset eg bayc
* @param amm amm eg bayc
* @returns mark price
*/
async getMarkPrice(asset) {
const spotPrice = await this._getSpotPrice(asset);
async getMarkPrice(amm) {
const spotPrice = await this._getSpotPrice(amm);
return (0, mathUtil_1.format)((0, mathUtil_1.fromWei)(spotPrice));

@@ -187,7 +190,7 @@ }

* Get index price (real price - as per marketplaces)
* @param asset asset eg bayc
* @param amm amm eg bayc
* @returns index price
*/
async getIndexPrice(asset) {
const indexPrice = await this._getUnderlyingPrice(asset);
async getIndexPrice(amm) {
const indexPrice = await this._getUnderlyingPrice(amm);
return (0, mathUtil_1.format)((0, mathUtil_1.fromWei)(indexPrice));

@@ -197,10 +200,10 @@ }

* Get funding details
* @param asset asset eg bayc
* @param amm amm eg bayc
* @returns funding period and next funding time, prev funding rate (`wei`)
*/
async getFundingInfo(asset) {
const assetInstance = this._getAssetInstance(asset);
const fundingPeriod = await assetInstance.fundingPeriod();
const nextFundingTime = await assetInstance.nextFundingTime();
const previousFundingRate = await assetInstance.fundingRate();
async getFundingInfo(amm) {
const ammInstance = this._getAmmInstance(amm);
const fundingPeriod = await ammInstance.fundingPeriod();
const nextFundingTime = await ammInstance.nextFundingTime();
const previousFundingRate = await ammInstance.fundingRate();
return {

@@ -214,13 +217,13 @@ fundingPeriod: (0, mathUtil_1.format)((0, mathUtil_1.toBig)(fundingPeriod)),

* Get asset info
* @param asset asset eg bayc
* @returns Asset Info
* @param amm amm eg bayc
* @returns Amm Info
*/
async getAssetInfo(asset) {
const markPrice = await this._getSpotPrice(asset);
const indexPrice = await this._getUnderlyingPrice(asset);
const { nextFundingTime, previousFundingPercent } = await this.getFundingInfo(asset);
const { openInterest, openInterestLongs, openInterestShorts } = await this._getOpenInterestInfo(asset);
const { imr, mmr, lfr } = await this._getRatios(asset);
async getAmmInfo(amm) {
const markPrice = await this._getSpotPrice(amm);
const indexPrice = await this._getUnderlyingPrice(amm);
const { nextFundingTime, previousFundingPercent } = await this.getFundingInfo(amm);
const { openInterest, openInterestLongs, openInterestShorts } = await this._getOpenInterestInfo(amm);
const { imr, mmr, lfr } = await this._getRatios(amm);
return {
asset: asset.toUpperCase(),
amm,
markPrice: (0, mathUtil_1.format)((0, mathUtil_1.fromWei)(markPrice)),

@@ -239,7 +242,7 @@ indexPrice: (0, mathUtil_1.format)((0, mathUtil_1.fromWei)(indexPrice)),

/**
* Get supported assets
* @returns assets
* Get supported Amms
* @returns Amms
*/
getSupportedAssets() {
return (0, helperUtil_1.getAssets)();
getSupportedAmms() {
return Object.keys(types_1.Amm);
}

@@ -251,7 +254,6 @@ //

* get notional longs and shorts
* @param asset
* @returns open interest info in `wei`
*/
async _getOpenInterestInfo(asset) {
const { openInterest, openInterestLongs, openInterestShorts } = await this._ch.openInterestMap((0, helperUtil_1.getAssetAddress)(asset));
async _getOpenInterestInfo(amm) {
const { openInterest, openInterestLongs, openInterestShorts } = await this._ch.openInterestMap(this._getAmmAddress(amm));
return {

@@ -267,5 +269,5 @@ openInterestLongs: (0, mathUtil_1.fromDecimal)(openInterestLongs),

*/
async _getSpotPrice(asset) {
const assetInstance = this._getAssetInstance(asset);
const spotPrice = await assetInstance.getSpotPrice();
async _getSpotPrice(amm) {
const ammInstance = this._getAmmInstance(amm);
const spotPrice = await ammInstance.getSpotPrice();
return (0, mathUtil_1.fromDecimal)(spotPrice);

@@ -277,5 +279,5 @@ }

*/
async _getUnderlyingPrice(asset) {
const assetInstance = this._getAssetInstance(asset);
const underlyingPrice = await assetInstance.getUnderlyingPrice();
async _getUnderlyingPrice(amm) {
const ammInstance = this._getAmmInstance(amm);
const underlyingPrice = await ammInstance.getUnderlyingPrice();
return (0, mathUtil_1.fromDecimal)(underlyingPrice);

@@ -287,4 +289,4 @@ }

*/
async _getPosition(asset, trader = this._wallet.address) {
const { size, margin, openNotional } = await this._ch.getPosition((0, helperUtil_1.getAssetAddress)(asset), trader);
async _getPosition(amm, trader = this._wallet.address) {
const { size, margin, openNotional } = await this._ch.getPosition(this._getAmmAddress(amm), trader);
return {

@@ -300,6 +302,6 @@ size: (0, mathUtil_1.fromDecimal)(size),

*/
async _getLiquidationPrice(asset, trader = this._wallet.address) {
const ratios = await this._getRatios(asset);
const position = await this._getPositionWithFundingPayment(asset, trader);
const reserves = await this._getReserves(asset);
async _getLiquidationPrice(amm, trader = this._wallet.address) {
const ratios = await this._getRatios(amm);
const position = await this._getPositionWithFundingPayment(amm, trader);
const reserves = await this._getReserves(amm);
const size = (0, mathUtil_1.fromWei)(position.size);

@@ -326,5 +328,5 @@ const margin = (0, mathUtil_1.fromWei)(position.margin);

*/
async _getReserves(asset) {
const assetInstance = this._getAssetInstance(asset);
const [quoteAssetReserve, baseAssetReserve] = await assetInstance.getReserves();
async _getReserves(amm) {
const ammInstance = this._getAmmInstance(amm);
const [quoteAssetReserve, baseAssetReserve] = await ammInstance.getReserves();
return {

@@ -339,4 +341,4 @@ quoteAssetReserve: (0, mathUtil_1.fromDecimal)(quoteAssetReserve),

*/
async _getPositionWithFundingPayment(asset, trader = this._wallet.address) {
const { size, margin, openNotional } = await this._chv.getPersonalPositionWithFundingPayment((0, helperUtil_1.getAssetAddress)(asset), trader);
async _getPositionWithFundingPayment(amm, trader = this._wallet.address) {
const { size, margin, openNotional } = await this._chv.getPersonalPositionWithFundingPayment(this._getAmmAddress(amm), trader);
return {

@@ -352,4 +354,4 @@ size: (0, mathUtil_1.fromDecimal)(size),

*/
async _getMarginRatio(asset, trader = this._wallet.address) {
const marginRatio = await this._ch.getMarginRatio((0, helperUtil_1.getAssetAddress)(asset), trader);
async _getMarginRatio(amm, trader = this._wallet.address) {
const marginRatio = await this._ch.getMarginRatio(this._getAmmAddress(amm), trader);
return (0, mathUtil_1.fromDecimal)(marginRatio);

@@ -361,4 +363,4 @@ }

*/
async _getPositionNotionalAndUpnl(asset, trader = this._wallet.address) {
const _ = await this._ch.getPositionNotionalAndUnrealizedPnl((0, helperUtil_1.getAssetAddress)(asset), trader, 0);
async _getPositionNotionalAndUpnl(amm, trader = this._wallet.address) {
const _ = await this._ch.getPositionNotionalAndUnrealizedPnl(this._getAmmAddress(amm), trader, 0);
return {

@@ -373,5 +375,5 @@ positionNotional: (0, mathUtil_1.fromDecimal)(_.positionNotional),

*/
async _getRatios(asset) {
const assetInstance = this._getAssetInstance(asset);
const ratios = await assetInstance.getRatios();
async _getRatios(amm) {
const ammInstance = this._getAmmInstance(amm);
const ratios = await ammInstance.getRatios();
return {

@@ -389,5 +391,5 @@ imr: (0, mathUtil_1.fromDecimal)(ratios.initMarginRatio),

*/
async _calcFee(asset, notional, side) {
const assetInstance = this._getAssetInstance(asset);
const fees = await assetInstance.calcFee((0, mathUtil_1.toDecimal)(notional), side);
async _calcFee(amm, notional, side) {
const ammInstance = this._getAmmInstance(amm);
const fees = await ammInstance.calcFee((0, mathUtil_1.toDecimal)(notional), side);
return (0, mathUtil_1.fromDecimal)(fees[0]).add((0, mathUtil_1.fromDecimal)(fees[1]));

@@ -400,5 +402,5 @@ }

*/
async _getBaseAssetOut(asset, dir, notional) {
const assetInstance = this._getAssetInstance(asset);
const baseAssetOut = await assetInstance.getInputPrice(dir, (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(notional)));
async _getBaseAssetOut(amm, dir, notional) {
const ammInstance = this._getAmmInstance(amm);
const baseAssetOut = await ammInstance.getInputPrice(dir, (0, mathUtil_1.toDecimal)((0, mathUtil_1.toWei)(notional)));
return (0, mathUtil_1.fromDecimal)(baseAssetOut);

@@ -411,5 +413,5 @@ }

*/
async _getQuoteAssetOut(asset, dir, size) {
const assetInstance = this._getAssetInstance(asset);
const quoteAssetOut = await assetInstance.getOutputPrice(dir, (0, mathUtil_1.toDecimal)(size));
async _getQuoteAssetOut(amm, dir, size) {
const ammInstance = this._getAmmInstance(amm);
const quoteAssetOut = await ammInstance.getOutputPrice(dir, (0, mathUtil_1.toDecimal)(size));
return (0, mathUtil_1.fromDecimal)(quoteAssetOut);

@@ -422,3 +424,3 @@ }

*/
async _getSlippageBaseAssetAmount(asset, side, notional, slippagePercent) {
async _getSlippageBaseAssetAmount(amm, side, notional, slippagePercent) {
if (!slippagePercent) {

@@ -429,3 +431,3 @@ return (0, mathUtil_1.toBig)(0);

const dir = side === types_1.Side.BUY ? types_1.DirectionOfAsset.ADD_TO_AMM : types_1.DirectionOfAsset.REMOVE_FROM_AMM;
const baseAssetOut = await this._getBaseAssetOut(asset, dir, notional);
const baseAssetOut = await this._getBaseAssetOut(amm, dir, notional);
const slippageFraction = (0, big_js_1.default)(slippagePercent).div(100);

@@ -442,3 +444,3 @@ if (side === types_1.Side.BUY) {

*/
async _getSlippageQuoteAssetAmount(asset, side, size, slippagePercent) {
async _getSlippageQuoteAssetAmount(amm, side, size, slippagePercent) {
if (!slippagePercent) {

@@ -449,3 +451,3 @@ return (0, mathUtil_1.toBig)(0);

const dir = side === types_1.Side.SELL ? types_1.DirectionOfAsset.ADD_TO_AMM : types_1.DirectionOfAsset.REMOVE_FROM_AMM;
const quoteAssetOut = await this._getQuoteAssetOut(asset, dir, size);
const quoteAssetOut = await this._getQuoteAssetOut(amm, dir, size);
const slippageFraction = (0, big_js_1.default)(slippagePercent).div(100);

@@ -471,3 +473,3 @@ if (side === types_1.Side.SELL) {

if (balance.lt(amount)) {
(0, helperUtil_1._throw)(`insufficient balance, required: ${(0, mathUtil_1.format)((0, mathUtil_1.fromWei)(amount))}`);
(0, commonUtil_1._throw)(`insufficient balance, required: ${(0, mathUtil_1.format)((0, mathUtil_1.fromWei)(amount))}`);
}

@@ -541,17 +543,27 @@ }

/**
* get side based on direction.
* @returns side
* get amm instance to interact with amm
*/
_getSide(direction) {
return direction === "long" ? types_1.Side.BUY : types_1.Side.SELL;
_getAmmInstance(amm) {
return new ethers_1.Contract(this._getAmmAddress(amm), abis_1.default.ammAbi, this._wallet);
}
/**
* get amm instance to interact with amm
* get amm address
*/
_getAssetInstance(asset) {
return new ethers_1.Contract((0, helperUtil_1.getAssetAddress)(asset), abis_1.default.ammAbi, this._wallet);
_getAmmAddress(amm) {
return (0, configDao_1.getAmmAddress)(this._instance, amm);
}
/**
* validate rpc and instance match
*/
async _validateWalletAndInstance(wallet, instance) {
const { chainId } = await wallet.provider.getNetwork();
if (chainId !== (0, configDao_1.getInstanceConfig)(instance).chainId) {
(0, commonUtil_1._throw)("provider rpc and instance do not match");
}
}
}
exports.SDK = SDK;
const nftperp = new SDK(new ethers_1.Wallet("d9e63a6832de2d7654b9c820ce6326c48ca9766f4d2f5023c33a4103a659f2fd", new ethers_1.providers.JsonRpcProvider("https://rinkeby.arbitrum.io/rpc")));
void nftperp.useFaucet();
const provider = new ethers_1.providers.JsonRpcProvider("https://arb1.arbitrum.io/rpc");
const wallet = new ethers_1.Wallet("d9e63a6832de2d7654b9c820ce6326c48ca9766f4d2f5023c33a4103a659f2fd", provider);
const nftperp = new SDK({ wallet, instance: types_1.Instance.BETA });
void nftperp.getAmmInfo(types_1.Amm.MAYC).then(console.log);
import Big from "big.js";
import { BigNumber } from "ethers";
import { ASSETS } from "../config/addresses";
export declare type Direction = "long" | "short";
export declare enum Amm {
BAYC = "BAYC",
MOONBIRDS = "MOONBIRDS",
MAYC = "MAYC",
DOODLES = "DOODLES",
CLONEX = "CLONEX"
}
export declare enum Side {

@@ -13,7 +18,8 @@ BUY = 0,

}
export declare type Asset = typeof ASSETS[number];
export declare type AssetAddressConfig = {
[key in Asset]: string;
};
export interface AddressConfig {
export declare enum Instance {
BETA = "BETA",
HACKATHON = "HACKATHON"
}
export declare type InstanceConfig = {
chainId: number;
ch: string;

@@ -23,39 +29,12 @@ chv: string;

weth: string;
assets: AssetAddressConfig;
}
amms: {
[key in Amm]?: string;
};
};
export declare type Config = {
[key in Instance]: InstanceConfig;
};
export declare type Decimal = {
d: string | BigNumber;
};
export interface OpenPositionParams {
asset: Asset;
direction: Direction;
margin: number;
leverage: number;
slippagePercent?: number;
}
export interface ClosePositionParams {
asset: Asset;
slippagePercent?: number;
}
export interface PartialCloseParams {
asset: Asset;
partialClosePercent: number;
slippagePercent?: number;
}
export interface AddMarginParams {
asset: Asset;
marginToAdd: number;
}
export interface RemoveMarginParams {
asset: Asset;
marginToRemove: number;
}
export interface GetPositionParams {
asset: Asset;
trader?: string;
}
export interface GetMarginRatioParams {
asset: Asset;
trader?: string;
}
export interface Position {

@@ -90,4 +69,4 @@ size: Big;

}
export interface AssetInfo {
asset: Uppercase<Asset>;
export interface AmmInfo {
amm: Amm;
markPrice: number;

@@ -94,0 +73,0 @@ indexPrice: number;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DirectionOfAsset = exports.Side = void 0;
exports.Instance = exports.DirectionOfAsset = exports.Side = exports.Amm = void 0;
// declare assets here
var Amm;
(function (Amm) {
Amm["BAYC"] = "BAYC";
Amm["MOONBIRDS"] = "MOONBIRDS";
Amm["MAYC"] = "MAYC";
Amm["DOODLES"] = "DOODLES";
Amm["CLONEX"] = "CLONEX";
})(Amm = exports.Amm || (exports.Amm = {}));
var Side;

@@ -14,1 +23,6 @@ (function (Side) {

})(DirectionOfAsset = exports.DirectionOfAsset || (exports.DirectionOfAsset = {}));
var Instance;
(function (Instance) {
Instance["BETA"] = "BETA";
Instance["HACKATHON"] = "HACKATHON";
})(Instance = exports.Instance || (exports.Instance = {}));
{
"name": "@nftperp/sdk",
"version": "1.0.1",
"version": "2.0.0",
"description": "SDK for nftperp protocol",

@@ -5,0 +5,0 @@ "main": "dist/sdk.js",

@@ -37,4 +37,4 @@ # nftperp sdk ✨

- `asset` the nft collection to trade. synonymous to _market pair_, _amm_.
- `direction` direction of trade. _long_ or _short_.
- `amm` the nft collection to trade. synonymous to _market pair_, _asset_.
- `side` direction of trade. _long_ or _short_.
- `margin` collateral amount. this is the amount you risk on liquidation.

@@ -53,3 +53,3 @@ - `notional` total value of position _margin x leverage_.

import { ethers } from "ethers";
import { SDK } from "@nftperp/sdk";
import { SDK, Instance } from "@nftperp/sdk";

@@ -62,3 +62,3 @@ /**

const wallet = new ethers.Wallet("<your-private-key>", provider);
const nftperp = new SDK(wallet);
const nftperp = new SDK({ wallet, instance: Instance.BETA });
```

@@ -75,5 +75,7 @@

```ts
import { Amm, Side } from "@nftperp/sdk";
const hash = await nftperp.openPosition({
asset: "bayc",
direction: "long",
amm: Amm.BAYC,
side: Side.BUY,
margin: 0.1, // this means 0.1 eth

@@ -87,5 +89,5 @@ leverage: 3,

```ts
console.log(nftperp.getSupportedAssets());
console.log(nftperp.getSupportedAssets(Instance.BETA));
/**
[ 'bayc', 'moonbirds', 'mayc', 'doodles', 'clonex' ]
[ 'BAYC', 'MOONBIRDS', 'MAYC', 'DOODLES', 'CLONEX' ]
*/

@@ -98,3 +100,3 @@ ```

const position = await nftperp.getPosition({
asset: "bayc",
amm: Amm.BAYC,
});

@@ -119,3 +121,3 @@ console.log(position);

const hash = await nftperp.closePosition({
asset: "bayc",
amm: Amm.BAYC,
});

@@ -127,4 +129,4 @@ ```

```ts
const assetInfo = await nftperp.getAssetInfo("bayc");
console.log(assetInfo);
const ammInfo = await nftperp.getAmmInfo(Amm.BAYC);
console.log(ammInfo);
/**

@@ -131,0 +133,0 @@ {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc