How to send ERC20 token to smart contract balance?
Asked Answered
P

1

10

I'm trying to build a smart contract and inherit some functions to swap ERC20 tokens,

Here are my questions?

Question A: Is it possible to transfer ERC20 token to smart contract balance?, Please provide an example, i.e. We can create a function to send ETH to smart contract

function contribute() external payable {}

//It will allow us to send ETH to smart contract balance,but how to send,for example, "BAND" token
//to smart contract balance?

Question B: If A is possible, how to get contract's token balance? i.e. We can get the contract ETH balance from this function:

// Get ETH balance
function getBalance() external view returns(uint) {
    return address(this).balance;    
}

// How to return contract's BAND balance, if A is possible ...

Question C:

If "A" is possible, How to make a swap to BAND/ETH liquidity pool, using Uniswap or Sushiswap API, Is it better to handle that process on server side proccesses using NodeJS, or implement it in solidity?


Full smart contract code:

pragma solidity ^0.5.11; 

contract SwapTest {
    address public manager;
    
    constructor() public {
        manager = msg.sender;
    }
    
    modifier OnlyManager() {
        require(msg.sender == manager);
        _;
    }
    
    // Add funds to contract
    function contribute() external payable {}
    
    
    // Get ETH balance
    function getBalance() external view returns(uint) {
        return address(this).balance;    
    } 
    
    // Send provided amount of WEI to recipient
    function sendEther (address payable recipient, uint weiAmount) external OnlyManager{
        recipient.transfer(weiAmount);    
    }
    
    // Send contract balance to recipient
    function withdrawBalance (address payable recipient) external OnlyManager{
        recipient.transfer(address(this).balance);
    }
}

Looking forward to hearing back from you guys, Thanks in advance.

Pianette answered 22/1, 2021 at 13:42 Comment(3)
For the StackOverflow Q&A format, I suggest you only add one question per question.Rainout
to create a Uniswav v2 pool you need to call Uniswap Factory contract with corresponding parameters. Download Uniswap sources and check it.Refluent
checked my DB of uniswap pairs. The pair you want to create already exists, the address is 0xf421c3f2e695C2D4C0765379cCace8adE4a480D9 . Also BAND token has another 16 pairs with other tokens: DIA, NMR,YFI, GEM,AXIA,DAI,BAT,USDC,YUNO,UNI,LINK,DREAM,AGIRefluent
W
11

When dealing with external contracts first include an interface definition, so you can call the contract's functions.

interface ERC20 {
  function balanceOf(address owner) external view returns (unit);
  function allowance(address owner, address spender) external view returns (unit);
  function approve(address spender, uint value) external returns (bool);
  function transfer(address to, uint value) external returns (bool);
  function transferFrom(address from, address to, uint value) external returns (bool); 
}

A: Tokens can be transferred directly to the contract's address, no additional functions need to be specified. Although, depending on your use case you can also write a function that will transfer tokens to itself.

function transferToMe(address _owner, address _token, unit _amount) public {
  ERC20(_token).transferFrom(_owner, address(this), _amount);
}

B: You can use this:

function getBalanceOfToken(address _address) public view returns (unit) {
  return ERC20(_address).balanceOf(address(this));
}

C: If you need your contract to be able to swap tokens you need to include the corresponding functions. Please refer to the Uniswap or Sushiswap documentation as they describe this in-depth.

Waal answered 22/1, 2021 at 21:36 Comment(4)
Can you please inform how it could be tested? So I deploy contract on Rinkeby network, the question is how to get any ERC20 token to test it, since metamask doesn't allows swap on test networksPianette
Ideally, I want to deploy contract on Rinkeby network and then send ERC20 tokens from Metamask and also call the swap functions later when I implement it, Thanks in advancePianette
You can switch networks in Metamask when on app.uniswap.org/#/swap. Then you can swap some Rinkeby Ether for some ERC20 token and test out your functions.Waal
You need a function to transfer the tokens out of the smart contract, otherwise those tokens will be lock there for ever. For this just use transfer as defined in the ERC20 interface. Also you can interface uniswap and directly provide liquidity.Stiff

© 2022 - 2024 — McMap. All rights reserved.