Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/venus protocol integration #869

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
640 changes: 640 additions & 0 deletions src/abi/venus/resilient-oracle.json

Large diffs are not rendered by default.

1,381 changes: 1,381 additions & 0 deletions src/abi/venus/token-converter.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/dex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import { Stader } from './stader/stader';
import { UsualBond } from './usual/usual-bond';
import { UsualMWrappedM } from './usual/usual-m-wrapped-m';
import { UsualMUsd0 } from './usual/usual-m-usd0';
import { Venus } from './venus/venus';

const LegacyDexes = [
CurveV2,
Expand Down Expand Up @@ -194,6 +195,7 @@ const Dexes = [
FluidDex,
UsualMWrappedM,
UsualMUsd0,
Venus,
];

export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
Expand Down
62 changes: 62 additions & 0 deletions src/dex/venus/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { DexParams } from './types';
import { DexConfigMap } from '../../types';
import { Network, SwapSide } from '../../constants';

const BASE_TOKENS = {
btcbAddress: '0x7130d2A12B9BCbFAe4f2634d864A1Ee1Ce3Ead9c',
ethAddress: '0x2170Ed0880ac9A755fd29B2688956BD959F933F8',
usdcAddress: '0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d',
usdtAddress: '0x55d398326f99059fF775485246999027B3197955',
xvsAddress: '0xcF6BB5389c92Bdda8a3747Ddb454cB7a64626C63',
};

const TOKEN_CONVERTERS = {
btcbPrimeConverterAddress: '0xE8CeAa79f082768f99266dFd208d665d2Dd18f53',
ethPrimeConverterAddress: '0xca430B8A97Ea918fF634162acb0b731445B8195E',
usdcPrimeConverterAddress: '0xa758c9C215B6c4198F0a0e3FA46395Fa15Db691b',
usdtPrimeConverterAddress: '0xD9f101AA67F3D72662609a2703387242452078C3',
xvsVaultConverterAddress: '0xd5b9AE835F4C59272032B3B954417179573331E0',
};

export const VenusConfig: DexConfigMap<DexParams> = {
BtcbPrimeConverter: {
[Network.BSC]: {
subgraphURL: '2ZCWgaBc8KoWW8kh7MRzf9KPdr7NTZ5cda9bxpFDk4wG',
baseAssetAddress: BASE_TOKENS.btcbAddress,
converterAddress: TOKEN_CONVERTERS.btcbPrimeConverterAddress,
protocolShareReserve: '0xCa01D5A9A248a830E9D93231e791B1afFed7c446',
},
},
EthPrimeConverter: {
[Network.BSC]: {
subgraphURL: '2ZCWgaBc8KoWW8kh7MRzf9KPdr7NTZ5cda9bxpFDk4wG',
baseAssetAddress: BASE_TOKENS.ethAddress,
converterAddress: TOKEN_CONVERTERS.ethPrimeConverterAddress,
protocolShareReserve: '0xCa01D5A9A248a830E9D93231e791B1afFed7c446',
},
},
UsdcPrimeConverter: {
[Network.BSC]: {
subgraphURL: '2ZCWgaBc8KoWW8kh7MRzf9KPdr7NTZ5cda9bxpFDk4wG',
baseAssetAddress: BASE_TOKENS.usdcAddress,
converterAddress: TOKEN_CONVERTERS.usdcPrimeConverterAddress,
protocolShareReserve: '0xCa01D5A9A248a830E9D93231e791B1afFed7c446',
},
},
UsdtPrimeConverter: {
[Network.BSC]: {
subgraphURL: '2ZCWgaBc8KoWW8kh7MRzf9KPdr7NTZ5cda9bxpFDk4wG',
baseAssetAddress: BASE_TOKENS.usdtAddress,
converterAddress: TOKEN_CONVERTERS.usdtPrimeConverterAddress,
protocolShareReserve: '0xCa01D5A9A248a830E9D93231e791B1afFed7c446',
},
},
XvsVaultConverter: {
[Network.BSC]: {
subgraphURL: '2ZCWgaBc8KoWW8kh7MRzf9KPdr7NTZ5cda9bxpFDk4wG',
baseAssetAddress: BASE_TOKENS.xvsAddress,
converterAddress: TOKEN_CONVERTERS.xvsVaultConverterAddress,
protocolShareReserve: '0xCa01D5A9A248a830E9D93231e791B1afFed7c446',
},
},
};
47 changes: 47 additions & 0 deletions src/dex/venus/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Address } from '../../types';

export interface Token {
id: Address;
address: Address;
symbol: string;
decimals: string;
}

export interface TokenConverter {
id: Address;
address: Address;
destinationAddress: Address;
baseAsset: Address;
paused: boolean;
priceOracleAddress: Address;
}

export interface PoolConfig {
address: Address;
baseAsset: Token;
priceOracleAddress: Address;
configs: {
tokenConverter: TokenConverter;
tokenIn: Token;
tokenOut: Token;
tokenOutBalance: string;
}[];
}

export type PoolState = Record<
string,
{ balance: bigint; price: bigint; amountConverted: bigint; amountOut: bigint }
>;

export type VenusData = {
tokenConverter: Address;
};

export type DexParams = {
subgraphURL: string;
baseAssetAddress: Address;
converterAddress: Address;
protocolShareReserve: Address;
};

export type MulticallResultOutputs = boolean | bigint | [bigint, bigint];
15 changes: 15 additions & 0 deletions src/dex/venus/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { BytesLike } from 'ethers';
import { MultiResult } from '../../lib/multi-wrapper';
import { generalDecoder } from '../../lib/decoders';

export const decodeAmountOut = (
result: MultiResult<BytesLike> | BytesLike,
): [bigint, bigint] => {
const decoded = generalDecoder(
result,
['uint256', 'uint256'],
[0n, 0n],
value => [value[0].toBigInt(), value[1].toBigInt()],
);
return decoded as [bigint, bigint];
};
126 changes: 126 additions & 0 deletions src/dex/venus/venus-e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* eslint-disable no-console */
import dotenv from 'dotenv';
dotenv.config();

import { testE2E } from '../../../tests/utils-e2e';
import {
Tokens,
Holders,
NativeTokenSymbols,
} from '../../../tests/constants-e2e';
import { Network, ContractMethod, SwapSide } from '../../constants';
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { generateConfig } from '../../config';

/*
README
======

This test script should add e2e tests for Venus. 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 Venus 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 Venus (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,
tokenASymbol: string,
tokenBSymbol: string,
tokenAAmount: string,
tokenBAmount: string,
nativeTokenAmount: string,
) {
const provider = new StaticJsonRpcProvider(
generateConfig(network).privateHttpProvider,
network,
);
const tokens = Tokens[network];
const holders = Holders[network];

const sideToContractMethods = new Map([
[SwapSide.SELL, [ContractMethod.swapExactAmountIn]],
]);

describe(`${network}`, () => {
sideToContractMethods.forEach((contractMethods, side) =>
describe(`${side}`, () => {
contractMethods.forEach((contractMethod: ContractMethod) => {
describe(`${contractMethod}`, () => {
it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
await testE2E(
tokens[tokenASymbol],
tokens[tokenBSymbol],
holders[tokenASymbol],
side === SwapSide.SELL ? tokenAAmount : tokenBAmount,
side,
dexKey,
contractMethod,
network,
provider,
);
});
});
});
}),
);
});
}

describe('Venus E2E', () => {
const dexKeys = [
['XvsVaultConverter', 'XVS', '10000000000000000000'],
['EthPrimeConverter', 'ETH', '10000000000000000'],
['BtcbPrimeConverter', 'bBTC', '500000000000000'],
['UsdtPrimeConverter', 'USDT', '10000000000000000000'],
['UsdcPrimeConverter', 'USDC', '10000000000000000000'],
];

describe('BSC', () => {
const network = Network.BSC;

const tokenSymbol: string = 'WBNB';

const nativeTokenAmount = '1000000000000000000';
for (const [dexKey, srcTokenSymbol, amount] of dexKeys) {
testForNetwork(
network,
dexKey,
srcTokenSymbol,
tokenSymbol,
amount,
'0',
nativeTokenAmount,
);
}
});
});
Loading