Create a swap function of tokens using solidity without using uniswap ( swap with rate btw)

Viewed 1087

I am newbie to this field, just learn some courses online about solidity and create simple contract, I would like to ask a simple project that I try but not successful yet. I want to create a swap function that can swap between 2 tokens that I created from ERC20, WITHOUT using uniswap or something like that, just dev a new function that swap between token A and token B, for example with the rate of 1 token A = 3 token B, the rate was ask to save in a mapping and got config at first, but I dont really know how to setting up the rate for swapping. Could anyone help me out with this, many thanks guys.

Existed Code of Swap contract, just temporary :

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 <0.9.0;

import "./Token.sol";
import "./safemath.sol";

contract SwapToken {
    using SafeMath for uint;

    Token public token1;
    address public owner1;
    Token public token2;
    address public owner2;
    uint public amount1;
    uint public amount2;
    uint public swapRate;

    constructor(
        address _token1,
        address _owner1,
        address _token2,
        address _owner2,
        uint _amount1
    ) {
        token1 = Token(_token1);
        owner1 = _owner1;
        token2 = Token(_token2);
        owner2 = _owner2;
        swapRate = 3;
        amount1 = _amount1;
        amount2 = _amount1.mul(swapRate);
    }

    function swap() public {
        require(msg.sender == owner1 || msg.sender == owner2, "Not authorized from owners");
        require(
            token1.allowance(owner1, address(this)) >= amount1,
            "Token 1 allowance too low"
        );
        require(
            token2.allowance(owner2, address(this)) >= amount2,
            "Token 2 allowance too low"
        );

        _safeTransferFrom(token1, owner1, owner2, amount1);
        _safeTransferFrom(token2, owner2, owner1, amount2);
    }

    function _safeTransferFrom(
        Token token,
        address sender,
        address recipient,
        uint amount
    ) private {
        bool sent = token.transferFrom(sender, recipient, amount);
        require(sent, "Token transfer failed");
    }
}
1 Answers

Let's start with a general answer and we'll build on it as we understand the question better.

I think you are not trying to create a swapping solution with a liquidity pool, but instead, want to match 2 people(?) that want to swap tokens.

A very simplistic approach to it could be

// have a mapping, as your tutor said. This will store information about how a token (token's contract address) and another token (again, contract address) rate against each other.
mapping(address => mapping (address => uint256)) rates;

function setRating(address memory token1, address memory token2, uint256 rate) public {
     rates[token1][token2] = rate;
}

Then, while swapping, check the rate and calculate accordingly.

Related