Smart Contract Inquiry

Hello, I want to deploy my smart contract but before I do I want to check one function that my frontend calls. I want my users to send their coins to this smart contract and it will redirect those coins to the wallet address that created the smart contract, does this code work?

owner_ = payable(msg.sender)…

function buyToken(uint8 status, address paymentcontract, uint256 value) public payable {
require(msg.sender != address(0), “Zero address”);
if(status==0){
owner
.transfer(msg.value);
}else{
wsum= IBEP20(paymentcontract);
require(wsum.allowance(msg.sender,address(this)) > value, “Please allow fund first”);
wsum.transferFrom(msg.sender,owner
,value);
}
}

I want to send it ERC20 tokens

Hey @m_devcoin

modifier payable is for recieveng funds in native crypto(it is not needed if your function is for Tokens).

  1. require(msg.sender != address(0), “Zero address”); means that person who wants to send his tokens cannot provide his own address?
  2. What is if(status==0) ?
  3. Instead of if else you should use require:
require(token.allowance(msg.sender, address(this)) >= amount,"Token allowance not enough");

Or maybe i don’t understand why you use if else there.

Also I reccommend you to use SafeERC20 (but your code will work correctly and without it):

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

instead of default transfer functions.

Take a look at READ BEFORE POSTING - How to post code in the forum and please send your code next time in code format :open_hands:

Happy BUIDLing

So would you use “contracts.methods. _safeTransferFrom”
to call this function on the frontend

Also will this allow me to send ERC20 tokens via polygon? So if a user transfers their ERC20 to the matic mainnet could they send these coins to the smart contract and then to the polygon wallet?

You can use this code on all solidity supporting chains with minor changes like ERC20 instead of BEP20

So would you use “contracts.methods. _safeTransferFrom”

I don’t remember is it allowed to call in js methods with _ . But you 100% can use omething like:
contracts.methods[_safeTransferFrom]