Skip to content

Commit

Permalink
feat(swaps): recalculate partial swap amts (#633)
Browse files Browse the repository at this point in the history
This recalculates the amounts for a swap when a swap we've requested
is accepted for less than the full amount of our request.
  • Loading branch information
ImmanuelSegol authored and sangaman committed Nov 16, 2018
1 parent 03b59af commit d1f1eef
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/swaps/Swaps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SwapPhase, SwapRole, SwapState } from '../types/enums';
import Peer from '../p2p/Peer';
import { Models } from '../db/DB';
import * as packets from '../p2p/packets/types';
import { createHash, randomBytes } from 'crypto';
import Logger from '../Logger';
Expand All @@ -10,7 +11,6 @@ import { EventEmitter } from 'events';
import SwapRepository from './SwapRepository';
import { OwnOrder, PeerOrder } from '../types/orders';
import assert from 'assert';
import { Models } from '../db/DB';
import { SwapDealInstance } from 'lib/types/db';
import { SwapDeal, SwapResult } from './types';

Expand All @@ -30,7 +30,6 @@ class Swaps extends EventEmitter {
private deals = new Map<string, SwapDeal>();
private usedHashes = new Set<string>();
private repository: SwapRepository;

/** The number of satoshis in a bitcoin. */
private static readonly SATOSHIS_PER_COIN = 100000000;

Expand Down Expand Up @@ -468,15 +467,17 @@ class Swaps extends EventEmitter {
if (quantity) {
deal.quantity = quantity; // set the accepted quantity for the deal
if (quantity <= 0) {
// TODO: accepted quantity must be a positive number, abort deal and penalize peer
this.setDealState(deal, SwapState.Error, 'accepted quantity must be a positive number');
// TODO: penalize peer
return;
} else if (quantity > deal.proposedQuantity) {
// TODO: accepted quantity should not be greater than proposed quantity, abort deal and penalize peer
this.setDealState(deal, SwapState.Error, 'accepted quantity should not be greater than proposed quantity');
// TODO: penalize peer
return;
} else if (quantity < deal.proposedQuantity) {
// TODO: handle partial acceptance
// the maker accepted only part of our swap request, adjust the deal amounts
// const { takerAmount, makerAmount } = Swaps.calculateSwapAmounts(quantity, deal.price);
// deal.takerAmount = takerAmount;
// deal.makerAmount = makerAmount;
const { makerAmount, takerAmount } = Swaps.calculateSwapAmounts(quantity, deal.price, deal.isBuy);
deal.takerAmount = takerAmount;
deal.makerAmount = makerAmount;
}
}

Expand Down

0 comments on commit d1f1eef

Please sign in to comment.