我想完成将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);
}
报错信息