Skip to content

Commit

Permalink
lint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ignaciopenia committed Jul 19, 2024
2 parents 7279731 + 0528b5f commit 0109b1b
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 72 deletions.
8 changes: 8 additions & 0 deletions test/alexSDK.mock-exceptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AlexSDK, Currency } from '../src';
import * as ammRouteResolver from '../src/utils/ammRouteResolver';
import { assertNever } from '../src/utils/utils';
import { configs } from '../src/config';

const sdk = new AlexSDK();
Expand Down Expand Up @@ -37,4 +38,11 @@ describe('AlexSDK - mock exceptions', () => {
)
).rejects.toThrow('Too many AMM pools in route');
}, 10000);

it('Attempt assertNever to throw unexpected object', () => {
const unexpectedObject = '' as never;
expect(() => assertNever(unexpectedObject)).toThrowError(
'Unexpected object: ' + unexpectedObject
);
});
});
20 changes: 10 additions & 10 deletions test/alexSDK.mock-externals.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { AlexSDK, Currency, TokenInfo } from '../src';
import fetchMock from 'fetch-mock';
import { configs } from '../src/config';
import { fetchBalanceForAccount, getAlexSDKData, getPrices } from '../src/utils/fetchData';
import { fetchBalanceForAccount, getPrices } from '../src/utils/fetchData';
import { transferFactory } from '../src/utils/postConditions';
import { readonlyCall } from '../src/utils/readonlyCallExecutor';

const sdk = new AlexSDK();

Expand All @@ -30,7 +29,9 @@ describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_AP
fetchMock.get(configs.SDK_API_HOST, 500);
fetchMock.get(`${configs.BACKEND_API_HOST}/v2/public/token-prices`, 500);
fetchMock.get(
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances`,500);
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances`,
500
);
});
afterEach(() => {
fetchMock.restore();
Expand Down Expand Up @@ -111,7 +112,9 @@ describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_AP
fetchMock.get(configs.SDK_API_HOST, 504);
fetchMock.get(`${configs.BACKEND_API_HOST}/v2/public/token-prices`, 504);
fetchMock.get(
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances`,504);
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances`,
504
);
});
afterEach(() => {
fetchMock.restore();
Expand Down Expand Up @@ -192,7 +195,9 @@ describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_AP
fetchMock.get(configs.SDK_API_HOST, 404);
fetchMock.get(`${configs.BACKEND_API_HOST}/v2/public/token-prices`, 404);
fetchMock.get(
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances`,404);
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances`,
404
);
});
afterEach(() => {
fetchMock.restore();
Expand Down Expand Up @@ -276,8 +281,3 @@ describe('Transfer Factory', () => {
);
});
});





47 changes: 41 additions & 6 deletions test/alexSDK.mock-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
dummyBalances,
dummyCurrencies,
dummyFee,
dummyPrices,
dummyRate,
dummyTx,
parsedDummyPrices,
Expand All @@ -22,6 +21,7 @@ import {
dummyFactorA,
dummyFactorB,
dummyTokenC,
DUMMY_DEPLOYER,
} from './mock-data/alexSDKMockResponses';
import { cvToValue, FungibleConditionCode } from '@stacks/transactions';

Expand Down Expand Up @@ -53,17 +53,31 @@ jest.mock('../src/helpers/SwapHelper', () => {
});
jest.mock('../src/utils/fetchData', () => {
const originalModule = jest.requireActual('../src/utils/fetchData');
const { dummyPrices, dummyCurrencies } = jest.requireActual(
'./mock-data/alexSDKMockResponses'
);
return {
__esModule: true,
...originalModule,
getPrices: jest.fn(async () => dummyPrices),
getPrices: jest
.fn()
.mockReturnValueOnce(dummyPrices)
.mockReturnValueOnce(originalModule.getPrices(dummyCurrencies)),
fetchBalanceForAccount: jest.fn(async () => dummyBalances),
getAlexSDKData: jest.fn(async () => dummyAlexSDKData),
};
});
jest.mock('../src/utils/ammRouteResolver', () => ({
resolveAmmRoute: jest.fn(() => dummyAmmRoute),
}));
jest.mock('../src/utils/ammRouteResolver', () => {
const originalModule = jest.requireActual('../src/utils/ammRouteResolver');
return {
resolveAmmRoute: jest.fn((tokenX, ...args) => {
if (tokenX === dummyTokenA) {
return dummyAmmRoute;
}
return originalModule.resolveAmmRoute(tokenX, ...args);
}),
};
});

describe('AlexSDK - mock helpers', () => {
it('Verify response value of getFeeRate function', async () => {
Expand Down Expand Up @@ -97,7 +111,7 @@ describe('AlexSDK - mock helpers', () => {
it('Verify response value of runSwap function', async () => {
expect(jest.isMockFunction(SwapHelper.runSpot)).toBeTruthy();
const result = await sdk.runSwap(
'SP111111111111111111111111111111111111111',
DUMMY_DEPLOYER,
tokenAlex,
tokenWUSDA,
BigInt(1),
Expand Down Expand Up @@ -126,12 +140,33 @@ describe('AlexSDK - mock helpers', () => {
expect(result.postConditions[0].amount).toStrictEqual(BigInt(0));
});

it('Verify response value of runSwap function (empty pools)', async () => {
expect(jest.isMockFunction(SwapHelper.runSpot)).toBeTruthy();
expect(jest.isMockFunction(ammRouteResolver.resolveAmmRoute)).toBeTruthy();
const amount = BigInt(2) * BigInt(1e8);
await expect(
sdk.runSwap(
configs.CONTRACT_DEPLOYER,
dummyTokenB,
dummyTokenC,
amount,
BigInt(0)
)
).rejects.toThrow("Can't find AMM route");
});

it('Verify response value of getLatestPrices function', async () => {
expect(jest.isMockFunction(fetchData.getPrices)).toBeTruthy();
const result = await sdk.getLatestPrices();
expect(result).toStrictEqual(parsedDummyPrices);
});

it('Verify response value of getLatestPrices function (null token cases)', async () => {
expect(jest.isMockFunction(fetchData.getPrices)).toBeTruthy();
const result = await sdk.getLatestPrices();
expect(result).toBeDefined();
});

it('Verify response value of getBalances function', async () => {
expect(jest.isMockFunction(fetchData.fetchBalanceForAccount)).toBeTruthy();
const stxAddress = 'SM2MARAVW6BEJCD13YV2RHGYHQWT7TDDNMNRB1MVT';
Expand Down
122 changes: 88 additions & 34 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,65 @@ 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 };
const routeLength2 = { from: tokenWmick, to: tokenDiko };
const routeLength3 = { from: tokenSSL, to: tokenDiko };
const routeLength4 = { 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
);
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 +118,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 +168,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 Expand Up @@ -207,6 +255,12 @@ describe('AlexSDK', () => {
});
}, 10000);

it('Verify response of getBalances function (with fungible token balance)', async () => {
const stxAddress = 'SP3ANPTPEQE72PNE31WE8BEV4VCKB2C38P48TPH0Q';
const balances = await sdk.getBalances(stxAddress);
expect(balances).toBeDefined();
}, 10000);

it('Attempt to Get Tx with an invalid stx address (checksum mismatch)', async () => {
await expect(
sdk.runSwap(
Expand Down Expand Up @@ -243,7 +297,7 @@ describe('AlexSDK', () => {
// TODO: Implement principal address verification in the SDK methods.
const wrongAddress = 'ABC';
await expect(sdk.getBalances(wrongAddress)).rejects.toThrow(
"Failed to fetch account balances"
'Failed to fetch account balances'
);
}, 10000);

Expand Down
Loading

0 comments on commit 0109b1b

Please sign in to comment.