Build your own Dapp based on QtumX

Click here to view original web page at www.programmersought.com


background

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.

Prerequisites

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

  1. 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).
  2. 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:
  3. We provide a demo to vote Ballot, the source code is as follows:

  4. 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:
  5. bytecode:
  1. 608060405234801561001057600080fd5b50604051602080610487833981016040908152905160008054600160a060020a0319163317808255600160a060020a03168152600160208190529290209190915560ff8116610060600282610067565b50506100b1565b81548183558181111561008b5760008381526020902061008b918101908301610090565b505050565b6100ae91905b808211156100aa5760008155600101610096565b5090565b90565b6103c7806100c06000396000f3006080604052600436106100615763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416635c19a95c8114610066578063609ff1bd146100895780639e7b8d61146100b4578063b3f98adc146100d5575b600080fd5b34801561007257600080fd5b50610087600160a060020a03600435166100f0565b005b34801561009557600080fd5b5061009e610250565b6040805160ff9092168252519081900360200190f35b3480156100c057600080fd5b50610087600160a060020a03600435166102bb565b3480156100e157600080fd5b5061008760ff6004351661031b565b33600090815260016020819052604082209081015490919060ff16156101155761024b565b5b600160a060020a0383811660009081526001602081905260409091200154620100009004161580159061016d5750600160a060020a0383811660009081526001602081905260409091200154620100009004163314155b1561019f57600160a060020a039283166000908152600160208190526040909120015462010000900490921691610116565b600160a060020a0383163314156101b55761024b565b506001818101805460ff1916821775ffffffffffffffffffffffffffffffffffffffff0000191662010000600160a060020a0386169081029190911790915560009081526020829052604090209081015460ff16156102435781546001820154600280549091610100900460ff1690811061022c57fe5b60009182526020909120018054909101905561024b565b815481540181555b505050565b600080805b60025460ff821610156102b6578160028260ff1681548110151561027557fe5b906000526020600020016000015411156102ae576002805460ff831690811061029a57fe5b906000526020600020016000015491508092505b600101610255565b505090565b600054600160a060020a0316331415806102f15750600160a060020a0381166000908152600160208190526040909120015460ff165b156102fb57610318565b600160a060020a0381166000908152600160208190526040909120555b50565b3360009081526001602081905260409091209081015460ff1680610344575060025460ff831610155b1561034e57610397565b6001818101805460ff191690911761ff00191661010060ff85169081029190911790915581546002805491929091811061038457fe5b6000918252602090912001805490910190555b50505600a165627a7a723058200864fb02ff362bbc06e105162a9fcbfe289feff2354dbe84cf794f1fb2ec38d60029
  1. 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.
  1. 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

  1. 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.)
  2. 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)
  1. 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.
  1. We give a brief introduction to several functions:
  2. 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;
  3. 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;
  4. 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;
  5. 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.

Precautions