Skip to content

Commit

Permalink
tests: complete custom and alternative routes coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
simsbluebox committed Jul 16, 2024
1 parent 924cca8 commit da706c8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 35 deletions.
110 changes: 77 additions & 33 deletions test/alexSDK.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Ajv from 'ajv';
import { createGenerator } from 'ts-json-schema-generator';
import path from 'node:path';
import { getAlexSDKData, getPrices } from '../src/utils/fetchData';
import { TxToBroadCast } from '../src/helpers/SwapHelper';

const runtimeTypescriptMatcher = (received: any, typeName: string) => {
const validator = new Ajv().compile(
Expand Down Expand Up @@ -33,31 +34,61 @@ declare global {
}
}

const checkSwapResult = (result: TxToBroadCast) => {
expect(typeof result).toBe('object');
expect(result).toHaveProperty('contractAddress');
expect(result).toHaveProperty('contractName');
expect(result).toHaveProperty('functionName');
expect(result).toHaveProperty('functionArgs');
expect(result).toHaveProperty('postConditions');
expect(result.contractAddress).toBe(configs.CONTRACT_DEPLOYER);
expect(result.contractName).toBe('amm-pool-v2-01');
expect([
'swap-helper',
'swap-helper-a',
'swap-helper-b',
'swap-helper-c',
]).toContain(result.functionName);
expect(Array.isArray(result.functionArgs)).toBeTruthy();
expect(Array.isArray(result.postConditions)).toBeTruthy();
};

expect.extend({ toMatchType: runtimeTypescriptMatcher });

const tokenAlex = 'age000-governance-token' as Currency;
const tokenDiko = 'token-wdiko' as Currency;
const tokenWmick = 'token-wmick' as Currency;
const tokenSSL = 'token-ssl-all-AESDE' as Currency;
const tokenBRC20ORMM = 'brc20-ormm' as Currency;
const wrongTokenAlex = '' as Currency;

const routeLength1 = {from: tokenAlex, to: Currency.STX};

Check failure on line 65 in test/alexSDK.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `from:·tokenAlex,·to:·Currency.STX` with `·from:·tokenAlex,·to:·Currency.STX·`
const routeLength2 = {from: tokenWmick, to: tokenDiko};

Check failure on line 66 in test/alexSDK.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `from:·tokenWmick,·to:·tokenDiko` with `·from:·tokenWmick,·to:·tokenDiko·`
const routeLength3 = {from: tokenSSL, to: tokenDiko};

Check failure on line 67 in test/alexSDK.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `from:·tokenSSL,·to:·tokenDiko` with `·from:·tokenSSL,·to:·tokenDiko·`
const routeLength4 = {from: tokenWmick, to: tokenBRC20ORMM};

Check failure on line 68 in test/alexSDK.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `from:·tokenWmick,·to:·tokenBRC20ORMM` with `·from:·tokenWmick,·to:·tokenBRC20ORMM·`
const alternativeRoutes = [routeLength2, routeLength3, routeLength4];

const sdk = new AlexSDK();
const CLARITY_MAX_UNSIGNED_INT = BigInt(
'340282366920938463463374607431768211455'
);

describe('AlexSDK', () => {
it('Verify response of getFeeRate function', async () => {
const result = await sdk.getFeeRate(tokenAlex, Currency.STX);
expect(typeof result).toBe('bigint');
expect(result >= BigInt(0)).toBeTruthy();
}, 10000);

it('Verify response of getFeeRate function (custom route)', async () => {
const customRoute = await sdk.getRoute(tokenAlex, Currency.STX);
const result = await sdk.getFeeRate(tokenAlex, Currency.STX, customRoute);
const customRoute = await sdk.getRoute(routeLength1.from, routeLength1.to);
const result = await sdk.getFeeRate(routeLength1.from, routeLength1.to, customRoute);

Check failure on line 79 in test/alexSDK.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `routeLength1.from,·routeLength1.to,·customRoute` with `⏎······routeLength1.from,⏎······routeLength1.to,⏎······customRoute⏎····`
expect(typeof result).toBe('bigint');
expect(result >= BigInt(0)).toBeTruthy();
}, 10000);

it('Verify response of getFeeRate function (alternative routes)', async () => {
for (const route of alternativeRoutes) {
const result = await sdk.getFeeRate(route.from, route.to);
expect(typeof result).toBe('bigint');
expect(result >= BigInt(0)).toBeTruthy();
}
}, 40000);

it('Attempt to Get Fee Rate with wrong tokens', async () => {
await expect(
sdk.getFeeRate(wrongTokenAlex, wrongTokenAlex)
Expand All @@ -83,16 +114,30 @@ describe('AlexSDK', () => {
);
}, 10000);

it('Verify response of getAmountTo function', async () => {
it('Verify response of Get Rate function (custom route)', async () => {
const customRoute = await sdk.getRoute(routeLength1.from, routeLength1.to);
const result = await sdk.getAmountTo(
Currency.STX,
BigInt(2) * BigInt(1e8),
tokenDiko
routeLength1.from,
BigInt(10000000) * BigInt(1e8),
routeLength1.to,
customRoute
);
expect(typeof result).toBe('bigint');
expect(result > BigInt(0)).toBeTruthy();
}, 10000);

it('Verify response of Get Rate function (alternative routes)', async () => {
for (const route of alternativeRoutes) {
const result = await sdk.getAmountTo(
route.from,
BigInt(10000000) * BigInt(1e8),
route.to
);
expect(typeof result).toBe('bigint');
expect(result > BigInt(0)).toBeTruthy();
}
}, 40000);

it('Attempt to Get Rate with a wrong From token', async () => {
await expect(
sdk.getAmountTo(wrongTokenAlex, BigInt(2) * BigInt(1e8), tokenDiko)
Expand All @@ -119,33 +164,32 @@ describe('AlexSDK', () => {
).rejects.toThrow('ClarityError: 2011');
}, 10000);

it('Verify response of runSwap function', async () => {
it('Verify response of runSwap function (custom route)', async () => {
const customRoute = await sdk.getRoute(routeLength1.from, routeLength1.to);
const result = await sdk.runSwap(
configs.CONTRACT_DEPLOYER,
Currency.STX,
tokenDiko,
routeLength1.from,
routeLength1.to,
BigInt(2) * BigInt(1e8),
BigInt(0)
BigInt(0),
customRoute
);

expect(typeof result).toBe('object');
expect(result).toHaveProperty('contractAddress');
expect(result).toHaveProperty('contractName');
expect(result).toHaveProperty('functionName');
expect(result).toHaveProperty('functionArgs');
expect(result).toHaveProperty('postConditions');
expect(result.contractAddress).toBe(configs.CONTRACT_DEPLOYER);
expect(result.contractName).toBe('amm-pool-v2-01');
expect([
'swap-helper',
'swap-helper-a',
'swap-helper-b',
'swap-helper-c',
]).toContain(result.functionName);
expect(Array.isArray(result.functionArgs)).toBeTruthy();
expect(Array.isArray(result.postConditions)).toBeTruthy();
checkSwapResult(result);
}, 10000);

it('Verify response of runSwap function (alternative routes)', async () => {
for (const route of alternativeRoutes) {
const result = await sdk.runSwap(
configs.CONTRACT_DEPLOYER,
route.from,
route.to,
BigInt(2) * BigInt(1e8),
BigInt(0)
);
checkSwapResult(result);
}
}, 40000);

it('Attempt to Get Tx with an invalid stx address (checksum mismatch)', async () => {
await expect(
sdk.runSwap(
Expand Down
2 changes: 0 additions & 2 deletions test/mock-data/alexSDKMockResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export const dummyTokenA = 'TokenA' as Currency;
export const dummyTokenB = 'TokenB' as Currency;
export const dummyTokenC = 'TokenC' as Currency;

export const dummyRoute = [dummyTokenA, dummyTokenB, dummyTokenC];

export const dummyFactorA = BigInt(670000000);
export const dummyFactorB = BigInt(680000000);
export const dummyAmmRoute: AMMRouteSegment[] = [
Expand Down

0 comments on commit da706c8

Please sign in to comment.