Skip to content

Commit

Permalink
fix: Take only https: nodes into account for context
Browse files Browse the repository at this point in the history
  • Loading branch information
sbp-rib committed May 5, 2021
1 parent d730655 commit b3e737e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
26 changes: 20 additions & 6 deletions src/modules/context/buildContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ jest.mock('./getNetworkDetails', () => ({
getNetworkDetails: ({ mode, bridge }: { mode: SkybridgeMode; bridge: SkybridgeBridge }) => {
const prefix = `${mode}__${bridge}__`;
return {
swapNodes: [{ restUri: `${prefix}swap-node`, lastSeenAt: '2021-05-05T00:00:00.000Z' }],
swapNodes: [
{ restUri: `https://${prefix}swap-node`, lastSeenAt: '2021-05-05T00:00:00.000Z' },
],
indexerNodes: [`${prefix}indexer-node`],
};
},
Expand All @@ -24,7 +26,10 @@ it('returns default according to result of getNetworkDetails()', () => {
btc_bep20: 'test__btc_bep20__indexer-node',
btc_erc: 'test__btc_erc__indexer-node',
},
swapNode: { btc_bep20: 'test__btc_bep20__swap-node', btc_erc: 'test__btc_erc__swap-node' },
swapNode: {
btc_bep20: 'https://test__btc_bep20__swap-node',
btc_erc: 'https://test__btc_erc__swap-node',
},
},
});
});
Expand All @@ -38,7 +43,10 @@ it('allows overwriting affiliateApi', () => {
btc_bep20: 'test__btc_bep20__indexer-node',
btc_erc: 'test__btc_erc__indexer-node',
},
swapNode: { btc_bep20: 'test__btc_bep20__swap-node', btc_erc: 'test__btc_erc__swap-node' },
swapNode: {
btc_bep20: 'https://test__btc_bep20__swap-node',
btc_erc: 'https://test__btc_erc__swap-node',
},
},
});
});
Expand All @@ -52,7 +60,10 @@ it('allows overwriting affiliateApi with empty string', () => {
btc_bep20: 'test__btc_bep20__indexer-node',
btc_erc: 'test__btc_erc__indexer-node',
},
swapNode: { btc_bep20: 'test__btc_bep20__swap-node', btc_erc: 'test__btc_erc__swap-node' },
swapNode: {
btc_bep20: 'https://test__btc_bep20__swap-node',
btc_erc: 'https://test__btc_erc__swap-node',
},
},
});
});
Expand All @@ -65,7 +76,10 @@ it('allows overwriting one server', () => {
mode,
servers: {
indexer: { btc_bep20: 'my-bep-a', btc_erc: 'test__btc_erc__indexer-node' },
swapNode: { btc_bep20: 'test__btc_bep20__swap-node', btc_erc: 'test__btc_erc__swap-node' },
swapNode: {
btc_bep20: 'https://test__btc_bep20__swap-node',
btc_erc: 'https://test__btc_erc__swap-node',
},
},
});
});
Expand All @@ -84,7 +98,7 @@ it('allows overwriting several servers', () => {
mode,
servers: {
indexer: { btc_bep20: 'my-i-bep-a', btc_erc: 'my-i-erc-a' },
swapNode: { btc_bep20: 'test__btc_bep20__swap-node', btc_erc: 'my-s-erc-a' },
swapNode: { btc_bep20: 'https://test__btc_bep20__swap-node', btc_erc: 'my-s-erc-a' },
},
});
});
18 changes: 13 additions & 5 deletions src/modules/context/buildContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ export const buildContext = async <M extends SkybridgeMode>({
const index = SKYBRIDGE_BRIDGES.findIndex((it) => it === bridge);
try {
const swapNodes = (() => {
const sorted = [...results[index].swapNodes].sort(
(a, b) =>
DateTime.fromJSDate(b.lastSeenAt).toMillis() -
DateTime.fromJSDate(a.lastSeenAt).toMillis(),
);
const sorted = [...results[index].swapNodes]
.filter((it) => {
try {
return new URL(it.restUri).protocol === 'https:';
} catch (e) {
return false;
}
})
.sort(
(a, b) =>
DateTime.fromJSDate(b.lastSeenAt).toMillis() -
DateTime.fromJSDate(a.lastSeenAt).toMillis(),
);

if (sorted.length <= 1) return sorted;
const latestDate = DateTime.fromJSDate(sorted[0].lastSeenAt);
Expand Down

0 comments on commit b3e737e

Please sign in to comment.