Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I deploy a smart contract using Ape Framework


How do I deploy a smart contract using Ape Framework


To deploy a smart contract using the Ape Framework, follow these steps:

Prerequisites

1. Install Ape Framework: Ensure you have Python (3.8 or later) and install the Ape Framework using pip or pipx:

bash
   pip install eth-ape
   

or

bash
   pipx install eth-ape
   

2. Set Up an Account: Import or create an account that will be used for deployment. For example, to import a MetaMask account, use:

bash
   ape accounts import meta_wallet
   

You will need to provide your private key and create a passphrase.

3. Configure Network: Set up your network configuration, such as connecting to Alchemy for Ethereum networks. Add your Alchemy API key to your environment variables:

bash
   export WEB3_ALCHEMY_API_KEY="YOUR_ALCHEMY_API_KEY"
   

Project Initialization

1. Initialize a Project: Create a new project structure by running:

bash
   ape init
   

This will create directories for contracts, scripts, and tests.

Write Your Contract

1. Create a Contract: In the `contracts/` directory, create a new file (e.g., `hello.vy`) and write your smart contract code. For example:

vyper
   #pragma version ^0.3.10

   @external
   def say_hello() -> string:
       return "Hello, World!"
   

Compile the Contract

1. Compile the Contract: Run the following command to compile your contract:

bash
   ape compile
   

This will generate the necessary artifacts, including the ABI and bytecode.

Deploy the Contract

1. Create a Deployment Script: In the `scripts/` directory, create a file named `deploy_hello.py` with the following code:

python
   from ape import project, accounts

   def main():
       # Load your account
       account = accounts.load('meta_wallet')

       # Deploy the contract
       contract = project.hello.deploy(sender=account)

       print(f"Contract deployed at address: {contract.address}")

   if __name__ == "__main__":
       main()
   

2. Run the Deployment Script: Execute the deployment script by specifying the network:

bash
   ape run deploy_hello --network ethereum:sepolia:alchemy
   

This command deploys the contract to the Sepolia testnet using Alchemy.

Verify Deployment

After running the deployment script, you should see the contract address printed in the console. You can verify the deployment on a block explorer like Etherscan.

By following these steps, you can successfully deploy a smart contract using the Ape Framework.

Citations:
[1] https://dev.to/mosesmuwawu/how-to-compile-deploy-and-interact-with-smart-contracts-using-apeworxape-and-vs-code-4hie
[2] https://github.com/Consensys/apeworx-ape
[3] https://academy.apeworx.io/articles/deploy-a-contract
[4] https://docs.apeworx.io/ape/stable/userguides/contracts.html
[5] https://github.com/ApeWorX/ape
[6] https://www.youtube.com/watch?v=aZvbrB1t2LQ
[7] https://www.degencode.com/p/introduction-to-ape-framework
[8] https://www.youtube.com/watch?v=LWFPMiXhlyI