简体   繁体   中英

Solidity - External contract call not working

So, I'm new to solidity and I've been trying to make a contract that can call another contract.

I've found some articles describing this exact problem such as: https://soliditydeveloper.com/uniswap2

Altough, even with the provided code I can't make it work.

The transactions go through, BNB is transfered for the contract although the caller does not receive the tokens it was supposed to neither does the contract.

Here is the code I'm working on:

pragma solidity >=0.7.1;

import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol";

contract PancakeProxyContract {
    address WBNB = 0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd;
    address internal constant addr = 0xD99D1c33F9fC3444f8101754aBC46c52416550D1;
    IUniswapV2Router02 public uniswapRouter;

    constructor() {
        uniswapRouter = IUniswapV2Router02(addr);
    }

    function EthToTokens(uint amountOutMin, address[] memory path,uint deadline) public payable {
        uniswapRouter.swapExactETHForTokens{ value: msg.value }(amountOutMin, path, address(this), deadline);
        // refund leftover ETH to user
        (bool success,) = msg.sender.call{ value: address(this).balance }("");
        require(success, "refund failed");
    }


    receive() payable external {}
}

Here is a transaction example: https://testnet.bscscan.com/tx/0x0b2849412825dc9c279a102161db4db945ffc54bb4a47538f2a25a477c2c6fe4

Use swapExactETHForTokensSupportingFeeOnTransferTokens instead.

Turns out that I was using the address from the which I created the contract as the source address, instead of the newly address created by the new contract.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM