UNPKG

@nexex/cli

Version:
78 lines 3.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const api_1 = require("@nexex/api"); const types_1 = require("@nexex/types"); const chalk_1 = __importDefault(require("chalk")); const utils_1 = require("ethers/utils"); const prompts_1 = __importDefault(require("prompts")); const MarketBase_1 = __importDefault(require("../../MarketBase")); class MarketTrade extends MarketBase_1.default { async run() { const { flags, args: { orderHash, amount } } = this.parse(MarketTrade); const obClient = this.initObClient(flags); const [walletAddr, order] = [ await this.readWalletAddr(), await obClient.queryOrder(orderHash), await this.initApi() ]; const { baseTokenAddress, quoteTokenAddress, side, signedOrder } = order; const tradeAmount = await this.getAmount(amount, baseTokenAddress); console.log(`${chalk_1.default.bold(`Amount of ${baseTokenAddress} to buy`)}: ${chalk_1.default.yellow(tradeAmount)}`); let fillTakenAmount, takerBalance; if (side === types_1.OrderSide.ASK) { fillTakenAmount = (await this.dex.token.parseAmount(baseTokenAddress, tradeAmount)) .mul(signedOrder.takerTokenAmount) .div(signedOrder.makerTokenAmount) .add(1); takerBalance = await this.dex.token.balanceOf(quoteTokenAddress, walletAddr); } else { fillTakenAmount = await this.dex.token.parseAmount(baseTokenAddress, tradeAmount); takerBalance = await this.dex.token.balanceOf(baseTokenAddress, walletAddr); } if (fillTakenAmount.gt(takerBalance)) { console.log(`no enough balance of ${signedOrder.takerTokenAddress}`); console.log(`require: ${fillTakenAmount.toDisplay()}, got: ${takerBalance.toDisplay()}`); this.exit(1); } const wallet = await this.readWallet(); const tx = await this.dex.exchange.fillOrder(wallet.connect(this.dex.eth), signedOrder, fillTakenAmount, api_1.constants.NULL_ADDRESS, false); await tx.wait(); this.exit(0); } async getAmount(amount, token) { if (amount) { return amount; } const response = await prompts_1.default({ type: 'text', name: 'amount', message: `Enter the amount of ${token} to buy? (unit: ether)`, validate: input => { try { utils_1.parseEther(input); return true; } catch (e) { return 'not a valid number'; } } }, { onCancel: () => this.exit(1) }); return response.amount; } } MarketTrade.description = 'describe the command here'; MarketTrade.examples = [ `$ nexex-cli hello hello world from ./src/hello.ts! ` ]; MarketTrade.flags = { ...MarketBase_1.default.flags }; MarketTrade.args = [{ name: 'orderHash', required: true }, { name: 'amount' }]; exports.default = MarketTrade; //# sourceMappingURL=trade.js.map