ETH交易值始终0
我正在尝试使用uniswap v3为我的项目添加掉期。每当我从前端调用合同中的功能时,值为0 ETH中的MetAmask中,只有支付费用。 我尝试了所有可以想到的方式将输入字符串转换为WEI,GWEI,HEX等,等等在MetAmask交易页面上始终为0。
function transferApprove(uint _amountIn, address _routerAddress) internal
{
TransferHelper.safeTransferFrom(
ETH,
msg.sender,
address(this),
_amountIn
);
TransferHelper.safeApprove(WETH9, _routerAddress, _amountIn);
}
function swapExactInputSingle(uint _amountIn) external returns (uint amountOut)
{
require(_amountIn > 0, "cannot swap 0 ETH");
emit value(_amountIn);
transferApprove(_amountIn, UNISWAP_V3_ROUTER);
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter
.ExactInputSingleParams({
tokenIn: ETH,
tokenOut: WETH9,
fee: 3000,
recipient: msg.sender,
deadline: block.timestamp,
amountIn: _amountIn,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0
});
amountOut = uniswapRouter.exactInputSingle(params);
}
///// JAvaScript部分
const sendSwap = async () => {
try{
if(!window.ethereum) return alert("Please install metamask");
const parsedAmount = ethers.utils.parseUnits(amount, 18);
const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
const signer = provider.getSigner()
const swap = new ethers.Contract(SwapAddress.address, SwapAbi.abi, signer)
const accounts = await ethereum.request({method:'eth_requestAccounts'})
setCurrentAccount(accounts[0])
setSwap(swap)
console.log(parsedAmount)
const transactionHash = await swap.swapExactInputSingle(parsedAmount)
setIsLoading(true);
await transactionHash.wait();
setIsLoading(false);
console.log({transactionHash});
} catch (error) {
console.log(error);
}
I am trying to use uniswap v3 to add swaps to my project. Everytime i call the function in my contract from the frontend the value is 0 ETH in metamask and only the fee is paid.
I have attempted all the ways i can think of converting the input string into wei, gwei, hex and so on and its always 0 on the metamask transaction page.
function transferApprove(uint _amountIn, address _routerAddress) internal
{
TransferHelper.safeTransferFrom(
ETH,
msg.sender,
address(this),
_amountIn
);
TransferHelper.safeApprove(WETH9, _routerAddress, _amountIn);
}
function swapExactInputSingle(uint _amountIn) external returns (uint amountOut)
{
require(_amountIn > 0, "cannot swap 0 ETH");
emit value(_amountIn);
transferApprove(_amountIn, UNISWAP_V3_ROUTER);
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter
.ExactInputSingleParams({
tokenIn: ETH,
tokenOut: WETH9,
fee: 3000,
recipient: msg.sender,
deadline: block.timestamp,
amountIn: _amountIn,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0
});
amountOut = uniswapRouter.exactInputSingle(params);
}
/////JAVASCRIPT SECTION
const sendSwap = async () => {
try{
if(!window.ethereum) return alert("Please install metamask");
const parsedAmount = ethers.utils.parseUnits(amount, 18);
const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
const signer = provider.getSigner()
const swap = new ethers.Contract(SwapAddress.address, SwapAbi.abi, signer)
const accounts = await ethereum.request({method:'eth_requestAccounts'})
setCurrentAccount(accounts[0])
setSwap(swap)
console.log(parsedAmount)
const transactionHash = await swap.swapExactInputSingle(parsedAmount)
setIsLoading(true);
await transactionHash.wait();
setIsLoading(false);
console.log({transactionHash});
} catch (error) {
console.log(error);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论