0

Is it possible to create a crowdsale contract that instead of eth/bnb to buy tokens, token will be use to buy tokens? like swap token to token? This is my crowdsale contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;

contract BIMPFinancePublicPresale is Crowdsale, CappedCrowdsale {
  
  address public owner;
        
  uint256 public investorHardCap =250000000000000000;
            
  mapping(address => uint256) private _contributions;
        
  constructor (uint256 rate,uint256 cap, address payable wallet, IERC20 token) public Crowdsale(rate, wallet, token) CappedCrowdsale(cap) {
          owner = msg.sender;
            // solhint-disable-previous-line no-empty-blocks
  }
        
  modifier onlyOwner() {
    if (owner == msg.sender) _;
  }
        
  function _preValidatePurchase(
    address beneficiary,
    uint256 weiAmount) internal view {
    super._preValidatePurchase(beneficiary, weiAmount);
    require( _contributions[beneficiary].add(weiAmount) <= investorHardCap, "Beneficiary's cap exceeded");
  }
        
  function _updatePurchasingState(address beneficiary, uint256 weiAmount) internal {
    super._updatePurchasingState(beneficiary, weiAmount);
    _contributions[beneficiary] = _contributions[beneficiary].add(weiAmount);
  }
        
  using SafeERC20 for IERC20;
  function transferToken(IERC20 token, address to, uint256 amount)  public onlyOwner  {
    token.safeTransfer(to, amount);
  }
    
}
1

1 Answer 1

0

Yes.

If the crowdsale contract sells tokenA, and accepts tokenB, it could work like this:

  1. User adds approval for the crowdsale contract to withdraw X of his tokenB

  2. User calls a specific function in the crowdsale, for example buy(uint amount)

  3. The function uses transferFrom to withdraw tokenB from the user, using the given allowance. Then the function sends tokenA to the user directly. The amount depends on the parameter and the sell ratio.

Remember to add full functionality to the crowdsale to handle tokenA and tokenB (such as transfer).

1
  • Hi do you have example contract for that? Thank you
    – Lar
    Commented Sep 10, 2021 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.