首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用智能契约在ethereum中部署另一个智能契约?

如何使用智能契约在ethereum中部署另一个智能契约?
EN

Ethereum用户
提问于 2021-04-07 08:31:51
回答 1查看 84关注 0票数 0

因为我有一个带有重入漏洞的测试合同,所以我想复制它。但是,只有当部署人员是重入攻击契约时,才能再现此漏洞。其守则如下:

代码语言:javascript
复制
contract Forwarder {
  // Address to which any funds sent to this contract will be forwarded
  address public parentAddress;
  event ForwarderDeposited(address from, uint value, bytes data);

  event TokensFlushed(
    address tokenContractAddress, // The contract address of the token
    uint value // Amount of token sent
  );

  /**
   * Create the contract, and set the destination address to that of the creator
   */
  function Forwarder() {
    parentAddress = msg.sender;
  }

  /**
   * Modifier that will execute internal code block only if the sender is a parent of the forwarder contract
   */
  modifier onlyParent {
    if (msg.sender != parentAddress) {
      throw;
    }
    _;
  }

  /**
   * Default function; Gets called when Ether is deposited and forwards it to the destination address
   */
  function() payable {
    if (**!parentAddress.call.value(msg.value**)(msg.data))
      throw;
    // Fire off the deposited event if we can forward it  
    ForwarderDeposited(msg.sender, msg.value, msg.data);
  }
}

因此,第一步是使用可以利用漏洞部署此易受攻击契约的契约,我如何使用智能契约来部署另一个契约,特别是通过Remix?

EN

回答 1

Ethereum用户

回答已采纳

发布于 2021-04-07 11:02:26

你可以这样做:

代码语言:javascript
复制
pragma solidity ^0.6.0;
import './Forwarder.sol';

contract SelfDeploy{
        
    event Addr(address);
    
    function getnewaddress() public returns (uint){
       emit Addr(address(new Forwarder()));
    }
}

在成功的事务处理之后,您可以检查以太扫描,也可以让侦听器获取转发器的地址。

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/96826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档