Qcore contracts deployment and testing

Qcore Protocol
7 min readOct 1, 2020

This article is to proof-concept for the Qcore protocol contracts.The actual user experience can be improved in the future by integrating in web applications assuming MetaMask-like Chrome Extension is available for Qtum.

800 QCORE tokens mind in this demo

Now we will work through how we create and deploy Qcore contracts and test farming QCORE! And we need a couple of tools:

  1. Qtum Desktop Wallet for contract creation and interaction
  2. Remix for compiling contracts and get abi and bytecode

👛 Qtum Wallet

use terminal to start QTUM Desktop Wallet in TestMode and wait all blocks are synced.

$ /Applications/Qtum-Qt.app/Contents/MacOS/Qtum-Qt -testnet

In Smart Contracts section
Create: Create new smart contract
Send to: Call write function/interface on contract
Call: Call read function/interface on contract

⛏️ Compiling contracts

Use https://remix.ethereum.org/ to compile our contracts, take notes of abi and bytecode, we need them later in Wallet for contract deployment and interaction. You can also find compiled abi, bytecode and contract details in our github repo https://github.com/QcoreProtocolOrg/qcoreprotocol deployment folder.

🚀 Creating Contracts

Use wallet to create contracts

QETH https://testnet.qtum.info/contract/6b8f4221e652c8f93bccc24f7ee52ad5f6b63ce0

QCore

https://testnet.qtum.info/contract/9f15f29e7c3f32161c289546d4665b3f8973ae39

QCorePool

https://testnet.qtum.info/contract/d22c2ffbea5e2501362b4be4ff86b8935afea31f

In QCorePool contract constructor, we need to pass a couple of paramters

rewardToken: the contract address of QCORE token, we will mine QCORE as rewards for users who stake QETH token.
QCorePerBlock: As normal speed, we are generating 10 QCORE (1e18 decimal) per block as rewards and distributed to all the pools currently active on the contracts
startBlock: genesis period start block, we just set it to 0, which means start as soon as contract created.
bonusEndBlock: genesis period end block, we set it to 697798, which is around 2 weeks from contract is created, assuming 2~3 minutes to produce a new block. During genesis period, we want to reward 10x QCore tokens, which is 100 QCORE per block!

🙍 Admin Tasks

We have our contracts deployed, which is awesome progress. But we still need to complete a few admin tasks before we can start farming

  1. First we need to get some QETH as a miner, for easy demo purpose, we will use our developer account qQof44vWJW74v4zBk6wZt3qZADVZLmMJ53 as the miner. Let’s mine some QETH to our developer account! On QETH contract, call AddMinter function to add developer account as minter. Then call Mint function to mint 100000000*1e18 to our developer account

Qtum address need to be converted to hex value to call contract, the hex value for our developer account is 4f7bf9690ba8ced59d1c5ab8ed15fc9791d421ea You can use wallet Window>Console tool and gethexaddress function to get the hex

Now if we use BalanceOf function to check our developer account, we have 100000000 QETH to play with!

2. On QCORE contract, we need to AddMinter for QCorePool contract, so pool contract can continuously mint QCORE token as rewards

Now QCorePool is a minter on QCORE

3. On QCorePool contract, as admin we need to add QETH as a farmable pool, call add function pass some config parameters

allocPoint: weight system to configure when there are multiple pools, the percentage of each pool to receive QCORE token rewards, for example, when we are generating 10 QCORE token per block, QETH pool is configured as 100, and another QRC20 token pool is added and configured as 50, then QETH pool will receive 10 * (100/(100+50)) = 6.66 QCORE per block.
lpToken: the contract address of added staking token, in this instance the address of QETH token
decimal: the decimal point for added token, this allows QRC20 token with different decimals can be added using the same token. for example, a QRC20 token with 1e18 decimal, and a LP Token with 1e12 like Uniswap LP token, can both be added to the pool using different decimal settings
withUpdate: boolean flag to update the reward pool settings, we will cover more details in later article when we deep dive into contract design.

Now we should be able to see QETH pool info by calling PoolInfo[0]

poolinfo is zero-index array

We have completed all the admin tasks and the pool is ready for farming!!

🌾 Testing Yield Farming!

  1. As a miner, the first thing need to do for yield farming, is to approve the allowance on staking Token contract

On QETH contract, call approve function and pass QCorePool address

This step is to make sure the owner (miner) allow the spender (QCorePool) to safeTransfer maximum of 1million QETH for staking, but in practice, even we believe in QCore project 😎, we will not use that much for testing, generally speaking, farming project would prefer to set allowance to a large number so miners do not have to approve additional spending every time! This is only for allowance, not the actual QETH token transfer.

Now we will see allowance for QETH to stake on QCorePool

2. Now let’s stake 100 QETH (1e18) on QCorePool by selecting pool 0 (QETH)

check QETH balance on QETH, yes! we have taken 100 QETH from dev account to stake in QCorePool

check UserInfo on QCorePool contract by pool id 0, it displays we have staked 100 QETH!

Now call pendingQCore function to check QCore earnings.

we have earned 400 QCORE Token so far yeah! Remember we are the only miner staking in this QETH pool at the moment, and the genesis period reward is 100 QCORE per block, so we have staked for 4 blocks ( about 10 minutes) and 400 is our earning! When there are more miners joined, reward will be distributed on a percentage of (staking amount)/(total staking amount on the pool) fair enough yeah?

3. Now it would be nice if we can withdraw our QCORE earnings as well as our QETH token to our own address. Call withdraw function to withdraw 100 QETH it automatically transfer QETH back to our account, as well as the QCORE earning!

Check userinfo again, we don’t have staked token on QCorePool anymore, correct!

Now go to QETH contract to check our balance, the 100 QETH is now back to our account.

Also go to QCore contract to check our balance, we have 800 QCORE, as we write this documentation and checking contracts, we have mined 4 more blocks reward, that is 400 additional QCORE tokens, perfect!

What if we just want to harvest the QCORE Token to our account without unstaking our QETH token from the pool but continue to win rewards? We can simply use deposit function on QCorePool contract, but pass 0 to staking amount, the contract is smart enough to know we only want to harvest QCORE but not unstaking QETH

🐣 Add QCORE token in your wallet

Now go to QRC20 token section in your wallet, click + Add new token

In the popup window, paste in QCORE Token address 9f15f29e7c3f32161c289546d4665b3f8973ae39

Token Name, Token Symbol and Decimals will automatically pop up as the wallet looks up the token address and retrieve token information, finally select the your wallet account address, in this case, our dev account

qQof44vWJW74v4zBk6wZt3qZADVZLmMJ53

Click confirm, now we should be able to see QCORE tokens in our account, sweet!

ALL DONE!

This is the end of our walk through of QCore Protocol and contracts interaction, in later article, we will deep dive into the QCorePool contract and explain the rational of our scalable contract design.

--

--