Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(platforms): updating eth stamp to use aggregate model #3149

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions platforms/src/ETH/Providers/accountAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { handleProviderAxiosError } from "../../utils/handleProviderAxiosError";
export type ModelResponse = {
data: {
human_probability: number;
gas_spent: number;
n_days_active: number;
n_transactions: number;
gas_spent?: number;
n_days_active?: number;
};
};

Expand Down Expand Up @@ -45,9 +45,37 @@ export async function getETHAnalysis(address: string, context: ETHAnalysisContex
return context.ethAnalysis;
}

const MODEL_SUBPATHS = {
eth: "eth-stamp-v2-predict",
zk: "zksync-model-v2-predict",
polygon: "polygon-model-predict",
arb: "arbitrum-model-predict",
op: "optimism-model-predict",
} as const;

type ModelKeys = keyof typeof MODEL_SUBPATHS;

type AggregateData = {
[K in ModelKeys as `score_${K}`]: number;
} & {
[K in ModelKeys as `txs_${K}`]: number;
};

export async function getAggregateAnalysis(address: string, context: ETHAnalysisContext): Promise<AggregateAnalysis> {
if (!context?.aggregateAnalysis) {
const { data } = await fetchModelData<ModelResponse>(address, "aggregate-model-predict");
const results = await Promise.all(
Object.entries(MODEL_SUBPATHS).map(async ([modelAbbreviation, subpath]) => {
const { data } = await fetchModelData<ModelResponse>(address, subpath);
return {
[`score_${modelAbbreviation}`]: data.human_probability,
[`txs_${modelAbbreviation}`]: data.n_transactions,
};
})
);

const aggregateData: AggregateData = Object.assign({}, ...results);

const { data } = await fetchModelData<ModelResponse>(address, "aggregate-model-predict", aggregateData);

context.aggregateAnalysis = {
humanProbability: data.human_probability,
Expand All @@ -56,12 +84,19 @@ export async function getAggregateAnalysis(address: string, context: ETHAnalysis
return context.aggregateAnalysis;
}

export async function fetchModelData<T>(address: string, url_subpath: string): Promise<T> {
export async function fetchModelData<T>(address: string, url_subpath: string, data?: AggregateData): Promise<T> {
try {
const response = await axios.post(`http://${dataScienceEndpoint}/${url_subpath}`, {
address,
});
return response.data as T;
const payload: { address: string; data?: AggregateData } = { address };
if (data) {
payload["data"] = data;
}
const url = `http://${dataScienceEndpoint}/${url_subpath}`;
const response = await axios.post<T>(url, payload);

if (response.status !== 200) {
throw new Error(`Failed to fetch data for model ${url}`);
}
return response.data;
} catch (e) {
handleProviderAxiosError(e, "model data (" + url_subpath + ")", [dataScienceEndpoint]);
}
Expand Down
7 changes: 4 additions & 3 deletions platforms/src/ETH/__tests__/accountAnalysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const mockResponse = ({
gasSpent?: number;
numberDaysActive?: number;
numberTransactions?: number;
}): { data: ModelResponse } => ({
}): { status:number, data: ModelResponse } => ({
status: 200,
data: {
data: {
human_probability: score || 0,
Expand Down Expand Up @@ -65,7 +66,7 @@ describe("AccountAnalysis Providers", () => {
describe("should check human_probability", () => {
it.each(scoreTestCases)("for score %i should return %s for %p", async (score, expected, provider) => {
const mockedResponse = mockResponse({ score });
mockedAxios.post.mockResolvedValueOnce(mockedResponse);
mockedAxios.post.mockResolvedValue(mockedResponse);
const ethAdvocateProvider = new provider();
const payload = await ethAdvocateProvider.verify({ address: mockAddress } as RequestPayload, mockContext);

Expand Down Expand Up @@ -143,7 +144,7 @@ describe("AccountAnalysis Providers", () => {
describe("getETHAnalysis", () => {
it("should use value from context if present", async () => {
const mockedResponse = mockResponse({ score: 80 });
mockedAxios.post.mockResolvedValueOnce(mockedResponse);
mockedAxios.post.mockResolvedValue(mockedResponse);
mockContext = {};
const response1 = await getETHAnalysis(mockAddress, mockContext);
const response2 = await getETHAnalysis(mockAddress, mockContext);
Expand Down
5 changes: 3 additions & 2 deletions platforms/src/ZkSync/__tests__/accountAnalysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {

const mockAddress = "0x0";
let mockContext = {};
const mockResponse = (score: number): { data: ModelResponse } => ({
const mockResponse = (score: number): { status:number, data: ModelResponse } => ({
status: 200,
data: {
data: {
human_probability: score,
Expand Down Expand Up @@ -52,7 +53,7 @@ describe("AccountAnalysis Providers", () => {
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);
mockedAxios.post.mockResolvedValue(mockedResponse);
const ethAdvocateProvider = new provider();
const payload = await ethAdvocateProvider.verify({ address: mockAddress } as RequestPayload, mockContext);

Expand Down
Loading