<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: DingTian</title>
    <description>The latest articles on DEV Community by DingTian (@dingtian).</description>
    <link>https://dev.to/dingtian</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F408532%2F149eb7e7-d79a-4db1-a968-2bbde7645f37.jpeg</url>
      <title>DEV Community: DingTian</title>
      <link>https://dev.to/dingtian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dingtian"/>
    <language>en</language>
    <item>
      <title>Building the staking smart contract with Solana blockchain</title>
      <dc:creator>DingTian</dc:creator>
      <pubDate>Sat, 26 Mar 2022 01:47:15 +0000</pubDate>
      <link>https://dev.to/dingtian/building-the-staking-smart-contract-with-solana-blockchain-20i0</link>
      <guid>https://dev.to/dingtian/building-the-staking-smart-contract-with-solana-blockchain-20i0</guid>
      <description>&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Solana is a decentralized blockchain platform created by Anatoly Yakavenko.Solana has a block time of 400 ms which is freakingly fast because of POH mechanism. I am not going to write the detailed information of POH in solana blockchain. If you want to get more information, I recommend you to go through a whitepaper of solana.In this article , I am going to deal the basic Xtoken staking smart contract programme in solana blockchain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Rust programming&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Solana Tool Suit (Solana CLI)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Anchor framework&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You need to install above prerequisites on your pc before we build the staking smart contract programming.Those needs to program for the task.&lt;br&gt;
Rust is a very powerful general-purpose programming language.&lt;br&gt;
Solana Tool Suit — This includes the Solana CLI.&lt;br&gt;
Anchor is a framework for developing Solana smart contract that contains several developer tools. So basically, the Anchor is a lifesaver and makes it really easy to develop smart contracts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting Started&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To get started, create a new anchor project in your console:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;anchor init basicStaking&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the project structure, you will see the following files and folders.&lt;/p&gt;

&lt;p&gt;program — This is the directory of Solana programs (Smart contracts)&lt;/p&gt;

&lt;p&gt;test — This is where javascript test code lives&lt;/p&gt;

&lt;p&gt;migrations — This is the deploy script&lt;/p&gt;

&lt;p&gt;app — This is where frontend is going to be built&lt;/p&gt;

&lt;p&gt;Let’s look at our lib.rs file in the program directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod basicStaking {
    use super::*;
    pub fn initialize(ctx: Context&amp;lt;Initialize&amp;gt;) -&amp;gt; ProgramResult {                  Ok(())
    }
}
#[derive(Accounts)]
pub struct Initialize {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the most basic program written by CLI. There is a function initialize when invoked do nothing just execute successfully, the initialize struct defines the context of the initialize function.&lt;/p&gt;

&lt;p&gt;The stakeholders who owns xToken stakes some amounts into the staking smart contract. Instead of staking some amounts by the holders, staking contract will provide the reward tokens as the xToken after some periods.&lt;/p&gt;

&lt;p&gt;We assume we created xToken by Solana cli with 100,000,000 totalSupply.If you don’t understand well for this part, you can reference the solana document how to create a custom token on the solana blockchain.&lt;/p&gt;

&lt;p&gt;As well, staking smart contract includes 10,000,000 xTokens to return the rewards to stakeholders.&lt;/p&gt;

&lt;p&gt;Those are allowed by tokenomics before TGE step. TGE — Token Generation Event.&lt;/p&gt;

&lt;p&gt;So let’s build simple staking smart contract now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// pub mod utils;
use {
// crate::utils::*,
anchor_lang::{prelude::*, solana_program::program::{invoke,invoke_signed}},
spl_token::state,
};
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod basic_staking {
use super::*;
pub fn initialize(ctx: Context&amp;lt;Initialize&amp;gt;) -&amp;gt; ProgramResult {
Ok(())
}
pub fn stake(ctx:Context&amp;lt;Stake&amp;gt;,amount: u64) -&amp;gt; ProgramResult{
    let account = &amp;amp;mut ctx.accounts.holder;
    //invoke to transfer token into staking contract
    let token_program = ctx.accounts.token_program.clone();
    let source = ctx.accounts.stake_token.clone();
    let destination = ctx.accounts.stake_pot.clone();
   let authority = ctx.accounts.authority.clone();
  invoke(&amp;amp;spl_token::instruction::transfer(
         token_program.key,
         source.key,
         destination.key,
         authority.key,
         &amp;amp;[],
         amount,
        )?,
        &amp;amp;[source, destination, authority, token_program],
    );
   account.stake_amount += amount;
  let now_ts = Clock::get().unwrap().unix_timestamp;
  account.stake_time = now_ts;
  Ok(())
}
pub fn unstake(ctx:Context&amp;lt;Stake&amp;gt;,amount:u64) -&amp;gt; ProgramResult{
   let account = &amp;amp;mut ctx.accounts.holder;
  //invoke to transfer token into holder address
  account.stake_amount -= amount;
  let now_ts = Clock::get().unwrap().unix_timestamp;
  account.stake_time = now_ts;
  Ok(())
}
pub fn claim(ctx:Context&amp;lt;Stake&amp;gt;) -&amp;gt; ProgramResult{
   let account = &amp;amp;mut ctx.accounts.holder;
  //invoke to transfer reward token into holder
  Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize {}
#[derive(Accounts)]
pub struct SetAuthority&amp;lt;'info&amp;gt;{
#[account(mut, signer)]
authority: AccountInfo&amp;lt;'info&amp;gt;,
#[account(mut)]
new_authority: AccountInfo&amp;lt;'info&amp;gt;,
#[account(mut, owner = spl_token::id())]
stake_pot:AccountInfo&amp;lt;'info&amp;gt;,
#[account(address=spl_token::id())]
token_program:AccountInfo&amp;lt;'info&amp;gt;,
}
#[derive(Accounts)]
pub struct Stake&amp;lt;'info&amp;gt;{
#[account(mut, signer)]
authority: AccountInfo&amp;lt;'info&amp;gt;,
#[account(mut,signer)]
pub holder:Account&amp;lt;'info,StakeAccount&amp;gt;,
#[account(mut, owner=spl_token::id())]
stake_token: AccountInfo&amp;lt;'info&amp;gt;,
#[account(mut, owner=spl_token::id())]
stake_pot: AccountInfo&amp;lt;'info&amp;gt;,
#[account(address=spl_token::id())]
token_program:AccountInfo&amp;lt;'info&amp;gt;,
}
#[account]
pub struct StakeAccount{
pub stake_amount:u64,
pub stake_time:i64,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the StakeAccount struct is our Account contains a count variable that is going to store our stake data.&lt;/p&gt;

&lt;p&gt;The Stake struct is our instruction struct that defines our context for creating an account.&lt;/p&gt;

&lt;p&gt;We declares three functions for stake,unstake,claim to implement the staking logic . Those are instructions to invoke.&lt;/p&gt;

&lt;p&gt;Cool. We are done with this amazing program.&lt;/p&gt;

&lt;p&gt;Now, run the test.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;anchor test&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After the test passes, we can now deploy the program. Be sure that solana-test-validator is running.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;anchor deploy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Please note that you might not use this codebase because of some codebases based on logic of staking. There are bunch of codebases to run the staking contract. I only explained how to configure out the staking contract building in this article. So I recommend you to understand those steps here. Thanks for considering my article today!&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>rust</category>
      <category>solana</category>
      <category>crypto</category>
    </item>
    <item>
      <title>Layer2 Innovations in Defi champions and Polygon Matic Bridge</title>
      <dc:creator>DingTian</dc:creator>
      <pubDate>Tue, 09 Nov 2021 20:49:06 +0000</pubDate>
      <link>https://dev.to/dingtian/layer2-innovations-in-defi-champions-and-polygon-matic-bridge-50oh</link>
      <guid>https://dev.to/dingtian/layer2-innovations-in-defi-champions-and-polygon-matic-bridge-50oh</guid>
      <description>&lt;p&gt;Summary&lt;br&gt;
During the past 30 days, the TVL across Ethereum bridges has topped $7.1 billion.In here , the Ethereum bridges have taken on L2 protocol such as Polygon,Arbitrum,Optimism and Avalanche.Those protocols are very useful to save expensive gas fee and fast transaction speed. We can resolve the many problems on L1 protocol ,which means the Ethereum mainnetwork.&lt;/p&gt;

&lt;p&gt;Definition&lt;br&gt;
What is Layer2 in blockchain? I’m going to describe my thought from this question.&lt;/p&gt;

&lt;p&gt;Layer-2 is a third-party protocol that integrate with an underlying Layer-1 blockchain to increase transactional throughput.&lt;/p&gt;

&lt;p&gt;Let’s see the Ethereum blockchain protocol.&lt;/p&gt;

&lt;p&gt;The Ethereum Layer-2 is the protocal that integrate with the Ethereum Layer-1 and Layer-2 solutions stay on the Ethereum network in the form of smart contract.&lt;/p&gt;

&lt;p&gt;Layer 1 blockchain technology is often referred to as an “On chain” solution because Layer1 is the base consensus layer of the Ethereum protocol. In other opposite, Layer 2 blockchain technology is often referred to as an “Off chain” solution. Above image is showing that each layer can process how many transactions of a block. As well, Layer2 solution resolved high gas fee issue on Layer1 solution. So many projects and platforms are migrating into Layer2 protocols and platforms.&lt;/p&gt;

&lt;p&gt;L2 Innovation&lt;br&gt;
There are various L2 protocols in Defi champions now. In this article, I am going to share about Polygon Matic ,which is powerful to incentive L2 innovation in Defi . The Polygon Matic supports the L1-L2 bridge behind on their professional Layer-2 solution.The polygon brings you a trustless two-way transaction channel between Polygon and Ethereum by introducing the cross-chain bridge with Plasma and Pos security.&lt;/p&gt;

&lt;p&gt;Polygon Matic Bridge&lt;br&gt;
A bridge is basically a set of contracts that help in moving assets from the root chain to the child chain.&lt;/p&gt;

&lt;p&gt;Let’s see in detail how to implement L1- L2 communication correctly on Polygon SDK.&lt;/p&gt;

&lt;p&gt;Steps to use Polygon SDK&lt;/p&gt;

&lt;p&gt;We are going to use polygon SDK to implement our bridge successfully .&lt;/p&gt;

&lt;p&gt;1) Npm install&lt;/p&gt;

&lt;p&gt;Npm install @maticnetwork/maticjs : ^2.0.4&lt;br&gt;
We installed this npm library on our project successfully .&lt;/p&gt;

&lt;p&gt;2) Configuration&lt;/p&gt;

&lt;p&gt;We will configure out the root chain and child chain at following :&lt;/p&gt;

&lt;p&gt;root: {&lt;/p&gt;

&lt;p&gt;RPC: '&lt;a href="https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161"&gt;https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161&lt;/a&gt;',&lt;br&gt;
POSRootChainManager: '0xBbD7cBFA79faee899Eaf900F13C9065bF03B1A74',&lt;br&gt;
DERC721: '0x839AB55B96B5137c35D7e647d4F0A675Ae27dECa',&lt;br&gt;
posERC721Predicate: '0x74D83801586E9D3C4dc45FfCD30B54eA9C88cf9b',&lt;br&gt;
posEtherPredicate: '0xe2B01f3978c03D6DdA5aE36b2f3Ac0d66C54a6D5',&lt;br&gt;
},&lt;br&gt;
child: {&lt;br&gt;
RPC: '&lt;a href="https://rpc-mumbai.maticvigil.com"&gt;https://rpc-mumbai.maticvigil.com&lt;/a&gt;',&lt;br&gt;
DERC721: '0xCa9Ebc8309bafc9d88815e4b0D9B1604498eb7B5',&lt;br&gt;
MaticWETH: '0x714550C2C1Ea08688607D86ed8EeF4f5E4F22323',&lt;br&gt;
},&lt;br&gt;
In this code, we have setup each RPC (remote procedure call) for the root chain and child chain .&lt;/p&gt;

&lt;p&gt;We are going to prepare our project on test environment first . The polygon are supporting goerli test network on L1 and Mumbai matic test network on L2 .&lt;/p&gt;

&lt;p&gt;First of all, we have to map our token contracts (one is the token contract on L1 and the other is the token contract on L2) by polygon support team .&lt;/p&gt;

&lt;p&gt;In this code , We will map the DERC721 address of root and the DERC721 address of child .&lt;/p&gt;

&lt;p&gt;Specially, there are two addressed more in root configure file .&lt;/p&gt;

&lt;p&gt;posERC721Predicate and POSRootChainManager addresses:&lt;/p&gt;

&lt;p&gt;These really needs to implement the bridge functionalities between polygon and mainnetwork. These addresses will not change by polygon . In here, these are test address and you can replace the addresses to mainnework ‘s address later.&lt;/p&gt;

&lt;p&gt;Rest is optional in root and chain.&lt;/p&gt;

&lt;p&gt;3) MaticPosClient Instance&lt;/p&gt;

&lt;p&gt;We will get MaticPosClient instance to use the function to implement each actions (approve, deposit, burn and withdraw) with configuration .&lt;/p&gt;

&lt;p&gt;return new MaticPOSClient({&lt;/p&gt;

&lt;p&gt;network: 'testnet',&lt;br&gt;
version: 'mumbai',&lt;br&gt;
parentProvider: Parent RPC,&lt;br&gt;
maticProvider: Matic PRC,&lt;br&gt;
posRootChainManager: config.root.POSRootChainManager,&lt;br&gt;
posERC721Predicate: config.root.posERC721Predicate,&lt;br&gt;
})&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;You can replace the networks and version to main network later&lt;/p&gt;

&lt;p&gt;1) Detailed Codes&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Approve&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;const tx = await maticPOSClient.approveERC721ForDeposit(config.root.DERC721, tokenId,{from:account})&lt;br&gt;
approveERC721ForDeposit is function to implement the approve function , there are two parameters : DERC721 of root , tokenId&lt;/p&gt;

&lt;p&gt;you have to set up address ( this is your wallet address) with from option .(Switch Goerli network)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deposit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;const tx = await maticPOSClient.depositERC721ForUser(config.root.DERC721, account, tokenId,{from:account})&lt;br&gt;
depositERC721ForUser is the function to implement the deposit function from your wallet into predicateERC721 contract on L1 . There are three parameters: DERC721 of root, account ( your wallet address) , tokenId&lt;/p&gt;

&lt;p&gt;you also have to set up the address( this is your wallet address ) with from option. (switch Goerli network)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Burn&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;const tx = await maticPOSClient.burnERC721(config.child.DERC721, tokenId,{from:account})&lt;br&gt;
burnERC721 is the function is the function to implement the burn function . there are two parameters : DERC721 of child , tokenId.&lt;/p&gt;

&lt;p&gt;You also have to set up the address (this is your wallet address) with from option. (Switch Polygon Mumbai testnetwork)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exit or Withdraw&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;const tx = await maticPOSClient.exitERC721(burnHash,{from:account,encodeAbi:true})&lt;br&gt;
exitERC721 is the function is the function to withdraw the deposit the NFT from predicateERC721 contract successfully again .&lt;/p&gt;

&lt;p&gt;There is one parameter: burnHash ( this is the hash after you burn your nft on polygon )&lt;/p&gt;

&lt;p&gt;You also have to set up the your wallet address with from option (Switch Goerli network)&lt;/p&gt;

&lt;p&gt;The important in here is that we have to confirm the checkpoint interval with polygon . Once it was completed , we can exit successfully .&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
Layer 2 tech will help us create blockchain systems that are usable, that can scale with whatever industry they’re implemented for, and that can compete with the likes of established, centralized systems like Visa. As well I have put the way how to implement Polygon Matic Bridge in this article. Hopefully ,everyone is getting closer to L2 innovations through my article.If you have any questions, please contact me.&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>ethereum</category>
      <category>polygon</category>
      <category>layer2</category>
    </item>
  </channel>
</rss>
