Skip to content
Merged
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
82 changes: 51 additions & 31 deletions v2/api-validator/tests/server-tests/rates.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import { randomUUID } from 'crypto';
import Client from '../../src/client';
import { ApiError, BadRequestError } from '../../src/client/generated';
import { ApiComponents, ApiError, BadRequestError, Rate } from '../../src/client/generated';
import { getAllCapableAccountIds, hasCapability } from '../utils/capable-accounts';

const noRatesCapability = !hasCapability('rates');
const accountIds = getAllCapableAccountIds('rates');
const ratesAccountIds = getAllCapableAccountIds('rates');

/**
* A rate can only be requested for a pair the account itself advertises, so the account
* under test must support both `rates` and the component that exposes the pair IDs.
*/
function findRatesAccountSupporting(component: keyof ApiComponents): string | undefined {
const capableAccountIds = new Set(getAllCapableAccountIds(component));
return ratesAccountIds.find((accountId) => capableAccountIds.has(accountId));
}

const rampsAccountId = findRatesAccountSupporting('ramps');
const liquidityAccountId = findRatesAccountSupporting('liquidity');

function expectValidRate(rate: Rate): void {
expect(rate).toHaveProperty('rate');
expect(rate).toHaveProperty('timestamp');
expect(typeof rate.rate).toBe('string');
expect(typeof rate.timestamp).toBe('number');
}

describe.skipIf(noRatesCapability)('Rates', () => {
let client: Client;
Expand All @@ -14,7 +32,7 @@ describe.skipIf(noRatesCapability)('Rates', () => {
});

describe('Get rate by account and assets', () => {
const accountId = accountIds[0];
const accountId = ratesAccountIds[0];

if (!accountId) {
it('should have at least one account with rates capability', () => {
Expand All @@ -23,50 +41,52 @@ describe.skipIf(noRatesCapability)('Rates', () => {
return;
}

describe('Successful rate retrieval', () => {
it('should return rate for conversion pair ID', async () => {
const conversionPairId = randomUUID();
const response = await client.rates.getRateByAccountAndPairId({
accountId,
conversionPairId,
rampsPairId: '',
orderBookPairId: '',
// Order book pairs are intentionally not covered: the API exposes no endpoint for
// discovering order book pair IDs, so a supported one cannot be resolved for a provider.

describe.skipIf(!rampsAccountId)('Ramps pair', () => {
let rampsPairId: string;

beforeAll(async () => {
const { capabilities } = await client.capabilities.getRampMethods({
accountId: rampsAccountId as string,
});

expect(response).toHaveProperty('rate');
expect(response).toHaveProperty('timestamp');
expect(typeof response.rate).toBe('string');
expect(typeof response.timestamp).toBe('number');
expect(capabilities.length).toBeGreaterThan(0);
rampsPairId = capabilities[0].id;
});

it('should return rate for ramps pair ID', async () => {
const rampsPairId = randomUUID();
const response = await client.rates.getRateByAccountAndPairId({
accountId,
accountId: rampsAccountId as string,
conversionPairId: '',
rampsPairId,
orderBookPairId: '',
});

expect(response).toHaveProperty('rate');
expect(response).toHaveProperty('timestamp');
expect(typeof response.rate).toBe('string');
expect(typeof response.timestamp).toBe('number');
expectValidRate(response);
});
});

describe.skipIf(!liquidityAccountId)('Conversion pair', () => {
let conversionPairId: string;

beforeAll(async () => {
const { capabilities } = await client.capabilities.getQuoteCapabilities({});

expect(capabilities.length).toBeGreaterThan(0);
conversionPairId = capabilities[0].id;
});

it('should return rate for order book pair ID', async () => {
const orderBookPairId = randomUUID();
it('should return rate for conversion pair ID', async () => {
const response = await client.rates.getRateByAccountAndPairId({
accountId,
conversionPairId: '',
accountId: liquidityAccountId as string,
conversionPairId,
rampsPairId: '',
orderBookPairId,
orderBookPairId: '',
});

expect(response).toHaveProperty('rate');
expect(response).toHaveProperty('timestamp');
expect(typeof response.rate).toBe('string');
expect(typeof response.timestamp).toBe('number');
expectValidRate(response);
});
});

Expand Down
Loading