Flow CLI Configuration using flow.json

CuriosityDeck
4 min readJun 18, 2023

--

Learn to configure your flow contracts for deployment using Flow CLI

Photo by Engin Akyurt: https://www.pexels.com/photo/abstract-illustration-over-black-surface-5200063/

Introduction

flow.json is a file provided to configure the state of the Flow CLI. You can initialize a flow project and create your flow.json using the command flow init . The flow configuration consists of the following properties:

  • networks: a list containing the connection configuration for the Flow emulator, testnet, and mainnet .
  • accounts: a list containing the Flow Emulator service account.

Example Configuration File

{
"networks": {
"emulator": "127.0.0.1:3569",
"mainnet": "access.mainnet.nodes.onflow.org:9000",
"sandboxnet": "access.sandboxnet.nodes.onflow.org:9000",
"testnet": "access.devnet.nodes.onflow.org:9000"
},
"accounts": {
"emulator-account": {
"address": "f8d6e0586b0a20c7",
"key": "ae1b44c0f5e8f6992ef2348898a35e50a8b0b9684000da8b1dade1b3bcd6ebee"
}
}
}

Understanding Configuration File

The following is a configuration file for a flow project. Don’t get overwhelmed we are gonna cover this. As a heads up this is just mapping between some keys and their values.

{
"contracts": {
"NonFungibleToken": "./cadence/contracts/NonFungibleToken.cdc",
"Kibble": "./cadence/contracts/Kibble.cdc",
"KittyItems": "./cadence/contracts/KittyItems.cdc",
"KittyItemsMarket": "./cadence/contracts/KittyItemsMarket.cdc",
"FungibleToken": {
"source": "./cadence/contracts/FungibleToken.cdc",
"aliases": {
"testnet": "9a0766d93b6608b7",
"emulator": "ee82856bf20e2aa6"
}
}
},

"deployments": {
"testnet": {
"admin-account": ["NonFungibleToken"],
"user-account": ["Kibble", "KittyItems", "KittyItemsMarket"]
},
"emulator": {
"emulator-account": [
"NonFungibleToken",
"Kibble",
"KittyItems",
"KittyItemsMarket"
]
}
},

"accounts": {
"admin-account": {
"address": "3ae53cb6e3f42a79",
"key": "12332967fd2bd75234ae9037dd4694c1f00baad63a10c35172bf65fbb8ad1111"
},
"user-account": {
"address": "e2a8b7f23e8b548f",
"key": "22232967fd2bd75234ae9037dd4694c1f00baad63a10c35172bf65fbb8ad1111"
},
"emulator-account": {
"address": "f8d6e0586b0a20c7",
"key": "2eae2f31cb5b756151fa11d82949c634b8f28796a711d7eb1e52cc301ed11111",
}
},

"networks": {
"emulator": "127.0.0.1:3569",
"mainnet": "access.mainnet.nodes.onflow.org:9000",
"testnet": "access.devnet.nodes.onflow.org:9000",
"testnetSecure": {
"Host": "access-001.devnet30.nodes.onflow.org:9001",
"NetworkKey": "ba69f7d2e82b9edf25b103c195cd371cf0cc047ef8884a9bbe331e62982d46daeebf836f7445a2ac16741013b192959d8ad26998aff12f2adc67a99e1eb2988d"
}
}
}

Contracts

This section of the configuration file deals with contracts that you use in your project and their relative location in your project. You can see that name of each contract is mapped to its location

  "contracts": {
"NonFungibleToken": "./cadence/contracts/NonFungibleToken.cdc",
"Kibble": "./cadence/contracts/Kibble.cdc",
"KittyItems": "./cadence/contracts/KittyItems.cdc",
"KittyItemsMarket": "./cadence/contracts/KittyItemsMarket.cdc",
"FungibleToken": {
"source": "./cadence/contracts/FungibleToken.cdc",
"aliases": {
"testnet": "9a0766d93b6608b7",
"emulator": "ee82856bf20e2aa6"
}
}
}

If you look closer FungibleToken is given an alias in the above example why is it so? Aliases are used to define an address where the contract is already deployed in that specific network. This is because some of the standard contracts like FungibleTokenare already deployed into the test & main network and can be imported from their addresses. Here the address of FungibleToken in testnetand emulator is already specified in aliases .

"CONTRACT NAME": {
"source": "CONTRACT SOURCE FILE LOCATION",
"aliases": {
"NETWORK NAME": "ADDRESS ON SPECIFIED NETWORK WITH DEPLOYED CONTRACT"
...
}
}

please note that we should not specify the contract in deployment section if it already has aliases .

Accounts

Th accountsection is used to list the properties of accounts like names, keys, and addresses. Below you can see the properties of accounts where the contracts are being deployed.

"accounts": {
"admin-account": {
"address": "3ae53cb6e3f42a79",
"key": "12332967fd2bd75234ae9037dd4694c1f00baad63a10c35172bf65fbb8ad1111"
},
"user-account": {
"address": "e2a8b7f23e8b548f",
"key": "22232967fd2bd75234ae9037dd4694c1f00baad63a10c35172bf65fbb8ad1111"
},
"emulator-account": {
"address": "f8d6e0586b0a20c7",
"key": "2eae2f31cb5b756151fa11d82949c634b8f28796a711d7eb1e52cc301ed11111",
}
},

Networks

This section will define the network and its parameters on which the contract is being deployed

"networks": {
"NETWORK NAME": {
"host": "ADDRESS",
"key": "ACCESS NODE NETWORK KEY"
}
}

Deployments

This section defines where the contracts specified are deployed. This can act as a link between contracts, accounts, networks .

"deployments": {
"NETWORK": {
"ACCOUNT NAME": ["CONTRACT NAME"]
}
}

Create Account Using Flow CLI

You can easily initiate creating the account in interactive mode by the command flow accounts createin the directory containing flow.json. This will ask you for an account name and options to select and network. After you enter, the account will be saved to flow.json.

flow accounts create

❗ Version warning: a new version of Flow CLI is available (v1.3.0).
Read the installation guide for upgrade instructions: https://docs.onflow.org/flow-cli/install

Enter an account name: jack
✔ Testnet

🎉 New account created with address 0x6929a7d1e1908188 and name jack on Testnet network.

Here’s a summary of all the actions that were taken:
- Added the new account to flow.json.
- Saved the private key to jack.pkey.
- Added jack.pkey to .gitignore.

The created flow.json` will look like

  "jack": {
"address": "6929a7d1e1908188",
"key": {
"type": "file",
"location": "jack.pkey"
}
}

As you notice the private is stored in a file named accountname.pkey

Deploy & Updates ContractsUsing Flow CLI

Deploy Contracts

You can deploy using the following command

flow accounts add-contract <filename> [<argument> <argument>...] [flags]

It can be deployed into testnet referring

flow accounts add-contract ./FungibleToken.cdc --signer alice --network testnet

Updates Contracts

You can update an existing contract deployed to a Flow account using the following command

flow accounts update-contract <filename> [<argument> <argument>...] [flags]

You can update the same in testnet by adding the argument —network testnet

flow accounts update-contract ./FungibleToken.cdc --signer alice --network testnet

This blog takes you through the basics Flow CLI like creating flow.json , its various parts, creating, deploying, and updating contracts in Flow blockchain. I hope this helps you in getting started with Flow CLI.

Reference:

--

--