Solidity-0.5.x

Flashloan callFunction 在內部被呼叫

  • October 25, 2020

我有機會看看這份契約

contract Flashloan is DyDxFlashLoan {
   uint256 public loan;

   constructor() public payable {
       (bool success, ) = WETH.call.value(msg.value)("");
       require(success, "fail to get weth");
   }

   function getFlashloan(address flashToken, uint256 flashAmount) external {
       uint256 balanceBefore = IERC20(flashToken).balanceOf(address(this));
       bytes memory data = abi.encode(flashToken, flashAmount, balanceBefore);
       flashloan(flashToken, flashAmount, data); // execution goes to `callFunction`
   }

   function callFunction(
       address, /* sender */
       Info calldata, /* accountInfo */
       bytes calldata data
   ) external onlyPool {
       (address flashToken, uint256 flashAmount, uint256 balanceBefore) = abi
           .decode(data, (address, uint256, uint256));
       uint256 balanceAfter = IERC20(flashToken).balanceOf(address(this));
       require(
           balanceAfter - balanceBefore == flashAmount,
           "contract did not get the loan"
       );
       loan = balanceAfter;

       // Use the money here!
   }
} 

getFlashloan()它呼叫該行flashloan和評論說

執行轉到“callFunction”

這是如何運作的?它是否以某種方式在內部呼叫它?

是的,這是一個內部呼叫。

flashloan 函式呼叫在DyDxPool 實現上執行。經過幾個步驟,如果一切正常,它將執行:

ICallee(args.callee).callFunction(
   msg.sender,
   args.account,
   args.data
);

callFunctionFlashloan契約中呼叫。

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