Skip to content

Commit 591d46b

Browse files
authored
Add method for fetching smart transactions API liveness (#9)
* feat: add stx liveness request * feat: add smart transactions liveness fetch call * chore: clean up prettier issue * chore: move liveness definition to controller * chore: add unit tests for fetchLiveness * chore: rename successLivenessApiResponse variable in tests
1 parent 1a6c749 commit 591d46b

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

src/SmartTransactionsController.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ const createSuccessBatchStatusApiResponse = () => {
132132
];
133133
};
134134

135+
const createSuccessLivenessApiResponse = () => ({
136+
lastBlock: 123456,
137+
});
138+
135139
const ethereumChainIdDec = CHAIN_IDS_HEX_TO_DEC[CHAIN_IDS.ETHEREUM];
136140

137141
describe('SmartTransactionsController', () => {
@@ -320,4 +324,15 @@ describe('SmartTransactionsController', () => {
320324
});
321325
});
322326
});
327+
328+
describe('fetchLiveness', () => {
329+
it('fetches a liveness for Smart Transactions API', async () => {
330+
const successLivenessApiResponse = createSuccessLivenessApiResponse();
331+
nock(API_BASE_URL)
332+
.get(`/networks/${ethereumChainIdDec}/health`)
333+
.reply(200, successLivenessApiResponse);
334+
const liveness = await smartTransactionsController.fetchLiveness();
335+
expect(liveness).toBe(true);
336+
});
337+
});
323338
});

src/SmartTransactionsController.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,12 @@ export default class SmartTransactionsController extends BaseController<
275275
body: JSON.stringify({ uuid }),
276276
});
277277
}
278+
279+
async fetchLiveness(): Promise<boolean> {
280+
const { chainId } = this.config;
281+
const response = await this.fetch(
282+
getAPIRequestURL(APIType.LIVENESS, chainId),
283+
);
284+
return Boolean(response.lastBlock);
285+
}
278286
}

src/utils.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ describe('src/utils.js', () => {
6666

6767
it('returns a URL for smart transactions API liveness', () => {
6868
expect(utils.getAPIRequestURL(APIType.LIVENESS, CHAIN_IDS.ETHEREUM)).toBe(
69-
`${API_BASE_URL}/networks/${ethereumChainIdDec}/liveness`,
69+
`${API_BASE_URL}/networks/${ethereumChainIdDec}/health`,
7070
);
7171
});
7272

7373
it('returns a URL for smart transactions API liveness for the BSC chainId', () => {
7474
const bscChainIdDec = CHAIN_IDS_HEX_TO_DEC[CHAIN_IDS.BSC];
7575
expect(utils.getAPIRequestURL(APIType.LIVENESS, CHAIN_IDS.BSC)).toBe(
76-
`${API_BASE_URL}/networks/${bscChainIdDec}/liveness`,
76+
`${API_BASE_URL}/networks/${bscChainIdDec}/health`,
7777
);
7878
});
7979
});

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function getAPIRequestURL(apiType: APIType, chainId: string): string {
3030
}
3131

3232
case APIType.LIVENESS: {
33-
return `${API_BASE_URL}/networks/${chainIdDec}/liveness`;
33+
return `${API_BASE_URL}/networks/${chainIdDec}/health`;
3434
}
3535

3636
default: {

0 commit comments

Comments
 (0)