Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Latest commit

 

History

History
231 lines (158 loc) · 7.53 KB

16. Declaring and Deploying Starknet contracts.md

File metadata and controls

231 lines (158 loc) · 7.53 KB

Declaring and Deploying Starknet Contracts

In this section, we will teach you how to declare and deploy starknet smart contract, but before that we will have to understand some concepts such as:

  • Transaction Types
  • Universal Deployer Contract (UDC)
  • Declaring vs Deploying
  • Counter Factual Deployment

Transaction Types

There are only 3 types of transaction on starknet:

  • deploy_account: Deploys a user account(smart wallets).
  • declare: Registers the sierra code of a smart contract on-chain.
  • invoke: Execute an external smart contract function.

Universal Deployer Contract (UDC)

Universal Deployer Contract creates a means by which contract deployment functionality is made external, specialized that can be invoked by any other contract safely and securely.

To learn more about the UDC, check out the links below:

Declaring vs Deploying:

Declaring a contract registers a blueprint of a smart contract on-chain using it's Sierra code. The blueprint is also known as the contract class and it's identified by its class hash .

The Universal Deployer utilizes the blueprint i.e the smart contract class hash to create an instance of the contract (Deployment of the contract).

To deploy your smart contract you need to set environment variables:

The following commands must run every time you open a new terminal to interact with Starknet. Setting them saves you time when using the CLI within the same terminal session.

# Use Starknet testnet
export STARKNET_NETWORK=alpha-goerli
# Set the default wallet implementation to be used by the CLI
export STARKNET_WALLET=starkware.starknet.wallets.open_zeppelin.OpenZeppelinAccount

Setting up an Account

You need to set up your CLI with an account contract and fund it.

Note: Starknet accounts are smart contracts. As such, creating one involves sending a transaction, and takes a bit longer than creating an EOA on other networks.

This process will involve three steps:

  • Generating your account address locally

  • Funding it

  • Deploying it

Creating a new account

starknet new_account --account account_name

Your terminal will return your account’s address.

Account address: 0x00d9d851f600d539a9f7811de4d9613a6b3c2634f8c0386a305c03216bd67559
Public key: 0x0293d6625d860b9a37a0319d1e3c1eecc27685075cbeaae4ef29ed717d93c58b
Move the appropriate amount of funds to the account, and then deploy the account
by invoking the 'starknet deploy_account' command.

NOTE: This is a modified version of the OpenZeppelin account contract. The signature is computed
differently.

Next step is to fund it.

  • Use the faucet to get some funds and send them to the account

  • Bridge funds using Starkgate

  • Fund the account through your Argent or Braavos Wallet

However you chose to do it, ensure that the funding transaction reaches the "PENDING" status before moving on. You can check the status on Starkscan or Voyager.

The process of creating an account locally, funding it and deploying it, is referred to as Counter Factual Deployment.

Deploying your account

starknet deploy_account --account account_name

Your sample output should look like this:

Sending the transaction with max_fee: 0.000568 ETH (568383605914463 WEI).
Sent deploy account contract transaction.
Contract address: 0x03f42fc2355be54197a8b270ff2cb8e2eb7902e777b3498f8ad58c6c147cce60
Transaction hash: 0x3d15e05389ecd1ff65555220be57f0ab43729877b20ca086048276917ed2838

Create and compile a contract

Note: We have assumed you have your development environment setup. If not, kindly refer the Section

//copy and paste the code in the terminal to check your compiler version
starknet --version
starknet-compile  --version

// Create a new folder for practicing your cairo skills
cd ~/
mkdir cairo_practice && cd cairo_practice

#Create a new file called ens_starknet.cairo
touch ens_starknet.cairo
  • Copy and paste the following piece of code into your ens_starknet.cairo file:
use starknet::ContractAddress;

#[starknet::interface]
trait IENS<TContractState> {
    fn store_name(ref self: TContractState, _name: felt252);
    fn get_name(self: @TContractState, _address: ContractAddress) -> felt252;
}

#[starknet::contract]
mod ENS {
    use starknet::get_caller_address;
    use starknet::ContractAddress;

    #[storage]
    struct Storage {
        names: LegacyMap::<ContractAddress, felt252>,
    }

    #[event]
    #[derive(Drop, starknet::Event)]
    enum Event {
        StoredName: StoredName
    }

    #[derive(Drop, starknet::Event)]
    struct StoredName {
        caller: ContractAddress,
        name: felt252
    }

    #[constructor]
    fn constructor(ref self: ContractState, _name: felt252, _address: ContractAddress) {
        self.names.write(_address, _name);
    }

    #[external(v0)]
    impl IENSImpl of super::IENS<ContractState> {
        fn store_name(ref self: ContractState, _name: felt252) {
            let caller = get_caller_address();
            self.names.write(caller, _name);
            self.emit(Event::StoredName(StoredName{ caller: caller, name: _name }));
        }

        fn get_name(self: @ContractState, _address: ContractAddress) -> felt252 {
            let name = self.names.read(_address);
            return name;
        }
    }
}
  • Compile the above Starknet contract using the following command:
starknet-compile ens_starknet.cairo ens_starknet.json

The above command should compile to produce a ens_starknet.json file in the same folder.

Declare a contract class

On Starknet, the deployment process is in two steps:

  • Declaring the class of your contract, or sending your contract’s code to the network

  • Deploying a contract, or creating an instance of the code you previously declared

Let’s start with declaring the above code.

//declare contract
starknet declare --contract ens_starknet.json --account account_name

You will see something like:

Sending the transaction with max_fee: 0.000132 ETH (131904173791637 WEI).
Declare transaction was sent.
Contract class hash: 0x8ceb9796d2809438d1e992b8ac17cfe83d0cf5944dbad948a370e0b5d5924f
Transaction hash: 0x334f16d9da30913c4a30194057793379079f35efa6bf5753bc6e724a591e9f0

The transaction hash allows you to track when the network will have received your contract’s code. Once this transaction has moved to "PENDING", you can deploy an instance of your contract.

Deploy Contract

Using the above generated class hash, deploy the contract:

starknet deploy --class_hash 0x8ceb9796d2809438d1e992b8ac17cfe83d0cf5944dbad948a370e0b5d5924f --account account_name

Note: If you run into any fee related issues, please add the flag --max_fee auto to your CLI commands to set an arbitrary gas limit for your deploy transaction.

You will see something like:

Sending the transaction with max_fee: 0.000197 ETH (197273405375932 WEI).
Invoke transaction for contract deployment was sent.
Contract address: 0x03a5cac216edec20350e1fd8369536fadebb20b83bfceb0c33aab0175574d35d
Transaction hash: 0x7895267b3e967e1c9c2f7da145e323bed60dfdd1b8ecc8efd243c9d587d579a

Monitor the deploy transaction. Once it has passed "PENDING", your contract has been successfully deployed!