to swap token, you have to interact with the swapRouterContract and allow that contract to get ether out of your wallet. For this, you need to use ERC20
contract's approve
function to allow swapRouterContract
. First step is create an ERC20
contract
const provider = new ethers.providers.JsonRpcProvider(URL)
// this is goerli
// https://goerli.etherscan.io/token/0xb4fbf271143a4fbf7b91a5ded31806e42b2208d6
// you could find erc20 abi online
addressOfWeth="0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6"
const ERC20Contract = new ethers.Contract(addressOfWeth, ERC20ABI, provider);
then you have to call the approve
await ERC20Contract
// connectedWallet is the signer
.connect(connectedWallet)
// define approvalAmount
.approve(swapRouterAddress, approvalAmount)
// https://docs.uniswap.org/protocol/reference/deployments
const swapRouterAddress = "0xE592427A0AEce92De3Edee1F18E0157C05861564";
STF
means that SafeTransferFrom failed: either you don't have the correct approvals, or insufficient amounts in wallet.