From 7356d4257ebbd35ef8735edd1a287f87e2aa1f1b Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sun, 20 Nov 2016 04:15:49 +0100 Subject: fix feeDeposit/max_fee corner case --- src/cryptoLib.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/cryptoLib.ts') diff --git a/src/cryptoLib.ts b/src/cryptoLib.ts index 6473e1b58..fe1d6f36b 100644 --- a/src/cryptoLib.ts +++ b/src/cryptoLib.ts @@ -173,8 +173,16 @@ namespace RpcFunctions { export function signDeposit(offer: OfferRecord, cds: CoinWithDenom[]): PayCoinInfo { let ret: PayCoinInfo = []; + + + let feeList: AmountJson[] = cds.map((x) => x.denom.feeDeposit); + let fees = Amounts.add(Amounts.getZero(feeList[0].currency), ...feeList).amount; + // okay if saturates + fees = Amounts.sub(fees, offer.contract.max_fee).amount; + let total = Amounts.add(fees, offer.contract.amount).amount; + let amountSpent = native.Amount.getZero(cds[0].coin.currentAmount.currency); - let amountRemaining = new native.Amount(offer.contract.amount); + let amountRemaining = new native.Amount(total); for (let cd of cds) { let coinSpend: Amount; @@ -191,6 +199,14 @@ namespace RpcFunctions { amountSpent.add(coinSpend); amountRemaining.sub(coinSpend); + let feeDeposit: Amount = new native.Amount(cd.denom.feeDeposit); + + // Give the merchant at least the deposit fee, otherwise it'll reject + // the coin. + if (coinSpend.cmp(feeDeposit) < 0) { + coinSpend = feeDeposit; + } + let newAmount = new native.Amount(cd.coin.currentAmount); newAmount.sub(coinSpend); cd.coin.currentAmount = newAmount.toJson(); @@ -342,4 +358,4 @@ namespace RpcFunctions { const b = native.ByteArray.fromStringWithNull(str); return b.hash().toCrock(); } -} \ No newline at end of file +} -- cgit v1.2.3