2

I'm writing a Vyper contract to call the swapExactTokensForTokens function of PancakeSwap, but when I try to deploy, I find it doesn't work. I don't know what happened. I really need help.

This is my contract:

# @version ^0.2

from vyper.interfaces import ERC20

interface PancakeRouter:
    def swapExactTokensForTokens(
        amountIn: uint256,
        amountOutMin: uint256,
        path: address[3],
        to: address,
        deadline: uint256
    ) -> uint256[3]: nonpayable

PANCAKESWAP: constant(address) = 0x10ED43C718714eb63d5aA57B78B54704E256024E
WBNB: constant(address) = 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c

@external
def swap(tokenIn: address, tokenOut: address, amountIn: uint256):
    ERC20(tokenIn).transferFrom(msg.sender, self, amountIn)
    ERC20(tokenIn).approve(PANCAKESWAP, amountIn)

    res: Bytes[128] = raw_call(
        PANCAKESWAP,
        concat(
            method_id("swapExactTokensForTokens(uint256, uint256, address[], address, uint256)"),
            convert(amountIn, bytes32),
            convert(0, bytes32),
            convert(160, bytes32),
            convert(self, bytes32),
            convert(block.timestamp, bytes32),
            convert(3, bytes32),
            convert(tokenIn, bytes32),
            convert(WBNB, bytes32),
            convert(tokenOut, bytes32),
        ),
        max_outsize=128,
    )
1
  • Please explain exactly what doesn't work, the deployment? Transaction you fire fails? Add as many details as possible.
    – Kof
    Commented May 21, 2022 at 18:17

1 Answer 1

1
+100

You're using this example code only changed Uniswap to Pancakeswap and WETH to WBNB. The code is otherwise the same, so that's not a contract code issue.

You probably haven't pre-approved tokenIn to allow PANCAKESWAP to spend your tokens, so it fails at

ERC20(tokenIn).transferFrom(msg.sender, self, amountIn)

Also make sure you're running on BSC blockchain and not Ethereum.

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.