Skip to content

Commit

Permalink
fix(credentials): fix validate function in blockchain balance validator
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vplasencia committed Jun 27, 2024
1 parent 3504e95 commit fe29b26
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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(
{
Expand Down
9 changes: 6 additions & 3 deletions libs/credentials/src/validators/blockchainBalance/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber } from "ethers"
import { utils } from "ethers"
import { BlockchainContext, Validator } from "../.."

export type Criteria = {
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit fe29b26

Please sign in to comment.