aboutsummaryrefslogtreecommitdiff
path: root/src/cryptoLib.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-11-20 04:15:49 +0100
committerFlorian Dold <florian.dold@gmail.com>2016-11-20 04:15:49 +0100
commit7356d4257ebbd35ef8735edd1a287f87e2aa1f1b (patch)
tree20238d7e55b109f57249531f99348d1425871e44 /src/cryptoLib.ts
parent9197bda90a9c88132545d85b054d29b36d449c1b (diff)
fix feeDeposit/max_fee corner case
Diffstat (limited to 'src/cryptoLib.ts')
-rw-r--r--src/cryptoLib.ts20
1 files changed, 18 insertions, 2 deletions
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
+}