A Technical Analysis of PancakeSwap

pancakeswap github repo
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;
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
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
src/components/ConnectWalletButton.tsx 
src/components/menu/config.ts    //the menu list
src/components/menu/index.tsx //uses pancakeswap uikit
src/App.tsx    //contains the routes
src/index.tsx //call MulticallUpdater to load data from contract
src/state/multicall/updater.tsx  //dispatch multicall result to redux store, MulticallUpdater(), fetchChunk(), call multicall contract
src/state/swap/hooks.ts
src/state/pools/index.ts //fetchCakeVaultPublicData(), call fetchPublicVaultData()
src/state/pools/hooks.ts //state.pools.data , useFetchCakeVault() , calls dispatch(fetchCakeVaultPublicData())
src/state/pools/fetchVaultPublic.ts //fetchPublicVaultData(), call multicall function
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
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
src/views/AddLiquidity/index.tsx   //call router addLiquidityETH or addLiquidity
src/utils/index.ts     //getRouterContract, uses abi of uniswap IUniswapV2Router02ABI
src/hooks/useSwapCallback.ts  
//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
‘swapExactTokensForTokensSupportingFeeOnTransferTokens’ or  ‘swapExactTokensForTokens’
PCS source code
PCS v2 router address
PancakeRouter.sol, removeLiquidity method
PancakePair.sol, burn method
// pancake-swap-core, PancakePair.sol
bool feeOn;
address feeTo = IPancakeFactory(factory).feeTo();
feeOn = feeTo != address(0);
// 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);
}
}
}
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;
}

--

--

--

Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Akropolis January Update: ADEL>AKRO swap, hack compensation & more

KubeDiff — A Kubernetes Resource Diff Operator

Definition of Done

SSRF with filter bypass via open redirection vulnerability

Home Office: 6 tips to get started

Windows 11 Update is causing problems

Microsoft advises users to uninstall Windows 11 update - Nairametrics

5 reasons you should NOT be a developer (in a startup).

Junk Dimension, A way of handling flags and attributes into a single dimension table

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Victor Yeo

Victor Yeo

Engineer

More from Medium

How to Boost Smart Contract Security and Mitigate Risks in DeFi

How to Boost Smart Contract Security and Mitigate Risks in DeFi

Solidity FundMe Contract

0x_NODES: ETHEREUM DEPLOYMENT

UNPACKING TRUFFLE