Web3js

嘗試在 Uniswap v3 中交換令牌時發生錯誤

  • August 30, 2021

我正在嘗試通過以下程式碼快照使用 web3 執行交換:

const fromTokenAddress = `0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2`; // WETH9
const toTokenAddress = `0x6B175474E89094C44Da98b954EedeAC495271d0F`; // DAI
const routerContract = new web3.eth.Contract(routerABI, routerAddress);
const expiryDate = Math.floor(Date.now() / 1000) + 900;
const web3 = new Web3(`http://192.168.0.11:8545/`);
const privateKey = credentials.privateKey;
const activeAccount = web3.eth.accounts.privateKeyToAccount(privateKey);
const qty = web3.utils.toWei('0.01', 'ether');

const params = {
   tokenIn: fromTokenAddress,
   tokenOut: toTokenAddress,
   fee: 3000,
   recipient: activeAccount.address,
   deadline: expiryDate,
   amountIn: qty,
   amountOutMinimum: 0,
   sqrtPriceLimitX96: 0,
};

let encodedTx = routerContract.methods.exactInputSingle(params).encodeABI();
let transactionObject = {
   gas: 238989, // gas fee needs updating?
   data: encodedTx,
   from: activeAccount.address,
   to: routerAddress
};

web3.eth.accounts.signTransaction(transactionObject, privateKey, (error, signedTx) => {
   if (error) {
       console.log(error);
   } else {
       web3.eth.sendSignedTransaction(signedTx.rawTransaction).on('receipt', (receipt) => {
           console.log(receipt);
       });
   }
});

我通過安全帽使用主網分叉,交易總是失敗並出現這樣的錯誤:

Returned error: Error: VM Exception while processing transaction: reverted with reason string 'STF'

有沒有人知道這個錯誤是什麼意思?或者有人可以給我指出一個在 Uniswap v3 中交換令牌的工作程式碼嗎?

STF 是安全的轉移 - 仔細檢查您是否批准您的契約以使用您的錢包的代幣,以及任何其他可能的批准。

引用自:https://ethereum.stackexchange.com/questions/108005