Tokens

如何創建購買時鑄造的代幣?

  • August 22, 2021

我想測試一個這樣的代幣系統:存入_token,收到x個token0。但我希望 token0 在存款時被鑄造。

使用 openzepellin 標准我需要做什麼才能工作?

// contracts/SimpleToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.2;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

/**
* @title SimpleToken
* @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
* Note they can later distribute these tokens as they wish using `transfer` and other
* `ERC20` functions.
* Based on https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.1/contracts/examples/SimpleToken.sol
*/
contract SimpleToken is ERC20 {
   /**
    * @dev Constructor that gives msg.sender all of existing tokens.
    */
   constructor(
       string memory name,
       string memory symbol,
       uint256 initialSupply
   ) public ERC20(name, symbol) {
       _mint(msg.sender, initialSupply);
   }

}

您需要處理存款的合約成為您的代幣合約的所有者,並且您需要呼叫mint您的deposit函式。像這樣的東西會起作用:

IERC20 depositToken = IERC20(depositTokenAddress)
IERC20 buyToken = IERC20(buyTokenAddress)

function deposit(uint256 amount) public {
   depositToken.transferFrom(msg.sender, address(this), amount);
   uint256 amountBought = // your math here, this should be pretty straightforward. 
   buyToken.mint(msg.sender, amountBought) // You need to have a mint function in your token contract. 
}

引用自:https://ethereum.stackexchange.com/questions/107554