Skip to content

Commit

Permalink
refactor: remove redundant try catch blocks that throw errors (should…
Browse files Browse the repository at this point in the history
… be handled by client using library code)
  • Loading branch information
ffmcgee725 committed Dec 4, 2023
1 parent fb9b152 commit d2c979c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 65 deletions.
22 changes: 9 additions & 13 deletions src/clients/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,15 @@ export class IpfsClient {
filePath: string,
data?: Iterable<Uint8Array>
): Promise<string> {
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);
}
}
66 changes: 27 additions & 39 deletions src/clients/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TransactionReceipt> {
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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}
16 changes: 3 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ export class IpfsPlugin extends Web3PluginBase {
filePath: string,
data?: Iterable<Uint8Array>
): Promise<TransactionReceipt> {
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);
}

/**
Expand All @@ -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);
}
}

Expand Down

0 comments on commit d2c979c

Please sign in to comment.