@bajetech/digitalbits-wallet-sdk
Version:
A library to make it easier to write wallets that interact with the DigitalBits blockchain
122 lines • 6.54 kB
JavaScript
import { __assign, __awaiter, __generator } from "tslib";
import BigNumber from "bignumber.js";
import { Asset } from "xdb-digitalbits-sdk";
import { NATIVE_ASSET_IDENTIFIER } from "../constants/digitalbits";
function isCreateAccount(obj) {
return obj.type === "create_account";
}
function isAccountMerge(obj) {
return obj.type === "account_merge";
}
function isPathPayment(obj) {
return (
// old, soon-to-be-deprecated name
obj.type === "path_payment" ||
// new names
obj.type === "path_payment_strict_send" ||
obj.type === "path_payment_strict_receive");
}
function getAccountMergePaymentAmount(payment, publicKey) {
return __awaiter(this, void 0, void 0, function () {
var effects, accountMergePayment, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, payment.effects()];
case 1:
effects = _a.sent();
accountMergePayment = effects.records.find(function (record) {
return record.type === "account_credited" && record.account === publicKey;
});
if (accountMergePayment && "amount" in accountMergePayment) {
return [2 /*return*/, accountMergePayment.amount];
}
return [2 /*return*/, undefined];
case 2:
e_1 = _a.sent();
return [2 /*return*/, undefined];
case 3: return [2 /*return*/];
}
});
});
}
function getMergedAccount(payment) {
return {
publicKey: payment.source_account,
};
}
export function makeDisplayablePayments(subjectAccount, payments) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, Promise.all(payments.map(function (payment) { return __awaiter(_this, void 0, void 0, function () {
var isRecipient, otherAccount, token, accountMergePaymentAmount, mergedAccount, transaction, e_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
isRecipient = payment.source_account !== subjectAccount.publicKey;
if (isCreateAccount(payment)) {
otherAccount = {
publicKey: payment.funder,
};
}
else {
otherAccount = { publicKey: isRecipient ? payment.from : payment.to };
}
token = isCreateAccount(payment)
? {
type: NATIVE_ASSET_IDENTIFIER,
code: Asset.native().getCode(),
}
: {
type: payment.asset_type,
code: payment.asset_code || Asset.native().getCode(),
issuer: payment.asset_type === NATIVE_ASSET_IDENTIFIER
? undefined
: {
key: payment.asset_issuer,
},
};
if (!isAccountMerge(payment)) return [3 /*break*/, 2];
return [4 /*yield*/, getAccountMergePaymentAmount(payment, subjectAccount.publicKey)];
case 1:
accountMergePaymentAmount = _a.sent();
mergedAccount = getMergedAccount(payment);
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, payment.transaction()];
case 3:
transaction = _a.sent();
return [3 /*break*/, 5];
case 4:
e_2 = _a.sent();
return [3 /*break*/, 5];
case 5: return [2 /*return*/, __assign(__assign({ id: payment.id, isInitialFunding: isCreateAccount(payment), isRecipient: isRecipient, token: token, amount: new BigNumber(isCreateAccount(payment)
? payment.starting_balance
: accountMergePaymentAmount || payment.amount), timestamp: Math.floor(new Date(payment.created_at).getTime() / 1000), otherAccount: otherAccount, sourceToken: isPathPayment(payment)
? {
type: payment.source_asset_type,
code: payment.source_asset_code || Asset.native().getCode(),
issuer: payment.source_asset_type === NATIVE_ASSET_IDENTIFIER
? undefined
: {
key: payment.source_asset_issuer,
},
}
: undefined, sourceAmount: isPathPayment(payment)
? new BigNumber(payment.source_amount)
: undefined, transactionId: payment.transaction_hash, type: payment.type }, (transaction
? {
memo: transaction.memo,
memoType: transaction.memo_type,
}
: {})), { mergedAccount: mergedAccount })];
}
});
}); }))];
});
});
}
//# sourceMappingURL=makeDisplayablePayments.js.map