Loading...
Welcome 👋 to this tutorial on building a Contract Deployer on VeChain.
If you're a business owner or developer looking to leverage the power of blockchain technology for your supply chain or other business processes, then VeChain is definitely worth considering. One of its key features is the ability to support smart contracts, which are contracts that can be automatically executed when certain conditions are met.
In this tutorial, we'll build a simple contract deployer using transactions on the VeChain blockchain. We'll cover everything else you need to know to get your smart contract up and running. By the end of this tutorial, you'll have a solid understanding of how to deploy smart contracts on VeChain.
So let's dive in!
There are two ways to deploy your smart contract as always, and both have their own unique advantages :
1. Deploying with Wallet - This method allows you to use your own wallet to sign and send the deployment transaction. This method can only be used on Front-end and is more convenient most of the times if you want users to make the transaction.
2. Deploying in Private Key - Alternatively, you can use a private key to sign and send the deployment transaction, which gives you more flexibility and control. This method can be used in Frontend and Backend both and it is specifically very useful for deploying contracts on behalf of the user, but you can’t expose your private key on Frontend 🤫 so this is mostly used in Backend i.e. on the server side.
We will look at Deploying with Wallet method in this tutorial.
First, let’s gather the tools required for building our program,
1. Connex library - It is a JavaScript library that provides a high-level API for interacting with the VeChain blockchain, You can think of it like web3.js for VeChain. You can check out on Vechain website here for connex library, you will find both command to install and the CDN of the library.
2. VeWorld wallet - It is a multi-functional cryptocurrency wallet that provides users with a secure and easy-to-use interface for managing their VeChain assets, in short, you use this wallet if you are interacting with VeChain just like the way you use Metamask for Ethereum. You can get the wallet extension from Chrome store here but better go to VeWorld official website here.
Once you have this, Let’s build our program step by step !
We will be initializing Connex library so that it connects with the VeChain Network and creates an instance of itself which we can use to execute different operations.
As discussed, we will be deploying our smart contract from Frontend where user will use their wallet to sign the deployment transaction and deploy the smart contract on VeChain.
So, let’s build a sample web page and add connex.js file, here we will be using a CDN of Connex library instead of importing the library, it's very easy that way
<!DOCTYPE html>
<html lang="en">
<head>
<title>Deploy Contract</title>
<script src="https://unpkg.com/@vechain/connex@2"></script> <!-- 1. ADDING CONNEX LIBRARY CONNEXT -->
</head>
<body>
<!-- Webpage body goes here -->
</body>
<script>
const connex = new Connex({
node: 'https://vethor-node-test.vechaindev.com',
network: 'test'
}) // 2. INITIALISING CONNEX
</script>
</html>
As you can see we are connecting with VeChain’s Test network and now our connex variable possesses all the powers to interact with VeChain test network 🦸🏻♂️ .
If you want to connect to Mainnet you can use following code -
const connex = new Connex({
node: 'https://vethor-node.vechain.com',
network: 'main'
})
What do we need to deploy a contract? 🤔 (hmmm….)
You must have noticed whenever we send a transaction on Blockchain, we send DATA parameter with it which looks something like,
0x60806040526101a6806100136000396000f3fe6080604052600436106100385760003560e01c80632e64cec1146100445780636057361d1461006f578063c1cfb99a146100985761003f565b3661003f57005b600080fd5b34801561005057600080fd5b506100596100c3565b60405161006691906100f7565b60405180910390f35b34801561007b57600080fd5b5061009660048036038101906100919190610143565b6100cc565b005b3480156100a457600080fd5b506100ad6100d6565b6040516100ba91906100f7565b60405180910390f35b60008054905090565b8060008190555050565b600047905090565b6000819050919050565b6100f1816100de565b82525050565b600060208201905061010c60008301846100e8565b92915050565b600080fd5b610120816100de565b811461012b57600080fd5b50565b60008135905061013d81610117565b92915050565b60006020828403121561015957610158610112565b5b60006101678482850161012e565b9150509291505056fea264697066735822122086b9ed79a828dc13c73c7aa8535ab20aee71147dcc2cc3106ef8cbb29de3b84364736f6c63430008110033
Aha ! 💡
BYTECODE 😆
Yes, you just need the BYTECODE of the contract you want to deploy. This bytecode is essentially the code that contains all the logic and rules, in machine-readable form, that define how your application will behave on the Blockchain. To put it simply, this code is what blockchain understands, whether it's a transaction or a deploy code, or a contract interaction everything you have to do, you need to convert it into Bytecode and send it on the blockchain for execution.
You can generate bytecode from anywhere you like, may it be remix or ganache or hardhat, just get the bytecode of your contract over here !
Following is the deployment code,
const bytecode = '0x608060405260610013600...6055606008110033';
const resp = await connex.vendor.sign('tx', [{ value: 0, data: bytecode, to: null }])
.comment('Deploy contract')
.request()
Let’s decipher the code one by one 👨🏻🔬🔎
{ value: 0, data: "0x6098...00331", to: null }
It's also called “Clause” in Vechain/Clause terms.
As we are deploying a contract, to must be null or 0 and we are not sending any VET tokens while deploying, value must also be 0.
5. comment() - You can add any comment here, it will be shown in your wallet at the time of signing the transaction
6. request() - There are two types of interaction with any blockchain one is Transaction and the other is Call. .request() makes the Transaction type of interaction. To put it simply it is a very important function that says you will be writing something on Blockchain, it is a must function you have to use to send a transaction on the blockchain.
Let’s put these instructions nicely in a function, let's call it the deploy_contract function. This is how the script part will look like →
async function deploy_contract() {
const bytecode = '0x608060405260610013600...6055606008110033';
const resp = await connex.vendor
.sign('tx', [{ value: 0, data: bytecode, to: null }])
.comment('Deploy contract')
.request()
console.log('Result --> \n', resp) // Here you will get transaction hash
}
Congratulations !! 🥳 You have successfully built a VeChain deployer.
Following is the final webpage, I just added a Button, Input, and Div for showing the result of the deployed transaction, this is very easy to understand.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Vechain Deployer</title>
<script src="https://unpkg.com/@vechain/connex@2"></script>
</head>
<style>
#_bytecode {
width: 500px;
height: 300px;
margin: 30px auto 50px auto;
display: block;
padding: 10px;
}
#deploy_contract {
width: 520px;
height: 50px;
margin: 20px auto 20px auto;
display: block;
padding: 10px;
font-size: 20px;
font-weight: 600;
cursor: pointer;
}
</style>
<body>
<h2 style="text-align: center;margin-top: 50px; font-size: 30px;"> Vechain Contract Deployer </h2>
<textarea id="_bytecode" placeholder="0x (Dont Forget to put 0x before bytecode if isnt present) : 0x608065b.....110033"></textarea>
<button id="deploy_contract" onclick="deploy_contract()">Deploy</button>
<div> Transaction ID : <span id="res" style="font-weight: 600;"></span></div>
</body>
<script>
const connex = new Connex({
node: 'https://vethor-node-test.vechaindev.com',
network: 'test'
})
async function deploy_contract() {
const bytecode = document.getElementById("_bytecode").value;
const resp = await connex.vendor
.sign('tx', [{ value: 0, data: bytecode, to: null }])
.comment('Deploy contract')
.request()
console.log('Result --> \n', resp) // Here you will get transaction hash
document.getElementById("res").innerHTML = resp.txid
}
</script>
</html>
I have uploaded the complete web application with server setup on GitHub, You can clone the project from here and follow these steps -
npm install
commandnode index.js
command.With this you will have smart contract deployer successfully running on port :3000. You have to input Bytecode in textarea and press deploy to deploy your contract. Now you can deploy your smart contract on VeChain using your own contract deployer, How cool is that ! 😎
Platform also has a complete certification course to help you start building on Vechain, checkout here -> Introduction to VeChain
You will find multiple smartbooks related to VeChain and others on DApp World itself (smartbooks), If you are interested in sharing your awesome knowledge and expertise about blockchain and web3 by writing tutorials and articles, join Web3 Authors ✍ on DApp World, Apply here 👈