After creating your own blockchain on QtumX, you can develop and publish smart contracts to provide blockchain-based applications.Since the built blockchain is a private chain or an alliance chain, smart contract applications do not need to consume tokens on the public chain, which greatly saves development costs.QtumX, like Qtum, supports the Ethereum virtual machine EVM. Therefore, all smart contract codes based on Solidity can be run directly on QtumX.
Install QtumX and publish your own private chain,Private chain building tutorial. This article introduces the deployment and interaction of smart contracts under the qtum-qt client. It is built on the Qtum private chain and requires some simple command line operations. The demo chose solidity's classic voting demo.
Smart contract deployment
- Start qtum-qt to enter the private chain environment, and start miner mining through the setpoaminer command in the Help-Debug window-Console of qtum-qt (be sure to start mining or else new blocks cannot be generated, and smart contracts cannot be recorded and cannot be deployed to private On the chain).
- Write Solidity smart contract code, you can choose online IDERemix. About how to write solidity, here are a few tutorials to share with you to learn:
We provide a demo to vote Ballot, the source code is as follows:
- Convert the source code to the bytecode and ABI required in qtum-qt. Find Compile in the right sidebar of Remix to compile. After the compilation is correct, click Details (select the "object" item in bytecode to copy, only need to enter this item) to view and copy. The following is the compiled bytecode and ABI:
- Deploy smart contracts to private chain through qtum-qt. Select Smart Contracts-Create in the graphical interface, paste the bytecode and ABI to the corresponding location; demo needs to initialize uint8 type _numProposal, fill in when deploying smart contracts; after the initialization parameters, we click the CREATE CONTRACT button in the lower right corner, after successful deployment Result will be returned, where you can see the transaction ID, deployer address SenderAddress and contract deployment address (used to interact with the smart contract) ContractAddress.
- After the deployment is complete, you can view the corresponding block information written in the Transaction List Transactions, and it will cost Gas.
Interact with smart contracts
- After the deployment of the smart contract is completed, we can interact with the smart contract under Send To and Call under Smart Contracts. Since there are no new nodes and users in the private chain environment, we can switch to the main chain, and use the getnewaddress command in the Debug Window to generate several new addresses for simulation. Later, you can add real user addresses (note: the generated address is not The contract needs the incoming hexadecimal. The hexadecimal of the address can be obtained through gethexaddress $address in the Debug Window, and there is no need to add a 0x prefix when entering.)
- In the Call Contract, you can call the relevant parameters returned by the function returned in the smart contract. You can view it by entering the contract address and the ABI corresponding to the parameter you want to view. All calculations of the Call operation are performed off-chain and will not consume Gas. What can be viewed in the demo is the winning outcome of the voting, winningProposal. We have not written any data yet, and the result of the voting returns the default value of 0. ((00) in Function is used to pass Value to the smart contract, which is qtum in the Qtum network, which can be ignored during Call Contract)
- Paste the contract address and ABI in the corresponding location in Send To to call the function of the smart contract. Send to will pass parameters to the smart contract and write data. Calculation and storage are performed on the chain, so Gas will be consumed. At this time, (00) can realize the transfer of qtum to the smart contract. You can also write the destruction address in the smart contract. After the smart contract is destroyed by the owner, the qtum will be returned to the account.
- We give a brief introduction to several functions:
- The giveRightToVote function can only be called by the chairman to implement the issuance of voting rights. The chairman deploys the address of the smart contract, and the weight of the address to obtain the voting right is +1;
- The delegate function is called from an address with voting rights. It can delegate its own voting rights to the specified address and record such as delegate variables. If the voting rights of the delegated address have also been delegated to other addresses, the current voting rights are concentrated in the final Of the principal;
- The vote function is to vote for an address (address mapping is done, the number can be replaced, the incoming is the Proposal number), which is called by the address with the voting right, and after voting, the voting weight is weight -1, the Proposal of the voted voteCount +1;
- winningProposal traverses the candidate proposal array, returns the array index (number) of the proposal with the largest voteCount, and realizes the viewing of the winner.
- During the deployment and interaction of smart contracts, you must not forget to set up mining nodes.
- In Remix, you must select the matching solidity version to compile to avoid compilation errors caused by incompatible versions.
- After the smart contract is deployed, you need to wait for confirmation to interact. You can enter getaccountinfo $CONTRACT in the Debug Window or directly view the smart contract information in the Transaction list.
- Generate a new address in the main chain and convert it to hexadecimal when interacting with a smart contract without adding a 0x prefix.
- The deployment of smart contracts and send to consume gas, if it is a test, you can first transfer to the address that needs to be operated, and reserve the appropriate qtum as Gas.
After creating your own blockchain on QtumX, you can develop and publish smart contracts to provide blockchain-based applications.Since the built blockchain is a private chain or an […]