UNPKG

@melonproject/protocol

Version:

Technology Regulated and Operated Investment Funds

77 lines (76 loc) 4.25 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const getContract_1 = require("../../../utils/solidity/getContract"); const transactionFactory_1 = require("../../../utils/solidity/transactionFactory"); const token_math_1 = require("@melonproject/token-math"); const Contracts_1 = require("../../../Contracts"); const approve_1 = require("../../dependencies/token/transactions/approve"); const ensure_1 = require("../../../utils/guards/ensure"); const ensureSufficientBalance_1 = require("../../dependencies/token/guards/ensureSufficientBalance"); const web3Utils = __importStar(require("web3-utils")); const getLogCurried_1 = require("../../../utils/environment/getLogCurried"); const getLog = getLogCurried_1.getLogCurried('melon:protocol:contracts:makeOrderFromAccountOasisDex'); const guard = (environment, params, contractAddress) => __awaiter(this, void 0, void 0, function* () { const log = getLog(environment); yield approve_1.approve(environment, { howMuch: params.sell, spender: contractAddress, }); const oasisDexContract = getContract_1.getContract(environment, Contracts_1.Contracts.MatchingMarket, contractAddress); const dust = yield oasisDexContract.methods ._dust(params.sell.token.address) .call(); log.debug({ dust }); ensure_1.ensure(token_math_1.greaterThan(params.sell, token_math_1.createQuantity(params.sell.token, dust)), 'Selling quantity too low.'); log.debug(params.sell.token.address, params.buy.token.address); const isWhitelisted = yield oasisDexContract.methods .isTokenPairWhitelisted(params.sell.token.address.toString(), params.buy.token.address.toString()) .call(); log.debug({ isWhitelisted }); ensure_1.ensure(isWhitelisted, 'Token pair not whitelisted'); ensureSufficientBalance_1.ensureSufficientBalance(environment, params.sell, environment.wallet.address); log.debug('Sufficient balance'); const isMarketClosed = yield oasisDexContract.methods.isClosed().call(); ensure_1.ensure(!isMarketClosed, 'Market closed'); log.debug('Guards passed'); }); const prepareArgs = (_, { sell, buy }) => __awaiter(this, void 0, void 0, function* () { return [ sell.quantity.toString(), sell.token.address, buy.quantity.toString(), buy.token.address, 0, ]; }); const postProcess = (environment, receipt, params, contractAddress) => __awaiter(this, void 0, void 0, function* () { const logEntry = receipt.events.LogMake || receipt.events.LogTake; ensure_1.ensure(!!logEntry, `No LogMake nor LogTake found in transaction: ${receipt.transactionHash}`); const matched = !!receipt.events.LogTrade; return { buy: token_math_1.createQuantity(params.buy.token, logEntry.returnValues.buy_amt), id: web3Utils.toDecimal(logEntry.returnValues.id), maker: logEntry.returnValues.maker, matched, sell: token_math_1.createQuantity(params.sell.token, logEntry.returnValues.pay_amt), taker: logEntry.returnValues.taker, timestamp: logEntry.returnValues.timestamp, }; }); const makeOrderFromAccountOasisDex = transactionFactory_1.transactionFactory('offer', Contracts_1.Contracts.MatchingMarket, guard, prepareArgs, postProcess); exports.makeOrderFromAccountOasisDex = makeOrderFromAccountOasisDex;