From fe29b26395b286aee1d4975735b09824685c77f6 Mon Sep 17 00:00:00 2001 From: Vivian Plasencia Date: Thu, 27 Jun 2024 14:05:17 +0200 Subject: [PATCH 1/2] fix(credentials): fix validate function in blockchain balance validator The balance returned by the jsonRpcProvider is Wei and it should be converted to Ethers so that people can work with Ethers instead of Wei. fix #532 --- .../src/validators/blockchainBalance/index.test.ts | 4 ++-- .../src/validators/blockchainBalance/index.ts | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/libs/credentials/src/validators/blockchainBalance/index.test.ts b/libs/credentials/src/validators/blockchainBalance/index.test.ts index 685e072d..10fa4001 100644 --- a/libs/credentials/src/validators/blockchainBalance/index.test.ts +++ b/libs/credentials/src/validators/blockchainBalance/index.test.ts @@ -8,7 +8,7 @@ describe("BlockchainBalance", () => { } it("Should return true if an account has a balance greater than or equal to 10", async () => { - jsonRpcProviderMocked.getBalance.mockReturnValue(BigNumber.from(12)) + jsonRpcProviderMocked.getBalance.mockReturnValue(BigNumber.from("12000000000000000000")) const result = await validateCredentials( { @@ -28,7 +28,7 @@ describe("BlockchainBalance", () => { }) it("Should return true if an account has a balance greater than or equal to 10 using the block number", async () => { - jsonRpcProviderMocked.getBalance.mockReturnValue(BigNumber.from(12)) + jsonRpcProviderMocked.getBalance.mockReturnValue(BigNumber.from("12000000000000000000")) const result = await validateCredentials( { diff --git a/libs/credentials/src/validators/blockchainBalance/index.ts b/libs/credentials/src/validators/blockchainBalance/index.ts index abff59eb..2047be26 100644 --- a/libs/credentials/src/validators/blockchainBalance/index.ts +++ b/libs/credentials/src/validators/blockchainBalance/index.ts @@ -1,4 +1,4 @@ -import { BigNumber } from "ethers" +import { utils } from "ethers" import { BlockchainContext, Validator } from "../.." export type Criteria = { @@ -38,13 +38,16 @@ const validator: Validator = { ? criteria.blockNumber : undefined - const balance = await ( + const balanceWei = await ( context as BlockchainContext ).jsonRpcProvider.getBalance( (context as BlockchainContext).address, blockNumber ) - return balance >= BigNumber.from(criteria.minBalance) + + const balanceETH = utils.formatEther(balanceWei) + + return parseFloat(balanceETH) >= parseFloat(criteria.minBalance) } throw new Error("No address value found") } From 2a38424a14c65c351372f8113ac9189b6e874dd9 Mon Sep 17 00:00:00 2001 From: Vivian Plasencia Date: Thu, 27 Jun 2024 14:11:31 +0200 Subject: [PATCH 2/2] style(credentials): format code with prettier --- .../src/validators/blockchainBalance/index.test.ts | 8 ++++++-- .../credentials/src/validators/blockchainBalance/index.ts | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/libs/credentials/src/validators/blockchainBalance/index.test.ts b/libs/credentials/src/validators/blockchainBalance/index.test.ts index 10fa4001..b79ddd07 100644 --- a/libs/credentials/src/validators/blockchainBalance/index.test.ts +++ b/libs/credentials/src/validators/blockchainBalance/index.test.ts @@ -8,7 +8,9 @@ describe("BlockchainBalance", () => { } it("Should return true if an account has a balance greater than or equal to 10", async () => { - jsonRpcProviderMocked.getBalance.mockReturnValue(BigNumber.from("12000000000000000000")) + jsonRpcProviderMocked.getBalance.mockReturnValue( + BigNumber.from("12000000000000000000") + ) const result = await validateCredentials( { @@ -28,7 +30,9 @@ describe("BlockchainBalance", () => { }) it("Should return true if an account has a balance greater than or equal to 10 using the block number", async () => { - jsonRpcProviderMocked.getBalance.mockReturnValue(BigNumber.from("12000000000000000000")) + jsonRpcProviderMocked.getBalance.mockReturnValue( + BigNumber.from("12000000000000000000") + ) const result = await validateCredentials( { diff --git a/libs/credentials/src/validators/blockchainBalance/index.ts b/libs/credentials/src/validators/blockchainBalance/index.ts index 2047be26..e683ea1d 100644 --- a/libs/credentials/src/validators/blockchainBalance/index.ts +++ b/libs/credentials/src/validators/blockchainBalance/index.ts @@ -46,7 +46,7 @@ const validator: Validator = { ) const balanceETH = utils.formatEther(balanceWei) - + return parseFloat(balanceETH) >= parseFloat(criteria.minBalance) } throw new Error("No address value found")