# Deploying the Smart Contract

## Installation and approval of Chaincode

Smart Contracts are officially called as chaincode in Fabric. A peer node can *install* a smart contract, meaning the smart contract is physically installed to the peer node. &#x20;

After installation, the chaincode should be approved by all organizations in the channel. Once a smart contract is approved and committed, the smart contract is now active.&#x20;

The functions in the smart contract can be invoked as transactions. However, only those nodes who have installed the same smart contract can invoke functions (transactions).

### Create a Fabric smart contract package using Fabric command "peer"&#x20;

In terminal 1,

```bash
peer lifecycle chaincode package ~/${USER}-chaincode.gz \
--path ~/fabric_lab_code \
--lang golang \
--label ${USER}-chaincode
```

### Install your smart contract package on each peer

In terminal 1,

```bash
# Install the packaged smart contract
peer lifecycle chaincode install ~/${USER}-chaincode.gz

# Verify if the smart contract is installed
peer lifecycle chaincode queryinstalled
```

You should see the following output,

```bash
Installed chaincodes on peer:
Package ID: cyliu-chaincode:146fb9941bc5ea850274ed72c23ce43451a4bc5ad7213b3d63db8419ef39e481, Label: cyliu-chaincode
```

Repeat the command in terminal 2.

```bash
# Install the packaged smart contract
peer lifecycle chaincode install ~/${USER}-chaincode.gz

# Verify if the smart contract is installed
peer lifecycle chaincode queryinstalled
```

And also terminal 3,

```bash
# Install the packaged smart contract
peer lifecycle chaincode install ~/${USER}-chaincode.gz

# Verify if the smart contract is installed
peer lifecycle chaincode queryinstalled
```

### Approve the installed smart contract on each peer

In terminal 1,

```bash
#Grep the package id of your smart contract
PACKAGEID=$(peer lifecycle chaincode queryinstalled | grep ${USER}-chaincode | sed -n 's/.*Package ID: \(.*\),.*/\1/p')

#Approve your smart contract
peer lifecycle chaincode approveformyorg -o node-1:7050 \
--name ${USER}-chaincode --channelID ${USER}-channel \
--package-id ${PACKAGEID} --version 0 --sequence 1
```

Repeat in terminal 2,

```bash
#Grep the package id of your smart contract
PACKAGEID=$(peer lifecycle chaincode queryinstalled | grep ${USER}-chaincode | sed -n 's/.*Package ID: \(.*\),.*/\1/p')

#Approve your smart contract
peer lifecycle chaincode approveformyorg -o node-1:7050 \
--name ${USER}-chaincode --channelID ${USER}-channel \
--package-id ${PACKAGEID} --version 0 --sequence 1
```

Also in terminal 3

```bash
#Grep the package id of your smart contract
PACKAGEID=$(peer lifecycle chaincode queryinstalled | grep ${USER}-chaincode | sed -n 's/.*Package ID: \(.*\),.*/\1/p')

#Approve your smart contract
peer lifecycle chaincode approveformyorg -o node-1:7050 \
--name ${USER}-chaincode --channelID ${USER}-channel \
--package-id ${PACKAGEID} --version 0 --sequence 1
```

To verify if all peers approve your smart contract. Execute the following command in terminal 1.

```bash
peer lifecycle chaincode checkcommitreadiness -o node-1:7050 \
--name ${USER}-chaincode --channelID  ${USER}-channel --version 0 \
--sequence 1
```

You should see the following output

```
Chaincode definition for chaincode 'cyliu-chaincode', version '0', sequence '1' on channel 'cyliu-channel' approval status by org:
greatinsurance: true
police: true
smartinsurance: true
```

If any organization show `false`, wait a few seconds and execute the command again until all organization show `true`

Execute the following command to commit the approved smart contract

```bash
peer lifecycle chaincode commit -o node-1:7050 \
--name ${USER}-chaincode --channelID  ${USER}-channel \
--version 0 --sequence 1 \
--peerAddresses node-1:7051 \
--peerAddresses node-2:7051 \
--peerAddresses node-3:7051

#Verify the commited smart contract
peer lifecycle chaincode querycommitted \
--channelID ${USER}-channel --name ${USER}-chaincode
```

You should see the following output

```
Committed chaincode definition for chaincode 'cyliu-chaincode' on channel 'cyliu-channel':
Version: 0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [greatinsurance: true, police: true, smartinsurance: true]
```

Now your smart contract is ready.

###

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eric-lo.gitbook.io/hyperledger-fabric/deploying-the-smart-contract.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
