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

Bsc pancake pool #146

Merged
merged 3 commits into from
Jan 4, 2024
Merged
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
26 changes: 18 additions & 8 deletions contracts/AMM/PancakeAMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ contract PancakeAMM {
ISweep public immutable sweep;
IPriceFeed public immutable oracleBase;
IPriceFeed public immutable sequencer;
address public immutable pool;
address public pool;
uint256 public immutable oracleBaseUpdateFrequency;
bool private immutable flag; // The sort status of tokens
PancakeLiquidityHelper private immutable liquidityHelper;
IMarketMaker public marketMaker;

uint32 private constant LOOKBACK = 1 days;
uint16 private constant DEADLINE_GAP = 15 minutes;
uint256 private constant PRECISION = 1e6;

constructor(
address _sweep,
address _base,
address _sequencer,
address _pool,
address _oracleBase,
uint256 _oracleBaseUpdateFrequency,
address _liquidityHelper
Expand All @@ -52,7 +52,6 @@ contract PancakeAMM {
base = IERC20Metadata(_base);
oracleBase = IPriceFeed(_oracleBase);
sequencer = IPriceFeed(_sequencer);
pool = _pool;
oracleBaseUpdateFrequency = _oracleBaseUpdateFrequency;
liquidityHelper = PancakeLiquidityHelper(_liquidityHelper);
flag = _base < _sweep;
Expand All @@ -78,6 +77,8 @@ contract PancakeAMM {
* @dev Get the quote for selling 1 unit of a token.
*/
function getPrice() public view returns (uint256 amountOut) {
if(address(pool) == address(0)) return 2e6;

(, int24 tick, , , , , ) = IPancakePool(pool).slot0();

uint256 quote = OracleLibrary.getQuoteAtTick(
Expand All @@ -91,9 +92,11 @@ contract PancakeAMM {
sequencer,
oracleBaseUpdateFrequency
);
uint8 decimals = ChainlinkLibrary.getDecimals(oracleBase);

amountOut = quote.mulDiv(price, 10 ** decimals);
uint8 quoteDecimals = base.decimals();
uint8 priceDecimals = ChainlinkLibrary.getDecimals(oracleBase);

amountOut = PRECISION.mulDiv(quote * price, 10 ** (quoteDecimals + priceDecimals));
}

/**
Expand All @@ -106,7 +109,6 @@ contract PancakeAMM {
sequencer,
oracleBaseUpdateFrequency
);
uint8 decimals = ChainlinkLibrary.getDecimals(oracleBase);

// Get the average price tick first
(int24 arithmeticMeanTick, ) = OracleLibrary.consult(pool, LOOKBACK);
Expand All @@ -119,7 +121,10 @@ contract PancakeAMM {
address(base)
);

amountOut = quote.mulDiv(price, 10 ** decimals);
uint8 quoteDecimals = base.decimals();
uint8 priceDecimals = ChainlinkLibrary.getDecimals(oracleBase);

amountOut = PRECISION.mulDiv(quote * price, 10 ** (quoteDecimals + priceDecimals));
}

function getPositions(uint256 tokenId)
Expand Down Expand Up @@ -209,8 +214,13 @@ contract PancakeAMM {
if(usdxAmount == 0 || sweepAmount == 0) revert ZeroAmount();
uint256 tokenFactor = 10 ** IERC20Metadata(usdxAddress).decimals();
uint256 sweepFactor = 10 ** sweep.decimals();
uint256 rate = usdxAmount * sweepFactor * 1e6 / (tokenFactor * sweepAmount);
uint256 rate = usdxAmount * sweepFactor * PRECISION / (tokenFactor * sweepAmount);

if(rate > 16e5 || rate < 6e5) revert BadRate();
}

function setPool(address poolAddress) external {
require(msg.sender == sweep.owner(), "PancakeAMM: Not Governance");
pool = poolAddress;
}
}
10 changes: 6 additions & 4 deletions contracts/Balancer/PancakeMarketMaker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ contract PancakeMarketMaker is IERC721Receiver, Stabilizer {
uint256 usdxAmount,
uint256 sweepAmount,
uint256 usdxMinAmount,
uint256 sweepMinAmount
uint256 sweepMinAmount,
address poolAddress
)
external onlyBorrower nonReentrant
returns (
Expand All @@ -226,7 +227,6 @@ contract PancakeMarketMaker is IERC721Receiver, Stabilizer {
TransferHelper.safeApprove(address(sweep), address(nonfungiblePositionManager), sweepAmount);

(int24 minTick, int24 maxTick) = showTicks();
IAMM _amm = amm();
(usdxAmount, sweepAmount, usdxMinAmount, sweepMinAmount) = flag
? (usdxAmount, sweepAmount, usdxMinAmount, sweepMinAmount)
: (sweepAmount, usdxAmount, sweepMinAmount, usdxMinAmount);
Expand All @@ -236,7 +236,7 @@ contract PancakeMarketMaker is IERC721Receiver, Stabilizer {
INonfungiblePositionManager.MintParams({
token0: token0,
token1: token1,
fee: IPancakePool(_amm.pool()).fee(),
fee: IPancakePool(poolAddress).fee(),
tickLower: minTick,
tickUpper: maxTick,
amount0Desired: usdxAmount,
Expand Down Expand Up @@ -358,10 +358,12 @@ contract PancakeMarketMaker is IERC721Receiver, Stabilizer {
* @return maxTick The maximum tick
*/
function showTicks() internal view returns (int24 minTick, int24 maxTick) {
uint8 decimals = sweep.decimals();
uint256 sweepPrice = sweep.targetPrice();
sweepPrice = sweepPrice * ((10 ** decimals) / PRECISION);

uint256 minPrice = (sweepPrice * 99) / 100;
uint256 maxPrice = (sweepPrice * 101) / 100;
uint8 decimals = sweep.decimals();

minTick = liquidityHelper.getTickFromPrice(minPrice, decimals, TICK_SPACE, flag);
maxTick = liquidityHelper.getTickFromPrice(maxPrice, decimals, TICK_SPACE, flag);
Expand Down
8 changes: 5 additions & 3 deletions contracts/Mocks/USDCMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import "@openzeppelin/contracts/token/ERC20/presets/ERC20PresetFixedSupply.sol";

contract USDCMock is ERC20PresetFixedSupply {
uint256 public constant GENESIS_SUPPLY = 2000000e18;
uint8 _decimals;

constructor()
constructor(uint8 dec)
ERC20PresetFixedSupply("USDC Mock", "USDC", GENESIS_SUPPLY, msg.sender) {
_decimals = dec;
}

function decimals() public pure override returns (uint8) {
return 6;
function decimals() public view override returns (uint8) {
return _decimals;
}
}
1 change: 1 addition & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = {
mainnet: scanApiKey,
avalanche: scanApiKey,
polygon: scanApiKey,
bsc: scanApiKey,
},
customChains: [
{
Expand Down
14 changes: 6 additions & 8 deletions scripts/deploy/amms/pancake_amm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ async function main() {
[deployer] = await ethers.getSigners();

const sweep = tokens.sweep;
const usdc = tokens.usdc;
const oracle = chainlink.usdc_usd;;
const base = tokens.usdt;
const oracle = chainlink.usdt_usd;;
const frequency = 86400;
const sequencer = chainlink.sequencer;
const helper = deployments.liquidity_helper;
const fee = 100;

console.log("===========================================");
console.log("PANCAKE AMM PLUGIN DEPLOY");
Expand All @@ -20,10 +19,9 @@ async function main() {
console.log("Deployer:", deployer.address);
console.log("===========================================");
console.log("SWEEP:", sweep);
console.log("USDC:", usdc);
console.log("USDT:", base);
console.log("Sequencer:", sequencer);
console.log("Fee:", fee);
console.log("USDC/USD Chainlink Oracle:", oracle);
console.log("USDT/USD Chainlink Oracle:", oracle);
console.log("Oracle Frequency:", frequency);
console.log("Liquidity helper:", helper);
console.log("===========================================");
Expand All @@ -32,11 +30,11 @@ async function main() {
console.log("Deploying...");

const pancakeAMMInstance = await ethers.getContractFactory("PancakeAMM");
const amm = await pancakeAMMInstance.deploy(sweep, usdc, sequencer, fee, oracle, frequency, helper);
const amm = await pancakeAMMInstance.deploy(sweep, base, sequencer, oracle, frequency, helper);

console.log("===========================================");
console.log(`PancakeAMM Deployed to:${amm.address}`);
console.log(`\nnpx hardhat verify --network ${network.name} ${amm.address} ${sweep} ${usdc} ${sequencer} ${fee} ${oracle} ${frequency} ${helper}`);
console.log(`\nnpx hardhat verify --network ${network.name} ${amm.address} ${sweep} ${base} ${sequencer} ${oracle} ${frequency} ${helper}`);
}

main();
22 changes: 11 additions & 11 deletions scripts/deploy/assets/pancake_market_maker.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const { ethers } = require("hardhat");
const { addresses } = require("../../../utils/address");
const { network, tokens, chainlink, deployments, wallets } = require("../../../utils/constants");
const { ask } = require("../../../utils/helper_functions");

async function main() {
[deployer] = await ethers.getSigners();
const assetName = 'Pancake Market Maker';
const sweep = addresses.sweep;
const usdc = addresses.usdc;
const liquidityHelper = addresses.liquidity_helper;
const oracleUsdc = addresses.oracle_usdc_usd;
const borrower = addresses.multisig;
const sweep = tokens.sweep;
const base = tokens.usdt;
const liquidityHelper = deployments.liquidity_helper;
const oracle = chainlink.usdt_usd;
const borrower = wallets.multisig;

console.log("===========================================");
console.log("PANCAKE MARKET MAKER ASSET DEPLOY");
Expand All @@ -19,9 +19,9 @@ async function main() {
console.log("===========================================");
console.log("Asset Name:", assetName);
console.log("SWEEP:", sweep);
console.log("USDC:", usdc);
console.log("USDC:", base);
console.log("Liquidity Helper:", liquidityHelper);
console.log("USDC/USD Chainlink Oracle:", oracleUsdc);
console.log("BASE/USD Chainlink Oracle:", oracle);
console.log("Borrower:", borrower);
console.log("===========================================");
const answer = (await ask("continue? y/n: "));
Expand All @@ -32,15 +32,15 @@ async function main() {
const stabilizer = await MarketMaker.deploy(
assetName,
sweep,
usdc,
base,
liquidityHelper,
oracleUsdc,
oracle,
borrower
);

console.log("===========================================");
console.log("MarketMaker deployed to: ", stabilizer.address);
console.log(`\nnpx hardhat verify --network ${network.name} ${stabilizer.address} "${assetName}" ${sweep} ${usdc} ${liquidityHelper} ${oracleUsdc} ${borrower}`);
console.log(`\nnpx hardhat verify --network ${network.name} ${stabilizer.address} "${assetName}" ${sweep} ${base} ${liquidityHelper} ${oracle} ${borrower}`);
}

main();
Expand Down
13 changes: 7 additions & 6 deletions scripts/deploy/pools/create_pancake_pool.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
const { ethers } = require('hardhat');
const { network, tokens, pancake } = require("../../../utils/constants");
const { getPriceAndData, ask } = require('../../../utils/helper_functions');
const { getPriceAndData, ask, toBN } = require('../../../utils/helper_functions');

async function main() {
const { token0, token1, sqrtPriceX96 } = getPriceAndData(tokens.sweep, tokens.usdc, 0, 0);
const { token0, token1 } = getPriceAndData(tokens.sweep, tokens.usdt, 0, 0);
const sqrtPriceX96 = toBN("79228162514264337593543950336", 0);
const FEE = 500;

console.log("===========================================");
console.log("CREATING PANCAKE POOL");
console.log("===========================================");
console.log("Network:", network.name);
console.log("===========================================");
console.log("SWEEP:", tokens.sweep);
console.log("USDC:", tokens.usdc);
console.log("UniswapFactory:", pancake.factory);
console.log("UniswapPositionManager:", pancake.positions_manager);
console.log("TOKEN 0:", token0);
console.log("TOKEN 1:", token1);
console.log("PancakeFactory:", pancake.factory);
console.log("PancakePositionManager:", pancake.positions_manager);
console.log("sqrtPriceX96:", sqrtPriceX96);
console.log("FEE:", FEE);
console.log("Cardinality:", pancake.observationCardinality);
Expand Down
4 changes: 1 addition & 3 deletions scripts/deploy/utils/pancake_liquidity_helper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const { ethers } = require("hardhat");
const { network } = require("../../../utils/address");
const { network } = require("../../../utils/constants");
const { ask } = require("../../../utils/helper_functions");

async function main() {
[deployer] = await ethers.getSigners();


console.log("===========================================");
console.log("PANCAKE LIQUIDITY HELPER DEPLOY");
console.log("===========================================");
Expand All @@ -17,7 +16,6 @@ async function main() {
if(answer !== 'y'){ process.exit(); }
console.log("Creating...");


const LiquidityHelper = await ethers.getContractFactory("PancakeLiquidityHelper");
const liquidityHelper = await LiquidityHelper.deploy();

Expand Down
2 changes: 1 addition & 1 deletion test/assets/balancer_market_maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ contract('Balancer Market Maker', async () => {
await sweep.connect(user).setTargetPrice(1e6, 1e6);

usdxAmount = toBN("500", 6);
sweepToGet = toBN("499", 18);
sweepToGet = toBN("495", 18);
sweepBefore = await sweep.balanceOf(borrower.address);
usdcBefore = await usdc.balanceOf(borrower.address);
vaultSweepBefore = await usdc.balanceOf(vaultAddress);
Expand Down
Loading