11import { SwapPhase , SwapRole , SwapState } from '../types/enums' ;
22import Peer from '../p2p/Peer' ;
3+ import { Models } from '../db/DB' ;
34import * as packets from '../p2p/packets/types' ;
45import { createHash , randomBytes } from 'crypto' ;
56import Logger from '../Logger' ;
@@ -10,7 +11,6 @@ import { EventEmitter } from 'events';
1011import SwapRepository from './SwapRepository' ;
1112import { OwnOrder , PeerOrder } from '../types/orders' ;
1213import assert from 'assert' ;
13- import { Models } from '../db/DB' ;
1414import { SwapDealInstance } from 'lib/types/db' ;
1515import { 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