PancakeswapV2RestrictedStrategyAddBaseTokenOnly Strategy

https://github.com/alpaca-finance/bsc-alpaca-contract/blob/main/contracts/6/protocol/strategies/pancakeswapV2-restricted/PancakeswapV2RestrictedStrategyAddBaseTokenOnly.sol

Table of content

Background

This contract is where the action with PancakeSwap happened. The nature of this contract is used when users want to open a position supplying a base token, together with this strategy address from Alpaca interface, this contract will convert virtually half of the base token to farm token and provide liquidity to the DEX. Eventually will transfer the LP token back to the caller (worker).

Abstract

Attributes

AddBaseTokenOnly Attribute

NameTypePublicDescription
factoryIPancakeFactory
PancakeSwap Factory address
routerIPancakeRouter02
PancakeSwap Router address
okWorkersaddressboolmapping
List of worker that is allowed to call this strategy

Modifier

onlyWhitelistedWorkers

/// @notice require that only allowed workers are able to do the rest of the method call
  modifier onlyWhitelistedWorkers() {
    require(
      okWorkers[msg.sender],
      "PancakeswapV2RestrictedStrategyAddBaseTokenOnly::onlyWhitelistedWorkers:: bad worker"
    );
    _;
  }

Logic

Functions

Functions

NameTypeDescriptionDocumented
initializeExternalConstructor
executeExternalPerform operations on DEX
setWorkersOkExternalOwnerSet whitelist of workers that can call this contract

initialize

Use case

function Params

NameTypeDescription
_routerPancakeSwapRouterAddress of PancakeSwap router which will be used

Logic

  1. Assign state variable

Execute

Use case

function Params

NameTypeDescription
_addressUser address (Not used in this implementation)
_uint256Debt amount (Not used in this implementation)
databytescalldataDynamic variable

Logic

  1. Check if the caller is in the whitelist (Modifier)
  1. Retrieve the farming token the contract will operate on with and min additional LP tokens if provided
  1. Compute the optimal amount of base token to be converted to farming token
    1. Fancy math occurred here.
  1. Swap the base token to farming token at PancakeSwap
  1. Provide liquidity for base token - farming token pair at PancakeSwap
  1. Transfer the LP token back to the caller / worker for further processing

setWorkersOk

Use case

function Params

NameTypeDescription
workersaddressarrayArray of worker addreses
isOkboolTrue if whitelist, false if to blacklist

Logic

  1. Loop through all address
  1. Set the flag to whitelist/blacklist according to isOk flag