UNPKG

@mak201010/bluefin-v2-client

Version:

The Bluefin client Library allows traders to sign, create, retrieve and listen to orders on Bluefin Exchange.

274 lines 14.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable prettier/prettier */ /* eslint-disable no-undef */ const chai_1 = __importStar(require("chai")); const chai_as_promised_1 = __importDefault(require("chai-as-promised")); const library_sui_1 = require("@mak201010/library-sui"); const index_1 = require("../index"); const utils_1 = require("../utils/utils"); chai_1.default.use(chai_as_promised_1.default); // please update deployer if contracts are redeployed const deployer = { phrase: "explain august dream guitar mail attend enough demise engine pulse wide later", privateAddress: "0x958fb79396a09247d6c05aa9bcd51a94f6233c1399ec15e07a07ff4b77e5578c", }; const TEST_WALLETS = [ { phrase: "weasel knee fault hammer gift joke ability tilt brass about ladder ramp", privateAddress: "0x551db36b08c694b8382fab5cec41641a07fbe6e8b36dfdb1d6376a8f39d9e1dc", }, { phrase: "blouse swim window brother elephant winner act pencil visa acoustic try west", privateAddress: "0xae718f76f26af59a9b33913be566627fa598ca80891a417d6a94e72bd5c24e6f", }, ]; describe("BluefinClient", () => { //* set environment from here let deplymentJson; const network = index_1.Networks.TESTNET_SUI; const symbol = "BTC-PERP"; const defaultLeverage = 1; let client; let onChainCalls; before(() => __awaiter(void 0, void 0, void 0, function* () { client = new index_1.BluefinClient(true, network, TEST_WALLETS[0].phrase, "Secp256k1"); yield client.init(); deplymentJson = yield (0, library_sui_1.readFile)("/home/radheem/github/bluefin-v2-client/deployment.json"); (0, chai_1.expect)(deplymentJson).to.be.not.eq(undefined); const signer = (0, library_sui_1.getSignerFromSeed)(deployer.phrase); const suiClient = new library_sui_1.SuiClient({ url: network.url }); onChainCalls = new library_sui_1.OnChainCalls(signer, deplymentJson, suiClient); yield (0, utils_1.setupTestAccounts)(onChainCalls, TEST_WALLETS, network.faucet); })); beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { client = new index_1.BluefinClient(true, network, TEST_WALLETS[0].phrase, "Secp256k1"); yield client.init(); })); afterEach(() => { client.sockets.close(); }); it("should initialize the client", () => __awaiter(void 0, void 0, void 0, function* () { (0, chai_1.expect)(client).to.be.not.eq(undefined); })); it("should return public address of account", () => __awaiter(void 0, void 0, void 0, function* () { (0, chai_1.expect)(client.getPublicAddress()).to.be.equal(TEST_WALLETS[0].privateAddress); })); describe("Balance", () => { it("should get 10K Test USDCs", () => __awaiter(void 0, void 0, void 0, function* () { const usdcBalance = yield client.getUSDCBalance(); const mintAmount = 10000; const tx = yield onChainCalls.mintUSDC({ amount: (0, library_sui_1.toBigNumberStr)(mintAmount, library_sui_1.BASE_DECIMALS), to: client.getPublicAddress(), }); (0, chai_1.expect)(library_sui_1.Transaction.getStatus(tx)).to.be.equal("success"); const expectedBalance = Math.round((usdcBalance + mintAmount) * 100) / 100; const newBalance = Math.round((yield client.getUSDCBalance()) * 100) / 100; (0, chai_1.expect)(newBalance).to.be.gte(expectedBalance); })); it("should move 1 USDC token to Margin Bank", () => __awaiter(void 0, void 0, void 0, function* () { var _a; const usdcBalance = yield client.getUSDCBalance(); const marginBankBalance = yield client.getMarginBankBalance(); const depositAmount = 1; (0, chai_1.expect)((_a = (yield client.depositToMarginBank(depositAmount))) === null || _a === void 0 ? void 0 : _a.ok).to.be.equal(true); const newBalance = Math.round((yield client.getUSDCBalance()) * 100) / 100; const expectedBalance = Math.round((usdcBalance - depositAmount) * 100) / 100; (0, chai_1.expect)(newBalance).to.be.lte(expectedBalance); const newMarginBankBalance = Math.round((yield client.getMarginBankBalance()) * 100) / 100; const expectedMarginBankBalance = Math.round((marginBankBalance + depositAmount) * 100) / 100; (0, chai_1.expect)(newMarginBankBalance).to.be.gte(expectedMarginBankBalance); })); it("should withdraw 1 USDC token from Margin Bank", () => __awaiter(void 0, void 0, void 0, function* () { var _b; const usdcBalance = yield client.getUSDCBalance(); (0, chai_1.expect)((_b = (yield client.withdrawFromMarginBank(1))) === null || _b === void 0 ? void 0 : _b.ok).to.be.equal(true); (0, chai_1.expect)(yield client.getUSDCBalance()).to.be.gte(usdcBalance + 1); })); it("should move all USDC token from Margin Bank", () => __awaiter(void 0, void 0, void 0, function* () { var _c; (0, chai_1.expect)((_c = (yield client.withdrawFromMarginBank())) === null || _c === void 0 ? void 0 : _c.ok).to.be.equal(true); (0, chai_1.expect)(yield client.getMarginBankBalance()).to.be.eql(0); })); }); describe("Create Orders", () => { beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { })); it("should create signed order", () => __awaiter(void 0, void 0, void 0, function* () { const signedOrder = yield client.createSignedOrder({ symbol, price: 0, quantity: 0.1, side: library_sui_1.ORDER_SIDE.SELL, orderType: library_sui_1.ORDER_TYPE.MARKET, }); (0, chai_1.expect)(signedOrder.leverage).to.be.equal(defaultLeverage); (0, chai_1.expect)(signedOrder.price).to.be.equal(0); (0, chai_1.expect)(signedOrder.quantity).to.be.equal(0.1); })); }); describe("Trade", () => { let maker; let taker; let signedMakerOrder; let signedTakerOrder; const tradePrice = 1800; const tradeQty = 0.1; const depositAmount = tradePrice * tradeQty; before(() => __awaiter(void 0, void 0, void 0, function* () { maker = new index_1.BluefinClient(true, index_1.Networks.TESTNET_SUI, TEST_WALLETS[0].phrase, "Secp256k1"); yield maker.init(); taker = new index_1.BluefinClient(true, index_1.Networks.TESTNET_SUI, TEST_WALLETS[1].phrase, "Secp256k1"); yield taker.init(); yield (0, utils_1.setupTestAccounts)(onChainCalls, TEST_WALLETS, index_1.Networks.TESTNET_SUI.faucet); let tx = yield onChainCalls.mintUSDC({ amount: (0, library_sui_1.toBigNumberStr)(10000, library_sui_1.USDC_BASE_DECIMALS), to: maker.getPublicAddress(), }); (0, chai_1.expect)(library_sui_1.Transaction.getStatus(tx)).to.be.equal("success"); tx = yield onChainCalls.mintUSDC({ amount: (0, library_sui_1.toBigNumberStr)(10000, library_sui_1.USDC_BASE_DECIMALS), to: taker.getPublicAddress(), }); (0, chai_1.expect)(library_sui_1.Transaction.getStatus(tx)).to.be.equal("success"); })); beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { })); afterEach(() => { }); it("should have required USDCs", () => __awaiter(void 0, void 0, void 0, function* () { const balance = yield maker.getUSDCBalance(); (0, chai_1.expect)(balance).to.be.gte(depositAmount); const balance2 = yield taker.getUSDCBalance(); (0, chai_1.expect)(balance2).to.be.gte(depositAmount); })); it("should move required USDC token to Margin Bank", () => __awaiter(void 0, void 0, void 0, function* () { const balance = yield maker.getMarginBankBalance(); const resp = yield maker.depositToMarginBank(depositAmount); (0, chai_1.expect)(resp.ok).to.be.equal(true); (0, chai_1.expect)(yield maker.getMarginBankBalance()).to.be.gte(balance + depositAmount); const balance1 = yield taker.getMarginBankBalance(); const resp1 = yield taker.depositToMarginBank(depositAmount); (0, chai_1.expect)(resp1.ok).to.be.equal(true); (0, chai_1.expect)(yield taker.getMarginBankBalance()).to.be.gte(balance1 + depositAmount); })); it("should create signed maker order", () => __awaiter(void 0, void 0, void 0, function* () { signedMakerOrder = yield maker.createSignedOrder({ symbol, price: tradePrice, quantity: tradeQty, side: library_sui_1.ORDER_SIDE.SELL, orderType: library_sui_1.ORDER_TYPE.LIMIT, timeInForce: library_sui_1.TIME_IN_FORCE.GOOD_TILL_TIME, }); (0, chai_1.expect)(signedMakerOrder.leverage).to.be.equal(defaultLeverage); (0, chai_1.expect)(signedMakerOrder.price).to.be.equal(tradePrice); (0, chai_1.expect)(signedMakerOrder.quantity).to.be.equal(tradeQty); })); it("should create signed taker order", () => __awaiter(void 0, void 0, void 0, function* () { signedTakerOrder = yield taker.createSignedOrder({ symbol, price: tradePrice, quantity: tradeQty, side: library_sui_1.ORDER_SIDE.BUY, orderType: library_sui_1.ORDER_TYPE.MARKET, timeInForce: library_sui_1.TIME_IN_FORCE.IMMEDIATE_OR_CANCEL, }); (0, chai_1.expect)(signedTakerOrder.leverage).to.be.equal(defaultLeverage); (0, chai_1.expect)(signedTakerOrder.price).to.be.equal(tradePrice); (0, chai_1.expect)(signedTakerOrder.quantity).to.be.equal(tradeQty); })); // it("should perform trade", async () => { // const [success, tx] = await performTrade( // onChainCalls, // getSignerFromSeed(deployer.phrase, maker.getProvider()), // signedMakerOrder, // signedTakerOrder, // tradePrice // ); // if (!success) { // console.log( // "\n#################################################################\nTransaction: ", // tx, // "\n#################################################################" // ); // } // expect(success).to.be.equal(true); // }); }); describe("Sub account Tests", () => { let clientSubAccount; before(() => __awaiter(void 0, void 0, void 0, function* () { clientSubAccount = new index_1.BluefinClient(true, network, TEST_WALLETS[0].phrase, "Secp256k1"); yield clientSubAccount.init(); // adding sub acc const resp = yield client.setSubAccount(TEST_WALLETS[1].privateAddress, true); if (!resp.ok) { throw Error(resp.message); } })); beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { clientSubAccount = new index_1.BluefinClient(true, network, TEST_WALLETS[0].phrase, "Secp256k1"); yield clientSubAccount.init(); })); // TODO: Uncomment once DAPI is up // it("get leverage on behalf of parent account", async () => { // const res = await clientSubAccount.getUserDefaultLeverage( // symbol, // TEST_WALLETS[0].privateAddress.toLowerCase() // ); // set leverage will do contract call as the account using is new // // Then // console.log("res", res); // expect(res).to.eq(defaultLeverage); // }); it("set leverage on behalf of parent account", () => __awaiter(void 0, void 0, void 0, function* () { // The user needs to have an open position to set leverage // When const newLeverage = 2; const res = yield clientSubAccount.adjustLeverage({ symbol, leverage: newLeverage, parentAddress: TEST_WALLETS[0].privateAddress.toLowerCase(), }); // set leverage will do contract call as the account using is new // Then if (!res.ok) { throw Error(res.message); } (0, chai_1.expect)(res.ok).to.eq(true); })); }); }); //# sourceMappingURL=bluefinClientContractCalls.test.js.map