8000 Erc20 tutorial by toschdev · Pull Request #271 · ignite/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Erc20 tutorial #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump 8000 to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/02 Using Starport/03_configuration/03_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,20 @@ genesis:

The `coins` specify amount of coins and their denomination on the blockchain. Here you can list a variety of coin denominations and their respective amounts to be used on your blockchain.

You can also manipulate parameters of different modules. If you wanted for example change the `staking` module, which contains staking parameters such as which token gets staked, you would add the following to your `config.yml``

```yml
genesis:
app_state:
staking:
params:
bond_denom: "denom"
```

After manipulating the `config.yml` to your likings, you can start the blockchain application with `starport serve`. This will create (or override) the folder located at your user homefolder `~/.myappd` (the name of your application with a `d` for `daemon` attached) and initiate your blockchain with the genesis file, located under `~/.myappd/config`. The second folder you can find in the `~/.myappd` folder is `data` - this is where the blockchain will write the consecutive blocks and transactions.
The other folder created is the `~/.myappcli` folder, which contains a configuration file for your current command line interface, such as `chain-id`, output parameters such as `json` or `indent` mode.

If you want to get sure all data from a blockchain setup get deleted, make sure to remove the `~/.myappd` folder.
If you want to get sure all data from a blockchain setup get deleted, make sure to remove the `~/.myappd` and `~/.myappcli` folder.

## Address denomination

Expand Down
2 changes: 1 addition & 1 deletion docs/04 Use cases/01_smart_contracts/01_smart_contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ In the next chapter, we will be looking into creating and uploading a Smart Cont
- The most Smart contracts run on the Ethereum virtual machine.
- Starport and Cosmos support smart contracts but also smart modules.

[◀️ Previous - Your own Module](../../03%20Modules/05_your_own_module/05_your_own_module.md) | [▶️ Next - CW20](../../04%20Use%20cases/03_cw20/03_cw20.md)
[◀️ Previous - Your own Module](../../03%20Modules/05_your_own_module/05_your_own_module.md) | [▶️ Next - ERC20 & ERC777](../../04%20Use%20cases/02_erc20/02_erc20.md)
158 changes: 116 additions & 42 deletions docs/04 Use cases/02_erc20/02_erc20.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,37 @@ Working with Ethermint can come in different forms. You can either work with the

[Read how to integrate the `evm` module into your Cosmos SDK application.](05_extras/05_01_cosmos_entermint/05_01_cosmos_ethermint.md)

On GitHub you can also find the `ethapp` application which you can fork and get started right away with an example `evm` implementation following the steps as outlied in the tutorial but with RPC integration, Makefile and initialisation script of the blockchain.

```bash
git clone https://github.com/Tosch110/ethapp && cd ethapp
make install
./init.sh
ethappd start
```

This application is very close to the starport scaffold and has an empty module in the `ethapp/x` folder where you can start integrating your blockchain logic as in any starport scaffolded application.

## Creating our environment

First, we start the blockchain with
If your blockchain is not already running `ethappd`, we start the blockchain with

```
starport serve --verbose
````

The `--verbose` flag is helpful in development and will show us if there are any errors in launching our blockchain application. From here we create our keys and fund them from the initial account that we created in the genesis block

```
# this will create keys and display the private, public key and cosmos address
ethappcli keys add mykey
# Now we can fund the given 'address'
ethappcli tx send user1 cosmosxxxxxxxxxxxxxxx "100000token"
```

Query for your account and balances to confirm the tranasction

```bash
ethappcli q auth account $(ethappcli keys show mykey -a) -o text
```
Query for your account and balances

Launch the Rest API Server with the newly created key
```bash
ethappcli rest-server --laddr "tcp://localhost:8545" --chain-id "123" --trace --unlock-key=mykey
ethappcli q auth account $(ethappcli keys show user1 -a) -o text
```

## Uploading a contract

With having the `evm` module available we now can upload contracts with the `create` command. First, we have to compile our contract into bytecode. We will choose the [ERC777 contract from OpenZeppelin](https://docs.openzeppelin.com/contracts/3.x/api/token/erc777) as an exercise. It is the upgrade of the ERC20 and allows for Fungible Token creation and transfer.

For this, we will fork the code from [OpenZeppelin ERC777 GitHub](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC777/ERC777.sol) and compile it to bytecode.

In order to compile a smart contract to bytecode, we will be using Truffle.

Truffle allows us to work with a contract and compile it.
Expand All @@ -95,8 +92,6 @@ Truffle allows us to work with a contract and compile it.

```bash
npm install -g truffle
# And adding file flattener
npm install -g truffle-flattener
```

First, let us setup our contract working repository
Expand All @@ -106,41 +101,78 @@ mkdir erc777 && cd erc777
truffle init
```

From here, we move into the `erc777/contracts` folder, we delete the available example `Counter.sol` and create our own `token.sol` contract file. We use the following code to mint a new ERC777 transaction that has a name that we can define.
For demonstration purposes we name it `PrintedTokenName` and give it the abbreviation `XRP` and mint `10000` Token with 18 decimal places (as usual on ETH).
First, let us install the Openzeppelin codebase with `npm install @openzeppelin/contracts`.

As preparation to use our blockchain with truffle, we make use of the `erc777/truffle-config.js`.

As network we use

```javascript
networks: {
// Useful for testing. The `development` name is special - truffle uses it by default
// if it's defined here and no other network is specified at the command line.
// You should run a client (like ganache-cli, geth or parity) in a separate terminal
// tab if you use this network and you must also set the `host`, `port` and `network_id`
// options below to some value.
//
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
// gas: 6000000, // Gas sent with each transaction (default: ~6700000)
// gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
},
```

and as compiler we are using solidity version 0.6.2

```javascript
// Configure your compilers
compilers: {
solc: {
version: "0.6.2", // Fetch exact version from solc-bin (default: truffle's version)
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
// settings: { // See the solidity docs for advice about optimization and evmVersion
// optimizer: {
// enabled: false,
// runs: 200
// },
// evmVersion: "byzantium"
// }
},
},
```
//token.sol

pragma solidity ^0.6.0;
Next, we get into defining the code from our ERC777 contract, we already had a look above. As we are using the code from OpenZeppelin, we can simply create a contract with their battle-tested standard and define our Token details in our piece of code.

import "@openzeppelin/contracts/token/ERC777/ERC777.sol";
From here, we move into the `erc777/contracts` folder and create our own `token.sol` contract file.

In our `erc777/contracts/token.sol` file add

```solidity
pragma solidity ^0.6.2;

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

/**
* @title Simple777Token
* @dev Very simple ERC777 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` or `ERC777` functions.
* Based on https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/examples/SimpleToken.sol
* @title My Token
* On-Blockchain Token in ERC777 Standard
*/
contract Simple777Token is ERC777 {
contract MyToken is ERC777 {
constructor(
string memory name,
string memory symbol,
uint256 initialSupply)

/**
* @dev Constructor that gives msg.sender all of existing tokens.
*/
constructor () public ERC777("PrintedTokenName", "XRP", new address[](0)) {
_mint(msg.sender, 10000 * 10 ** 18, "", "");
ERC777(name, symbol, new address[](0))

public{
_mint(msg.sender, initialSupply, "", "");
}
}
```

We can compile the code to a single file with

```bash
truffle-flattener contracts/erc777.sol > erc777_production.sol
```

## Deployment

We have compiled our contract. Now we need to upload it to the blockchain.
Expand All @@ -149,10 +181,52 @@ Useful commands for deployment

Start rest server
```bash
ethappcli rest-server --rpc --rpcaddr "0.0.0.0" --rpccorsdomain "*" --rpcapi "db,eth,web3,personal,admin,txpool"
ethappcli rest-server --laddr "tcp://localhost:8545" --trace --chain-id ethapp-1 --unlock-key user1
```

Migrate with truffle
```bash
truffle migrate --network development
```

When everything has been setup correctly, you should see an output like this

```bash
Starting migrations...
======================
> Network name: 'development'
> Network id: 1
> Block gas limit: 4294967295 (0xffffffff)


1_initial_migration.js
======================

Replacing 'Migrations'
----------------------
> transaction hash: 0x1acafe0bc636c2166949baa58bfd94eb87f7e2a69a18fb9d0977e5a61df83524
> Blocks: 1 Seconds: 4
> contract address: 0xf428192AdC18e86b36344b23a1610A87Db3e1b4E
> block number: 425
> block timestamp: 1601163038
> account: 0x46a52F73D536eaB8078b359234297044340Ff61c
> balance: 2.8278025
> gas used: 132391 (0x20527)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00264782 ETH


> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00264782 ETH


Summary
=======
> Total deployments: 1
> Final cost: 0.00264782 ETH
```

[◀️ Previous - Smart Contracts](../../04_usecases/01_smart_contracts/01_smart_contracts.md) | [▶️ Next - EVM NFT (ERC721)](../../04%20Use%20cases/03_nft/03_nft.md)
2 changes: 1 addition & 1 deletion docs/04 Use cases/03_cw20/03_cw20.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@ As execerise you should try out to send tokens from one account to another, from

Don't forget to checkout the documentation at https://github.com/CosmWasm/cosmwasm-plus/tree/master/packages/cw20

[◀️ Previous - Smart Contracts](../../04_usecases/01_smart_contracts/01_smart_contracts.md)
[◀️ Previous - ERC20 & ERC777](../../04_usecases/02_erc20/02_erc20.md)
4 changes: 2 additions & 2 deletions docs/04 Use cases/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Use Cases


1. [Smart Contracts](01_smart_contracts/01_smart_contracts.md)
3. [CosmWasm CW20](03_cw20/03_cw20.md)
2. [ERC20 & ERC777](02_erc20/02_erc20.md)
3. [CosmWasm CW20](03_cw20/03_cw20.md)
Empty file.
3 changes: 3 additions & 0 deletions docs/05 Extras/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Extras

1. [Cosmos EVM Integration](05_01_cosmos_ethermint.md)
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
5. [Your own Modules](03%20Modules/05_your_own_module/05_your_own_module.md)
4. Usecases
1. [Smart Contracts](04%20Use%20cases/01_smart_contracts/01_smart_contracts.md)
3. [CosmWasm CW20](04%20Use%20cases/03_cw20/03_cw20.md)
2. [ERC20 & ERC777](04%20Use%20cases/02_erc20/02_erc20.md)
3. [CosmWasm CW20](04%20Use%20cases/03_cw20/03_cw20.md)
5. Extras
1. [Evm Module integration](05%20Extras/05_01_cosmos_ethermint/05_01_cosmos_ethermint.md)

Expand Down
0