ERC20: El estándar de Tokens en el Blockahin

turupawn

Ahmed Castro

Posted on June 24, 2021

ERC20: El estándar de Tokens en el Blockahin

Los ERC20 son el estándar de tokens en todos los blockahins compatibles con la Ethereum Virtual Machine. En este video creamos nuestro propio Token y realizamos operaciones mediante un segundo Smart Contract operador.

Contrato de Token ERC20

// SPDX-License-Identifier: MIT
pragma solidity 0.8.5;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.1.0/contracts/token/ERC20/ERC20.sol";

contract MyERC20 is ERC20 {
  uint256 public token_transfer_count = 0;

  constructor () ERC20("Token", "TKN") {
    _mint(msg.sender, 1000 ether);
  }

  function mint(address account, uint256 amount) public {
    _mint(account, amount);
  }

  function burn(address account, uint256 amount) public {
    _burn(account, amount);
  }

  function _beforeTokenTransfer(address from, address to, uint256 amount) internal override
  {
    token_transfer_count += 1;
  }
}
Enter fullscreen mode Exit fullscreen mode

Contrato Operador

// SPDX-License-Identifier: MIT
pragma solidity 0.8.5;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.1.0/contracts/token/ERC20/ERC20.sol";

contract MyERC20Operator {

  mapping (address=>uint) public balances;
  ERC20 my_erc20;

  constructor (address my_erc20_address)
  {
    my_erc20 = ERC20(my_erc20_address);
  }

  function deposit(uint amount) public
  {
    my_erc20.transferFrom(address(msg.sender), address(this), amount);
    balances[msg.sender] += amount;
  }

  function retrieveTokens() public
  {
    my_erc20.transfer(address(msg.sender), balances[msg.sender]);
    balances[msg.sender] = 0;
  }
}
Enter fullscreen mode Exit fullscreen mode

Documentación oficial

Gracias por ver este tutorial!

Sígueme en dev.to y en Youtube para todo lo relacionado al desarrollo en Blockchain en Español.

💖 💪 🙅 🚩
turupawn
Ahmed Castro

Posted on June 24, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related