0

I'm not able to make a swap using the contract for some reason (I'm sure that I'm missing something fundamental). Here is the code.

What the contract does is:

  1. safeApprove PANCAKE_ROUTE to use the tokens in _path
  2. Call swapExactTokensForTokens of PancakeSwap V2
  3. Send the last token to the original caller (i.e. my wallet)

The inputs are:

  • _path: ["0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c","0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82"]
  • _initAmount: 30000000000000000 (~= 0.03 WBNB)

I have > 0.03 WBNB (0.05 BNB) in my wallet, and it's working with no problem when testing it using hardhat. Could anyone help me identify the cause?

1 Answer 1

0

It turned out you need to add code to transfer the initial token from your wallet to the contract unlike directly calling swapExactTokensForTokens.

// transfer token from your wallet to the contract
IERC20(firstToken).transferFrom(tx.origin, address(this), _initAmount);

That also requires a client-side of approval before sending a transaction to the contract.

// approve the contract to transfer token from your wallet to the contract
let approve_tx = await ownerTokenBase.approve(
    smartContractInstance.address,
    tradeAmount,
    GAS_CONFIG
);
4
  • Hi! Could you clarify your answer a bit? I think what you're saying is that when calling the pool directly, you must send the tokens directly instead of approving (and the router calling transferFrom). If that is correct, could make the answer a bit more clear in differentiating between pool and router contracts? Jan 1 at 10:39
  • Hi mate, I updated my answer comment a bit, hopefully it gives clearer information. Yes, actually I needed to send the initial token that I was swapping with to the smart contract (called pool? Not sure about the terminology as I'm newbie - would appreciate any information that helps me understand it!). To do that, a client approval is also necessary using e.g. ethers-js obviously because a smart contract should not be able to transfer a token from an arbitrary account to the contract.
    – somaito
    Jan 2 at 12:16
  • Basically, transferFrom has the flexibility to transfer someone else's tokens, but with one condition - the account the tokens are being transferred from has to approved the address transferring the tokens. When a contract transfers your tokens, which is how the router works, you have to approve it first. (Otherwise, the router would be able to steal everyone's tokens.) The pool contracts (or whatever they're called on Pancake) however do not directly transfer in order to perform a swap, so you need to send them yourself in order for the swap to work. Jan 2 at 17:31
  • That's what I understood and implemented too. Thanks for helping me for this one!
    – somaito
    Jan 9 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.