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

Issue related to the allowance of token in liquidty pool smart contract #5641

Open
Tejas7477 opened this issue Jan 12, 2025 · 1 comment
Open

Comments

@Tejas7477
Copy link

Hii i have created the liquidty pool on the remix ide on the ploygon chain for the two tokens namely dogelon and dogeverse as i have these both tokens in my metamusk wallet i directly trnasferred them into the liquidty pool smart contract that i have created and the pool contract has many functions such as swapping , both coin reserves , thier addresses , confirm liquidty. when i transferred both the coins from my metamusk wallet to the pool address the and clicked the confirm liquidty the liquidty was added successfully but when i was clicked on the swap function there it shows the error of insufficeint dogelon coin i tried again added liquidty agian still the issue persists after that i added the approve function i successfully approved the tokens and agian tried still the same issue of insufficent doge eoln persisted.Every thing is correct still i am getting that error.
Here is my code snippet or code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract LiquidityPool {
IERC20 public dogeElon; // Doge Elon token interface
IERC20 public dogeVerse; // Doge Verse token interface

uint256 public dogeElonReserve; // Doge Elon reserve in the pool
uint256 public dogeVerseReserve; // Doge Verse reserve in the pool

address public owner; // Contract owner

// Events
event TokensSwapped(address indexed user, string direction, uint256 amountIn, uint256 amountOut);
event LiquidityConfirmed(address indexed user, uint256 dogeElonReserve, uint256 dogeVerseReserve);

// Modifier to restrict access to the owner
modifier onlyOwner() {
    require(msg.sender == owner, "Only the owner can perform this action");
    _;
}

// Constructor to initialize the contract with token addresses
constructor(address _dogeElon, address _dogeVerse) {
    require(_dogeElon != address(0), "Invalid Doge Elon token address");
    require(_dogeVerse != address(0), "Invalid Doge Verse token address");

    dogeElon = IERC20(_dogeElon);
    dogeVerse = IERC20(_dogeVerse);
    owner = msg.sender;
}

// Confirm liquidity after tokens are directly transferred
function confirmLiquidity() external onlyOwner {
    uint256 actualDogeElonBalance = dogeElon.balanceOf(address(this));
    uint256 actualDogeVerseBalance = dogeVerse.balanceOf(address(this));

    dogeElonReserve = actualDogeElonBalance;
    dogeVerseReserve = actualDogeVerseBalance;

    emit LiquidityConfirmed(msg.sender, actualDogeElonBalance, actualDogeVerseBalance);
}

// Approve tokens to be used in swaps
function approveTokens(uint256 dogeElonAmount, uint256 dogeVerseAmount) external {
    require(dogeElon.approve(address(this), dogeElonAmount), "Doge Elon approval failed");
    require(dogeVerse.approve(address(this), dogeVerseAmount), "Doge Verse approval failed");
}

// Swap Doge Elon for Doge Verse
function swapDogeElonForDogeVerse(uint256 dogeElonAmount) external {
    require(dogeElonAmount > 0, "Amount must be greater than zero");
    require(dogeElonReserve > 0 && dogeVerseReserve > 0, "Insufficient liquidity");

    uint256 dogeVerseAmount = (dogeElonAmount * dogeVerseReserve) / dogeElonReserve;
    require(dogeVerseAmount > 0, "Output amount must be greater than zero");

    // Check allowance for Doge Elon
    require(dogeElon.allowance(msg.sender, address(this)) >= dogeElonAmount, "Insufficient allowance for Doge Elon");

    // Transfer Doge Elon from user to contract
    require(dogeElon.transferFrom(msg.sender, address(this), dogeElonAmount), "Doge Elon transfer failed");

    // Transfer Doge Verse from contract to user
    require(dogeVerse.transfer(msg.sender, dogeVerseAmount), "Doge Verse transfer failed");

    // Update reserves
    dogeElonReserve += dogeElonAmount;
    dogeVerseReserve -= dogeVerseAmount;

    emit TokensSwapped(msg.sender, "Doge Elon to Doge Verse", dogeElonAmount, dogeVerseAmount);
}

// Swap Doge Verse for Doge Elon
function swapDogeVerseForDogeElon(uint256 dogeVerseAmount) external {
    require(dogeVerseAmount > 0, "Amount must be greater than zero");
    require(dogeElonReserve > 0 && dogeVerseReserve > 0, "Insufficient liquidity");

    uint256 dogeElonAmount = (dogeVerseAmount * dogeElonReserve) / dogeVerseReserve;
    require(dogeElonAmount > 0, "Output amount must be greater than zero");

    // Check allowance for Doge Verse
    require(dogeVerse.allowance(msg.sender, address(this)) >= dogeVerseAmount, "Insufficient allowance for Doge Verse");

    // Transfer Doge Verse from user to contract
    require(dogeVerse.transferFrom(msg.sender, address(this), dogeVerseAmount), "Doge Verse transfer failed");

    // Transfer Doge Elon from contract to user
    require(dogeElon.transfer(msg.sender, dogeElonAmount), "Doge Elon transfer failed");

    // Update reserves
    dogeVerseReserve += dogeVerseAmount;
    dogeElonReserve -= dogeElonAmount;

    emit TokensSwapped(msg.sender, "Doge Verse to Doge Elon", dogeVerseAmount, dogeElonAmount);
}

// Get the current reserves in the pool
function getReserves() external view returns (uint256, uint256) {
    return (dogeElonReserve, dogeVerseReserve);
}

}

@Joanfxx
Copy link

Joanfxx commented Jan 17, 2025

Hello @Tejas7477
I’ve carefully checked your logs and why it’s throwing that error
Kindly redirect your complain to the support portal

Connect to the support page and use the chat icon on the page to initiate a chat with the team there via;
Swap Support Request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
@Tejas7477 @Joanfxx and others