简体   繁体   中英

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.

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. 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?

Can I use web3.js to connect to my test network? 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. 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.

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.

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.

Here's a faucet you can use to get Rinkeby ether: https://faucet.rinkeby.io/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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