Solidity

從函式/合約創建合約

  • July 22, 2019

**大家好 !我只是嘗試訂立契約以創建契約。

它應該是 ERC-20 介面,所以我為此簽訂了第一份契約。

但我不知道我該怎麼做 _mint 和 createContract 功能

我有錯誤,所以有人可以幫助我嗎?我會很感激** pragma solidity ^0.5.0;

library SafeMath {
   function add(uint a, uint b) internal pure returns (uint c) {
       c = a + b;
       require(c >= a);
   }
   function sub(uint a, uint b) internal pure returns (uint c) {
       require(b <= a);
       c = a - b;
   }
   function mul(uint a, uint b) internal pure returns (uint c) {
       c = a * b;
       require(a == 0 || c / a == b);
   }
   function div(uint a, uint b) internal pure returns (uint c) {
       require(b > 0);
       c = a / b;
   }
}
contract SubToken  {
   function totalSupply() public view returns (uint);
   function balanceOf(address tokenOwner) public view returns (uint balance);
   function allowance(address tokenOwner, address spender) public view returns (uint remaining);
   function transfer(address to, uint tokens) public returns (bool success);
   function approve(address spender, uint tokens) public returns (bool success);
   function transferFrom(address from, address to, uint tokens) public returns (bool success);
   function mint(address from, uint amount) public returns(bool success);
   constructor(string memory name,string memory symbol,uint8 decimals,uint256 initialSupply) 
   public{
       mint(msg.sender, initialSupply);

   }

   event Transfer(address indexed from, address indexed to, uint tokens);
   event Approval(address indexed tokenOwner, address indexed spender, uint tokens);

}
contract MainContrat{
   mapping(address => uint) _balances;
   mapping(address => mapping(address => uint)) allowed;
   mapping(address => SubToken) public tokenlist;
   address[] public contracts;
   address owner = msg.sender;

   using SafeMath for uint;
   string public symbol;
   string public  name;
   uint8 public decimals;
   uint _totalSupply;

   constructor(string memory name,string memory symbol,uint8 decimals,uint256 initialSupply) public{
       _mint(msg.sender, initialSupply);
   }    

   function _mint(address account, uint256 amount) internal {
       //require(account != address(0), "ERC20: mint to the zero address");

       _totalSupply = _totalSupply.add(amount);
       _balances[account] = _balances[account].add(amount);
       //emit Transfer(address(0), account, amount);
   }
   function createNewContract(string memory name,string memory symbol,uint8 decimals,uint256 initialSupply) public returns(address newContract){
   SubToken st = new SubToken(name,symbol,decimals,initialSupply);
   contracts.push(st);
   return st;
   }
}

**錯誤在這裡: browser/crtcont2.sol:49:19: TypeError: Trying to create an instance of a abstract contract。SubToken st = new SubToken(); ^———-^ browser/crtcont2.sol:14:5: 缺少實現:函式allowance(address tokenOwner, address spender) public view return (uint剩餘); ^————————————————- ——————————^ 瀏覽器/crtcont2.sol:16 :5:缺少實現:函式批准(地址支出者,uint 令牌)公共返回(布爾成功);^————————————————- ————————–^ browser/crtcont2.sol:13:5: 缺少實現:函式 balanceOf(address tokenOwner) 公共視圖返回 (單位餘額);^————————————————- ————————————^ browser/crtcont2.sol:18:5: 缺少實現:function mint(address from, uint amount) public returns(bool success); ^————————————————- ——————-^ browser/crtcont2.sol:12:5:缺少實現:函式totalSupply()公共視圖返回(uint);^———————————————— ^ browser/crtcont2.sol:15:5:缺少實現:函式傳輸(地址到,uint令牌)公共返回(布爾成功);^————————————————- ———————-^ browser/crtcont2.sol:17:5: 缺少實現:函式 transferFrom(address from, address to, uint tokens) 公共返回(布爾成功);^————————————————- —————————————————-^ ^————————————————- ——————-^ browser/crtcont2.sol:12:5:缺少實現:函式totalSupply()公共視圖返回(uint);^———————————————— ^ browser/crtcont2.sol:15:5:缺少實現:函式傳輸(地址到,uint令牌)公共返回(布爾成功);^————————————————- ———————-^ browser/crtcont2.sol:17:5: 缺少實現:函式 transferFrom(address from, address to, uint tokens) 公共返回(布爾成功);^————————————————- —————————————————-^ ^————————————————- ——————-^ browser/crtcont2.sol:12:5:缺少實現:函式totalSupply()公共視圖返回(uint);^———————————————— ^ browser/crtcont2.sol:15:5:缺少實現:函式傳輸(地址到,uint令牌)公共返回(布爾成功);^————————————————- ———————-^ browser/crtcont2.sol:17:5: 缺少實現:函式 transferFrom(address from, address to, uint tokens) 公共返回(布爾成功);^————————————————- —————————————————-^ 缺少實現:函式 transfer(address to, uint tokens) public 返回(bool 成功);^————————————————- ———————-^ browser/crtcont2.sol:17:5: 缺少實現:函式 transferFrom(address from, address to, uint tokens) 公共返回(布爾成功);^————————————————- —————————————————-^ 缺少實現:函式 transfer(address to, uint tokens) public 返回(bool 成功);^————————————————- ———————-^ browser/crtcont2.sol:17:5: 缺少實現:函式 transferFrom(address from, address to, uint tokens) 公共返回(布爾成功);^————————————————- —————————————————-^

browser/crtcont2.sol:50:20: TypeError: 函式呼叫中的參數類型無效。從合約 SubToken 到請求地址的隱式轉換無效。契約.push(st); ^^ browser/crtcont2.sol:51:12:TypeError:返回參數類型契約 SubToken 不能隱式轉換為預期類型(第一個返回變數的類型)地址。返回聖;^^**

它會假設是我的建構子,但我不知道如何修復:(

看起來要修復您的錯誤,您需要做一些事情:

首先,對於 SubContract 中的每個函式,您需要定義它們。例如,

totalSupply()

需要一些程式碼來配合它,所以編譯器知道當你呼叫那個函式時要做什麼。所有“缺少介面”錯誤都與此有關。你需要定義

function totalSupply() public view returns (uint);
function balanceOf(address tokenOwner) public view returns (uint balance);
function allowance(address tokenOwner, address spender) public view returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
function mint(address from, uint amount) public returns(bool success);

接下來,您需要使用程式碼底部的“address(st)”將“st”轉換為地址。您的程式碼在哪裡說:

contracts.push(st);
return st;

編譯器需要一個地址,所以使用它而不是“st”

address(st)

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