HRC20 Tokens
Creating and using HRC20 tokens
HRC20 is the equivalent of ERC20.
In this chapter, we will explore the details surrounding the deployment and use of HRC20 tokens in the realm of HYDRA.
HRC20 is the implementation of a standard API for tokens within smart contracts on HYDRA Methods and Events:
Creating HRC20 Tokens
You can use this sample Token code to create your own HRC20 token on Hydra. For this example, we will use Hydra testnet, first getting some testnet HYDRA.
After installing the Hydra Core wallet from https://github.com/Hydra-Chain/node/releases, get the wallet receiving address by selecting Receive tab, select and copy your wallet receiving address TuPacA47YxXmr2Y8LWUr2XRQkGcobTSEER
.
We'll need some HYDRA to pay for the smart contract transaction fees and if using the Testnet we can get some test Hydra from the Hydra Faucet. Paste the receiving address into the Faucet address field and press the Submit button.
Next, copy the token code from Qtum's Token project. In the QRC20Token.sol file you can change name
, symbol
, and totalSupply
to your preferences. For this example, we will edit the code to name the token "HRC TEST 420", the symbol "HRC420" and have a total supply of 1,000,000,000 which is entered as "10 9". We will leave decimals
set for 8, to give 8 decimal places for each token, so for example, you could send a 1.12345678 token amount.
After editing the Solidity file, save it locally or just copy to paste it into Remix.
Next, we will use Remix to compile the Solidity code into bytecode. In a browser, go to Remix at http://remix.ethereum.org/ and select the SOLIDITY Environment and compiler version 0.4.26
Click the file button to create a new file.
Enter the file name "HRC20Token.sol" and press the 'Enter' key to create the new file.
Paste in the source code from the QRC20Token.sol file.
Here you can see the we have edited the code to name the token "HRC TEST 420", with the symbol "HRC420", and a supply of 1 billion.
Also, create a new file, enter the file name "SafeMath.sol" and paste in the code for SafeMath.sol.
Select the compiler button on the left side.
The compiler tab is shown below. You can set the Solidity version to 0.4.26. At the top, click the tab for HRC20Token.sol to select that file to compile, and click the blue Compile HRC20Token.sol button to compile the source code into bytecode.
Ignore the warnings.
Click on the Bytecode button to copy the bytecode.
Paste the copied bytecode into a text editor, and select just the numeric characters for the object code (shown here highlighted in blue). The object code starts with "60806" and ends with "0029" (not shown in this image).
You can also copy the ABI code and paste it into a text editor for later usage for advanced functions such as calling the contract:
In the wallet, go to Smart Contracts - Create Contract and paste the copied object code into the "Bytecode" field.
At the bottom of the Create Contract form, click the drop-down on "Sender Address" and select TuPacA47YxXmr2Y8LWUr2XRQkGcobTSEER. This sets the address to be used by the contract. Leave the gas set at 25000000 unless you know how to safely change it. Click the Create Contract button and Yes to send the transaction.
The wallet will confirm the transaction
on the "Result 1" tab. Copy the Contract Address 85e0779eb17f636d3d49bfa5841338da01595464.
The wallet Transactions page will show the transactions so far.
Adding Tokens
Smart contract transactions are sent to the smart contract address, not the wallet address, and for the wallet to see or make smart contract transactions we must inform the wallet, in this case by "adding" the token. To see the new token in the wallet, select HRC Tokens and the "+" button to the right of the Add new token.
Paste the contract address 85e0779eb17f636d3d49bfa5841338da01595464 into the "Contract Address" field, and rest of the form will be autofilled.
It may take a few minutes for the token to populate. We recommend waiting until the contract creation transaction is confirmed before adding the token.
At the bottom of the form click the drop-down arrow to the right of the Token address field and select TuPacA47YxXmr2Y8LWUr2XRQkGcobTSEER and Confirm. If the wallet is using multiple addresses, chose the correct Hydra address that was used to create the token.
You may see the Log events prompt "Enable log events from the option menu to receive token transactions". We will do this step below.
Sending Tokens
Fill in the fields for "PayTo" and "Amount". The "Description" field is optional.
Click Send and Yes to complete the transaction.
Wallet Transactions will now show the contract send transaction. Right-click on the transaction to see the details including the transaction ID.
Enable Log Events
We can follow up now on the previous prompt to enable log events. For the wallet to fully display token transactions it needs to have log events enabled. Select Settings - Options and click to select Enable log events. You must restart the wallet and rescan. The prompt will show "Client restart required to activate changes." Select OK then Yes. The wallet will exit, then restart the wallet.
When the wallet restarts, click OK to rebuild the block database.
The wallet status will show "Reindexing blocks on disk..." and "Syncing headers" for several minutes or several tens of minutes, depending on your computer.
Multiple Tokens in Wallet
HRC20 token balances are managed by the smart contract for individual Hydra addresses, even if these Hydra addresses are for the same wallet.
Continuing the example above, we sent 1000 tokens to the wallet on a new receiving address TvvMyFarb2BPFHHRFxx2rq9CovyfCQo5Rn. To show this new transaction of HRC420 tokens in the wallet we must complete the Add Token step for this new address.
Now the tokens for each tied address are shown separately and each row can be used separately for send and receive operations.
Contract functions
To access the contract directly we can copy the ABI code that we had pasted earlier from the solidity compiler. Make sure you select all the code between the 2 brackets [ ]
Click on Smart Contracts above and paste the code in the Interface ABI section then paste the Contract address from earlier. Then click the button 'Save contract info':
Give the token contract a label so you can identify it later, Then click OK to save.
You will now be able to retrieve this contract at any time in order to perform more advanced contract functions such as sending commands to a contract or calling a contract for information.
Last updated