forked from passportxyz/passport
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(platforms): added zksync model providers (passportxyz#2389)
* feat(platforms): added zksync model providers * feat(platforms): parameterized zk model tests * fix: small type change
- Loading branch information
1 parent
07ed83e
commit be2ed2c
Showing
6 changed files
with
264 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// ----- Types | ||
import { type Provider } from "../../types"; | ||
import type { RequestPayload, VerifiedPayload, ProviderContext, PROVIDER_ID } from "@gitcoin/passport-types"; | ||
import { fetchModelData } from "../../ETH/Providers/accountAnalysis"; | ||
|
||
export type ModelResponse = { | ||
data: { | ||
human_probability: number; | ||
}; | ||
}; | ||
|
||
type ZkSyncAnalysis = { | ||
humanProbability: number; | ||
}; | ||
|
||
export type ZkSyncAnalysisContext = ProviderContext & { | ||
zkSyncAnalysis?: ZkSyncAnalysis; | ||
}; | ||
|
||
export async function getZkSyncAnalysis(address: string, context: ZkSyncAnalysisContext): Promise<ZkSyncAnalysis> { | ||
if (!context?.zkSyncAnalysis) { | ||
const response = await fetchModelData<ModelResponse>(address, "zksync-model-predict"); | ||
|
||
context.zkSyncAnalysis = { | ||
humanProbability: response.data.human_probability, | ||
}; | ||
} | ||
return context.zkSyncAnalysis; | ||
} | ||
|
||
export type EthOptions = { | ||
type: PROVIDER_ID; | ||
minimum: number; | ||
}; | ||
|
||
export class ZkSyncAccountAnalysis implements Provider { | ||
type: PROVIDER_ID; | ||
minimum: number; | ||
|
||
// construct the provider instance with supplied options | ||
constructor(options: EthOptions) { | ||
this.type = options.type; | ||
this.minimum = options.minimum; | ||
} | ||
|
||
async verify(payload: RequestPayload, context: ZkSyncAnalysisContext): Promise<VerifiedPayload> { | ||
const { address } = payload; | ||
const zkSyncAnalysis = await getZkSyncAnalysis(address, context); | ||
|
||
if (zkSyncAnalysis.humanProbability < this.minimum) { | ||
return { | ||
valid: false, | ||
errors: [ | ||
`You received a score of ${zkSyncAnalysis.humanProbability} from our analysis. You must have a score of ${this.minimum} or higher to obtain this stamp.`, | ||
], | ||
}; | ||
} | ||
|
||
return { | ||
valid: true, | ||
record: { | ||
address, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
export class ZkSyncScore5Provider extends ZkSyncAccountAnalysis { | ||
constructor() { | ||
super({ | ||
type: "zkSyncScore#5", | ||
minimum: 5, | ||
}); | ||
} | ||
} | ||
|
||
export class ZkSyncScore20Provider extends ZkSyncAccountAnalysis { | ||
constructor() { | ||
super({ | ||
type: "zkSyncScore#20", | ||
minimum: 20, | ||
}); | ||
} | ||
} | ||
|
||
export class ZkSyncScore50Provider extends ZkSyncAccountAnalysis { | ||
constructor() { | ||
super({ | ||
type: "zkSyncScore#50", | ||
minimum: 50, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { RequestPayload } from "@gitcoin/passport-types"; | ||
import axios from "axios"; | ||
import { | ||
ZkSyncScore20Provider, | ||
ZkSyncScore50Provider, | ||
ModelResponse, | ||
getZkSyncAnalysis, | ||
ZkSyncScore5Provider, | ||
} from "../Providers/accountAnalysis"; | ||
|
||
const mockAddress = "0x0"; | ||
let mockContext = {}; | ||
const mockResponse = (score: number): { data: ModelResponse } => ({ | ||
data: { | ||
data: { | ||
human_probability: score, | ||
}, | ||
}, | ||
}); | ||
|
||
jest.mock("axios"); | ||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
|
||
const scoreTestCases = [ | ||
//[score, [result for ZkSyncScore5Provider, result for ZkSyncScore20Provider, result for ZkSyncScore50Provider]] | ||
[0, [false, false, false]], | ||
[1, [false, false, false]], | ||
[5, [true, false, false]], | ||
[20, [true, true, false]], | ||
[50, [true, true, true]], | ||
[100, [true, true, true]], | ||
] | ||
.map(([score, expected]: [number, boolean[]]) => { | ||
return [ | ||
[score, expected[0], ZkSyncScore5Provider], | ||
[score, expected[1], ZkSyncScore20Provider], | ||
[score, expected[2], ZkSyncScore50Provider], | ||
]; | ||
}) | ||
.flat() as [ | ||
number, | ||
boolean, | ||
typeof ZkSyncScore5Provider | typeof ZkSyncScore20Provider | typeof ZkSyncScore50Provider, | ||
][]; | ||
|
||
describe("AccountAnalysis Providers", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockContext = {}; | ||
}); | ||
|
||
describe("should return valid/invalid based on score", () => { | ||
it.each(scoreTestCases)("score %i should return %s for %p", async (score, expected, provider) => { | ||
const mockedResponse = mockResponse(score); | ||
mockedAxios.post.mockResolvedValueOnce(mockedResponse); | ||
const ethAdvocateProvider = new provider(); | ||
const payload = await ethAdvocateProvider.verify({ address: mockAddress } as RequestPayload, mockContext); | ||
|
||
expect(payload.valid).toBe(expected); | ||
if (expected) { | ||
// eslint-disable-next-line jest/no-conditional-expect | ||
expect(payload.record).toEqual({ address: mockAddress }); | ||
} | ||
}); | ||
}); | ||
|
||
it("should handle errors gracefully", async () => { | ||
jest.clearAllMocks(); | ||
mockedAxios.post.mockRejectedValueOnce(new Error("Test Error")); | ||
const ethAdvocateProvider = new ZkSyncScore50Provider(); | ||
await expect(ethAdvocateProvider.verify({ address: mockAddress } as RequestPayload, mockContext)).rejects.toThrow(); | ||
}); | ||
describe("getZkSyncAnalysis", () => { | ||
it("should use value from context if present", async () => { | ||
const mockedResponse = mockResponse(80); | ||
mockedAxios.post.mockResolvedValueOnce(mockedResponse); | ||
mockContext = {}; | ||
const response1 = await getZkSyncAnalysis(mockAddress, mockContext); | ||
const response2 = await getZkSyncAnalysis(mockAddress, mockContext); | ||
expect(response1.humanProbability).toEqual(80); | ||
expect(response2.humanProbability).toEqual(80); | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
expect(axios.post).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters