-2

I am new to blockchain dev. so the first learning I fork sushiswap from github, so the code very not clear for me

The difficulty understanding is how to get price tokens to exchange? example 1BNB will be 157.902SUSHI, I checked the network tab in chrome, and not showing any request.

So how to exchange the price from Token A - to Token B is there using API, Service, Smartconrtact, or any provider? I would appreciate it if someone could explain this more deeply so that I can understand

enter image description here

Thanks

Peter Jack
  • 847
  • 5
  • 14
  • 29

1 Answers1

0

Jack.

I am sure you wrote this smart contract using Token Standards like ERC-20.

There is no such an API you can use. But you can do this using simple variable for example "rate". You should use "AccessControl.sol" and "Ownable.sol". Your token should be the child of these contracts.

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

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
import "./SUSHI.sol";

contract BNB is ERC20, Ownable, AccessControl{

    string tokenName = "BNB token";
    string tokenSymbol = "BNB";
    uint decimal = 2;
    uint initFund = ...;
    uint rate = 32;

    constructor() ERC20 (tokenName, tokenSymbol){
        _mint(msg.sender, initFund);
    }

    function setRate(uint _rate) public onlyOwner {
        rate = _rate;
    }
    function getRate() public view returns(uint) {
        return rate;
    }

    function buyBNB(uint _amount){
        address owner = owner();
        require(_amount * rate == msg.value)
        transfer(msg.sender, _amount)
    }
    function exchange(uint _amount){
        SUSHI sushi = SUSHI(address(this));
        uint sushiRate = sushi.getRate();
        require(sushi.balanceOf(msg.sender) >= _amount * sushiRate / rate)
        transfer(msg.sender, _amount);
    }
    
    receive() payable {}

}

Here, the "rate" is the ratio of "Wei" over "BNB". Users buy "BNB" using their ETH via "_mint" function.

So you can define "SUSHI" Token like above and use another "exchangeBNB" function.

Yoloex
  • 1
  • 1