I have a question on Aave Credit Delegation. I have gone through the documentation and based on that tried to set it up so that an address that deposits the collateral can delegate the borrowing power to another user. Assuming the below contract is the depositor and has deposited sufficient collateral, here is the code for approving delegation:
(, , address variableDebtTokenAddress) = dataProvider.getReserveTokensAddresses(
address(_token)
);
IVariableDebtToken(variableDebtTokenAddress).approveDelegation(_to, _amount);
uint256 allowance = IVariableDebtToken(variableDebtTokenAddress).borrowAllowance(address(this), _to);
console.log("variable approved allowance %s", allowance); // this shows the correct amount delegated
After that I am trying to borrow to the _to
address which is the delegatee
:
pool.borrow(address(_token), _amount, variableRate, referral, _to);
btw msg.sender
will the delegator
in the above call.
This results in error 9 which is for zero collateral.
Error: VM Exception while processing transaction: reverted with reason string '9'
This suggests that delegation isnt working and the pool is looking for collateral from the _to
user even though it should be using the collateral from the delegator
.
If I change the borrow receipient to address(this)
then borrow works fine.
Can someone please suggest what I could be doing wrong or how can I debug this further? Thanks!