How can I automatically convert all incoming coins and tokens to BUSD?

e.g; With the receive() function in the BEP20 network, I capture when a money is transferred and trade with a script. But when other tokens such as WBNB, USDT are sent, I cannot do anything.

What I want to do: convert all coins and tokens directly transferred to the contract to busd via receive or fallback (or whichever works with) pancakeswap and mapping(address => uint) balance; I want to import the MAP into it.

I searched a lot but couldn't find the result I was looking for.

Could you please share which is the required source code for this process?

I using this function:

contract SendMoney{

        mapping(address => uint) balance;

        receive() external payable {
                    SendedMoney(msg.sender, msg.value);

       function SendedMoney(address _senderaddress, uint _amount){
        balance[_senderaddress] = _amount;

Hi, welcome to the community! :wave:

I think a little hard to do this, cause you do not know when and what kind of token you will receive, so you can not update mapping(address => uint) balance; automatically, and maybe there is not enough liquidity on the pancake for pair token-busd.
I think you have got to do this manually.

Hey @hertac
as @Skyge said you can't know when a erc20 is sent to your contract.
If you want to take some erc20 from a user, you can use transferFrom

As mentioned, one of the challenges with this kind of automation would be the available liquidity for the token. However, if you knew in advance the tokens you would like to execute swaps for, you could set up a Forta bot to monitor your contract for an increase in balance for those tokens, and then either have Forta send you a notification or do this by having a Defender Sentinel subscribe to the bot(s).

I just set up a demo repo for a similar use case that might be helpful if you wanted to go this route.

It's not full automatic conversion, but it's a step in that direction!

1 Like