DEV Community

GuildAudits
GuildAudits

Posted on

5 2

Deploy a smart contract to an arbitrary address

This is one of the beauties of foundry framework, the ability to deploy a smart contract on any address. web3 developers gain alot from this because it aids you in experimenting with address with smart contract without having to go through hassle of getting an address deployed on the testnet or mainnet with code.

getCode

Signature

function getCode(string calldata) external returns (bytes memory);

Enter fullscreen mode Exit fullscreen mode

Description

Returns the creation bytecode for a contract in the project given the path to the contract.

The calldata parameter can either be in the form ContractFile.sol (if the filename and contract name are the same),ContractFile.sol:ContractName, or the path to an artifact, relative to the root of your project.

Enter fullscreen mode Exit fullscreen mode

ℹ️ Note

getCode requires read permission for the output directory, see file cheatcodes.

To grant read access set fs_permissions = [{ access = "read", path = "./out"}] in your foundry.toml.

Enter fullscreen mode Exit fullscreen mode

Examples

MyContract myContract = new MyContract(arg1, arg2);

// Let's do the same thing with `getCode`
bytes memory args = abi.encode(arg1, arg2);
bytes memory bytecode = abi.encodePacked(vm.getCode("MyContract.sol:MyContract"), args);
address anotherAddress;
assembly {
    anotherAddress := create(0, add(bytecode, 0x20), mload(bytecode))
}

assertEq0(address(myContract).code, anotherAddress.code); // [PASS]

Enter fullscreen mode Exit fullscreen mode

Deploy a contract to an arbitrary address by combining getCode and etch

// Deploy

bytes memory args = abi.encode(arg1, arg2);
bytes memory bytecode = abi.encodePacked(vm.getCode("MyContract.sol:MyContract"), args);
address deployed;
assembly {
deployed := create(0, add(bytecode, 0x20), mload(bytecode))
}
Enter fullscreen mode Exit fullscreen mode
// Set the bytecode of an arbitrary address
vm.etch(targetAddr, deployed.code);
Enter fullscreen mode Exit fullscreen mode

for more info check https://book.getfoundry.sh/cheatcodes/get-code?highlight=etch#examples

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

πŸ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay