@nftperp/sdk
Advanced tools
Comparing version 8.1.0 to 9.0.0
@@ -15,11 +15,13 @@ "use strict"; | ||
clearingHouse_blast: "0xFfc0555EC5F5C44A6B529Cef94b9055799696272", | ||
clearingHouse_wbtc_blast: "0x0C2309C6554AabBFBcA7d5219545795DcAF6f4A9", | ||
insuranceFund: "0x087E8C29d0743120A9b9d003F702FB7F450291ba", | ||
insuranceFund_blast: "0xe2F4A2845D4183F7913EC66945b20E4c0c15DAFf", | ||
insuranceFund_wbtc_blast: "0x0b32E91C276377b3b3CdB332e902207Defe542bd", | ||
priceFeed: "0xE9725791a6A7D16355bF939f75f7011753e87583", | ||
priceFeed_blast: "0x780Fa04Fe4806b31359bd612dAe85D60Eab7C6d4", | ||
whitelist: "0xc98f3436A5dC926F66091E8a68CC027aa1ecCBb6", | ||
whitelist_blast: "0x2f6b968B487010b0F2D5DCdF7F4D5a0811E08aF1", | ||
priceFeed_wbtc_blast: "0x979F130fD56aAdef6Be7E97d3f615F68fA66a676", | ||
weth: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", | ||
weth_blast: "0x4300000000000000000000000000000000000004", | ||
degenDraw_blast: "0xfB33141A9C0201a998B44B64684d056C7560E395", | ||
wbtc: "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f", | ||
wbtc_blast: "0xF7bc58b8D8f97ADC129cfC4c9f45Ce3C0E1D2692", | ||
amms: { | ||
@@ -31,4 +33,6 @@ milady: "0x66945724757a91199AB0B0c77EcBB90abA897F75", | ||
milady_blast: "0x80532f01eD2Cc8e962251bA23D2A3F481E62A203", | ||
monke_wbtc_blast: "0xc0Aa1EB6746111e9A5d2AeAb6efE66A1f2358E02", | ||
puppets_wbtc_blast: "0xDefcCa12e39A868B9DE2FDdb8F281B5e4a38818c", | ||
}, | ||
}, | ||
}; |
{ | ||
"name": "@nftperp/sdk", | ||
"version": "8.1.0", | ||
"version": "9.0.0", | ||
"description": "SDK for nftperp protocol", | ||
@@ -5,0 +5,0 @@ "main": "sdk.js", |
@@ -9,6 +9,10 @@ import * as config from "./config"; | ||
readonly clearingHouse_blast: types.ClearingHouse; | ||
readonly clearingHouse_wbtc_blast: types.ClearingHouse; | ||
readonly insuranceFund: types.InsuranceFund; | ||
readonly insuranceFund_blast: types.InsuranceFund; | ||
readonly insuranceFund_wbtc_blast: types.InsuranceFund; | ||
readonly weth: types.ERC20; | ||
readonly weth_blast: types.ERC20; | ||
readonly wbtc: types.ERC20; | ||
readonly wbtc_blast: types.ERC20; | ||
private readonly _api; | ||
@@ -200,2 +204,3 @@ /** | ||
max?: boolean; | ||
amm: types.Amm; | ||
}): Promise<web3.ContractTransactionResponse>; | ||
@@ -446,2 +451,6 @@ /** | ||
private _getWethInstance; | ||
/** | ||
* get contract instance | ||
*/ | ||
private _getWbtcInstance; | ||
} |
97
sdk.js
@@ -38,6 +38,10 @@ "use strict"; | ||
clearingHouse_blast; | ||
clearingHouse_wbtc_blast; | ||
insuranceFund; | ||
insuranceFund_blast; | ||
insuranceFund_wbtc_blast; | ||
weth; | ||
weth_blast; | ||
wbtc; | ||
wbtc_blast; | ||
// api instance | ||
@@ -67,2 +71,7 @@ _api; | ||
}); | ||
this.clearingHouse_wbtc_blast = web3.getClearingHouseOrdinalsInstance({ | ||
network: `blast`, | ||
rpcUrl: params?.rpcUrl_blast, | ||
wallet: this.wallet_blast, | ||
}); | ||
this.insuranceFund = web3.getInsuranceFundInstance({ | ||
@@ -78,2 +87,7 @@ network: `arb`, | ||
}); | ||
this.insuranceFund_wbtc_blast = web3.getInsuranceFundOrdinalsInstance({ | ||
network: `blast`, | ||
rpcUrl: params?.rpcUrl_blast, | ||
wallet: this.wallet_blast, | ||
}); | ||
this.weth = web3.getWethInstance({ | ||
@@ -89,2 +103,12 @@ network: `arb`, | ||
}); | ||
this.wbtc = web3.getWbtcInstance({ | ||
network: `arb`, | ||
rpcUrl: params?.rpcUrl, | ||
wallet: this.wallet, | ||
}); | ||
this.wbtc_blast = web3.getWbtcInstance({ | ||
network: `blast`, | ||
rpcUrl: params?.rpcUrl_blast, | ||
wallet: this.wallet_blast, | ||
}); | ||
this._api = new apis.NftperpApis(); | ||
@@ -108,3 +132,3 @@ } | ||
if (!options?.skipChecks) { | ||
await this._checkBalance({ amount: utils.big(summary.totalCost), network }); | ||
await this._checkBalance({ amount: utils.big(summary.totalCost), network, amm }); | ||
await this._checkAllowance({ | ||
@@ -114,2 +138,3 @@ amount: utils.big(summary.totalCost), | ||
maxApprove: options?.maxApprove, | ||
amm, | ||
}); | ||
@@ -136,7 +161,7 @@ } | ||
if (!options?.skipChecks && !reduceOnly) { | ||
await this._checkBalance({ amount: utils.big(margin), network }); | ||
await this._checkAllowance({ amount: utils.big(margin), network, maxApprove: options?.maxApprove }); | ||
await this._checkBalance({ amount: utils.big(margin), network, amm }); | ||
await this._checkAllowance({ amount: utils.big(margin), network, maxApprove: options?.maxApprove, amm }); | ||
} | ||
const trader = await this._getAddress(); | ||
const ch = this._getClearingHouseInstance(network); | ||
const ch = utils.isOrdinalsAmm(amm) ? this.clearingHouse_wbtc_blast : this._getClearingHouseInstance(network); | ||
return ch.createLimitOrder({ | ||
@@ -167,3 +192,3 @@ trader, | ||
const network = utils.getNetworkFromAmmName(amm); | ||
const ch = this._getClearingHouseInstance(network); | ||
const ch = utils.isOrdinalsAmm(amm) ? this.clearingHouse_wbtc_blast : this._getClearingHouseInstance(network); | ||
return ch.createTriggerOrder({ | ||
@@ -330,4 +355,4 @@ trader, | ||
const network = utils.getNetworkFromAmmName(amm); | ||
await this._checkBalance({ amount: utils.big(amount), network }); | ||
await this._checkAllowance({ amount: utils.big(amount), network }); | ||
await this._checkBalance({ amount: utils.big(amount), network, amm }); | ||
await this._checkAllowance({ amount: utils.big(amount), network, amm }); | ||
return this._addMargin(amm, utils.toWeiStr(amount), overrides); | ||
@@ -357,7 +382,8 @@ } | ||
async approve(params) { | ||
const { network, amount, max } = params; | ||
const { network, amount, max, amm } = params; | ||
if (!amount && !max) { | ||
throw new Error("specify either amount or max option"); | ||
} | ||
return this._approve({ amount: utils.toWeiStr(amount || 0), network, maxApprove: !!max }); | ||
const decimals = utils.isOrdinalsAmm(amm) ? 8 : 18; | ||
return this._approve({ amount: utils.toWeiStr(amount || 0, decimals), network, maxApprove: !!max, amm }); | ||
} | ||
@@ -660,5 +686,6 @@ /** | ||
*/ | ||
async _getBalance(network) { | ||
const weth = this._getWethInstance(network); | ||
return utils.fromWei(await weth.balanceOf(await this._getAddress())); | ||
async _getBalance(network, amm) { | ||
const token = utils.isOrdinalsAmm(amm) ? this._getWbtcInstance(network) : this._getWethInstance(network); | ||
const decimals = utils.isOrdinalsAmm(amm) ? 8 : 18; | ||
return utils.fromWei(await token.balanceOf(await this._getAddress()), decimals); | ||
} | ||
@@ -670,4 +697,4 @@ /** | ||
async _checkBalance(params) { | ||
const { amount, network } = params; | ||
const balance = await this._getBalance(network); | ||
const { amount, network, amm } = params; | ||
const balance = await this._getBalance(network, amm); | ||
if (balance.lt(amount)) { | ||
@@ -681,6 +708,7 @@ throw new Error(`insufficient balance, required: ${utils.stringify(amount)}`); | ||
*/ | ||
async _getAllowance(network) { | ||
const weth = this._getWethInstance(network); | ||
const ch = this._getClearingHouseInstance(network); | ||
return utils.fromWei(await weth.allowance(await this._getAddress(), await ch.getAddress())); | ||
async _getAllowance(network, amm) { | ||
const token = utils.isOrdinalsAmm(amm) ? this._getWbtcInstance(network) : this._getWethInstance(network); | ||
const ch = utils.isOrdinalsAmm(amm) ? this.clearingHouse_wbtc_blast : this._getClearingHouseInstance(network); | ||
const decimals = utils.isOrdinalsAmm(amm) ? 8 : 18; | ||
return utils.fromWei(await token.allowance(await this._getAddress(), await ch.getAddress()), decimals); | ||
} | ||
@@ -692,7 +720,7 @@ /** | ||
async _checkAllowance(params) { | ||
const { amount, network, maxApprove: _maxApprove } = params; | ||
const { amount, network, maxApprove: _maxApprove, amm } = params; | ||
const maxApprove = typeof _maxApprove === `undefined` ? true : _maxApprove; | ||
const allowance = await this._getAllowance(network); | ||
const allowance = await this._getAllowance(network, amm); | ||
if (allowance.lt(amount)) { | ||
await this._approve({ amount: utils.toWeiStr(amount), network, maxApprove }); | ||
await this._approve({ amount: utils.toWeiStr(amount), network, maxApprove, amm }); | ||
} | ||
@@ -705,6 +733,6 @@ } | ||
async _approve(params) { | ||
const { amount, network, maxApprove } = params; | ||
const weth = this._getWethInstance(network); | ||
const ch = this._getClearingHouseInstance(network); | ||
const tx = await weth.approve(await ch.getAddress(), maxApprove ? web3.MaxUint256 : amount); | ||
const { amount, network, maxApprove, amm } = params; | ||
const token = utils.isOrdinalsAmm(amm) ? this._getWbtcInstance(network) : this._getWethInstance(network); | ||
const ch = utils.isOrdinalsAmm(amm) ? this.clearingHouse_wbtc_blast : this._getClearingHouseInstance(network); | ||
const tx = await token.approve(await ch.getAddress(), maxApprove ? web3.MaxUint256 : amount); | ||
return tx; | ||
@@ -718,3 +746,3 @@ } | ||
const network = utils.getNetworkFromAmmName(amm); | ||
const ch = this._getClearingHouseInstance(network); | ||
const ch = utils.isOrdinalsAmm(amm) ? this.clearingHouse_wbtc_blast : this._getClearingHouseInstance(network); | ||
const ammAddress = this._getAmmAddress(amm); | ||
@@ -729,3 +757,3 @@ return ch.openPosition(ammAddress, side, margin, leverage, baseLimit, overrides); | ||
const network = utils.getNetworkFromAmmName(amm); | ||
const ch = this._getClearingHouseInstance(network); | ||
const ch = utils.isOrdinalsAmm(amm) ? this.clearingHouse_wbtc_blast : this._getClearingHouseInstance(network); | ||
const ammAddress = this._getAmmAddress(amm); | ||
@@ -740,3 +768,3 @@ return ch.closePosition(ammAddress, size, quoteLimit, overrides); | ||
const network = utils.getNetworkFromAmmName(amm); | ||
const ch = this._getClearingHouseInstance(network); | ||
const ch = utils.isOrdinalsAmm(amm) ? this.clearingHouse_wbtc_blast : this._getClearingHouseInstance(network); | ||
const ammAddress = this._getAmmAddress(amm); | ||
@@ -751,3 +779,3 @@ return ch.addMargin(ammAddress, marginToAdd, overrides); | ||
const network = utils.getNetworkFromAmmName(amm); | ||
const ch = this._getClearingHouseInstance(network); | ||
const ch = utils.isOrdinalsAmm(amm) ? this.clearingHouse_wbtc_blast : this._getClearingHouseInstance(network); | ||
const ammAddress = this._getAmmAddress(amm); | ||
@@ -832,3 +860,14 @@ return ch.removeMargin(ammAddress, marginToRemove, overrides); | ||
} | ||
/** | ||
* get contract instance | ||
*/ | ||
_getWbtcInstance(network) { | ||
switch (network) { | ||
case "blast": | ||
return this.wbtc_blast; | ||
default: | ||
return this.wbtc; | ||
} | ||
} | ||
} | ||
exports.SDK = SDK; |
@@ -10,3 +10,5 @@ import * as config from "../config"; | ||
MILADY_BLAST = "milady_blast", | ||
PUNKS_BLAST = "punks_blast" | ||
PUNKS_BLAST = "punks_blast", | ||
MONKE_WBTC_BLAST = "monke_wbtc_blast", | ||
PUPPETS_WBTC_BLAST = "puppets_wbtc_blast" | ||
} | ||
@@ -39,11 +41,13 @@ export declare enum Side { | ||
clearingHouse_blast: string; | ||
clearingHouse_wbtc_blast: string; | ||
insuranceFund: string; | ||
insuranceFund_blast: string; | ||
insuranceFund_wbtc_blast: string; | ||
priceFeed: string; | ||
priceFeed_blast: string; | ||
whitelist: string; | ||
whitelist_blast: string; | ||
priceFeed_wbtc_blast: string; | ||
weth: string; | ||
weth_blast: string; | ||
degenDraw_blast: string; | ||
wbtc: string; | ||
wbtc_blast: string; | ||
amms: { | ||
@@ -50,0 +54,0 @@ [key in Amm]: string; |
@@ -28,2 +28,4 @@ "use strict"; | ||
Amm["PUNKS_BLAST"] = "punks_blast"; | ||
Amm["MONKE_WBTC_BLAST"] = "monke_wbtc_blast"; | ||
Amm["PUPPETS_WBTC_BLAST"] = "puppets_wbtc_blast"; | ||
})(Amm || (exports.Amm = Amm = {})); | ||
@@ -30,0 +32,0 @@ var Side; |
@@ -6,5 +6,10 @@ import * as types from "../types"; | ||
export declare const getClearingHouseAddress: (network: types.Network) => string; | ||
export declare const clearingHouseOrdinalsAddress: string; | ||
export declare const getInsuranceFundAddress: (network: types.Network) => string; | ||
export declare const insuranceFundOrdinalsAddress: string; | ||
export declare const getWethAddress: (network: types.Network) => string; | ||
export declare const getWbtcAddress: (network: types.Network) => string; | ||
export declare const getChainId: (network: types.Network) => number; | ||
export declare const getNetworkFromAmmName: (ammName: types.Amm) => types.Network; | ||
export declare const isOrdinalsAmm: (ammName: types.Amm) => boolean; | ||
export declare const getAmmNameFromAddress: (addr: string) => types.Amm; |
@@ -26,3 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getNetworkFromAmmName = exports.getChainId = exports.getWethAddress = exports.getInsuranceFundAddress = exports.getClearingHouseAddress = exports.getRpcUrl = exports.shortAddr = exports.getEnvVar = void 0; | ||
exports.getAmmNameFromAddress = exports.isOrdinalsAmm = exports.getNetworkFromAmmName = exports.getChainId = exports.getWbtcAddress = exports.getWethAddress = exports.insuranceFundOrdinalsAddress = exports.getInsuranceFundAddress = exports.clearingHouseOrdinalsAddress = exports.getClearingHouseAddress = exports.getRpcUrl = exports.shortAddr = exports.getEnvVar = void 0; | ||
const config = __importStar(require("../config")); | ||
@@ -58,2 +58,3 @@ const getEnvVar = (varName) => { | ||
exports.getClearingHouseAddress = getClearingHouseAddress; | ||
exports.clearingHouseOrdinalsAddress = config.info.contracts.clearingHouse_wbtc_blast; | ||
const getInsuranceFundAddress = (network) => { | ||
@@ -68,2 +69,3 @@ switch (network) { | ||
exports.getInsuranceFundAddress = getInsuranceFundAddress; | ||
exports.insuranceFundOrdinalsAddress = config.info.contracts.insuranceFund_wbtc_blast; | ||
const getWethAddress = (network) => { | ||
@@ -78,2 +80,11 @@ switch (network) { | ||
exports.getWethAddress = getWethAddress; | ||
const getWbtcAddress = (network) => { | ||
switch (network) { | ||
case "blast": | ||
return config.info.contracts.wbtc_blast; | ||
default: | ||
return config.info.contracts.wbtc; | ||
} | ||
}; | ||
exports.getWbtcAddress = getWbtcAddress; | ||
const getChainId = (network) => { | ||
@@ -93,1 +104,10 @@ switch (network) { | ||
exports.getNetworkFromAmmName = getNetworkFromAmmName; | ||
const isOrdinalsAmm = (ammName) => { | ||
return ammName.includes(`wbtc`); | ||
}; | ||
exports.isOrdinalsAmm = isOrdinalsAmm; | ||
const getAmmNameFromAddress = (addr) => { | ||
const ammName = Object.keys(config.info.contracts.amms).find((amm) => config.info.contracts.amms[amm].toLowerCase() === addr.toLowerCase()); | ||
return ammName; | ||
}; | ||
exports.getAmmNameFromAddress = getAmmNameFromAddress; |
@@ -14,2 +14,7 @@ import Big from "big.js"; | ||
}) => types.ClearingHouse; | ||
export declare const getClearingHouseOrdinalsInstance: (params: { | ||
network: types.Network; | ||
rpcUrl?: string; | ||
wallet?: ethers.Wallet; | ||
}) => types.ClearingHouse; | ||
export declare const getInsuranceFundInstance: (params: { | ||
@@ -20,2 +25,7 @@ network: types.Network; | ||
}) => types.InsuranceFund; | ||
export declare const getInsuranceFundOrdinalsInstance: (params: { | ||
network: types.Network; | ||
rpcUrl?: string; | ||
wallet?: ethers.Wallet; | ||
}) => types.InsuranceFund; | ||
export declare const getWethInstance: (params: { | ||
@@ -26,2 +36,7 @@ network: types.Network; | ||
}) => types.ERC20; | ||
export declare const getWbtcInstance: (params: { | ||
network: types.Network; | ||
rpcUrl?: string; | ||
wallet?: ethers.Wallet; | ||
}) => types.ERC20; | ||
export declare const checksum: typeof ethers.ethers.getAddress; | ||
@@ -28,0 +43,0 @@ export declare const getBalance: (params: { |
@@ -32,3 +32,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getWethBalance = exports.getBalance = exports.checksum = exports.getWethInstance = exports.getInsuranceFundInstance = exports.getClearingHouseInstance = exports.getProvider = void 0; | ||
exports.getWethBalance = exports.getBalance = exports.checksum = exports.getWbtcInstance = exports.getWethInstance = exports.getInsuranceFundOrdinalsInstance = exports.getInsuranceFundInstance = exports.getClearingHouseOrdinalsInstance = exports.getClearingHouseInstance = exports.getProvider = void 0; | ||
const abis_1 = __importDefault(require("../abis")); | ||
@@ -53,2 +53,10 @@ const ethers = __importStar(require("ethers")); | ||
exports.getClearingHouseInstance = getClearingHouseInstance; | ||
const getClearingHouseOrdinalsInstance = (params) => { | ||
const { network, rpcUrl, wallet } = params; | ||
const signerOrProvider = wallet || (0, exports.getProvider)({ network, rpcUrl }); | ||
const clearingHouseAddress = utils.clearingHouseOrdinalsAddress; | ||
const clearingHouse = new ethers.Contract(clearingHouseAddress, abis_1.default.clearingHouse, signerOrProvider); | ||
return clearingHouse; | ||
}; | ||
exports.getClearingHouseOrdinalsInstance = getClearingHouseOrdinalsInstance; | ||
const getInsuranceFundInstance = (params) => { | ||
@@ -62,2 +70,10 @@ const { network, rpcUrl, wallet } = params; | ||
exports.getInsuranceFundInstance = getInsuranceFundInstance; | ||
const getInsuranceFundOrdinalsInstance = (params) => { | ||
const { network, rpcUrl, wallet } = params; | ||
const signerOrProvider = wallet || (0, exports.getProvider)({ network, rpcUrl }); | ||
const insuranceFundAddress = utils.insuranceFundOrdinalsAddress; | ||
const insuranceFund = new ethers.Contract(insuranceFundAddress, abis_1.default.insuranceFund, signerOrProvider); | ||
return insuranceFund; | ||
}; | ||
exports.getInsuranceFundOrdinalsInstance = getInsuranceFundOrdinalsInstance; | ||
const getWethInstance = (params) => { | ||
@@ -71,2 +87,10 @@ const { network, rpcUrl, wallet } = params; | ||
exports.getWethInstance = getWethInstance; | ||
const getWbtcInstance = (params) => { | ||
const { network, rpcUrl, wallet } = params; | ||
const signerOrProvider = wallet || (0, exports.getProvider)({ network, rpcUrl }); | ||
const wbtcAddress = utils.getWbtcAddress(network); | ||
const weth = new ethers.Contract(wbtcAddress, abis_1.default.erc20, signerOrProvider); | ||
return weth; | ||
}; | ||
exports.getWbtcInstance = getWbtcInstance; | ||
exports.checksum = ethers.getAddress; | ||
@@ -73,0 +97,0 @@ const getBalance = async (params) => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
818709
25336