我正在编写一个Vyper合同来调用PancakeSwap的PancakeSwap函数,但是当我尝试部署时,我发现它不起作用。我不知道发生了什么。我真的需要帮助。
这是我的合同:
# @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,
)发布于 2022-05-21 19:54:47
您使用的是此示例代码,只将Uniswap改为Pancakeswap,将WETH更改为WBNB。在其他情况下,代码是相同的,所以这不是契约代码问题。
您可能还没有预先批准的tokenIn允许PANCAKESWAP使用您的令牌,因此它在
ERC20(tokenIn).transferFrom(msg.sender, self, amountIn)也要确保你运行的是BSC区块链,而不是埃瑟姆。
https://ethereum.stackexchange.com/questions/124038
复制相似问题