Skip to content

Commit

Permalink
Cleaning up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagofilipenunes committed Jul 6, 2023
1 parent 1a9e556 commit e7c25d4
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 207 deletions.
57 changes: 8 additions & 49 deletions src/dex/carbon/carbon-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,6 @@ import { Network, ContractMethod, SwapSide } from '../../constants';
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { generateConfig } from '../../config';

/*
README
======
This test script should add e2e tests for Carbon. The tests
should cover as many cases as possible. Most of the DEXes follow
the following test structure:
- DexName
- ForkName + Network
- ContractMethod
- ETH -> Token swap
- Token -> ETH swap
- Token -> Token swap
The template already enumerates the basic structure which involves
testing simpleSwap, multiSwap, megaSwap contract methods for
ETH <> TOKEN and TOKEN <> TOKEN swaps. You should replace tokenA and
tokenB with any two highly liquid tokens on Carbon for the tests
to work. If the tokens that you would like to use are not defined in
Tokens or Holders map, you can update the './tests/constants-e2e'
Other than the standard cases that are already added by the template
it is highly recommended to add test cases which could be specific
to testing Carbon (Eg. Tests based on poolType, special tokens,
etc).
You can run this individual test script by running:
`npx jest src/dex/<dex-name>/<dex-name>-e2e.test.ts`
e2e tests use the Tenderly fork api. Please add the following to your
.env file:
TENDERLY_TOKEN=Find this under Account>Settings>Authorization.
TENDERLY_ACCOUNT_ID=Your Tenderly account name.
TENDERLY_PROJECT=Name of a Tenderly project you have created in your
dashboard.
(This comment should be removed from the final implementation)
*/

function testForNetwork(
network: Network,
dexKey: string,
Expand All @@ -68,18 +29,17 @@ function testForNetwork(
const holders = Holders[network];
const nativeTokenSymbol = NativeTokenSymbols[network];

// TODO: Add any direct swap contractMethod name if it exists
const sideToContractMethods = new Map([
[
SwapSide.SELL,
[
ContractMethod.simpleSwap,
ContractMethod.multiSwap,
ContractMethod.megaSwap,
// ContractMethod.multiSwap,
// ContractMethod.megaSwap,
],
],
// TODO: If buy is not supported remove the buy contract methods
[SwapSide.BUY, [ContractMethod.simpleBuy, ContractMethod.buy]],
[SwapSide.BUY, [ContractMethod.simpleBuy]],
// [SwapSide.BUY, [ContractMethod.simpleBuy, ContractMethod.buy]],
]);

describe(`${network}`, () => {
Expand Down Expand Up @@ -139,13 +99,12 @@ describe('Carbon E2E', () => {
describe('Mainnet', () => {
const network = Network.MAINNET;

// TODO: Modify the tokenASymbol, tokenBSymbol, tokenAAmount;
const tokenASymbol: string = 'DAI';
const tokenASymbol: string = 'WBTC';
const tokenBSymbol: string = 'USDC';

const tokenAAmount: string = '2340000000000000000';
const tokenBAmount: string = '1265000000000000000000';
const nativeTokenAmount = '1000000000000000000';
const tokenAAmount: string = '10000'; // 0.0001 WBTC
const tokenBAmount: string = '12600000000'; // 126 USDC
const nativeTokenAmount = '10000000000000000'; // 0.01 ETH

testForNetwork(
network,
Expand Down
33 changes: 0 additions & 33 deletions src/dex/carbon/carbon-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,6 @@ import { PoolState } from './types';
import { CarbonConfig } from './config';
import { DeepReadonly } from 'ts-essentials';

/*
README
======
This test script adds unit tests for Carbon event based
system. This is done by fetching the state on-chain before the
event block, manually pushing the block logs to the event-subscriber,
comparing the local state with on-chain state.
Most of the logic for testing is abstracted by `testEventSubscriber`.
You need to do two things to make the tests work:
1. Fetch the block numbers where certain events were released. You
can modify the `./scripts/fetch-event-blocknumber.ts` to get the
block numbers for different events. Make sure to get sufficient
number of blockNumbers to cover all possible cases for the event
mutations.
2. Complete the implementation for fetchPoolState function. The
function should fetch the on-chain state of the event subscriber
using just the blocknumber.
The template tests only include the test for a single event
subscriber. There can be cases where multiple event subscribers
exist for a single DEX. In such cases additional tests should be
added.
You can run this individual test script by running:
`npx jest src/dex/<dex-name>/<dex-name>-events.test.ts`
(This comment should be removed from the final implementation)
*/

jest.setTimeout(300 * 1000);
const dexKey = 'Carbon';
const network = Network.MAINNET;
Expand Down
219 changes: 94 additions & 125 deletions src/dex/carbon/carbon-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,95 +16,80 @@ import {
import { Tokens } from '../../../tests/constants-e2e';
import { Action, MatchActionBNStr, TradeActionBNStr } from './sdk/common/types';

/*
README
======
This test script adds tests for Carbon general integration
with the DEX interface. The test cases below are example tests.
It is recommended to add tests which cover Carbon specific
logic.
You can run this individual test script by running:
`npx jest src/dex/<dex-name>/<dex-name>-integration.test.ts`
(This comment should be removed from the final implementation)
*/

function getReaderCalldata(
exchangeAddress: string,
readerIface: Interface,
amounts: bigint[],
funcName: string,
strategyId: string,
token0: string,
token1: string,
tradeActions: TradeActionBNStr[][],
) {
return amounts.map(amount => ({
target: exchangeAddress,
callData: readerIface.encodeFunctionData(funcName, [
token0,
token1,
strategyId,
amount,
tradeActions,
]),
}));
}

function decodeReaderResult(
results: Result,
readerIface: Interface,
funcName: string,
) {
// TODO: Adapt this function for your needs
return results.map(result => {
const parsed = readerIface.decodeFunctionResult(funcName, result);
return BigInt(parsed[0]._hex);
});
}

async function checkOnChainPricing(
carbon: Carbon,
funcName: string,
blockNumber: number,
prices: bigint[],
amounts: bigint[],
strategyId: string,
sourceToken: string,
targetToken: string,
tradeActions: TradeActionBNStr[][],
) {
const exchangeAddress = '0xC537e898CD774e2dCBa3B14Ea6f34C93d5eA45e1';

const readerIface = new Interface(CarbonControllerABI);

const readerCallData = getReaderCalldata(
exchangeAddress,
readerIface,
amounts.slice(1),
funcName,
strategyId,
sourceToken,
targetToken,
tradeActions,
);

const readerResult = (
await carbon.dexHelper.multiContract.methods
.aggregate(readerCallData)
.call({}, blockNumber)
).returnData;

console.log(readerResult);

const expectedPrices = [0n].concat(
decodeReaderResult(readerResult, readerIface, funcName),
);

expect(prices).toEqual(expectedPrices);
}
// function getReaderCalldata(
// exchangeAddress: string,
// readerIface: Interface,
// amounts: bigint[],
// funcName: string,
// strategyId: string,
// token0: string,
// token1: string,
// tradeActions: TradeActionBNStr[][],
// ) {
// return amounts.map(amount => ({
// target: exchangeAddress,
// callData: readerIface.encodeFunctionData(funcName, [
// token0,
// token1,
// strategyId,
// amount,
// tradeActions,
// ]),
// }));
// }

// function decodeReaderResult(
// results: Result,
// readerIface: Interface,
// funcName: string,
// ) {
// // TODO: Adapt this function for your needs
// return results.map(result => {
// const parsed = readerIface.decodeFunctionResult(funcName, result);
// return BigInt(parsed[0]._hex);
// });
// }

// async function checkOnChainPricing(
// carbon: Carbon,
// funcName: string,
// blockNumber: number,
// prices: bigint[],
// amounts: bigint[],
// strategyId: string,
// sourceToken: string,
// targetToken: string,
// tradeActions: TradeActionBNStr[][],
// ) {
// const exchangeAddress = '0xC537e898CD774e2dCBa3B14Ea6f34C93d5eA45e1';

// const readerIface = new Interface(CarbonControllerABI);

// const readerCallData = getReaderCalldata(
// exchangeAddress,
// readerIface,
// amounts.slice(1),
// funcName,
// strategyId,
// sourceToken,
// targetToken,
// tradeActions,
// );

// const readerResult = (
// await carbon.dexHelper.multiContract.methods
// .aggregate(readerCallData)
// .call({}, blockNumber)
// ).returnData;

// console.log(readerResult);

// const expectedPrices = [0n].concat(
// decodeReaderResult(readerResult, readerIface, funcName),
// );

// expect(prices).toEqual(expectedPrices);
// }

async function testPricingOnNetwork(
carbon: Carbon,
Expand All @@ -115,11 +100,6 @@ async function testPricingOnNetwork(
destTokenSymbol: string,
side: SwapSide,
amounts: bigint[],
funcNameToCheck: string,
strategyId: string,
sourceToken: string,
targetToken: string,
tradeActions: TradeActionBNStr[][],
) {
const networkTokens = Tokens[network];

Expand All @@ -137,25 +117,27 @@ async function testPricingOnNetwork(

expect(pools.length).toBeGreaterThan(0);

// const poolPrices = await carbon.getPricesVolume(
// networkTokens[srcTokenSymbol],
// networkTokens[destTokenSymbol],
// amounts,
// side,
// blockNumber,
// pools,
// );
// console.log(
// `${srcTokenSymbol} <> ${destTokenSymbol} Pool Prices: `,
// poolPrices,
// );
const poolPrices = await carbon.getPricesVolume(
networkTokens[srcTokenSymbol],
networkTokens[destTokenSymbol],
amounts,
side,
blockNumber,
pools,
);
console.log(
`${srcTokenSymbol} <> ${destTokenSymbol} Pool Prices: `,
poolPrices,
);

expect(poolPrices).not.toBeNull();
if (carbon.hasConstantPriceLargeAmounts) {
checkConstantPoolPrices(poolPrices!, amounts, dexKey);
} else {
checkPoolPrices(poolPrices!, amounts, side, dexKey, false);
}

// expect(poolPrices).not.toBeNull();
// if (carbon.hasConstantPriceLargeAmounts) {
// checkConstantPoolPrices(poolPrices!, amounts, dexKey);
// } else {
// checkPoolPrices(poolPrices!, amounts, side, dexKey);
// }
// Cannot obtain on-chain pricing on Carbon

// Check if onchain pricing equals to calculated ones
// await checkOnChainPricing(
Expand Down Expand Up @@ -221,9 +203,6 @@ describe('Carbon', function () {
}
});

let tradeActions: TradeActionBNStr[][] = [];
let strategyId = '';

it('getPoolIdentifiers and getPricesVolume SELL', async function () {
await testPricingOnNetwork(
carbon,
Expand All @@ -234,11 +213,6 @@ describe('Carbon', function () {
destTokenSymbol,
SwapSide.SELL,
amountsForSell,
'', // TODO: Put here proper function name to check pricing
strategyId,
tokens[srcTokenSymbol].address,
tokens[destTokenSymbol].address,
tradeActions,
);
});

Expand All @@ -252,11 +226,6 @@ describe('Carbon', function () {
destTokenSymbol,
SwapSide.BUY,
amountsForBuy,
'', // TODO: Put here proper function name to check pricing
strategyId,
tokens[srcTokenSymbol].address,
tokens[destTokenSymbol].address,
tradeActions,
);
});

Expand Down

0 comments on commit e7c25d4

Please sign in to comment.