How to Deploy a Smart Contract with GetBlock

Dive straight into our freshly brewed guide from the devs' desk. Your dream of deploying a smart contract on Ontology is about to become a reality with these easy, step-by-step instructions.

1. Get Ready

Set up a Development Environment:

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, 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.

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!

Last updated