DB tweaks

This commit is contained in:
Florian Dold 2020-09-08 20:45:33 +05:30
parent f79d10eea2
commit ddbb09b140
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
5 changed files with 46 additions and 51 deletions

View File

@ -34,6 +34,7 @@ import {
WalletContractData, WalletContractData,
CoinRecord, CoinRecord,
DenominationRecord, DenominationRecord,
PayCoinSelection,
} from "../types/dbTypes"; } from "../types/dbTypes";
import { NotificationType } from "../types/notifications"; import { NotificationType } from "../types/notifications";
import { import {
@ -83,37 +84,6 @@ import { initRetryInfo, updateRetryInfoTimeout, getRetryDuration } from "../util
*/ */
const logger = new Logger("pay.ts"); const logger = new Logger("pay.ts");
/**
* Result of selecting coins, contains the exchange, and selected
* coins with their denomination.
*/
export interface PayCoinSelection {
/**
* Amount requested by the merchant.
*/
paymentAmount: AmountJson;
/**
* Public keys of the coins that were selected.
*/
coinPubs: string[];
/**
* Amount that each coin contributes.
*/
coinContributions: AmountJson[];
/**
* How much of the wire fees is the customer paying?
*/
customerWireFees: AmountJson;
/**
* How much of the deposit fees is the customer paying?
*/
customerDepositFees: AmountJson;
}
/** /**
* Structure to describe a coin that is available to be * Structure to describe a coin that is available to be
* used in a payment. * used in a payment.
@ -141,9 +111,6 @@ export interface AvailableCoinInfo {
feeDeposit: AmountJson; feeDeposit: AmountJson;
} }
export interface PayCostInfo {
totalCost: AmountJson;
}
/** /**
* Compute the total cost of a payment to the customer. * Compute the total cost of a payment to the customer.
@ -155,7 +122,7 @@ export interface PayCostInfo {
export async function getTotalPaymentCost( export async function getTotalPaymentCost(
ws: InternalWalletState, ws: InternalWalletState,
pcs: PayCoinSelection, pcs: PayCoinSelection,
): Promise<PayCostInfo> { ): Promise<AmountJson> {
const costs = []; const costs = [];
for (let i = 0; i < pcs.coinPubs.length; i++) { for (let i = 0; i < pcs.coinPubs.length; i++) {
const coin = await ws.db.get(Stores.coins, pcs.coinPubs[i]); const coin = await ws.db.get(Stores.coins, pcs.coinPubs[i]);
@ -183,9 +150,7 @@ export async function getTotalPaymentCost(
costs.push(pcs.coinContributions[i]); costs.push(pcs.coinContributions[i]);
costs.push(refreshCost); costs.push(refreshCost);
} }
return { return Amounts.sum(costs).amount;
totalCost: Amounts.sum(costs).amount,
};
} }
/** /**
@ -470,7 +435,7 @@ async function recordConfirmPay(
contractData: d.contractData, contractData: d.contractData,
lastSessionId: sessionId, lastSessionId: sessionId,
payCoinSelection: coinSelection, payCoinSelection: coinSelection,
payCostInfo, totalPayCost: payCostInfo,
coinDepositPermissions, coinDepositPermissions,
timestampAccept: getTimestampNow(), timestampAccept: getTimestampNow(),
timestampLastRefundStatus: undefined, timestampLastRefundStatus: undefined,
@ -1057,15 +1022,15 @@ export async function preparePayForUri(
}; };
} }
const costInfo = await getTotalPaymentCost(ws, res); const totalCost = await getTotalPaymentCost(ws, res);
logger.trace("costInfo", costInfo); logger.trace("costInfo", totalCost);
logger.trace("coinsForPayment", res); logger.trace("coinsForPayment", res);
return { return {
status: PreparePayResultType.PaymentPossible, status: PreparePayResultType.PaymentPossible,
contractTerms: JSON.parse(d.contractTermsRaw), contractTerms: JSON.parse(d.contractTermsRaw),
proposalId: proposal.proposalId, proposalId: proposal.proposalId,
amountEffective: Amounts.stringify(costInfo.totalCost), amountEffective: Amounts.stringify(totalCost),
amountRaw: Amounts.stringify(res.paymentAmount), amountRaw: Amounts.stringify(res.paymentAmount),
}; };
} }
@ -1092,7 +1057,7 @@ export async function preparePayForUri(
contractTermsHash: purchase.contractData.contractTermsHash, contractTermsHash: purchase.contractData.contractTermsHash,
paid: true, paid: true,
amountRaw: Amounts.stringify(purchase.contractData.amount), amountRaw: Amounts.stringify(purchase.contractData.amount),
amountEffective: Amounts.stringify(purchase.payCostInfo.totalCost), amountEffective: Amounts.stringify(purchase.totalPayCost),
proposalId, proposalId,
}; };
} else if (!purchase.timestampFirstSuccessfulPay) { } else if (!purchase.timestampFirstSuccessfulPay) {
@ -1102,7 +1067,7 @@ export async function preparePayForUri(
contractTermsHash: purchase.contractData.contractTermsHash, contractTermsHash: purchase.contractData.contractTermsHash,
paid: false, paid: false,
amountRaw: Amounts.stringify(purchase.contractData.amount), amountRaw: Amounts.stringify(purchase.contractData.amount),
amountEffective: Amounts.stringify(purchase.payCostInfo.totalCost), amountEffective: Amounts.stringify(purchase.totalPayCost),
proposalId, proposalId,
}; };
} else { } else {
@ -1113,7 +1078,7 @@ export async function preparePayForUri(
contractTermsHash: purchase.contractData.contractTermsHash, contractTermsHash: purchase.contractData.contractTermsHash,
paid, paid,
amountRaw: Amounts.stringify(purchase.contractData.amount), amountRaw: Amounts.stringify(purchase.contractData.amount),
amountEffective: Amounts.stringify(purchase.payCostInfo.totalCost), amountEffective: Amounts.stringify(purchase.totalPayCost),
...(paid ? { nextUrl: purchase.contractData.orderId } : {}), ...(paid ? { nextUrl: purchase.contractData.orderId } : {}),
proposalId, proposalId,
}; };

View File

@ -470,7 +470,7 @@ export async function applyRefund(
return { return {
contractTermsHash: purchase.contractData.contractTermsHash, contractTermsHash: purchase.contractData.contractTermsHash,
proposalId: purchase.proposalId, proposalId: purchase.proposalId,
amountEffectivePaid: Amounts.stringify(purchase.payCostInfo.totalCost), amountEffectivePaid: Amounts.stringify(purchase.totalPayCost),
amountRefundGone: Amounts.stringify(amountRefundGone), amountRefundGone: Amounts.stringify(amountRefundGone),
amountRefundGranted: Amounts.stringify(amountRefundGranted), amountRefundGranted: Amounts.stringify(amountRefundGranted),
pendingAtExchange, pendingAtExchange,

View File

@ -238,7 +238,7 @@ export async function getTransactions(
transactions.push({ transactions.push({
type: TransactionType.Payment, type: TransactionType.Payment,
amountRaw: Amounts.stringify(pr.contractData.amount), amountRaw: Amounts.stringify(pr.contractData.amount),
amountEffective: Amounts.stringify(pr.payCostInfo.totalCost), amountEffective: Amounts.stringify(pr.totalPayCost),
status: pr.timestampFirstSuccessfulPay status: pr.timestampFirstSuccessfulPay
? PaymentStatus.Paid ? PaymentStatus.Paid
: PaymentStatus.Accepted, : PaymentStatus.Accepted,

View File

@ -27,7 +27,6 @@ import { AmountJson } from "../util/amounts";
import { import {
Auditor, Auditor,
CoinDepositPermission, CoinDepositPermission,
TipResponse,
ExchangeSignKeyJson, ExchangeSignKeyJson,
MerchantInfo, MerchantInfo,
Product, Product,
@ -43,8 +42,7 @@ import {
ReserveClosingTransaction, ReserveClosingTransaction,
ReserveRecoupTransaction, ReserveRecoupTransaction,
} from "./ReserveTransaction"; } from "./ReserveTransaction";
import { Timestamp, Duration, getTimestampNow } from "../util/time"; import { Timestamp, Duration } from "../util/time";
import { PayCoinSelection, PayCostInfo } from "../operations/pay";
import { IDBKeyPath } from "idb-bridge"; import { IDBKeyPath } from "idb-bridge";
import { RetryInfo } from "../util/retries"; import { RetryInfo } from "../util/retries";
@ -1255,6 +1253,37 @@ export interface WalletContractData {
maxDepositFee: AmountJson; maxDepositFee: AmountJson;
} }
/**
* Result of selecting coins, contains the exchange, and selected
* coins with their denomination.
*/
export interface PayCoinSelection {
/**
* Amount requested by the merchant.
*/
paymentAmount: AmountJson;
/**
* Public keys of the coins that were selected.
*/
coinPubs: string[];
/**
* Amount that each coin contributes.
*/
coinContributions: AmountJson[];
/**
* How much of the wire fees is the customer paying?
*/
customerWireFees: AmountJson;
/**
* How much of the deposit fees is the customer paying?
*/
customerDepositFees: AmountJson;
}
/** /**
* Record that stores status information about one purchase, starting from when * Record that stores status information about one purchase, starting from when
* the customer accepts a proposal. Includes refund status if applicable. * the customer accepts a proposal. Includes refund status if applicable.
@ -1280,7 +1309,7 @@ export interface PurchaseRecord {
payCoinSelection: PayCoinSelection; payCoinSelection: PayCoinSelection;
payCostInfo: PayCostInfo; totalPayCost: AmountJson;
/** /**
* Timestamp of the first time that sending a payment to the merchant * Timestamp of the first time that sending a payment to the merchant

View File

@ -931,3 +931,4 @@ export const codecForAcceptTipRequest = (): Codec<AcceptTipRequest> =>
buildCodecForObject<AcceptTipRequest>() buildCodecForObject<AcceptTipRequest>()
.property("walletTipId", codecForString()) .property("walletTipId", codecForString())
.build("AcceptTipRequest"); .build("AcceptTipRequest");