合约调用Balancer Vault Swap失败。

我想完成将ETH换成WstETH的功能,试图通过在智能合约中调用Balancer Vault Swap来实现。

        uint256 amount = msg.value;
        IBalancerRouter.SingleSwap memory singleSwap = IBalancerRouter.SingleSwap({
            poolId: 0x36bf227d6bac96e2ab1ebb5492ecec69c691943f000200000000000000000316,
            kind: IBalancerRouter.SwapKind.GIVEN_IN,
            assetIn: IAsset(address(0)),
            assetOut: IAsset(wstETH),
            amount: amount,
            userData: bytes("")
        });

        IBalancerRouter.FundManagement memory funds = IBalancerRouter.FundManagement({
            sender: msg.sender,
            fromInternalBalance: false,
            recipient: payable(msg.sender),
            toInternalBalance: false
        });

        uint256 amountOut = amount * _getPrice() / 1e18;
        uint256 limit = 999 * amountOut / 1000;
        uint256 deadline = block.timestamp + 5 minutes;
        console.log(address(this).balance);
        wstETHAmount = IBalancerRouter(BalancerVault).swap(singleSwap, funds, limit, deadline);

    }

但是在使用Foundry测试是,它失败了。 测试函数

        vm.deal(alice, 10 ether);
        vm.prank(alice);
        IBalancerRouter(BalancerVault).setRelayerApproval(alice, address(wstETHSwapper), true);
        bool approved = IBalancerRouter(BalancerVault).hasApprovedRelayer(alice, address(wstETHSwapper));
        console.log(alice.balance);
        console.log(approved);
        assertTrue(approved);
        vm.prank(alice);
        wstETHSwapper.swap{value: 1 ether}();

        uint256 balance = IERC20(WSTETH).balanceOf(alice);
        emit log_uint(balance);
    }

报错信息

image.png

请先 登录 后评论

1 个回答

Tiny熊
  擅长:智能合约,以太坊
请先 登录 后评论
  • 1 关注
  • 0 收藏,2111 浏览
  • Joe 提出于 2023-04-26 16:28