微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在部署智能合约时,合约的“所有者”默认情况下不是部署它的人吗?

如何解决在部署智能合约时,合约的“所有者”默认情况下不是部署它的人吗?

我正在尝试在使用 onlyOwner 修饰符的闪贷合同中使用一个函数。当我发送闪贷交易时,我收到错误消息:“来电者不是所有者”。

我在运行此闪贷时使用的帐户与创建合同时使用的帐户相同,所以我不确定为什么它没有将我识别为所有者。

您可以在此处看到创建合约和发送失败交易的地址相同。 https://kovan.etherscan.io/address/0x91109bde85e0ab631d1983ce07b910ce4e99078a

点击那里的第一笔交易 ^^ 查看错误信息。

onlyOwner 修饰符的代码https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol

合同文件

    pragma solidity ^0.6.6;

import "./aave/FlashLoanReceiverBaseV2.sol";
import "../../interfaces/v2/ILendingPoolAddressesProviderV2.sol";
import "../../interfaces/v2/ILendingPoolV2.sol";

contract FlashloanV2 is FlashLoanReceiverBaseV2,Withdrawable {

    constructor(address _addressprovider) FlashLoanReceiverBaseV2(_addressprovider) public {}

    /**
     * @dev This function must be called only be the LENDING_POOL and takes care of repaying
     * active debt positions,migrating collateral and incurring new V2 debt token debt.
     *
     * @param assets The array of flash loaned assets used to repay debts.
     * @param amounts The array of flash loaned asset amounts used to repay debts.
     * @param premiums The array of premiums incurred as additional debts.
     * @param initiator The address that initiated the flash loan,unused.
     * @param params The byte array containing,in this case,the arrays of aTokens and aTokenAmounts.
     */
    function executeOperation(
        address[] calldata assets,uint256[] calldata amounts,uint256[] calldata premiums,address initiator,bytes calldata params
    )
        external
        override
        returns (bool)
    {
        
        //
        // This contract Now has the funds requested.
        // Your logic goes here.
        //
        
        // At the end of your logic above,this contract owes
        // the flashloaned amounts + premiums.
        // Therefore ensure your contract has enough to repay
        // these amounts.
        
        // Approve the LendingPool contract allowance to *pull* the owed amount
        for (uint i = 0; i < assets.length; i++) {
            uint amountOwing = amounts[i].add(premiums[i]);
            IERC20(assets[i]).approve(address(LENDING_POOL),amountOwing);
        }

        for (uint i = 0; i < assets.length; i++) {
            withdraw(assets[i]);
        }

        return true;
    }

    function _flashloan(address[] memory assets,uint256[] memory amounts) internal {
        address receiverAddress = address(this);

        address onBehalfOf = address(this);
        bytes memory params = "";
        uint16 referralCode = 0;

        uint256[] memory modes = new uint256[](assets.length);

        // 0 = no debt (flash),1 = stable,2 = variable
        for (uint256 i = 0; i < assets.length; i++) {
            modes[i] = 0;
        }

        LENDING_POOL.flashLoan(
            receiverAddress,assets,amounts,modes,onBehalfOf,params,referralCode
        );
    }

    /*
     *  Flash multiple assets 
     */
    function flashloan(address[] memory assets,uint256[] memory amounts) public onlyOwner {
        _flashloan(assets,amounts);
    }

    /*
     *  Flash loan 1000000000000000000 wei (1 ether) worth of `_asset`
     */
    function flashloan(address _asset) public onlyOwner {
        bytes memory data = "";
        uint amount = 1 ether;

        address[] memory assets = new address[](1);
        assets[0] = _asset;

        uint256[] memory amounts = new uint256[](1);
        amounts[0] = amount;

        _flashloan(assets,amounts);
    }
}

Withdrawable.sol(withdraw() 函数的来源)

pragma solidity ^0.6.6;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

/**
    Ensures that any contract that inherits from this contract is able to
    withdraw funds that are accidentally received or stuck.
 */
 
contract Withdrawable is Ownable {
    using SafeERC20 for ERC20;
    address constant ETHER = address(0);

    event LogWithdraw(
        address indexed _from,address indexed _assetAddress,uint amount
    );

    /**
     * @dev Withdraw asset.
     * @param _assetAddress Asset to be withdrawn.
     */
    function withdraw(address _assetAddress) public onlyOwner {
        uint assetBalance;
        if (_assetAddress == ETHER) {
            address self = address(this); // workaround for a possible solidity bug
            assetBalance = self.balance;
            msg.sender.transfer(assetBalance);
        } else {
            assetBalance = ERC20(_assetAddress).balanceOf(address(this));
            ERC20(_assetAddress).safeTransfer(msg.sender,assetBalance);
        }
        emit LogWithdraw(msg.sender,_assetAddress,assetBalance);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。