UNPKG

@melonproject/protocol

Version:

Technology Regulated and Operated Investment Funds

96 lines (95 loc) 5.22 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 order_utils_1 = require("@0x/order-utils"); const token_math_1 = require("@melonproject/token-math"); const Contracts_1 = require("../../../../Contracts"); const getExchangeIndex_1 = require("../calls/getExchangeIndex"); const transactionFactory_1 = require("../../../../utils/solidity/transactionFactory"); const getHub_1 = require("../../hub/calls/getHub"); const getRoutes_1 = require("../../hub/calls/getRoutes"); const getToken_1 = require("../../../dependencies/token/calls/getToken"); const ensureSufficientBalance_1 = require("../../../dependencies/token/guards/ensureSufficientBalance"); const FunctionSignatures_1 = require("../utils/FunctionSignatures"); const emptyAddress_1 = require("../../../../utils/constants/emptyAddress"); const isValidSignature_1 = require("../../../exchanges/third-party/0x/calls/isValidSignature"); const ensure_1 = require("../../../../utils/guards/ensure"); const ensureNotInOpenMakeOrder_1 = require("../guards/ensureNotInOpenMakeOrder"); const guard = (environment, { signedOrder }, 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 makerTokenAddress = order_utils_1.assetDataUtils.decodeERC20AssetData(signedOrder.makerAssetData).tokenAddress; const makerToken = yield getToken_1.getToken(environment, makerTokenAddress); const makerQuantity = token_math_1.createQuantity(makerToken, signedOrder.makerAssetAmount.toString()); yield ensureSufficientBalance_1.ensureSufficientBalance(environment, makerQuantity, vaultAddress); yield ensureNotInOpenMakeOrder_1.ensureNotInOpenMakeOrder(environment, contractAddress, { makerToken }); }); const prepareArgs = (environment, { signedOrder }, contractAddress) => __awaiter(this, void 0, void 0, function* () { const exchangeIndex = yield getExchangeIndex_1.getExchangeIndex(environment, contractAddress, { exchange: Contracts_1.Exchanges.ZeroEx, }); const makerTokenAddress = order_utils_1.assetDataUtils.decodeERC20AssetData(signedOrder.makerAssetData).tokenAddress; const takerTokenAddress = order_utils_1.assetDataUtils.decodeERC20AssetData(signedOrder.takerAssetData).tokenAddress; const args = [ exchangeIndex, FunctionSignatures_1.FunctionSignatures.makeOrder, [ contractAddress.toString(), emptyAddress_1.emptyAddress, makerTokenAddress, takerTokenAddress, signedOrder.feeRecipientAddress, emptyAddress_1.emptyAddress, ], [ signedOrder.makerAssetAmount.toString(), signedOrder.takerAssetAmount.toString(), signedOrder.makerFee.toString(), signedOrder.takerFee.toString(), signedOrder.expirationTimeSeconds.toString(), signedOrder.salt.toString(), 0, 0, ], web3Utils.padLeft('0x0', 64), signedOrder.makerAssetData, signedOrder.takerAssetData, `${signedOrder.signature}`, ]; return args; }); const postProcess = (environment, _, { signedOrder }, contractAddress) => __awaiter(this, void 0, void 0, function* () { // Check after the transaction if the signature is valid const zeroExAddress = environment.deployment.exchangeConfigs[Contracts_1.Exchanges.ZeroEx].exchange; const validSignature = yield isValidSignature_1.isValidSignature(environment, zeroExAddress, { signedOrder, }); ensure_1.ensure(validSignature, 'Signature invalid'); // console.log(signedOrder, contractAddress); // TODO: This fails // const validSignatureOffChain = await isValidSignatureOffChain( // environment, // R.omit(['signature'], signedOrder), // signedOrder.signature, // zeroExAddress.toString(), // ); // ensure(validSignatureOffChain, 'Off-chain Signature invalid'); return true; }); const make0xOrder = transactionFactory_1.transactionFactory('callOnExchange', Contracts_1.Contracts.Trading, guard, prepareArgs, postProcess); exports.make0xOrder = make0xOrder;