0

enter image description hereI am trying to write a foundry script that deposits the asset token into a newly deployed EIP4626 Yearn vault. However, when I try to call the EIP4626 "deposit(uint256)" function on the vault, I get the error message "SafeERC20: low-level call failed". I approve the vault and deposit amount before calling the deposit function, so I don't understand why the transfer is failing. I have attached an image of the logs. And the relevant code is here and here:

uint256 initialDepositAmount = 1e18; 

 console.log("Deployer asset balance before deposit: ", asset.balanceOf(deployer)); // 1000000000000000000

    console.log("making initial Vault deposit");
    asset.approve(address(vault), initialDepositAmount);
    // Error: "SafeERC20: low-level call failed"
    uint256 shares = vault.deposit(initialDepositAmount);
    if (shares > 0) {[![enter image description here][2]][2]
      console.log("initial Vault deposit complete");
      console.log("Deployer Vault shares: ", shares);
      console.log("Deployer asset balance after deposit complete: ", asset.balanceOf(deployer)); // 0
      console.log("Vault asset balance after deposit complete: ", asset.balanceOf(address(vault))); // 0
    } else if (shares == 0) {
      console.log("initial Vault deposit failed");
    }

1 Answer 1

0

Maybe the problem is that the approve function is not successfully finished before calling the deposit function. One way you could try is to require the successful approval, and additionally catch any other potential errors with a trycatch block:

uint256 initialDepositAmount = 1e18; 

console.log("Deployer asset balance before deposit: ", asset.balanceOf(deployer)); // 1000000000000000000
console.log("making initial Vault deposit");

try {
  // approve token transfer to vault
  bool approvalSuccess = asset.approve(address(vault), initialDepositAmount);
  require(approvalSuccess, "Token approval failed");

  // deposit tokens into vault
  uint256 shares = vault.deposit(initialDepositAmount);
  if (shares > 0) {
    console.log("initial Vault deposit complete");
    console.log("Deployer Vault shares: ", shares);
    console.log("Deployer asset balance after deposit complete: ", asset.balanceOf(deployer)); // 0
    console.log("Vault asset balance after deposit complete: ", asset.balanceOf(address(vault))); // 0
  } else {
    console.log("initial Vault deposit failed");
  }
} catch (error) {
  console.log("Error occurred during deposit: ", error);
}

Maybe this helps!

1
  • thank you! I will try it :D Feb 19, 2023 at 17:31

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.