UNPKG

@bajetech/digitalbits-wallet-sdk

Version:

A library to make it easier to write wallets that interact with the DigitalBits blockchain

58 lines 2.73 kB
import { __spreadArray } from "tslib"; import BigNumber from "bignumber.js"; import { Asset } from "xdb-digitalbits-sdk"; import { makeDisplayableTrades } from "./makeDisplayableTrades"; import { NATIVE_ASSET_IDENTIFIER } from "../constants/digitalbits"; export function makeDisplayableOffers(subjectAccount, params) { var offers = params.offers, tradeResponses = params.tradeResponses; var trades = tradeResponses.flat(); // make a map of offerids to the trades involved with them // (reminder that each trade has two offerids, one for each side) var offeridsToTradesMap = makeDisplayableTrades(subjectAccount, trades).reduce(function (memo, trade) { if (trade.paymentOfferId) { memo[trade.paymentOfferId] = __spreadArray(__spreadArray([], (memo[trade.paymentOfferId] || []), true), [ trade, ], false); } return memo; }, {}); return offers.map(function (offer) { var id = offer.id, selling = offer.selling, seller = offer.seller, buying = offer.buying, amount = offer.amount, price_r = offer.price_r, last_modified_time = offer.last_modified_time; var paymentToken = { type: selling.asset_type, code: selling.asset_code || Asset.native().getCode(), issuer: selling.asset_type === NATIVE_ASSET_IDENTIFIER ? undefined : { key: selling.asset_issuer, }, }; var incomingToken = { type: buying.asset_type, code: buying.asset_code || Asset.native().getCode(), issuer: buying.asset_type === NATIVE_ASSET_IDENTIFIER ? undefined : { key: buying.asset_issuer, }, }; var tradePaymentAmount = (offeridsToTradesMap[id] || []).reduce(function (memo, trade) { return memo.plus(trade.paymentAmount); }, new BigNumber(0)); return { id: "".concat(id), offerer: { publicKey: seller, }, timestamp: Math.floor(new Date(last_modified_time).getTime() / 1000), paymentToken: paymentToken, paymentAmount: new BigNumber(amount), initialPaymentAmount: new BigNumber(amount).plus(tradePaymentAmount), incomingToken: incomingToken, incomingAmount: new BigNumber(price_r.n).div(price_r.d).times(amount), incomingTokenPrice: new BigNumber(1).div(price_r.n).times(price_r.d), resultingTrades: (offeridsToTradesMap[id] || []).map(function (trade) { return trade.id; }), }; }); } //# sourceMappingURL=makeDisplayableOffers.js.map