Skip to content

Commit

Permalink
Removal of callbacks from getJsonResponse function
Browse files Browse the repository at this point in the history
  • Loading branch information
szynwelski committed Nov 8, 2023
1 parent 889a3f5 commit c28ab68
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 76 deletions.
40 changes: 19 additions & 21 deletions src/contract/sequencer/CentralizedSequencerClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BundlrResponse } from 'contract/Contract';
import { WarpFetchWrapper } from 'core/WarpFetchWrapper';
import { NetworkCommunicationError, getJsonResponse } from '../../utils/utils';
import { NetworkCommunicationError } from '../../utils/utils';
import { DataItem } from 'warp-arbundles';
import { SendDataItemResponse, SequencerClient } from './SequencerClient';

Expand Down Expand Up @@ -28,7 +28,7 @@ export class CentralizedSequencerClient implements SequencerClient {
* It sends an interaction to the sequencer and checks if the response has a status of 301 (Moved Permanently).
*/
async sendDataItem(dataItem: DataItem): Promise<SendDataItemResponse> {
const result = this.warpFetchWrapper.fetch(this.registerUrl, {
const response = await this.warpFetchWrapper.fetch(this.registerUrl, {
redirect: 'manual',
method: 'POST',
headers: {
Expand All @@ -37,25 +37,23 @@ export class CentralizedSequencerClient implements SequencerClient {
},
body: dataItem.getRaw()
});
return getJsonResponse<SendDataItemResponse>(
result,
(result) => {
return {
bundlrResponse: result as BundlrResponse,
sequencerMoved: false
};
},
async (response) => {
if (response.status == 301) {
return {
bundlrResponse: undefined,
sequencerMoved: true
};
}

const text = await response.text();
throw new NetworkCommunicationError(`Wrong response code: ${response.status}. ${text}`);
}
);
if (response.ok) {
const bundlrResponse = (await response.json()) as BundlrResponse;
return {
bundlrResponse,
sequencerMoved: false
};
}

if (response.status == 301) {
return {
bundlrResponse: undefined,
sequencerMoved: true
};
}

const text = await response.text();
throw new NetworkCommunicationError(`Wrong response code: ${response.status}. ${text}`);
}
}
91 changes: 43 additions & 48 deletions src/contract/sequencer/DecentralizedSequencerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,44 +122,42 @@ export class DecentralizedSequencerClient implements SequencerClient {
}

private async tryToSendDataItem(dataItem: DataItem): Promise<boolean> {
const response = this.warpFetchWrapper.fetch(this.sendDataItemUrl, {
const response = await this.warpFetchWrapper.fetch(this.sendDataItemUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream'
},
body: dataItem.getRaw()
});

return getJsonResponse<boolean>(
response,
() => true,
async (response) => {
if (response.status == 503) {
return false;
}

if (response.status == 409) {
const error = await response.json();
throw new Error(
`Interaction (id = ${await dataItem.id}) rejected by the sequencer due to an invalid nonce, error message: ${
error.message.RawLog
}}`
);
}

if (response.status == 400) {
const error = await response.json();
throw new Error(
`Interaction (id = ${await dataItem.id}) rejected by the sequencer: error type: ${
error.type
}, error message: ${JSON.stringify(error.message)}`
);
}

const text = await response.text();
throw new NetworkCommunicationError(`Wrong response code: ${response.status}. ${text}`);
}
);
if (response.ok) {
return true;
}

if (response.status == 503) {
return false;
}

if (response.status == 409) {
const error = await response.json();
throw new Error(
`Interaction (id = ${await dataItem.id}) rejected by the sequencer due to an invalid nonce, error message: ${
error.message.RawLog
}}`
);
}

if (response.status == 400) {
const error = await response.json();
throw new Error(
`Interaction (id = ${await dataItem.id}) rejected by the sequencer: error type: ${
error.type
}, error message: ${JSON.stringify(error.message)}`
);
}

const text = await response.text();
throw new NetworkCommunicationError(`Wrong response code: ${response.status}. ${text}`);
}

/**
Expand All @@ -183,29 +181,26 @@ export class DecentralizedSequencerClient implements SequencerClient {
}

private async checkTx(dataItemId: string): Promise<CheckTxResponse> {
const response = this.warpFetchWrapper.fetch(this.getTxUrl, {
const response = await this.warpFetchWrapper.fetch(this.getTxUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ data_item_id: dataItemId })
});

return getJsonResponse<CheckTxResponse>(
response,
(result) => {
this.logger.info(`The transaction with hash ${result.tx_hash} confirmed.`);
return { confirmed: true, txHash: result.tx_hash };
},
async (response) => {
if (response.status == 404) {
this.logger.debug(`The transaction with data item id (${dataItemId}) not confirmed yet.`);
return { confirmed: false };
}

const text = await response.text();
throw new NetworkCommunicationError(`${response.status}: ${text}`);
}
);
if (response.ok) {
const result = await response.json();
this.logger.info(`The transaction with hash ${result.tx_hash} confirmed.`);
return { confirmed: true, txHash: result.tx_hash };
}

if (response.status == 404) {
this.logger.debug(`The transaction with data item id (${dataItemId}) not confirmed yet.`);
return { confirmed: false };
}

const text = await response.text();
throw new NetworkCommunicationError(`${response.status}: ${text}`);
}
}
8 changes: 1 addition & 7 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class NetworkCommunicationError<T> extends Error {
}
}

export async function getJsonResponse<T>(response: Promise<Response>, successCallback?: (result: any) => T, errorCallback?: (response: Response) => Promise<T>): Promise<T> {
export async function getJsonResponse<T>(response: Promise<Response>): Promise<T> {
let r: Response;
try {
r = await response;
Expand All @@ -103,18 +103,12 @@ export async function getJsonResponse<T>(response: Promise<Response>, successCal
}

if (!r?.ok) {
if (errorCallback) {
return errorCallback(r)
}
const text = await r.text();
throw new NetworkCommunicationError(`Wrong response code: ${r.status}. ${text}`);
}

try {
const result = await r.json();
if (successCallback) {
return successCallback(result);
}
return result as T;
} catch (e) {
throw new NetworkCommunicationError(`Error while parsing json response: ${JSON.stringify(e)}`);
Expand Down

0 comments on commit c28ab68

Please sign in to comment.