From ee8445b1b42236128b0689824c49e49f45a0c996 Mon Sep 17 00:00:00 2001 From: YamenMerhi Date: Fri, 14 Jul 2023 11:20:00 +0300 Subject: [PATCH] refactor: refactor getAllData into generic function --- src/provider/providerWrapper.ts | 60 +++++++-------------------------- 1 file changed, 12 insertions(+), 48 deletions(-) diff --git a/src/provider/providerWrapper.ts b/src/provider/providerWrapper.ts index d097fae5..a3ae58eb 100644 --- a/src/provider/providerWrapper.ts +++ b/src/provider/providerWrapper.ts @@ -208,9 +208,13 @@ export class ProviderWrapper { switch (erc725Version) { case ERC725_VERSION.ERC725_v5: - return this._getAllDataBatch(address, keyHashes); + return this._getAllDataGeneric( + address, + keyHashes, + Method.GET_DATA_BATCH, + ); case ERC725_VERSION.ERC725_v2: - return this._getAllData(address, keyHashes); + return this._getAllDataGeneric(address, keyHashes, Method.GET_DATA); case ERC725_VERSION.ERC725_LEGACY: return this._getAllDataLegacy(address, keyHashes); default: @@ -218,61 +222,21 @@ export class ProviderWrapper { } } - private async _getAllDataBatch( - address: string, - keyHashes: string[], - ): Promise { - if (this.type === ProviderTypes.ETHEREUM) { - const encodedResults = await this.callContract( - constructJSONRPC( - address, - Method.GET_DATA_BATCH, - abiCoder.encodeParameter('bytes32[]', keyHashes), - ), - ); - - const decodedValues = decodeResult(Method.GET_DATA_BATCH, encodedResults); - - return keyHashes.map((keyHash, index) => ({ - key: keyHash, - value: decodedValues ? decodedValues[index] : decodedValues, - })); - } - - const payload: JsonRpc[] = [ - constructJSONRPC( - address, - Method.GET_DATA_BATCH, - abiCoder.encodeParameter('bytes32[]', keyHashes), - ), - ]; - - const results: any = await this.callContract(payload); - const decodedValues = decodeResult( - Method.GET_DATA_BATCH, - results[0].result, - ); - - return keyHashes.map((key, index) => ({ - key, - value: decodedValues ? decodedValues[index] : decodedValues, - })); - } - - private async _getAllData( + private async _getAllDataGeneric( address: string, keyHashes: string[], + method: Method, ): Promise { if (this.type === ProviderTypes.ETHEREUM) { const encodedResults = await this.callContract( constructJSONRPC( address, - Method.GET_DATA, + method, abiCoder.encodeParameter('bytes32[]', keyHashes), ), ); - const decodedValues = decodeResult(Method.GET_DATA, encodedResults); + const decodedValues = decodeResult(method, encodedResults); return keyHashes.map((keyHash, index) => ({ key: keyHash, @@ -283,13 +247,13 @@ export class ProviderWrapper { const payload: JsonRpc[] = [ constructJSONRPC( address, - Method.GET_DATA, + method, abiCoder.encodeParameter('bytes32[]', keyHashes), ), ]; const results: any = await this.callContract(payload); - const decodedValues = decodeResult(Method.GET_DATA, results[0].result); + const decodedValues = decodeResult(method, results[0].result); return keyHashes.map((key, index) => ({ key,