# 代理合约部署合约 **Published by:** [Web3 Zoom](https://paragraph.com/@web3-zoom/) **Published on:** 2025-07-18 **URL:** https://paragraph.com/@web3-zoom/6NYHroKE1Vk39XFhYSCd ## Content 对于已部署的合约,需要做到灵活的调用,应该怎么使用? 对于这种问题,应该使用代理合约实现 // SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.30; contract TestContract1{ address public owner = msg.sender; function setOwner(address _owner) public{ require(msg.sender == owner, "not owner"); owner = _owner; } } contract TestContract2{ address public owner = msg.sender; uint public value = msg.value; uint public x; uint public y; constructor(uint _x, uint _y){ x = _x; y = _y; } } contract Proxy{ event Deploy(address); fallback() external payable{} function deploy(bytes memory _code) external payable returns(address){ address addr; assembly{ // create(v,p,n) // v: amount of ETH to send // p: pointer in memory to start of code // n: size of code // bytecode 数据的实际位置(Solidity 中的字节数组前 32 字节用于存储长度信息)。 addr := create( callvalue(), // v add(_code, 0x20), // p mload(_code) // n mload 获取大小 ) } require(addr != address(0), "deploy failed"); emit Deploy(addr); return addr; } function execute(address _target, bytes memory _data) external payable{ (bool success, ) = _target.call{value: msg.value}(_data); require(success, "call failed"); } } contract Helper { function getBytecode1() external pure returns(bytes memory){ bytes memory bytecode = type(TestContract1).creationCode; return bytecode; } // 获取字节码 function getBytecode2(uint _x, uint _y) external pure returns(bytes memory){ bytes memory bytecode = type(TestContract2).creationCode; return abi.encodePacked(bytecode, abi.encode(_x,_y)); } // 获取 function getCalldata(address _owner) external pure returns(bytes memory){ return abi.encodeWithSignature("setOwner(address)", _owner); } } ## Publication Information - [Web3 Zoom](https://paragraph.com/@web3-zoom/): Publication homepage - [All Posts](https://paragraph.com/@web3-zoom/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@web3-zoom): Subscribe to updates