diff --git a/src/clients/ipfs.ts b/src/clients/ipfs.ts index 7b491f6..e57cdad 100644 --- a/src/clients/ipfs.ts +++ b/src/clients/ipfs.ts @@ -34,19 +34,15 @@ export class IpfsClient { filePath: string, data?: Iterable ): Promise { - try { - if (isBrowserEnvironment()) { - if (!data) throw new Error("please provide data to be uploaded."); - return await this.storage.upload(data); - } - - // we dynamically import "fs" node lib if we are not in a browser environment, so implementation doesn't conflict with - // a client calling this from a web browser. - const { readFileSync } = await import("fs"); - const fileBuffer = readFileSync(filePath); - return await this.storage.upload(fileBuffer); - } catch (e) { - throw e as Error; + if (isBrowserEnvironment()) { + if (!data) throw new Error("please provide data to be uploaded."); + return await this.storage.upload(data); } + + // we dynamically import "fs" node lib if we are not in a browser environment, so implementation doesn't conflict with + // a client calling this from a web browser. + const { readFileSync } = await import("fs"); + const fileBuffer = readFileSync(filePath); + return await this.storage.upload(fileBuffer); } } diff --git a/src/clients/web3.ts b/src/clients/web3.ts index d412ea2..34d80f6 100644 --- a/src/clients/web3.ts +++ b/src/clients/web3.ts @@ -42,17 +42,13 @@ export class Web3Client { * @returns {TransactionReceipt} transaction receipt of the transaction that stored CID on Ethereum. If something goes wrong, an error is thrown. */ public async storeCID(cid: string): Promise { - try { - const from = this.providerAddress; - const contractCall = this.registryContract.methods.store(cid); + const from = this.providerAddress; + const contractCall = this.registryContract.methods.store(cid); - const gas = String(await contractCall.estimateGas({ from })); - const transactionReceipt = await contractCall.send({ gas, from }); + const gas = String(await contractCall.estimateGas({ from })); + const transactionReceipt = await contractCall.send({ gas, from }); - return transactionReceipt; - } catch (e) { - throw e as Error; - } + return transactionReceipt; } /** @@ -63,30 +59,26 @@ export class Web3Client { public async listCIDsForAddress( callerAddress: string ): Promise<(string | EventLog)[]> { - try { - const result: (string | EventLog)[] = []; - const latestBlock = await this.web3.eth.getBlockNumber(); - let fromBlock = this.CONTRACT_INCEPTION_BLOCK; - - while (fromBlock <= latestBlock) { - const upToBlock = fromBlock + this.BLOCK_NUMBER_THRESHOLD; - const toBlock = upToBlock > latestBlock ? latestBlock : upToBlock; + const result: (string | EventLog)[] = []; + const latestBlock = await this.web3.eth.getBlockNumber(); + let fromBlock = this.CONTRACT_INCEPTION_BLOCK; - const pastEvents = await this.getPastEvents( - callerAddress, - fromBlock, - toBlock - ); + while (fromBlock <= latestBlock) { + const upToBlock = fromBlock + this.BLOCK_NUMBER_THRESHOLD; + const toBlock = upToBlock > latestBlock ? latestBlock : upToBlock; - fromBlock += this.BLOCK_NUMBER_THRESHOLD; - result.push(...pastEvents); - } + const pastEvents = await this.getPastEvents( + callerAddress, + fromBlock, + toBlock + ); - console.log(result); // we print all CIDStored events from contract to the console as per the requirements - return result; - } catch (e) { - throw e as Error; + fromBlock += this.BLOCK_NUMBER_THRESHOLD; + result.push(...pastEvents); } + + console.log(result); // we print all CIDStored events from contract to the console as per the requirements + return result; } /** @@ -101,16 +93,12 @@ export class Web3Client { fromBlock: bigint, toBlock: bigint ): Promise<(string | EventLog)[]> { - try { - const result = await this.registryContract.getPastEvents("CIDStored", { - fromBlock, - toBlock, - filter: { owner }, - }); + const result = await this.registryContract.getPastEvents("CIDStored", { + fromBlock, + toBlock, + filter: { owner }, + }); - return result; - } catch (e) { - throw e as Error; - } + return result; } } diff --git a/src/index.ts b/src/index.ts index 1fd3f15..bb70ace 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,12 +28,8 @@ export class IpfsPlugin extends Web3PluginBase { filePath: string, data?: Iterable ): Promise { - try { - const cid = await this.ipfsClient.uploadFile(filePath, data); - return await this.web3Client.storeCID(cid); - } catch (e) { - throw new Error(`something went wrong: ${(e as Error).message}`); - } + const cid = await this.ipfsClient.uploadFile(filePath, data); + return await this.web3Client.storeCID(cid); } /** @@ -44,13 +40,7 @@ export class IpfsPlugin extends Web3PluginBase { public async listAllByAddress( address: string ): Promise<(string | EventLog)[]> { - try { - return await this.web3Client.listCIDsForAddress(address); - } catch (e) { - throw new Error( - `failed to fetch CIDs for address ${address}: ${(e as Error).message}` - ); - } + return await this.web3Client.listCIDsForAddress(address); } }