-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetch risk assessment information from a dedicated API (#2375)
- Loading branch information
Showing
11 changed files
with
227 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './riskAssessment'; |
117 changes: 117 additions & 0 deletions
117
packages/checkout/sdk/src/riskAssessment/riskAssessment.test.ts
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,117 @@ | ||
import axios, { AxiosResponse } from 'axios'; | ||
import { fetchRiskAssessment, isAddressSanctioned } from './riskAssessment'; | ||
import { CheckoutConfiguration } from '../config'; | ||
|
||
jest.mock('axios'); | ||
|
||
describe('riskAssessment', () => { | ||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
const mockRemoteConfig = jest.fn(); | ||
|
||
const mockedConfig = { | ||
remote: { | ||
getConfig: mockRemoteConfig, | ||
}, | ||
} as unknown as CheckoutConfiguration; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('fetchRiskAssessment', () => { | ||
it('should fetch risk assessment and process it according to config', async () => { | ||
mockRemoteConfig.mockResolvedValue({ | ||
enabled: true, | ||
levels: ['severe'], | ||
}); | ||
|
||
const address1 = '0x1234567890'; | ||
const address2 = '0xabcdef1234'; | ||
|
||
const mockRiskResponse = { | ||
status: 200, | ||
data: [{ | ||
address: address1, | ||
risk: 'Low', | ||
risk_reason: 'No reason', | ||
}, { | ||
address: address2, | ||
risk: 'Severe', | ||
risk_reason: 'Sanctioned', | ||
}], | ||
} as AxiosResponse; | ||
mockedAxios.post.mockResolvedValueOnce(mockRiskResponse); | ||
|
||
const sanctions = await fetchRiskAssessment( | ||
[address1, address2], | ||
mockedConfig, | ||
); | ||
|
||
expect(sanctions[address1.toLowerCase()]).toEqual({ sanctioned: false }); | ||
expect(sanctions[address2.toLowerCase()]).toEqual({ sanctioned: true }); | ||
}); | ||
|
||
it('should return default risk assessment if disabled', async () => { | ||
mockRemoteConfig.mockResolvedValue({ | ||
enabled: false, | ||
levels: [], | ||
}); | ||
|
||
const address1 = '0x1234567890'; | ||
|
||
const sanctions = await fetchRiskAssessment( | ||
[address1], | ||
mockedConfig, | ||
); | ||
|
||
expect(sanctions[address1.toLowerCase()]).toEqual({ sanctioned: false }); | ||
expect(mockedAxios.post).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return default risk assessment not found for address', async () => { | ||
mockRemoteConfig.mockResolvedValue({ | ||
enabled: true, | ||
levels: ['severe'], | ||
}); | ||
|
||
const address1 = '0x1234567890'; | ||
|
||
const mockRiskResponse = { | ||
status: 200, | ||
data: [], | ||
} as AxiosResponse; | ||
mockedAxios.post.mockResolvedValueOnce(mockRiskResponse); | ||
|
||
const sanctions = await fetchRiskAssessment( | ||
[address1], | ||
mockedConfig, | ||
); | ||
|
||
expect(sanctions[address1.toLowerCase()]).toEqual({ sanctioned: false }); | ||
}); | ||
}); | ||
|
||
describe('isAddressSanctioned', () => { | ||
it('should return true if address is sanctioned', () => { | ||
const address = '0x1234567890ABCdef'; | ||
const assessment = { | ||
[address.toLowerCase()]: { | ||
sanctioned: true, | ||
}, | ||
}; | ||
|
||
expect(isAddressSanctioned(assessment, address)).toBe(true); | ||
}); | ||
|
||
it('should return false if address is not sanctioned', () => { | ||
const address = '0x1234567890ABCdef'; | ||
const assessment = { | ||
[address.toLowerCase()]: { | ||
sanctioned: false, | ||
}, | ||
}; | ||
|
||
expect(isAddressSanctioned(assessment, address)).toBe(false); | ||
}); | ||
}); | ||
}); |
68 changes: 68 additions & 0 deletions
68
packages/checkout/sdk/src/riskAssessment/riskAssessment.ts
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,68 @@ | ||
import axios from 'axios'; | ||
import { IMMUTABLE_API_BASE_URL } from '../env'; | ||
import { RiskAssessmentConfig } from '../types'; | ||
import { CheckoutConfiguration } from '../config'; | ||
|
||
type RiskAssessment = { | ||
address: string; | ||
risk: RiskAssessmentLevel; | ||
risk_reason: string; | ||
}; | ||
|
||
export enum RiskAssessmentLevel { | ||
LOW = 'Low', | ||
MEDIUM = 'Medium', | ||
HIGH = 'High', | ||
SEVERE = 'Severe', | ||
} | ||
|
||
export type AssessmentResult = { | ||
[address: string]: { | ||
sanctioned: boolean; | ||
}; | ||
}; | ||
|
||
export const fetchRiskAssessment = async ( | ||
addresses: string[], | ||
config: CheckoutConfiguration, | ||
): Promise<AssessmentResult> => { | ||
const result = Object.fromEntries( | ||
addresses.map((address) => [address.toLowerCase(), { sanctioned: false }]), | ||
); | ||
|
||
const riskConfig = (await config.remote.getConfig('riskAssessment')) as | ||
| RiskAssessmentConfig | ||
| undefined; | ||
|
||
if (!riskConfig?.enabled) { | ||
return result; | ||
} | ||
|
||
try { | ||
const riskLevels = riskConfig?.levels.map((l) => l.toLowerCase()) ?? []; | ||
|
||
const response = await axios.post<RiskAssessment[]>( | ||
`${IMMUTABLE_API_BASE_URL[config.environment]}/v1/sanctions/check`, | ||
{ | ||
addresses, | ||
}, | ||
); | ||
|
||
for (const assessment of response.data) { | ||
result[assessment.address.toLowerCase()].sanctioned = riskLevels.includes( | ||
assessment.risk.toLowerCase(), | ||
); | ||
} | ||
|
||
return result; | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error fetching risk assessment', error); | ||
return result; | ||
} | ||
}; | ||
|
||
export const isAddressSanctioned = ( | ||
riskAssessment: AssessmentResult, | ||
address: string, | ||
): boolean => riskAssessment[address.toLowerCase()].sanctioned; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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