简体   繁体   English

以太坊中的简单令牌系统

[英]Simple token system in Ethereum

How can I add a specific number of tokens to the Ethereum blockchain? 如何在以太坊区块链中添加特定数量的令牌?

I guess I need to create a contract with an array of the tokens with something like 我想我需要创建一个包含一系列令牌的契约

contract Token {
  uint[] public tokens;
}

I don't want to do anything fancy. 我不想做任何花哨的事情。 I just want to have eg 10 tokens in the blockchain that can be transferred among different addresses. 我只想在区块链中有10个令牌,可以在不同的地址之间传输。

So one address should be able to hold multiple tokens. 因此,一个地址应该能够容纳多个令牌。

I have tried reading about contracts, but they look quite complex compared to what I want to accomplish. 我曾尝试阅读合同,但与我想要完成的相比,它们看起来相当复杂。 I just want to create the tokens, be able to assign the tokens to different owners, and lookup an address to see which tokens the address holds. 我只想创建令牌,能够将令牌分配给不同的所有者,并查找地址以查看地址所包含的令牌。

I know it might be a project too big for a stackoverflow quesiton, but I want to know which tools to use. 我知道这可能是一个对于stackoverflow问题来说太大的项目,但我想知道要使用哪些工具。 I guess I should set up a test node (with eg Truffle Ganache) so I can create some accounts (who should be able to hold the tokens), but how can I simply create the tokens, assign them to the accounts and lookup which tokens each account holds? 我想我应该设置一个测试节点(例如Truffle Ganache),这样我就可以创建一些帐户(谁应该能够持有令牌),但我怎样才能简单地创建令牌,将它们分配给帐户并查找哪些令牌每个帐户持有?

Can I use web3.js to connect to my test network? 我可以使用web3.js连接到我的测试网络吗? Do I need to create a contract using truffle? 我是否需要使用松露制作合同? Are there any very very simple projects with truffle, only issuing tokens and making it possible to see the tokens and assign them to different users? 是否有非常简单的松露项目,只发放令牌并可以查看令牌并将其分配给不同的用户?

For something like this you don't need to create your own test network. 对于这样的事情,您不需要创建自己的测试网络。 Instead, you can use the public testnets like Rinkeby or Rospten. 相反,您可以使用Rinkeby或Rospten等公共测试网。 You can even google around to find out how you get free test ether for those testnets. 您甚至可以谷歌了解如何获得这些测试网络的免费测试以太网。

If you just want to get used to programming a token and running it, I would recommend that you use something like Remix to create and deploy a token on the testnet. 如果您只是想习惯编写令牌并运行它,我建议您使用Remix之类的东西在testnet上创建和部署令牌。

Now for the token... 现在为令牌......

https://www.ethereum.org/token has a good example of a minimum viable token and I'll explain what each part does. https://www.ethereum.org/token有一个最小可行令牌的好例子,我将解释每个部分的作用。

contract MyToken {
    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;

    /* Initializes contract with initial supply tokens to the creator of the contract */
    function MyToken(
        uint256 initialSupply
        ) {
        balanceOf[msg.sender] = initialSupply;              // Give the creator all initial tokens
    }

    /* Send coins */
    function transfer(address _to, uint256 _value) {
        require(balanceOf[msg.sender] >= _value);           // Check if the sender has enough
        require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
        balanceOf[msg.sender] -= _value;                    // Subtract from the sender
        balanceOf[_to] += _value;                           // Add the same to the recipient
    }
}

This code will allow you to initialize a token with an initial supply that you set and then set that balance to your address. 此代码允许您使用您设置的初始供应初始化令牌,然后将该余额设置为您的地址。 You can then use the transfer method below to send any number of tokens to another address. 然后,您可以使用下面的传输方法将任意数量的令牌发送到另一个地址。

You can do all of this through Remix without worrying about setting up truffle or web3, etc. 您可以通过Remix完成所有这些操作,而无需担心设置松露或web3等。

Here's a faucet you can use to get Rinkeby ether: https://faucet.rinkeby.io/ 这是一个可以用来获得Rinkeby以太的水龙头: https//faucet.rinkeby.io/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM