Code Architecture

Tethereum DEX is a fork of PanCakeSwap v2 smart contracts and features all the main features with its own tweaks.

How Does It Work?
The PancakeSwap clone script uses the AMM (Automated Market Making) model to make seamless and quicker transactions. The AMM approach makes matching traders in the liquidity pool simple. New users can use their cryptocurrency and contribute to the increasing liquidity pool after successfully registering their wallets. The user becomes eligible for transaction fees and liquidity provider tokens after contributing.

These LP (Liquidity Providing) tokens can be used to stake and earn significant returns. T99 tokens are created through the staking of liquidity pool tokens. The T99 tokens can be staked in exchange for more tips.

Why is it necessary to create a PancakeSwap clone script?
DeFi applications are currently supported by Ethereum, the most popular Blockchain network. In addition, the platform is critical in raising the profiles of DeFi apps on a global basis. Although Ethereum has a lot of potential, it also has several drawbacks. Users are forced to seek an alternative option due to pitfalls such as high gas prices and frequently limited bandwidth.

As a result, a solid Ethereum alternative is desperately needed. This is where the Binance Smart Chain network comes into the picture. The platform has minimal trading fees and low saturation rates.

Pancake Farm

The tethereum-farm repo contains the smart contracts for farms and pools, T99 tokens and lottery rewards. The T99 Token is the native token used in Tethereum DEX.

MasterChef contract contains code to handle staking to earn Cake. When MasterChef contract is instantiated, it creates a default T99 pool. MasterChef contract contains code to add additional LP token to pool (function add), deposit LP tokens to pool (function deposit), add(stake) Cake to pool (function enterStaking). When you stake T99, you get Syrup tokens.

SousChef contract handles staking Syrup token (aka SyrupBar token) to earn a reward. The Syrup token is a kind of LP token, as well as a BEP20 token. The reward can be in the T99 token or other tokens.

The pool that we mention above, it is an array of structure. Each LP token is a contract, and has different contract address.

struct PoolInfo {
  IBEP20 lpToken; // Address of LP token contract.
  uint256 allocPoint; // How many allocation points assigned to this pool. CAKEs to distribute per block.
  uint256 lastRewardBlock; // Last block number that CAKEs distribution occurs.
  uint256 accCakePerShare; // Accumulated CAKEs per share, times 1e12. See below.
PoolInfo[] public poolInfo;

Tethereum DEX Core

The tethereum-swap-core repo contains the smart contracts to handle swapping. There are IERC20 and IPancakeREC20 interfaces which are very similar, except for additional PERMIT_TYPEHASH and DOMAIN_SEPARATOR, nonces and permit in IPancakeREC20. The permit function uses ecrecover to validate address of owner. PancakeERC20 is the contract that implements IPancakeERC20 interface.

TethereumFactory contract initialises the pair of crypto to be swapped. Tethereum contract contains the crypto pair information and performs crypto swap. The minting and burning of LP tokens are also in TethereumPair contract. Some crypto pair token requires creating an LP token first, then the crypto pair token can be swapped.

Tethereum Frontend #

This is where the main logic of Tethereum DEX is located.

The config subfolder contains the address and abi of the smart contracts deployed in the BSC mainnet.

src/config/abi/                    //contracts abi
src/config/constants/contracts.ts //masterChef sousChef contract address
src/config/constants/tokens.ts //tokens contract address
src/config/constants/pools.ts //pools contract address

The hooks subfolder contains hooks to access contracts and login callback.

src/hooks/useContract.ts       //get contract instance
src/hooks/useAuth.ts //login, logout callback
src/hooks/useCallWithGasPrice.ts // call when click Enable Pool button, called by useVaultApprove
src/hooks/useSwapCallback.ts // get callback to execute swap

The connect wallet button that calls login callback in useAuth.


The menu bar on Tethereum DEX web is controlled below.

src/components/menu/config.ts    //the menu list
src/components/menu/index.tsx //uses pancakeswap uikit

The main app code

src/App.tsx    //contains the routes
src/index.tsx //call MulticallUpdater to load data from contract

The state contains the redux store, dispatch, and code to use state from redux store.

src/state/multicall/updater.tsx  //dispatch multicall result to redux store, MulticallUpdater(), fetchChunk(), call multicall contract
src/state/pools/index.ts //fetchCakeVaultPublicData(), call fetchPublicVaultData()
src/state/pools/hooks.ts // , useFetchCakeVault() , calls dispatch(fetchCakeVaultPublicData())
src/state/pools/fetchVaultPublic.ts //fetchPublicVaultData(), call multicall function

The views are the UI components for the pages. This src/views/Pools/components/PoolsTable/ActionPanel/Stake.tsx will show “Connect Wallet button”, “Enable pool” button, or Stake button, depending on user context.

For example, if the user allowance is not greater than 0, and it is not BNB pool and it is manual pool, that means the pool needs approval (user needs to “Enable pool”).

src/views/Home/components/Hero.tsx      //home page
src/views/Pools/components/PoolsTable //pools page
src/views/Pools/index.tsx //call useFetchCakeVault()
src/views/Pools/components/PoolsTable/ActionPanel/Stake.tsx //Enable Pool button, call handleVaultApprove or handlePoolApprove, or call VaultStakeModal
src/views/Pools/hooks/useApprove.tsx //useVaultApprove, called when you click Enable Pool button in AutoCake
src/views/Pools/hooks/useStakePool.ts //useStakePool, called when clicking stake button, call deposit method of sousChef contract
src/views/Pools/components/CakeVaultCard/VaultStakeModal.tsx // VaultStakeModel, called when clicking stake button, handleDeposit, call deposit method of cakeVault contract
src/views/Pools/components/PoolCard/Modals/StakeModal.tsx // call useStakePool

The utils functions for contract, address and multicalls are located here.

src/utils/contractHelpers.ts   //get new contract instance
src/utils/addressHelpers.ts //get contract address
src/utils/multicall.ts //define multicall function, call Multicall contract

To perform swapping, Tethereum DEX calls IUniswapV2Router02 method addLiquidityETH or addLiquidity to add liquidity.

src/views/AddLiquidity/index.tsx   //call router addLiquidityETH or addLiquidity

The IUniswapV2Router02 router abi is returned from:

src/utils/index.ts     //getRouterContract, uses abi of uniswap IUniswapV2Router02ABI

The swap call back (useSwapCallback) is defined in :

//define useSwapCallback, which is a callback to execute swap. It calls getRouterContract to get IUniswapV2Router02 address. It then calls pancake-swap-sdk Router.swapCallParameters to get IUniswapV2Router02 method of swapExactETHForTokens to perform swapping

Tethereum DEX SDK

This repo contains router.ts. The file contains swapCallParameters function that is called by the frontend (src/hooks/useSwapCallback.ts). Depending on there is fee or not, it calls :

‘swapExactTokensForTokensSupportingFeeOnTransferTokens’ or  ‘swapExactTokensForTokens’

The fee parameter “feeOnTransfer” is set in the frontend.

Tethereum DEX Periphery

It contains the smart contract PancakeRouter.sol. The methods “swapExactTokensForTokensSupportingFeeOnTransferTokens” and “swapExactTokensForTokens” are defined in this file.

Token Swap

In PCS frontend, src/config/constants/index.ts, the router address is set to 0x10ED43C718714eb63d5aA57B78B54704E256024E. This is a PCS v2 router address. Even if the name refers to Uniswap (as in IUniswapV2Router02), the address is the PCS router address. (The developers are lazy to rename it.)

In fact, IUniswapV2Router02.sol is the same as IPancakeRouter02.solThe only difference is the naming. Therefore, PCS v2 router is used in pancake swap.

Compare this:

PCS source code

and below, they have the same router address.

PCS v2 router address

Looking further into swap, IUniswapV2Router02 method of swapExactETHForTokens, swapExactTokensForTokens call private method _swap(). That private method _swap() then calls IUniswapV2Pair.swap

Swap Fees

When liquidity is removed from the token Pair, in :

PancakeRouter.sol, removeLiquidity method

the tokens are burnt. An then in :

PancakePair.sol, burn method

the fees are transferred to LP address.

In PancakeSwap, traders pay a fee of 0.2% . Most of the fees (0.17%) goes to LP. The remaining 0.03% goes to PCS treasury, and is controlled by code.

// pancake-swap-core, PancakePair.sol
bool feeOn;
address feeTo = IPancakeFactory(factory).feeTo();
feeOn = feeTo != address(0);

If the feeTo address is zero, then there is no feeOn.

// pancake-swap-core, PancakePair.sol
if (feeOn) {
if (_kLast != 0) {
uint rootK = Math.sqrt(uint(_reserve0).mul(_reserve1));
uint rootKLast = Math.sqrt(_kLast); if (rootK > rootKLast) { uint numerator = totalSupply.mul(rootK.sub(rootKLast));
uint denominator = rootK.mul(3).add(rootKLast);
uint liquidity = numerator / denominator;
if (liquidity > 0) _mint(feeTo, liquidity); }

No feeOn, then _mint will not be called to create additional liquidity token and assign them to feeTo.

In pancake-swap-core, PancakeLibrary.sol, the functions getAmountOut and getAmountIn controls the 0.2% swap fee.

function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
require(amountIn > 0, ‘PancakeLibrary: INSUFFICIENT_INPUT_AMOUNT’);
require(reserveIn > 0 && reserveOut > 0, ‘PancakeLibrary: INSUFFICIENT_LIQUIDITY’);
uint amountInWithFee = amountIn.mul(998);
uint numerator = amountInWithFee.mul(reserveOut);
uint denominator = reserveIn.mul(1000).add(amountInWithFee);
amountOut = numerator / denominator;

As an example, the above function getAmountOut multiplies the numerator with 998, and multiplies the denominator with 1000, to achieve the 0.2% cut of swap fee from the trader. Therefore, liquidity providers (LP) get their fee simply by the tokens which they take the cut from the trader.

Powered by BetterDocs