UNPKG

@melonproject/protocol

Version:

Technology Regulated and Operated Investment Funds

90 lines (89 loc) 4.82 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 token_math_1 = require("@melonproject/token-math"); const web3Utils = __importStar(require("web3-utils")); const transactionFactory_1 = require("../../../../utils/solidity/transactionFactory"); const getExchangeIndex_1 = require("../calls/getExchangeIndex"); const ensureSufficientBalance_1 = require("../../../dependencies/token/guards/ensureSufficientBalance"); const getRoutes_1 = require("../../hub/calls/getRoutes"); const getHub_1 = require("../../hub/calls/getHub"); const ensureFundOwner_1 = require("../guards/ensureFundOwner"); const ensureTakePermitted_1 = require("../guards/ensureTakePermitted"); const Contracts_1 = require("../../../../Contracts"); const FunctionSignatures_1 = require("../utils/FunctionSignatures"); const emptyAddress_1 = require("../../../../utils/constants/emptyAddress"); const getToken_1 = require("../../../dependencies/token/calls/getToken"); const ensure_1 = require("../../../../utils/guards/ensure"); const guard = (environment, { id, makerQuantity, takerQuantity, fillTakerQuantity = 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); yield ensureSufficientBalance_1.ensureSufficientBalance(environment, fillTakerQuantity, vaultAddress); yield ensureFundOwner_1.ensureFundOwner(environment, contractAddress); yield token_math_1.ensureSameToken(fillTakerQuantity.token, takerQuantity.token); // TODO: add all preflights yield ensureTakePermitted_1.ensureTakePermitted(environment, contractAddress, Contracts_1.Exchanges.MatchingMarket, makerQuantity, takerQuantity, fillTakerQuantity, id); }); const prepareArgs = (environment, { id, makerQuantity, takerQuantity, maker, fillTakerQuantity = 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.takeOrder, [ maker.toString(), contractAddress.toString(), makerQuantity.token.address.toString(), takerQuantity.token.address.toString(), emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress, ], [ makerQuantity.quantity.toString(), takerQuantity.quantity.toString(), '0', '0', '0', '0', fillTakerQuantity.quantity.toString(), 0, ], `0x${Number(id) .toString(16) .padStart(64, '0')}`, web3Utils.padLeft('0x0', 64), web3Utils.padLeft('0x0', 64), web3Utils.padLeft('0x0', 64), ]; }); const postProcess = (environment, receipt) => __awaiter(this, void 0, void 0, function* () { const logTake = receipt.events.LogTake.returnValues; ensure_1.ensure(!!logTake, 'No LogTake found in logs'); const sellToken = yield getToken_1.getToken(environment, logTake.pay_gem); const buyToken = yield getToken_1.getToken(environment, logTake.buy_gem); return { buy: token_math_1.createQuantity(buyToken, logTake.take_amt), id: web3Utils.toDecimal(logTake.id), maker: new token_math_1.Address(logTake.maker), sell: token_math_1.createQuantity(sellToken, logTake.give_amt), taker: new token_math_1.Address(logTake.taker), timestamp: logTake.timestamp, }; }); const takeOasisDexOrder = transactionFactory_1.transactionFactory('callOnExchange', Contracts_1.Contracts.Trading, guard, prepareArgs, postProcess); exports.takeOasisDexOrder = takeOasisDexOrder;