> For the complete documentation index, see [llms.txt](https://docs.ont.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ont.io/guides-and-tutorials/development-guides/smart-contract-dev/evm-contract/how-to-deploy-a-smart-contract-with-getblock.md).

# How to Deploy a Smart Contract with GetBlock

## 1. Get Ready

Set up a Development Environment:

* [Solidity programming language](https://soliditylang.org/).
* Development tools like \[[Truffle](https://trufflesuite.com/)] or \[[Remix IDE](https://remix.ethereum.org/#lang=en\&optimize=false\&runs=200\&evmVersion=null\&version=soljson-v0.8.18+commit.87f61d96.js)].
* [Node.js](https://nodejs.org/en) on your computer.
* [Web3.js](https://web3js.readthedocs.io/en/v1.10.0/) library.
* The Ontology RPC URL endpoint by [GetBlock](https://getblock.io/).

## 2. Create Your Smart Contract (Example)

```
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;

contract FunctionalityContract {
    string message;
    function setMessage(string memory newMessage) public {
        message = newMessage;
    }
    function getMessage() public view returns (string memory) {
        return message;
    }
}
```

Save this as *Test.sol* or a name of your choice with *.sol* extension.

## 3. Compile Your Smart Contract

`truffle compile Test.sol`

You'll receive an output similar to:

```
Compiling your contracts...

> Compiling ./Test.sol
> Artifacts written to /Users/hannask/explorer/ontologi/build/contracts
> Compiled successfully using:
— solc: 0.8.19+commit.7dd6d4e4.Emscripten.clang
```

This generates two crucial files: the bytecode (`.bin`) and ABI (`.abi`). The bytecode is the version ready for the Ontology blockchain, while the ABI describes how you can interact with the contract.

## 4. Connect to Ontology's RPC Node with Web3.js

You can achieve this connection by using the following script. Remember to replace `<Ontology RPC URL>` with the URL you got from GetBlock.

In order to get one, register on [*GetBlock.io*](https://getblock.io/), choose Ontology in the protocols, testnet as a network and create your endpoint, then copy it to use for your purposes. If you want to deploy contract on mainnet, then choose mainnet as a network.

<figure><img src="/files/10tR17mnX1nB7K8OLSfh" alt=""><figcaption></figcaption></figure>

```
const Web3 = require('web3');
const web3 = new Web3('https://ont.getblock.io/<API-KEY>/testnet/web3/');
```

## 5. Deploy Your Smart Contract with Web3.js

The following script assists in deploying the smart contract:

```
const fs = require('fs');

// Read ABI and bytecode files
const abi = JSON.parse(fs.readFileSync('<path-to-abi-file>', 'utf8'));
const bytecode = fs.readFileSync('<path-to-bytecode-file>', 'utf8');

// Create a new Contract object using ABI
const myContract = new web3.eth.Contract(abi);

// Create a transaction object using the bytecode
const deployTransaction = myContract.deploy({
    data: '0x' + bytecode,
    arguments: [arg1, arg2, ...]
});

// Send the transaction to Ontology
deployTransaction.send({
    from: '<sender-address>',
    gas: <gas-limit>,
    gasPrice: <gas-price>
})
.on('receipt', (receipt) => {
    console.log('Contract deployed at address:', receipt.contractAddress);
});
```

Don't forget to fill in the placeholders (`<path-to-abi-file>`, `<path-to-bytecode-file>`, `<sender-address>`, `<gas-limit>`, and `<gas-price>`) with the corresponding details.

## 6. Test Your Smart Contract

Once your smart contract is deployed, it's crucial to test its functionality to ensure it behaves as expected.

```
// Set a message
myContract.methods.setMessage("Hello, Ontology!").send({ from: '<sender-address>' }).then(receipt => {
    console.log('Transaction receipt:', receipt);
});

// Get the message
myContract.methods.getMessage().call().then(result => {
    console.log('Stored message is:', result);
});
```

This test involves setting a new message in the contract and retrieving it. You should see "Hello, Ontology!" printed in your console.

## 7. Celebrate Your Success! 🎉

You've now written, deployed, and tested a smart contract on the Ontology network using the GetBlock RPC node provider. Cheers to your contribution to the decentralized world!

<br>
