1
1
import { SwapPhase , SwapRole , SwapState } from '../types/enums' ;
2
2
import Peer from '../p2p/Peer' ;
3
+ import { Models } from '../db/DB' ;
3
4
import * as packets from '../p2p/packets/types' ;
4
5
import { createHash , randomBytes } from 'crypto' ;
5
6
import Logger from '../Logger' ;
@@ -10,7 +11,6 @@ import { EventEmitter } from 'events';
10
11
import SwapRepository from './SwapRepository' ;
11
12
import { OwnOrder , PeerOrder } from '../types/orders' ;
12
13
import assert from 'assert' ;
13
- import { Models } from '../db/DB' ;
14
14
import { SwapDealInstance } from 'lib/types/db' ;
15
15
import { SwapDeal , SwapResult } from './types' ;
16
16
@@ -30,7 +30,6 @@ class Swaps extends EventEmitter {
30
30
private deals = new Map < string , SwapDeal > ( ) ;
31
31
private usedHashes = new Set < string > ( ) ;
32
32
private repository : SwapRepository ;
33
-
34
33
/** The number of satoshis in a bitcoin. */
35
34
private static readonly SATOSHIS_PER_COIN = 100000000 ;
36
35
@@ -468,15 +467,17 @@ class Swaps extends EventEmitter {
468
467
if ( quantity ) {
469
468
deal . quantity = quantity ; // set the accepted quantity for the deal
470
469
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 ;
472
473
} 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 ;
474
477
} 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 ;
480
481
}
481
482
}
482
483
0 commit comments