Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Commit

Permalink
Merge #179
Browse files Browse the repository at this point in the history
179: Replace timeout function with external dependency r=mergify[bot] a=luckysori

Fixes #120 (imo).

## Straight from the commit

Motivated by the combination of wanting to test `tryExecuteSirenAction` and realising that we would just be testing `timeoutPromise`. We have already decided to replace exactly the same function in
`comit-rs` (comit-network/comit-rs#2330 (comment)), so replacing it here has the nice consequence of not needing to write a new test, since it's not our own code.

Side-effects:

- Move `TryParams` to `swap.ts` since it's directly related and only used for executing a swap.
- Rename `util/timeout_promise.ts` to `util/sleep.ts`, since it now only holds a `sleep` function.

## Notes

I don't believe this requires a changelog update. Am I wrong?

Co-authored-by: Lucas Soriano del Pino <l.soriano.del.pino@gmail.com>
  • Loading branch information
bors[bot] and luckysori committed Mar 31, 2020
2 parents 8bc816d + e1d2dec commit cef77e4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 40 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"express": "^4.17.1",
"moment": "^2.24.0",
"p-event": "^4.1.0",
"p-timeout": "^3.2.0",
"urijs": "^1.19.2"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export { BigNumber } from "bignumber.js";

export { ComitClient } from "./comit_client";

export { Swap } from "./swap";
export { Swap, TryParams } from "./swap";

export { Order, OrderAsset } from "./negotiation/order";
export {
Expand All @@ -46,5 +46,4 @@ export {
export { MakerNegotiator } from "./negotiation/maker/maker_negotiator";
export { TakerNegotiator } from "./negotiation/taker/taker_negotiator";

export { TryParams } from "./util/timeout_promise";
export { Lnd } from "./lnd";
10 changes: 6 additions & 4 deletions src/negotiation/maker/maker_negotiator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import express from "express";
import * as http from "http";
import pTimeout from "p-timeout";
import { ComitClient } from "../../comit_client";
import { sleep, timeoutPromise, TryParams } from "../../util/timeout_promise";
import { TryParams } from "../../swap";
import { sleep } from "../../util/sleep";
import { ExecutionParams } from "../execution_params";
import { isOrderValid, Order, toTradingPair } from "../order";
import orderSwapMatches from "./swap_order_matching";
Expand Down Expand Up @@ -122,9 +124,9 @@ export class MakerNegotiator {
order: Order,
{ maxTimeoutSecs, tryIntervalSecs }: TryParams
): Promise<void> {
return timeoutPromise(
maxTimeoutSecs * 1000,
this.acceptSwap(swapId, order, tryIntervalSecs)
return pTimeout(
this.acceptSwap(swapId, order, tryIntervalSecs),
maxTimeoutSecs * 1000
);
}

Expand Down
17 changes: 13 additions & 4 deletions src/swap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { AxiosResponse } from "axios";
import { BigNumber } from "bignumber.js";
import pTimeout from "p-timeout";
import { Cnd, LedgerAction, ledgerIsEthereum, SwapDetails } from "./cnd/cnd";
import { Field } from "./cnd/siren";
import { Transaction } from "./transaction";
import { sleep, timeoutPromise, TryParams } from "./util/timeout_promise";
import { sleep } from "./util/sleep";
import { AllWallets, Wallets } from "./wallet";

export class WalletError extends Error {
Expand Down Expand Up @@ -148,9 +149,9 @@ export class Swap {
actionName: string,
{ maxTimeoutSecs, tryIntervalSecs }: TryParams
): Promise<AxiosResponse<R>> {
return timeoutPromise(
maxTimeoutSecs * 1000,
this.executeSirenAction(actionName, tryIntervalSecs)
return pTimeout(
this.executeSirenAction(actionName, tryIntervalSecs),
maxTimeoutSecs * 1000
);
}

Expand Down Expand Up @@ -503,3 +504,11 @@ export class Swap {
}
}
}

/**
* Defines the parameters (for how long and how often) to try executing an action of a {@link Swap}.
*/
export interface TryParams {
maxTimeoutSecs: number;
tryIntervalSecs: number;
}
3 changes: 3 additions & 0 deletions src/util/sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
30 changes: 0 additions & 30 deletions src/util/timeout_promise.ts

This file was deleted.

0 comments on commit cef77e4

Please sign in to comment.