Permalink
Browse files

feat(eosio): add demo contract and instructions

  • Loading branch information...
gaboesquivel committed Dec 8, 2018
1 parent 864fbff commit faf4aacc664f7f218226e264ff882c9e594bc511
Showing with 155 additions and 18 deletions.
  1. +2 βˆ’3 .gitignore
  2. +0 βˆ’3 makefile
  3. +137 βˆ’8 readme.md
  4. +0 βˆ’4 scripts/unlock.sh
  5. +16 βˆ’0 services/eosio/contracts/hello/hello.cpp
@@ -2,6 +2,8 @@
build/
dist/
*.wast
*.abi
*.wasm

# IDE's
.vscode
@@ -31,6 +33,3 @@ __docs

# ipfs
services/ipfs/*

# custom contracts
services/eosio/contracts/*
@@ -7,7 +7,4 @@ flush: scripts/flush.sh
fresh: scripts/fresh.sh
./scripts/fresh.sh

unlock: scripts/unlock.sh
./scripts/unlock.sh

FORCE:
145 readme.md
@@ -53,6 +53,10 @@ EOS Local is a community-driven project led by EOS Costa Rica. We welcome contri
- [Using Cleos on EOS Local](#using-cleos-on-eos-local)
- [Invoking cleos through docker exec](#invoking-cleos-through-docker-exec)
- [Open a shell window on the containers and use cleos directly](#open-a-shell-window-on-the-containers-and-use-cleos-directly)
- [Contract Development](#contract-development)
- [Compiling and deploying contracts](#compiling-and-deploying-contracts)
- [from host machine](#from-host-machine)
- [from within the eosio container](#from-within-the-eosio-container)
- [Frequently Asked Questions](#frequently-asked-questions)
- [Why Containers ?](#why-containers-)
- [Contributing](#contributing)
@@ -115,6 +119,7 @@ It is useful to have aliases for the `docker`, `docker-compose` and `cleos` comm
```
alias cleos='docker exec -i eoslocal_eosio cleos -u http://eosio:8888 --wallet-url http://wallet:8901'
alias eosio='docker exec -i eoslocal_eosio bash'
alias unlock_eoslocal='docker exec -i eoslocal_eosio ./scripts/unlock.sh'
alias dk='docker'
alias dc='docker-compose'
```
@@ -130,14 +135,14 @@ Execute `make setup` for:
Run the following command to verify afterwards

```terminal
➜ eos-local git:(master) docker network ls
➜ docker network ls
NETWORK ID NAME DRIVER SCOPE
f72523603a2d bridge bridge local
113510307ce3 eoslocal bridge local
886afbf5db1a host host local
53d0d3f8d910 none null local
➜ eos-local git:(master) βœ— docker-compose ps
➜ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------------------
eoslocal_eosio /bin/sh -c /wait && /opt/a ... Up 0.0.0.0:8888->8888/tcp, 0.0.0.0:9830->9876/tcp
@@ -147,7 +152,7 @@ eoslocal_nginx /app/docker-entrypoint.sh ... Up 0.0.0.0:80->80/tcp
eoslocal_wallet /opt/eosio/bin/keosd --wal ... Up 0.0.0.0:8901->8901/tcp
➜ eos-local git:(master) cleos wallet keys
➜ cleos wallet keys
[
"EOS5VdFvRRTtVQAPUJZQCYvpBekYV4nc1cFe7og9aYPTBMXZ38Koy",
"EOS5k6Jht1epqZ2mnRLFVDXDTosaTneR6xFhvenVLiFfz5Ue125dL",
@@ -158,7 +163,7 @@ eoslocal_wallet /opt/eosio/bin/keosd --wal ... Up 0.0.0.0:8901->8901/tc
"EOS84BLRbGbFahNJEpnnJHYCoW9QPbQEk2iHsHGGS6qcVUq9HhutG"
]
➜ eos-local git:(master) cleos get info
➜ cleos get info
{
"server_version": "59626f1e",
"chain_id": "cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f",
@@ -175,7 +180,7 @@ eoslocal_wallet /opt/eosio/bin/keosd --wal ... Up 0.0.0.0:8901->8901/tc
"server_version_string": "v1.4.4"
}
➜ eos-local git:(master) curl http://eosio.eoslocal.io/v1/chain/get_info | jq
➜ curl http://eosio.eoslocal.io/v1/chain/get_info | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 566 100 566 0 0 72554 0 --:--:-- --:--:-- --:--:-- 80857
@@ -195,7 +200,7 @@ eoslocal_wallet /opt/eosio/bin/keosd --wal ... Up 0.0.0.0:8901->8901/tc
"server_version_string": "v1.4.4"
}
➜ eos-local git:(master) curl http://localhost:8888/v1/chain/get_info | jq
➜ curl http://localhost:8888/v1/chain/get_info | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 568 100 568 0 0 78172 0 --:--:-- --:--:-- --:--:-- 81142
@@ -222,7 +227,6 @@ eoslocal_wallet /opt/eosio/bin/keosd --wal ... Up 0.0.0.0:8901->8901/tc
- `make setup` run chain initialization.
- `make flush` stops all services and removes all data.
- `make fresh` stops all services and removes all data and run chain initialization again.
- `make unlock` unlocks the eoslocal wallet.
- `docker-compose start` starts all containers.
- `docker-compose exec [service_name] [bash | sh]` open bash or sh in a container.
- `docker-compose stop` stops all containers.
@@ -335,7 +339,7 @@ That will log you in and you will be able to execute cleos directly within the u
Eg.

```
➜ eos-local git:(master) βœ— docker exec -it eoslocal_eosio bash
➜ docker exec -it eoslocal_eosio bash
root@b39ffe3c43c0:/opt/eosio/bin# cleos get info
{
"server_version": "f9a3d023",
@@ -354,6 +358,131 @@ root@b39ffe3c43c0:/opt/eosio/bin# cleos get info
}
```

## Contract Development

The default folder for smart contracts is the `services/eosio/contract` folder.
This folder is mounted to the ubuntu / eosio 1.4 container which also has the eosio.cdt for contract compilation on it. This means you can either compile from host machine or the within the container using `eosio-cpp` command.

### Compiling and deploying contracts

There 2 ways you can do this.

We'll use the hello world contract as example
https://developers.eos.io/eosio-home/docs/your-first-contract

##### from host machine

Make sure you install https://github.com/EOSIO/eosio.cdt.

```
➜ eosio-cpp --version
eosio-cpp version 1.4.1
➜ cd services/eosio/contracts/hello
➜ eosio-cpp -o hello.wasm hello.cpp --abigen
Warning, empty ricardian clause file
Warning, empty ricardian clause file
Warning, action <hi> does not have a ricardian contract
➜ docker exec -i eoslocal_eosio ./scripts/unlock.sh
+ cleos='cleos -u http://eosio:8888 --wallet-url http://wallet:8901'
+ unlock_wallet
+ echo 'unlocking default wallet...'
unlocking default wallet...
++ cat /opt/application/config/keys/eoslocal_wallet_password.txt
+ cleos -u http://eosio:8888 --wallet-url http://wallet:8901 wallet unlock --name eoslocal --password PW5JWU79n8sXuNSrd92mNG7TR69K7q1Ax3a37x1FfFSAQE9MgXosh
Unlocked: eoslocal
+ sleep .5
➜ cleos wallet keys
[
"EOS5VdFvRRTtVQAPUJZQCYvpBekYV4nc1cFe7og9aYPTBMXZ38Koy",
"EOS5k6Jht1epqZ2mnRLFVDXDTosaTneR6xFhvenVLiFfz5Ue125dL",
"EOS6Jv4RykLZQQopCBdBHSwaGoMyFxyaxFNXimqFPdEXNWqgWbG1a",
"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"EOS6TVQ6EmphCWavUuYiZMmDNYMRgbb96wgqWDncjrkvFPcpokgdD",
"EOS7CB47VMLWp49QhajE3uTuHuf9qoSeR6scUHMKGCD6LXYufRUDc",
"EOS84BLRbGbFahNJEpnnJHYCoW9QPbQEk2iHsHGGS6qcVUq9HhutG"
]
➜ cleos create account eosio hello EOS5VdFvRRTtVQAPUJZQCYvpBekYV4nc1cFe7og9aYPTBMXZ38Koy -p eosio@active
executed transaction: 1b9a9b7d323542d4151ab9c8b6242c85272bc7ce7144772bb65b8633f7349389 200 bytes 517 us
warning: transaction executed locally, but may not be confirmed by the network yet ]
# eosio <= eosio::newaccount {"creator":"eosio","name":"hello","owner":{"threshold":1,"keys":[{"key":"EOS5VdFvRRTtVQAPUJZQCYvpBek...
➜ cleos set contract hello contracts/hello -p hello@active
Reading WASM from contracts/hello/hello.wasm...
Publishing contract...
executed transaction: 0af66be6acf1a40d5925ac95ac6673e87cde20b7d9dc07b355900d9a9f77ce11 1432 bytes 1133 us
warning: transaction executed locally, but may not be confirmed by the network yet ]
# eosio <= eosio::setcode {"account":"hello","vmtype":0,"vmversion":0,"code":"0061736d0100000001390b60027f7e006000017f60027f7f...
# eosio <= eosio::setabi {"account":"hello","abi":"0e656f73696f3a3a6162692f312e31000102686900010475736572046e616d650100000000...
➜ cleos push action hello hi '["eoslocalusra"]' -p eoslocalusra@active
executed transaction: a4c094be2ee7d614c3932dfd626e43c18eb0be5c8a70512bfbc9658e9ebbd030 104 bytes 3072 us
# hello <= hello::hi {"user":"eoslocalusra"}
>> Hello, eoslocalusra
warning: transaction executed locally, but may not be confirmed by the network yet ]
```

**Important note:** notice the contract folder path is actually the within the container, we're just invoking the command from the host machine `contracts/hello` is relative to the container working directory which is `/opt/application`. so the absolute path in the container would also work `/opt/applicationcontracts/hello`.

##### from within the eosio container

```
# first get into the container, eosio is an alias of docker exec -i eoslocal_eosio bash
➜ eosio
root@cb008277a194:/opt/application#
# make sure the eoslocal wallet is unlocked
root@cb008277a194:/opt/application# ./scripts/unlock.sh
+ cleos='cleos -u http://eosio:8888 --wallet-url http://wallet:8901'
+ unlock_wallet
+ echo 'unlocking default wallet...'
unlocking default wallet...
++ cat /opt/application/config/keys/eoslocal_wallet_password.txt
+ cleos -u http://eosio:8888 --wallet-url http://wallet:8901 wallet unlock --name eoslocal --password PW5KjU3AFWFip8TqiJQHS3LnNupLnCCCDSi2ZHfaY3JHTVn7p3cah
Error 3120007: Already unlocked
Error Details:
Wallet is already unlocked: eoslocal
+ sleep .5
# get the keys listed and copy one of the those for the contract account
root@cb008277a194:/opt/application# ./scripts/cleos.sh wallet keys
[
"EOS5VdFvRRTtVQAPUJZQCYvpBekYV4nc1cFe7og9aYPTBMXZ38Koy",
"EOS5k6Jht1epqZ2mnRLFVDXDTosaTneR6xFhvenVLiFfz5Ue125dL",
"EOS6Jv4RykLZQQopCBdBHSwaGoMyFxyaxFNXimqFPdEXNWqgWbG1a",
"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"EOS6TVQ6EmphCWavUuYiZMmDNYMRgbb96wgqWDncjrkvFPcpokgdD",
"EOS7CB47VMLWp49QhajE3uTuHuf9qoSeR6scUHMKGCD6LXYufRUDc",
"EOS84BLRbGbFahNJEpnnJHYCoW9QPbQEk2iHsHGGS6qcVUq9HhutG"
]
# create the account for the contract
root@cb008277a194:/opt/application# ./scripts/cleos.sh create account eosio hello EOS5VdFvRRTtVQAPUJZQCYvpBekYV4nc1cFe7og9aYPTBMXZ38Koy -p eosio@active
executed transaction: 17fec7f4557ab2bf9d1625dfe226de24c12cbb82c64e100fca6f7d5d11a32b1c 200 bytes 305 us
# eosio <= eosio::newaccount {"creator":"eosio","name":"hello","owner":{"threshold":1,"keys":[{"key":"EOS5VdFvRRTtVQAPUJZQCYvpBek...
warning: transaction executed locally, but may not be confirmed by the network yet ]
# deploy the contract
root@cb008277a194:/opt/application# ./scripts/cleos.sh set contract hello contracts/hello -p hello@active
Reading WASM from contracts/hello/hello.wasm...
Publishing contract...
executed transaction: 937f6fd6a4cfc8bc264ede7e43eb1a1ced9ffd20cd59c7c33adde665578c4de1 1432 bytes 495 us
# eosio <= eosio::setcode {"account":"hello","vmtype":0,"vmversion":0,"code":"0061736d0100000001390b60027f7e006000017f60027f7f...
# eosio <= eosio::setabi {"account":"hello","abi":"0e656f73696f3a3a6162692f312e31000102686900010475736572046e616d650100000000...
warning: transaction executed locally, but may not be confirmed by the network yet ]
# test the action
root@cb008277a194:/opt/application# ./scripts/cleos.sh push action hello hi '["eoslocalusra"]' -p eoslocalusra@active
executed transaction: a4c094be2ee7d614c3932dfd626e43c18eb0be5c8a70512bfbc9658e9ebbd030 104 bytes 3072 us
# hello <= hello::hi {"user":"eoslocalusra"}
>> Hello, eoslocalusra
warning: transaction executed locally, but may not be confirmed by the network yet ]
```


## Frequently Asked Questions

### Why Containers ?

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,16 @@
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>

using namespace eosio;

class hello : public contract {
public:
using contract::contract;

[[eosio::action]]
void hi( name user ) {
print( "Hello, ", user);
}
};

EOSIO_DISPATCH( hello, (hi))

0 comments on commit faf4aac

Please sign in to comment.