Examples

1. Setting Up the Development Environment

For Truffle:

npm install -g truffle
truffle init

Truffle Configuration (truffle-config.js):

module.exports = {
  networks: {
    metachain: {
      host: "127.0.0.1",
      port: 8555,
      network_id: "*" // Match any network id
    }
  }
};

2. Writing the DST-20 Token Contract

Basic DST-20 Token Contract (DST20Token.sol):

pragma solidity ^0.8.0;

contract DST20Token {
    mapping(address => uint256) public balances;
    mapping(address => mapping(address => uint256)) public allowed;

    string public name = "Sample DST-20 Token";
    string public symbol = "SDT";
    uint8 public decimals = 18;
    uint256 public totalSupply = 1000000 * (10 ** uint256(decimals));

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

    function balanceOf(address _owner) public view returns (uint256) {
        return balances[_owner];
    }

    function transfer(address _to, uint256 _value) public returns (bool) {
        require(_to != address(0));
        require(balances[msg.sender] >= _value);

        balances[msg.sender] -= _value;
        balances[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
        return true;
    }

    function approve(address _spender, uint256 _value) public returns (bool) {
        allowed[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
        require(_to != address(0));
        require(balances[_from] >= _value);
        require(allowed[_from][msg.sender] >= _value);

        balances[_from] -= _value;
        balances[_to] += _value;
        allowed[_from][msg.sender] -= _value;
        emit Transfer(_from, _to, _value);
        return true;
    }
}

3. Testing the Contract

Sample Test (test/DST20Token.test.js):

const DST20Token = artifacts.require("DST20Token");

contract("DST20Token", accounts => {
    it("should put 1000000 SDT in the first account", async () => {
        const instance = await DST20Token.deployed();
        const balance = await instance.balanceOf.call(accounts[0]);
        assert.equal(balance.valueOf(), 1000000, "1000000 wasn't in the first account");
    });
});

4. Deploying to Mainnet

Migration Script (migrations/2_deploy_token.js):

const DST20Token = artifacts.require("DST20Token");

module.exports = function(deployer) {
    deployer.deploy(DST20Token);
};

Deploy Command:

truffle migrate --network metachain

5. Verifying the Contract

(This step typically involves using a service like Etherscan for Ethereum. For MetaChain, the exact process might differ, but the idea is to submit your contract's source code and constructor arguments to verify.)

6. Interacting with the Deployed Token

Web3.js Example:

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8555'));

const contractABI = [...]; // ABI of your deployed contract
const contractAddress = '0x...'; // Address of your deployed contract
const DST20TokenContract = new web3.eth.Contract(contractABI, contractAddress);

// Transfer tokens
DST20TokenContract.methods.transfer('0xRecipientAddress', 100).send({ from: '0xYourAddress' });

By following these code examples and the steps provided, developers can successfully deploy and interact with DST-20 tokens on MetaChain.

DST-20 Token Basics:

A DST-20 token contract typically includes the following attributes:

contract DST20Token {
    string public name;           // Token name (e.g., "MetaChain DeFi Token")
    string public symbol;         // Token symbol (e.g., "MDT")
    uint8 public decimals;        // Number of decimal places (commonly set to 18)
    uint256 public totalSupply;   // Total supply of the token

    mapping(address => uint256) public balanceOf;  // Track the balance of each holder's address
    mapping(address => mapping(address => uint256)) public allowance;  // Track the amount an address is allowed to spend on behalf of another address
}

Example: Lending Protocol using DST-20 Token

Let's create a basic lending protocol contract that utilizes the DST-20 token standard:

// Assuming DST20Token is already defined

contract LendingProtocol {
    DST20Token public token;
    mapping(address => uint256) public deposits;  // Track deposited amounts
    mapping(address => uint256) public loans;     // Track loaned amounts

    constructor(address _tokenAddress) {
        token = DST20Token(_tokenAddress);
    }

    // Deposit DST-20 tokens into the lending protocol
    function deposit(uint256 _amount) external {
        require(token.balanceOf(msg.sender) >= _amount, "Insufficient balance");
        token.transferFrom(msg.sender, address(this), _amount);
        deposits[msg.sender] += _amount;
    }

    // Borrow DST-20 tokens from the lending protocol
    function borrow(uint256 _amount) external {
        require(deposits[msg.sender] >= _amount, "Insufficient deposit collateral");
        loans[msg.sender] += _amount;
        token.transfer(msg.sender, _amount);
    }

    // Repay borrowed DST-20 tokens
    function repay(uint256 _amount) external {
        require(loans[msg.sender] >= _amount, "Invalid repayment amount");
        token.transferFrom(msg.sender, address(this), _amount);
        loans[msg.sender] -= _amount;
    }
}

Disclaimer

The provided DST-20 token contract examples are for educational and illustrative purposes only. They have not undergone any formal auditing or rigorous testing processes. Developers should exercise caution and conduct their own thorough review, testing, and, if necessary, consultation with blockchain security experts before deploying any smart contract, especially in a live or production environment.

The authors, contributors, and maintainers of these examples disclaim all warranties, both express and implied, including but not limited to any implied warranties of merchantability, fitness for a particular purpose, or non-infringement of proprietary rights. In no event shall the authors, contributors, or maintainers be liable for any claim, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the provided examples or the use or other dealings in these examples.

By using these examples, you acknowledge and agree to this disclaimer and assume all risks and responsibilities associated with the deployment and use of the DST-20 token contracts.

Last updated