Skip to content

Commit

Permalink
Merge pull request #372 from privacy-scaling-explorations/feat/balanc…
Browse files Browse the repository at this point in the history
…e-credential

feat(credentials): add the blockchain balance validator
  • Loading branch information
vplasencia authored Feb 6, 2024
2 parents 9ab8978 + 737c8e5 commit 814c224
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 5 deletions.
4 changes: 2 additions & 2 deletions libs/credentials/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ describe("Credentials library", () => {
it("Should add a validator to the list of supported validators", () => {
addValidator({} as any)

expect(validators).toHaveLength(7)
expect(validators).toHaveLength(8)
})
})

describe("# addValidators", () => {
it("Should add 2 validators to the list of supported validators", () => {
addValidators([{} as any, {} as any])

expect(validators).toHaveLength(9)
expect(validators).toHaveLength(10)
})
})

Expand Down
6 changes: 4 additions & 2 deletions libs/credentials/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
githubRepositoryCommits,
twitterFollowers,
twitterFollowingUser,
blockchainTransactions
blockchainTransactions,
blockchainBalance
} from "./validators/index"

const validators: Validator[] = [
Expand All @@ -14,7 +15,8 @@ const validators: Validator[] = [
githubRepositoryCommits,
twitterFollowers,
twitterFollowingUser,
blockchainTransactions
blockchainTransactions,
blockchainBalance
]

export default validators
107 changes: 107 additions & 0 deletions libs/credentials/src/validators/blockchainBalance/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { BigNumber } from "ethers"
import { validateCredentials } from "../.."
import blockchainBalance from "./index"

describe("BlockchainBalance", () => {
const jsonRpcProviderMocked = {
getBalance: jest.fn()
}

it("Should return true if an account has a balance greater than or equal to 10", async () => {
jsonRpcProviderMocked.getBalance.mockReturnValue(BigNumber.from(12))

const result = await validateCredentials(
{
id: blockchainBalance.id,
criteria: {
minBalance: "10"
}
},
{
address: "0x",
jsonRpcProvider: jsonRpcProviderMocked
}
)

expect(result).toBeTruthy()
})

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))

const result = await validateCredentials(
{
id: blockchainBalance.id,
criteria: {
minBalance: "10"
}
},
{
address: "0x",
jsonRpcProvider: jsonRpcProviderMocked,
blockNumber: 4749638
}
)

expect(result).toBeTruthy()
})

it("Should throw an error if a criteria parameter is missing", async () => {
const fun = () =>
validateCredentials(
{
id: blockchainBalance.id,
criteria: {}
},
{
address: "0x",
jsonRpcProvider: undefined
}
)

await expect(fun).rejects.toThrow(
"Parameter 'minBalance' has not been defined"
)
})

it("Should throw an error if a criteria parameter should not exist", async () => {
const fun = () =>
validateCredentials(
{
id: blockchainBalance.id,
criteria: {
minBalance: "100",
minStars: 200
}
},
{
address: "0x",
jsonRpcProvider: undefined
}
)

await expect(fun).rejects.toThrow(
"Parameter 'minStars' should not be part of the criteria"
)
})

it("Should throw a type error if a criteria parameter has the wrong type", async () => {
const fun = () =>
validateCredentials(
{
id: blockchainBalance.id,
criteria: {
minBalance: 100
}
},
{
address: "0x",
jsonRpcProvider: undefined
}
)

await expect(fun).rejects.toThrow(
"Parameter 'minBalance' is not a string"
)
})
})
35 changes: 35 additions & 0 deletions libs/credentials/src/validators/blockchainBalance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { BigNumber } from "ethers"
import { BlockchainContext, Validator } from "../.."

export type Criteria = {
minBalance: string
}

const validator: Validator = {
id: "BLOCKCHAIN_BALANCE",

criteriaABI: {
minBalance: "string"
},

/**
* It checks if a user has a balance greater than or equal to 'minBalance'.
* @param criteria The criteria used to check user's credentials.
* @param context Context variables.
* @returns True if the user meets the criteria.
*/
async validate(criteria: Criteria, context) {
if ("address" in context) {
const balance = await (
context as BlockchainContext
).jsonRpcProvider.getBalance(
(context as BlockchainContext).address,
(context as BlockchainContext).blockNumber
)
return balance >= BigNumber.from(criteria.minBalance)
}
throw new Error("No address value found")
}
}

export default validator
4 changes: 3 additions & 1 deletion libs/credentials/src/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import githubRepositoryCommits from "./githubRepositoryCommits"
import twitterFollowers from "./twitterFollowers"
import twitterFollowingUser from "./twitterFollowingUser"
import blockchainTransactions from "./blockchainTransactions"
import blockchainBalance from "./blockchainBalance"

export {
githubFollowers,
githubRepositoryCommits,
twitterFollowers,
twitterFollowingUser,
githubPersonalStars,
blockchainTransactions
blockchainTransactions,
blockchainBalance
}

0 comments on commit 814c224

Please sign in to comment.