UNPKG

@volare.finance/volare.js

Version:
129 lines 6.01 kB
"use strict"; /** * @file Exchange.ts * @author astra <astra@volare.com> * @date 2022 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Exchange = void 0; const tslib_1 = require("tslib"); const utils_js_1 = require("@volare.finance/utils.js"); const ethers_1 = require("ethers"); const Exchange_json_1 = require("../artifacts/Exchange.json"); const orders_1 = require("./orders"); const protocols_1 = require("./protocols"); class Exchange extends utils_js_1.Provider { static ABI() { return Exchange_json_1.abi; } constructor(options) { super(options.endpoint); this.contract = new ethers_1.Contract(options.contracts.Exchange, Exchange.ABI(), this.provider); this.usdc = new utils_js_1.ERC20(options.contracts.USDC, this.endpoint); } getLimitOrderRelevantState(order) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { const [{ orderHash, status }, actualFillableTakerTokenAmount, isSignatureValid] = yield ((_a = this.contract) === null || _a === void 0 ? void 0 : _a.getLimitOrderRelevantState(order.limitOrder, order.signature)); return { orderHash, isSignatureValid, status, actualFillableTakerTokenAmount: actualFillableTakerTokenAmount.toString(), }; }); } getLimitOrderHash(order) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { const orderHash = yield ((_a = this.contract) === null || _a === void 0 ? void 0 : _a.getLimitOrderHash(Object.assign(Object.assign({}, order), { makerAmount: order.makerAmount.toString(10), takerAmount: order.takerAmount.toString(10), takerTokenFeeAmount: order.takerTokenFeeAmount.toString(10) }))); return orderHash; }); } /*** * @param maker * @param order */ cancelLimitOrder(maker, order) { var _a; return tslib_1.__awaiter(this, void 0, void 0, function* () { const limitOrder = order.limitOrder; return (_a = this.contract) === null || _a === void 0 ? void 0 : _a.connect(maker).cancelLimitOrder(Object.assign(Object.assign({}, limitOrder), { makerAmount: limitOrder.makerAmount.toString(10), takerAmount: limitOrder.takerAmount.toString(10), takerTokenFeeAmount: limitOrder.takerTokenFeeAmount.toString(10) })); }); } /** * @param maker The maker wallet * @param assetAddress The address of asset being bought by the maker. * @param assetAmount The amount of asset being bought by the maker. * @param assetPrice The price of asset filled by the maker. * @param expiry The unix timestamp in seconds when this order expires. * @returns */ buy(maker, assetAddress, assetAmount, assetPrice, expiry) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const makerAmount = (0, utils_js_1.$)(assetAmount.multipliedBy(assetPrice), protocols_1.USDC_DECIMALS); const takerAmount = (0, utils_js_1.$)(assetAmount, protocols_1.VTOKEN_DECIMALS); const allowance = yield this.usdc.allowance(yield maker.getAddress(), this.address); if (allowance.lt(makerAmount)) { const tx = yield this.usdc.approve(maker, this.address, new utils_js_1.BigNumber(utils_js_1.ONE_BYTES32)); yield tx.wait(); } const limitOrder = new orders_1.LimitOrder({ chainId: yield Exchange.ChainId(), verifyingContract: this.address, makerToken: this.usdc.address, takerToken: assetAddress, makerAmount, takerAmount, maker: yield maker.getAddress(), expiry: expiry, salt: Date.now(), }); const signature = yield limitOrder.getSignature(maker, orders_1.SignatureType.EIP712); return { orderHash: limitOrder.getHash(), limitOrder, signature, }; }); } /** * @param maker The maker wallet (ethers.Wallet) * @param assetAddress The address of asset being bought by the maker. * @param assetAmount The amount of asset being bought by the maker. * @param assetPrice The price of asset filled by the maker. * @param expiry The unix timestamp in seconds when this order expires. * @returns */ sell(maker, assetAddress, assetAmount, assetPrice, expiry) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const makerAmount = (0, utils_js_1.$)(assetAmount, protocols_1.VTOKEN_DECIMALS); const takerAmount = (0, utils_js_1.$)(assetAmount.multipliedBy(assetPrice), protocols_1.USDC_DECIMALS); const asset = new utils_js_1.ERC20(assetAddress, this.endpoint); const allowance = yield asset.allowance(yield maker.getAddress(), this.address); if (allowance.lt(makerAmount)) { const tx = yield asset.approve(maker, this.address, new utils_js_1.BigNumber(utils_js_1.ONE_BYTES32)); yield tx.wait(); } const limitOrder = new orders_1.LimitOrder({ chainId: yield Exchange.ChainId(), verifyingContract: this.address, makerToken: assetAddress, takerToken: this.usdc.address, makerAmount, takerAmount, maker: yield maker.getAddress(), expiry: expiry, salt: Date.now(), }); const signature = yield limitOrder.getSignature(maker, orders_1.SignatureType.EIP712); return { orderHash: limitOrder.getHash(), limitOrder, signature, }; }); } } exports.Exchange = Exchange; //# sourceMappingURL=Exchange.js.map