Skip to content

Commit d1f1eef

Browse files
ImmanuelSegolsangaman
authored andcommitted
feat(swaps): recalculate partial swap amts (#633)
This recalculates the amounts for a swap when a swap we've requested is accepted for less than the full amount of our request.
1 parent 03b59af commit d1f1eef

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lib/swaps/Swaps.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SwapPhase, SwapRole, SwapState } from '../types/enums';
22
import Peer from '../p2p/Peer';
3+
import { Models } from '../db/DB';
34
import * as packets from '../p2p/packets/types';
45
import { createHash, randomBytes } from 'crypto';
56
import Logger from '../Logger';
@@ -10,7 +11,6 @@ import { EventEmitter } from 'events';
1011
import SwapRepository from './SwapRepository';
1112
import { OwnOrder, PeerOrder } from '../types/orders';
1213
import assert from 'assert';
13-
import { Models } from '../db/DB';
1414
import { SwapDealInstance } from 'lib/types/db';
1515
import { SwapDeal, SwapResult } from './types';
1616

@@ -30,7 +30,6 @@ class Swaps extends EventEmitter {
3030
private deals = new Map<string, SwapDeal>();
3131
private usedHashes = new Set<string>();
3232
private repository: SwapRepository;
33-
3433
/** The number of satoshis in a bitcoin. */
3534
private static readonly SATOSHIS_PER_COIN = 100000000;
3635

@@ -468,15 +467,17 @@ class Swaps extends EventEmitter {
468467
if (quantity) {
469468
deal.quantity = quantity; // set the accepted quantity for the deal
470469
if (quantity <= 0) {
471-
// TODO: accepted quantity must be a positive number, abort deal and penalize peer
470+
this.setDealState(deal, SwapState.Error, 'accepted quantity must be a positive number');
471+
// TODO: penalize peer
472+
return;
472473
} else if (quantity > deal.proposedQuantity) {
473-
// TODO: accepted quantity should not be greater than proposed quantity, abort deal and penalize peer
474+
this.setDealState(deal, SwapState.Error, 'accepted quantity should not be greater than proposed quantity');
475+
// TODO: penalize peer
476+
return;
474477
} else if (quantity < deal.proposedQuantity) {
475-
// TODO: handle partial acceptance
476-
// the maker accepted only part of our swap request, adjust the deal amounts
477-
// const { takerAmount, makerAmount } = Swaps.calculateSwapAmounts(quantity, deal.price);
478-
// deal.takerAmount = takerAmount;
479-
// deal.makerAmount = makerAmount;
478+
const { makerAmount, takerAmount } = Swaps.calculateSwapAmounts(quantity, deal.price, deal.isBuy);
479+
deal.takerAmount = takerAmount;
480+
deal.makerAmount = makerAmount;
480481
}
481482
}
482483

0 commit comments

Comments
 (0)