Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #957 from gre/satstack-notready-error-in-prepareTr…
Browse files Browse the repository at this point in the history
…ansaction

Add SatStackNotReady error to also throw in send flow
  • Loading branch information
gre authored Nov 20, 2020
2 parents a02222b + 90b66f3 commit d319375
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
21 changes: 12 additions & 9 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,20 @@ export const CosmosBroadcastError = {
"7": createCustomErrorClass("CosmosBroadcastCodeInvalidAddress"),
"8": createCustomErrorClass("CosmosBroadcastCodeInvalidPubKey"),
"9": createCustomErrorClass("CosmosBroadcastCodeUnknownAddress"),
"10": createCustomErrorClass(" CosmosBroadcastCodeInsufficientCoins"),
"11": createCustomErrorClass(" CosmosBroadcastCodeInvalidCoins"),
"12": createCustomErrorClass(" CosmosBroadcastCodeOutOfGas"),
"13": createCustomErrorClass(" CosmosBroadcastCodeMemoTooLarge"),
"14": createCustomErrorClass(" CosmosBroadcastCodeInsufficientFee"),
"15": createCustomErrorClass(" CosmosBroadcastCodeTooManySignatures"),
"16": createCustomErrorClass(" CosmosBroadcastCodeGasOverflow"),
"17": createCustomErrorClass(" CosmosBroadcastCodeNoSignatures"),
"10": createCustomErrorClass("CosmosBroadcastCodeInsufficientCoins"),
"11": createCustomErrorClass("CosmosBroadcastCodeInvalidCoins"),
"12": createCustomErrorClass("CosmosBroadcastCodeOutOfGas"),
"13": createCustomErrorClass("CosmosBroadcastCodeMemoTooLarge"),
"14": createCustomErrorClass("CosmosBroadcastCodeInsufficientFee"),
"15": createCustomErrorClass("CosmosBroadcastCodeTooManySignatures"),
"16": createCustomErrorClass("CosmosBroadcastCodeGasOverflow"),
"17": createCustomErrorClass("CosmosBroadcastCodeNoSignatures"),
};

export const SatStackNotReady = createCustomErrorClass("SatStackNotReady");
export const SatStackAccessDown = createCustomErrorClass("SatStackAccessDown");
export const SatStackStillSyncing = createCustomErrorClass(
"SatStackStillSyncing"
);

export const SwapNoAvailableProviders = createCustomErrorClass(
"SwapNoAvailableProviders"
Expand Down
4 changes: 4 additions & 0 deletions src/families/bitcoin/bridge/libcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { getMainAccount } from "../../../account";
import { getMinRelayFee } from "../fees";
import { isChangeOutput, perCoinLogic } from "../transaction";
import { makeAccountBridgeReceive } from "../../../bridge/jsHelpers";
import { requiresSatStackReady } from "../satstack";

const receive = makeAccountBridgeReceive({
injectGetAddressParams: (account) => {
Expand Down Expand Up @@ -190,6 +191,9 @@ const prepareTransaction = async (
a: Account,
t: Transaction
): Promise<Transaction> => {
if (a.currency.id === "bitcoin") {
await requiresSatStackReady();
}
let networkInfo = t.networkInfo;
if (!networkInfo) {
networkInfo = await getAccountNetworkInfo(a);
Expand Down
10 changes: 3 additions & 7 deletions src/families/bitcoin/presync.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// @flow
import { SatStackNotReady } from "../../errors";
import type { CryptoCurrency } from "../../types";
import { isSatStackEnabled, fetchSatStackStatus } from "./satstack";
import { requiresSatStackReady } from "./satstack";

export default async function presync(currency: CryptoCurrency) {
if (isSatStackEnabled() && currency.id === "bitcoin") {
const status = await fetchSatStackStatus();
if (status.type !== "ready") {
throw new SatStackNotReady();
}
if (currency.id === "bitcoin") {
await requiresSatStackReady();
}
}
16 changes: 16 additions & 0 deletions src/families/bitcoin/satstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Observable, interval, from } from "rxjs";
import url from "url";
import { share, switchMap } from "rxjs/operators";
import { SatStackAccessDown, SatStackStillSyncing } from "../../errors";
import { getCryptoCurrencyById } from "../../currencies";
import network from "../../network";
import { RPCFieldRequired } from "../../errors";
Expand Down Expand Up @@ -232,6 +233,21 @@ export async function fetchSatStackStatus(): Promise<SatStackStatus> {
return { type: "ready" };
}

export async function requiresSatStackReady() {
if (isSatStackEnabled()) {
const status = await fetchSatStackStatus();
switch (status.type) {
case "ready":
return;
case "syncing":
case "scanning":
throw new SatStackStillSyncing();
default:
throw new SatStackAccessDown();
}
}
}

export const statusObservable: Observable<SatStackStatus> = interval(1000).pipe(
switchMap(() => from(fetchSatStackStatus())),
share()
Expand Down
24 changes: 24 additions & 0 deletions src/families/bitcoin/satstack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
setMockStatus,
fetchSatStackStatus,
statusObservable,
requiresSatStackReady,
} from "./satstack";
import dataset from "./datasets/bitcoin";
import { inferDescriptorFromAccount } from "./descriptor";
Expand Down Expand Up @@ -349,3 +350,26 @@ describe("statusObservable", () => {
await p;
});
});

describe("requiresSatStackReady", () => {
beforeEach(() => {
setEnv("MOCK", "1");
});
afterEach(() => {
setMockStatus({ type: "ready" });
setEnv("SATSTACK", false);
setEnv("MOCK", "");
});
test("without satstack", async () => {
await expect(requiresSatStackReady()).resolves.toBe();
});
test("with satstack", async () => {
setEnv("SATSTACK", true);
await expect(requiresSatStackReady()).resolves.toBe();
});
test("with satstack not ready", async () => {
setEnv("SATSTACK", true);
setMockStatus({ type: "node-disconnected" });
await expect(requiresSatStackReady()).rejects.toThrow();
});
});

1 comment on commit d319375

@vercel
Copy link

@vercel vercel bot commented on d319375 Nov 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.