Solidity
從合約地址獲取資訊?
pragma solidity ^0.5.0; import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol'; contract SubToken{ using SafeMath for uint; struct token{ string name; string symbol; uint8 decimals; uint totalSupply; } token public mytokens; mapping(address => token) Tokens; mapping(address => uint) balances; constructor(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply) public{ mytokens = token(_name,_symbol,_decimals,_totalSupply); balances[msg.sender]=_totalSupply; } function getInformations(address contract_address) public view returns(string memory name,string memory symbol,uint8 decimals,uint totalSupply){ return (Tokens[contract_address].name,Tokens[contract_address].symbol,Tokens[contract_address].decimals,Tokens[contract_address].totalSupply); } //function transferFrom(address from, address to, uint tokens) public returns (bool success); //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); event Transfer(address indexed from, address indexed to, uint tokens); event Approval(address indexed tokenOwner, address indexed spender, uint tokens); } contract MainContract { mapping(address => mapping(address=>uint)) balances; mapping(address => mapping(address => uint)) allowed; address[] public contracts; using SafeMath for uint; address contract_address; struct token{ string name; string symbol; uint8 decimals; uint totalSupply; } mapping(address => token) public tokens; function createNewContract(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply) public returns(address youraddress){ SubToken st = new SubToken(_name,_symbol,_decimals,_totalSupply); tokens[address(st)] = token(_name,_symbol,_decimals,_totalSupply); return(address(st)); } event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value); }
嘿伙計們,我是一個堅固的初學者,所以我的問題是關於聯繫地址並獲取一些資訊。
所以我有 2 份契約,第一份是次要的,第二份是主要的。在我的主契約中,我為創建子契約創建了契約功能,並且效果很好。
但在我的分包契約中,我有獲取資訊的功能。這個函式接受一個合約地址參數並返回資訊。那裡的問題這個功能不起作用。
我檢查了我的程式碼,但我不應該做什麼
任何人都可以幫助我:)
當您從 中創建 a
SubToken
時MainContract
,SubToken
建構子不會將新創建的令牌添加到SubToken
Tokens 映射中,而在getInformations
.您的工廠 (MainContract) 和令牌 (SubToken) 都有令牌映射。
我不知道您對在這兩個地方擷取這些數據的要求。我會懷疑這僅在 MainContract 中需要。
創建 ERC20 代幣的工廠(使用 OpenZeppelin ERC20 實現)可能如下所示:
pragma solidity ^0.5.0; import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/token/ERC20/ERC20.sol"; import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/token/ERC20/ERC20Detailed.sol"; contract Token is ERC20, ERC20Detailed { constructor (string memory name, string memory symbol, uint8 decimals, uint256 totalSupply) public ERC20Detailed(name, symbol, decimals) { _mint(msg.sender, totalSupply); } } contract Factory { struct tokenData { string name; string symbol; uint8 decimals; uint256 totalSupply; } event TokenCreated(address indexed tokenAddress, string name, string symbol, uint8 decimals, uint256 totalSupply); mapping(address => tokenData) private _tokens; function createToken(string memory name, string memory symbol, uint8 decimals, uint256 totalSupply) public returns(address tokenAddress){ Token token = new Token(name, symbol, decimals, totalSupply); _tokens[address(token)] = tokenData(name, symbol, decimals, totalSupply); emit TokenCreated(address(token), name, symbol, decimals, totalSupply); return(address(token)); } function getTokenData(address tokenAddress) public view returns(string memory name, string memory symbol, uint8 decimals, uint256 totalSupply){ return (_tokens[tokenAddress].name, _tokens[tokenAddress].symbol, _tokens[tokenAddress].decimals, _tokens[tokenAddress].totalSupply); } }
如果您正在創建 ERC20 代幣,您可能需要查看 OpenZeppelin Contracts 實現,看看這是否滿足您的需求。
有關詳細資訊,請參閱文件:https ://docs.openzeppelin.com/contracts/2.x/tokens#ERC20
如果您對使用 OpenZeppelin 有任何疑問,可以在社區論壇中提問:https ://forum.openzeppelin.com/
披露:我是 OpenZeppelin 的社區經理