UNPKG

@melonproject/protocol

Version:

Technology Regulated and Operated Investment Funds

102 lines (101 loc) 5.39 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 web3Utils = __importStar(require("web3-utils")); const token_math_1 = require("@melonproject/token-math"); const transactionFactory_1 = require("../../../../utils/solidity/transactionFactory"); const getExchangeIndex_1 = require("../calls/getExchangeIndex"); const ensureMakePermitted_1 = require("../guards/ensureMakePermitted"); const getToken_1 = require("../../../dependencies/token/calls/getToken"); const ensureSufficientBalance_1 = require("../../../dependencies/token/guards/ensureSufficientBalance"); const getHub_1 = require("../../hub/calls/getHub"); const getRoutes_1 = require("../../hub/calls/getRoutes"); const ensureIsNotShutDown_1 = require("../../hub/guards/ensureIsNotShutDown"); const ensureFundOwner_1 = require("../guards/ensureFundOwner"); const Contracts_1 = require("../../../../Contracts"); const FunctionSignatures_1 = require("../utils/FunctionSignatures"); const emptyAddress_1 = require("../../../../utils/constants/emptyAddress"); const ensureNotInOpenMakeOrder_1 = require("../guards/ensureNotInOpenMakeOrder"); const ensure_1 = require("../../../../utils/guards/ensure"); const guard = (environment, { makerQuantity, takerQuantity }, contractAddress) => __awaiter(this, void 0, void 0, function* () { const hubAddress = yield getHub_1.getHub(environment, contractAddress); const { vaultAddress } = yield getRoutes_1.getRoutes(environment, hubAddress); const minBalance = makerQuantity; yield ensureSufficientBalance_1.ensureSufficientBalance(environment, minBalance, vaultAddress); yield ensureFundOwner_1.ensureFundOwner(environment, contractAddress); yield ensureIsNotShutDown_1.ensureIsNotShutDown(environment, hubAddress); yield ensureNotInOpenMakeOrder_1.ensureNotInOpenMakeOrder(environment, contractAddress, { makerToken: makerQuantity.token, }); // Ensure fund not shut down. // Ensure exchange method is allowed. // Ensure not buying/selling of own fund token. // Ensure price provided on this asset pair. // Ensure price feed data is not outdated. // Ensure there are no other open orders for the asset. // IF MATCHINGMARKET: // Ensure selling quantity is not too low. yield ensureMakePermitted_1.ensureMakePermitted(environment, contractAddress, makerQuantity, takerQuantity); }); const prepareArgs = (environment, { makerQuantity, takerQuantity }, contractAddress) => __awaiter(this, void 0, void 0, function* () { const exchangeIndex = yield getExchangeIndex_1.getExchangeIndex(environment, contractAddress, { exchange: Contracts_1.Exchanges.MatchingMarket, }); return [ exchangeIndex, FunctionSignatures_1.FunctionSignatures.makeOrder, [ contractAddress.toString(), emptyAddress_1.emptyAddress, makerQuantity.token.address.toString(), takerQuantity.token.address.toString(), emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress, ], [ makerQuantity.quantity.toString(), takerQuantity.quantity.toString(), '0', '0', '0', '0', '0', 0, ], web3Utils.padLeft('0x0', 64), web3Utils.padLeft('0x0', 64), web3Utils.padLeft('0x0', 64), web3Utils.padLeft('0x0', 64), ]; }); const postProcess = (environment, receipt) => __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; const sellToken = yield getToken_1.getToken(environment, logEntry.returnValues.pay_gem); const buyToken = yield getToken_1.getToken(environment, logEntry.returnValues.buy_gem); return { buy: token_math_1.createQuantity(buyToken, logEntry.returnValues.buy_amt), id: web3Utils.toDecimal(logEntry.returnValues.id), maker: logEntry.returnValues.maker, matched, sell: token_math_1.createQuantity(sellToken, logEntry.returnValues.pay_amt), timestamp: logEntry.returnValues.timestamp, }; }); const makeOasisDexOrder = transactionFactory_1.transactionFactory('callOnExchange', Contracts_1.Contracts.Trading, guard, prepareArgs, postProcess); exports.makeOasisDexOrder = makeOasisDexOrder;