Skip to content

Commit

Permalink
added place order function ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi8960git committed Sep 15, 2024
1 parent dd4d8fb commit 3317297
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/TakeProfitHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import {IPoolManager} from "v4-core/interfaces/IPoolManager.sol";
import {Hooks} from "v4-core/libraries/Hooks.sol";
import {PoolId, PoolIdLibrary} from "v4-core/types/PoolId.sol";
import {PoolKey} from "v4-core/types/PoolKey.sol";
import {Currency, CurrencyLibrary} from "v4-core/types/Currency.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract TakeProfitsHook is BaseHook, ERC1155 {
using PoolIdLibrary for PoolKey;
using CurrencyLibrary for Currency;

mapping(PoolId poolId => int24 tickLower) public tickLowerLasts;
mapping(PoolId poolId => mapping(int24 tick => mapping(bool zeroForOne => int256 amount)))
Expand Down Expand Up @@ -75,6 +78,44 @@ contract TakeProfitsHook is BaseHook, ERC1155 {
return intervals * tickSpacing;
}

// Core Utility Place Order
function placeOrder(
PoolKey calldata key,
int24 tick,
uint256 amountIn,
bool zeroForOne
) external returns (int24) {
int24 tickLower = _getTickLower(tick, key.tickSpacing);
takeProfitPositions[key.toId()][tickLower][zeroForOne] += int256(
amountIn
);

uint256 tokenId = getTokenId(key, tickLower, zeroForOne);

// If token id doesn't already exist, add it to the mapping
// Not every order creates a new token id, as it's possible for users to add more tokens to a pre-existing order
if (!tokenIdExists[tokenId]) {
tokenIdExists[tokenId] = true;
tokenIdData[tokenId] = TokenData(key, tickLower, zeroForOne);
}

// Mint ERC-1155 tokens to the user
_mint(msg.sender, tokenId, amountIn, "");
tokenIdTotalSupply[tokenId] += amountIn;

// Extract the address of the token the user wants to sell
address tokenToBeSoldContract = zeroForOne
? Currency.unwrap(key.currency0)
: Currency.unwrap(key.currency1);
// Move the tokens to be sold from the user to this contract
IERC20(tokenToBeSoldContract).transferFrom(
msg.sender,
address(this),
amountIn
);
return tickLower;
}

// ERc115 helpers

function getTokenId(
Expand Down

0 comments on commit 3317297

Please sign in to comment.