Integrate Content Staking

Content Staking

This guide will go over the function calls that should be performed to interact with the Content Staking feature.

Prereq

In order to interact with the Gateway be sure to grab the latest ABI from the github release page. You need to add both the Gateway ABI & the Content Staking ABI.

Every release of the Huddln Protocol will need dapp creators to update their ABI.

https://github.com/Huddln/contracts/releases

Add the Gateway & Content Staking Contracts to your project

let GatewayContract = new web3.eth.Contract(ENV.GATEWAY_ABI, ENV.GATEWAY_CONTRACT_ADDRESS);
let StakingContract = new web3.eth.Contract(ENV.STAKING_ABI, ENV.STAKING_CONTRACT_ADDRESS);

Replace all ENV variables with the addresses & abi of the current release version available here -> https://jgonzalez0886.gitbook.io/huddln/networkdetails and here -> https://github.com/Huddln/contracts/releases

Calls

  1. Create: Creator creates a post using content_createPost(bytes32 _postId, uint256 _postLength)

    the postId passed into the function should be 32 Bytes long and unique. It is recommended to use the SHA256 digest value of the content since this is exactly 32 Bytes. The staking period parameter should be a hex value of 0,1,2 -> '0x0', '0x1', ' 0x03'. The postID hash should be stored in your dapp for other to reference.

Example: content_createPost
let stakingPeriodEnum = '0x1';
let postID_Hash = '0x39E5F1270C7874DB7FA9ECC78EAC251BAA4E5D748D1A4F13AA3208E1B0C9C5D0';
GatewayContract.methods.content_createPost(postID_Hash, stakingPeriodEnum).send({
data: web3.utils.toHex('CreatePost'),
from: checksumAddress,
gas: ENV.ETHEREUM_CONSTANTS.CREATE_POST_GAS_LIMIT,
gasPrice: gasPrice
})
.once('transactionHash', () => { onTransactionHash() })
.on(('error'), (err) => { onError(err) })
.catch((err) => { });

2. Staking: Any potential stakers should call the content_placeStake(bytes32 _postId). This function requires that the user send their staking amount with the call. On your font-end you should let the user know there is a 10% staking fee that goes directly to the owner and is taken out of the value you send. Also there is a 10% earnings fee that is applied when the stake is closed. Users can only place a stake when in the Staking Period.

Example: content_placeStake
let postID_Hash = '0x39E5F1270C7874DB7FA9ECC78EAC251BAA4E5D748D1A4F13AA3208E1B0C9C5D0';
let amount = 250000000000000000//value in smallest wei
GatewayContract.methods.content_placeStake(postID_Hash).send({
data: web3.utils.toHex('PlaceStake'),
from: checksumAddress,
gas: ENV.GASLIMIT,
gasPrice: gasPrice,
value: amount,
})
.once('transactionHash', () => { onTransactionHash() })
.on(('error'), (err) => { onError(err) })
.catch((err) => { }

3. Tipping: You should not allow any tipping until the tippingBlockStart has passed. Check this by looking up the this value and comparing it with the blockchains block number. If you call the tipping function before this time the call will fail.

Example: content_tip
let postID_Hash = '0x39E5F1270C7874DB7FA9ECC78EAC251BAA4E5D748D1A4F13AA3208E1B0C9C5D0';
let amount = 150000000000000000//value in smallest wei
GatewayContract.methods.content_tip(postID_Hash).send({
data: web3.utils.toHex('Tip'),
from: checksumAddress,
gas: ENV.GASLIMIT,
gasPrice: gasPrice,
value: amount,
})
.once('transactionHash', () => { onTransactionHash() })
.on(('error'), (err) => { onError(err) })
.catch((err) => { }

4. A. Creator Payout: The creator of the content can call the content_claimPostEarnings(bytes32 _postId) function after the tippingBlockStart has passed & can call it as many times as necessary. The creator needs to call this function in order to receive their earnings from the stakeFeePool. This Fee pool accumulated funds as stakers place their stakes on the post. Seeing as a staker can only place stakes in the Staking Round and closing a stake is final, this would mean that the creator would only need to call this function once. However the creator actually should have the ability to call this function as many times as they need to, to handle the case of when people tip the post and all the stakers have left. This case can be seen when looking at a post object on chain, if the stakesOpenCount of the object is equal to 0 and there are funds in the tipPool, the creator can call the content_claimPostEarnings(bytes32 _postId) and they will also be sent the tipps seeing as the creator is the sole beneficiary of these earnings.

Example: claim earnings
let postID_Hash = '0x39E5F1270C7874DB7FA9ECC78EAC251BAA4E5D748D1A4F13AA3208E1B0C9C5D0';
GatewayContract.methods.content_claimPostEarnings(postID_Hash).send({
data: web3.utils.toHex('ClaimEarnings'),
from: checksumAddress,
gas: ENV.GASLIMIT,
gasPrice: gasPrice,
})
.once('transactionHash', () => { onTransactionHash() })
.on(('error'), (err) => { onError(err) })
.catch((err) => { }

4.B. Staker Payout: Any time after the Staking Round has passed the stakers can close out their stake and claim their earnings. It should be noted on the the front-end that a staker can only do this once and it's final. Also it should be noted that they will pay a 10% earnings fee. After this call their stake(minus stakingFee) will be returned to them plus their earnings(minus earningsFee).

Example: claim earnings staker
let postID_Hash = '0x39E5F1270C7874DB7FA9ECC78EAC251BAA4E5D748D1A4F13AA3208E1B0C9C5D0';
GatewayContract.methods.content_closeStake(postID_Hash).send({
data: web3.utils.toHex('CloseStake'),
from: checksumAddress,
gas: ENV.GASLIMIT,
gasPrice: gasPrice,
})
.once('transactionHash', () => { onTransactionHash() })
.on(('error'), (err) => { onError(err) })
.catch((err) => { }