LOADING STUFF...

Blockchain decentralized pre-sales platform development specific solutions

Technical Blog1years go (2023)更新 Dexnav
0

Blockchain decentralized pre-sales platform development specific solutions

With the development of blockchain technology, pre-sale platform has become one of the important scenarios of blockchain application. The decentralized pre-sale platform (Initial Decentralized Offering, IDO) realizes the decentralized issuance and circulation of assets through blockchain technology, providing users with a more decentralized, fair and transparent way to participate. In this paper, we will introduce the development process and technical implementation scheme of blockchain decentralized pre-sale platform, and take Pink Pre-sale platform and Green Horse Pre-sale platform as examples.
CustomizedDevelopmentContact by telegram:Dex
Welcome to join the Scientist Development Community:Dexnav

I. Fundamentals of pre-sales platform

IDO is the process of selling tokens to early investors in the community through a series of decentralized pre-sale platforms prior to the project token offering. Typically, IDOs use multiple presale platforms to achieve a more equitable offering.

IDO's pre-sales platform consists of the following main segments.

  1. Token creation by developers: Developers create tokens through blockchain technology and set parameters such as the initial total number of tokens, issue price, and exchange ratio.
  2. Pre-sale platform to publish tokens: Developers publish token information to the pre-sale platform, including token name, introduction, total quantity, issue price, exchange ratio and other parameters.
  3. User participation in the pre-sale: Users exchange cryptocurrencies such as ETH on the pre-sale platform and receive the corresponding number of tokens.
  4. Token online: After the token issuance is completed, it can be circulated on exchanges and other venues.

Second, the development process and technical implementation plan of pink pre-sale platform

Pink Pre-sale Platform is a decentralized pre-sale platform based on Ether, which has the advantages of decentralization, fairness and transparency. The following will introduce the development process and technical implementation plan of Pink Pre-sale platform.

1. Development environment

The Pink Presale platform is developed in Solidity language and deployed and tested using the Truffle framework. Ganache is required as a local test network.

2.Token Contract Development

In Solidity, a simple token contract can be created with the following code.

contract IDO {
    // The pink pre-sale platform
    address public pinkPlatform;
    // greenPlatform
    address public greenPlatform;
    // token contract address
    address public token;
    // Sales start time
    uint public startTime;
    // end time of the sale
    uint public endTime;
    // Number of tokens that can be purchased per ETH
    uint public exchangeRate;

    // Pink presale platform balance
    uint public pinkBalance;
    // green horse presale platform balance
    uint public greenBalance;

    // Number of tokens sold
    uint public soldTokens;

    // Number of tokens purchased by customers
    mapping(address => uint) public balances;

    constructor(address _pinkPlatform, address _greenPlatform, address _token, uint _startTime, uint _endTime, uint _exchangeRate) public {
        pinkPlatform = _pinkPlatform;
        greenPlatform = _greenPlatform;
        token = _token;
        startTime = _startTime;
        endTime = _endTime;
        exchangeRate = _exchangeRate;
    }

    // Customer buys tokens
    function buyTokens() payable public {
        // Verify the time
        require(now >= startTime && now <= endTime, "Sale not active");
        // Verify the purchase amount
        uint tokens = msg.value * exchangeRate;
        require(oldTokens + tokens  0, "Insufficient balance");
        // Transfer tokens
        IERC20(token).transfer(msg.sender, balances[msg.sender]);
        // Update the balance
        balances[msg.sender] = 0;
    }

    // Administrator withdraws ETH
    function withdrawETH() public onlyOwner {
        uint balance = address(this).balance;
        uint pinkAmount = balance * pinkBalance / (pinkBalance + greenBalance);
        uint greenAmount = balance - pinkAmount;
        pinkBalance = 0;
        greenBalance = 0;
        msg.sender.transfer(pinkAmount);
        address(uint160(pinkPlatform)).transfer(greenAmount);
    }

    // administrator withdraw tokens
    function withdrawToken() public onlyOwner {
        uint balance = IERC20(token).balanceOf(address(this));
        IERC20(token).transfer(msg.sender, balance);
    }

    modifier onlyOwner {

 

3. Front-end interface design

The front-end interface of the pre-sale platform needs to clearly display various functions, such as project information, pre-sale start time, pre-sale end time, purchase rules, etc., and also needs to provide an entrance for users to purchase tokens. In order to improve user experience, responsive design can be considered to adapt the front-end interface to the screen size of different devices.

In addition, the front-end interface of the pre-sale platform needs to interact with the smart contract to query information such as token price and remaining quantity, and also needs to send user requests to buy tokens to the smart contract for processing. In order to improve the efficiency of interaction, tool libraries such as Web3.js can be used to implement the interaction with smart contracts.

The following is a sample code for the front-end of a pre-sales platform implemented on the React framework.

import React, { useState, useEffect } from 'react';
import Web3 from 'web3';
import PinkSaleContract from '. /contracts/PinkSale.json';
import GreenSaleContract from '. /contracts/GreenSale.json';

const web3 = new Web3(Web3.givenProvider);

const App = () =&gt; {
  const [contractAddress, setContractAddress] = useState('');
  const [saleContract, setSaleContract] = useState(null);
  const [tokenName, setTokenName] = useState('');
  const [tokenSymbol, setTokenSymbol] = useState('');
  const [tokenDecimals, setTokenDecimals] = useState('');
  const [startTime, setStartTime] = useState('');
  const [endTime, setEndTime] = useState('');
  const [totalSupply, setTotalSupply] = useState('');
  const [price, setPrice] = useState('');
  const [balance, setBalance] = useState('');

  useEffect(() =&gt; {
    const getContract = async () =&gt; {
      const contract = new web3.eth.Contract(PinkSaleContract.abi, contractAddress);
      setSaleContract(contract);
    };

    if (contractAddress ! == '') {
      getContract();
    }
  }, [contractAddress]);

  const handleConnectWallet = async () =&gt; {
    await window.ethereum.request({ method: 'eth_requestAccounts' });
    const accounts = await web3.eth.getAccounts();
    const balance = await web3.eth.getBalance(accounts[0]);
    setBalance(web3.utils.fromWei(balance));
  };

  const handleLoadToken = async () =&gt; {
    const name = await saleContract.methods.name().call();
    const symbol = await saleContract.methods.symbol().call();
    const decimals = await saleContract.methods.decimals().call();
    const totalSupply = await saleContract.methods.totalSupply().call();
    const price = await saleContract.methods.price().call();
    const startTime = await saleContract.methods.startTime().call();
    const endTime = await saleContract.methods.endTime().call();

    setTokenName(name);
    setTokenSymbol(symbol);
    setTokenDecimals(decimals);
    setTotalSupply(totalSupply);
    setPrice(price);
    setStartTime(startTime);
    setEndTime(endTime);
  };

  const handlePurchaseToken = async () =&gt; {
    const amount = document.getElementById('amount').value;
    await saleContract.methods.buyTokens().send({ value: web3.utils.toWei(price * amount) });
    alert('Purchase success');
  };

  return (
    <div>
      <h2>Pink Sale Platform</h2>
      <div>
        <label>

 

4. Contract deployment and testing

After the pre-sale contract has been written, we need to deploy it to the test network or the main network to test and run it. The most commonly used test networks on Ether are Ropsten and Kovan, which provide a similar environment to the main network, but use test tokens and test ethers. Before deploying a contract, one needs to make sure that we have enough test tokens and Ether.

The deployment of contracts can be done through the Web3.js library. The following is a simple JavaScript code snippet demonstrating how contracts can be deployed to the Ropsten test network using the Web3.js library.

const Web3 = require('web3');
const HDWalletProvider = require('@truffle/hdwallet-provider');
const contractJSON = require('. /build/contracts/PinkPresale.json');

// Set up Web3 to connect to the Ropsten test network
const provider = new HDWalletProvider({
    mnemonic: {
        phrase: 'your mnemonic phrase'
    },
    providerOrUrl: 'https://ropsten.infura.io/v3/your-infura-project-id',
    addressIndex: 0,
    numberOfAddresses: 1,
    chainId: 3
});

const web3 = new Web3(provider);

// Get the contract ABI and bytecode
const abi = contractJSON.abi;
const bytecode = contractJSON.bytecode;

// Deploy the contract
const deployContract = async () => {
    const accounts = await web3.eth.getAccounts();
    console.log('Attempting to deploy from account', accounts[0]);

    const contract = new web3.eth.Contract(abi);
    const result = await contract.deploy({
        data: bytecode,
        arguments: [/* contract arguments */]
    }).send({
        from: accounts[0],
        gas: '3000000'
    });

    console.log('Contract deployed to', result.options.address);
};

deployContract();

In this code, we use the@truffle/hdwallet-providerlibrary to connect to the Ropsten test network with our helper and Infura project ID. we then retrieve the ABI and bytecode from the contract JSON file and use theweb3.eth.Contractclass to create contract instances. Finally, we use thedeploy()method to deploy the contract, specifying the contract's bytecode and constructor parameters. After the deployment is complete, we can use theresult.options.addressGet the address of the contract.

IV. Summary

The development of a decentralized pre-sales platform involves several aspects of technology, including the design and implementation of smart contracts, the development of front-end interfaces, and wallet integration and contract deployment. During the development process, we need to pay attention to the security of the contract and the maintainability of the code to ensure the stable operation of the platform. The Pink Pre-sale platform and Green Horse Pre-sale platform are two very popular decentralized pre-sale platforms that have unique features in terms of user experience and transaction process. When developing pre-sale platforms, we can refer to the design and implementation of these platforms.

CustomizedDevelopmentContact by telegram:Dex
© 版权声明

Related posts

No comments

No comments...