Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(swaps): recalculate partial swap amounts #633

Merged
merged 9 commits into from
Nov 16, 2018
35 changes: 21 additions & 14 deletions lib/swaps/Swaps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { SwapPhase, SwapRole, SwapState } from '../types/enums';
import { SwapPhase, SwapRole, SwapState, ReputationEvent } from '../types/enums';
import Peer from '../p2p/Peer';
import P2PRepository from '../p2p/P2PRepository';
import NodeList from '../p2p/NodeList';
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 +13,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 @@ -29,14 +31,17 @@ class Swaps extends EventEmitter {
/** A map between payment hashes and swap deals. */
private deals = new Map<string, SwapDeal>();
private usedHashes = new Set<string>();
private repository: SwapRepository;

private swapRepository: SwapRepository;
private p2pRepository: P2PRepository;
private nodes: NodeList;
/** The number of satoshis in a bitcoin. */
private static readonly SATOSHIS_PER_COIN = 100000000;

constructor(private logger: Logger, private models: Models, private pool: Pool, private lndBtcClient: LndClient, private lndLtcClient: LndClient) {
super();
this.repository = new SwapRepository(this.models);
this.swapRepository = new SwapRepository(this.models);
ImmanuelSegol marked this conversation as resolved.
Show resolved Hide resolved
this.p2pRepository = new P2PRepository(this.models);
this.nodes = new NodeList(this.p2pRepository);
ImmanuelSegol marked this conversation as resolved.
Show resolved Hide resolved
this.bind();
}

Expand Down Expand Up @@ -83,7 +88,7 @@ class Swaps extends EventEmitter {

public init = async () => {
// Load Swaps from data base
const result = await this.repository.getSwapDeals();
const result = await this.swapRepository.getSwapDeals();
result.forEach((deal: SwapDealInstance) => {
this.usedHashes.add(deal.rHash);
});
Expand Down Expand Up @@ -149,7 +154,7 @@ class Swaps extends EventEmitter {
*/
private persistDeal = async (deal: SwapDeal) => {
if (this.usedHashes.has(deal.rHash)) {
await this.repository.addSwapDeal(deal);
await this.swapRepository.addSwapDeal(deal);
this.removeDeal(deal);
}
}
Expand Down Expand Up @@ -435,15 +440,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');
await this.nodes.addReputationEvent(peer.nodePubKey!, ReputationEvent.SwapFailure);
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');
await this.nodes.addReputationEvent(peer.nodePubKey!, ReputationEvent.SwapFailure);
ImmanuelSegol marked this conversation as resolved.
Show resolved Hide resolved
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