Coder Social home page Coder Social logo

lisoleg / pyeos Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learnforpractice/pyeos

1.0 1.0 0.0 125.54 MB

A Self Evolving Universal Smart Contract Platform

License: MIT License

CMake 2.37% Dockerfile 0.04% Shell 0.83% C++ 53.49% C 2.28% Makefile 1.77% Thrift 0.02% Python 4.57% Java 0.08% WebAssembly 32.69% Objective-C 0.01% Go 1.77% Julia 0.01% Lua 0.04% Perl 0.05%

pyeos's Introduction

A Self Evolving Universal Smart Contract Platform Base on The Development of EOSIO

Table of contents

  1. Building PyEos

  2. Smart Contract Development

    2.1 Python Smart Contract Development

    2.2 Ethereum Smart Contract Development

    2.3 Lua Smart Contract Development

    2.4 Java Smart Contract Development

  3. Debugging With C++ Smart Contract

  4. Debugging With Python Smart Contract

  5. PyEos api overview

Building PyEos

Downloading Source Code

git clone https://www.github.com/learnforpractice/pyeos
cd pyeos
git submodule update --init --recursive

Installing dependencies (Ubuntu)

sudo apt-get install libleveldb-dev
sudo apt-get install libreadline-dev

Installing dependencies (macOS)

brew install leveldb
brew install readline

Installing JDK 10 or above (Optional)

For these who want to try Java Smart Contract, please update JDK to version 10 or above, then export JAVA_HOME to the root directory of JDK install directory.

export JAVA_HOME=<directory of jdk>

If you have early JDK installed, and JAVA_HOME is not empty and you don't want to try Java smart contract, please empty JAVA_HOME with the following command before build:

export JAVA_HOME=

Building

./eosio_build.sh

Smart Contract Development

Python Smart Contract Development

Running PyEos

Open a terminal, cd to [PROJECT_DIR]/build/program, run the following command

./pyeos/pyeos --manual-gen-block --debug -i  --contracts-console

If it's the first time you start PyEos, PyEos will create a testing wallet for you, which placed in data-dir/mywallet.wallet, and then console will print the wallet password as below:

wallet password: PW5JWE5g6RZ7Fyr2kmCphDqZo4uivdeGpUpndgFZ52rsduhtf9PRJ

Since it's for testing only, password will save to data-dir/data.pkl, So next time you start pyeos for testing, pyeos will unlock wallet for you.

Also PyEos will import three private key to the wallet, which is useful for testing.

'5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3',
'5JEcwbckBCdmji5j8ZoMHLEUS8TqQiqBG1DRx1X9DN124GUok9s',
'5JbDP55GXN7MLcNYKCnJtfKi9aD2HvHAdY7g8m67zFTAFkY1uBB'

Keep in mind that these private key should never be used in real account, otherwise you may lose all of your crypto property in the account.

Beside that, PyEos will create four important accounts for you:

eosio.bios, eosio.msig, eosio.system, eosio.token

and publish their smart contract on testnet.

Although the above steps will never happen in the real world, but it's really provide a great convenience for testing smart contract. Thus save a lot of your precious time and make the development more efficient.

Generating source code with sketch tool

Run the following command in PyEos console,

sketch.create('hello', 'helloworld', 'py')

That will create a helloworld directory under your current directory with hello as the testing account name. There are three file generated in the directory:

helloworld.py
helloworld.abi
t.py

Which helloworld.py is the Python smart contract source code, helloworld.abi is the ABI(Application Binary Interface) file for smart contract, t.py contains code for testing smart contract.

In addition, sketch can also create a wasm smart contract project for you, just type the following code in PyEos console, and the testing process has no difference with Python smart contract.

sketch.create('helloworld', 'helloworld', 'cpp')

Testing

Now it's time to run your helloworld smart contract program. Type or copy the following command to the PyEos console:

from helloworld import t
t.test()

You will see the following output on console in green words:

3289633ms thread-1   mpeoslib.cpp:63               print                ] hello,world

Congratulations, you have successfully run your first Python smart contract.

Now you can open helloworld.py for coding. Once it's done, just run t.test() again, there is no need to run other command to publish your testing smart contract, the smart contract will be automatically republish to the testnet if it's been changed during the running of t.test(). You can also edit the testing code in t.py for testing your smart contract. Once it's done, just run t.test() again, there is no need to run reload(t), PyEos will do the magic for you. That also works at the situation of adding a new function in test.

Ethereum Smart Contract Development

Please see the example in pyeos/programs/pyeos/tests/evm/evm

Lua Smart Contract Development

Please see the example in pyeos/programs/pyeos/tests/lua/hello

Java Smart Contract Development

Please see the example in pyeos/programs/pyeos/tests/java/hello

Reminder: for this want to try Java Smart Contract, please update JDK to version 10 or above. And then set JAVA_HOME to the appropriate JDK or JRE root directory

Debugging With C++ Smart Contract

On Eos, C++ Smart Contract code is compiled to WebAssembly bytecode, that makes debugging C++ suffer. Fortunately now it's able to compile C++ Smart Contract to a shared library, that makes debugging a C++ Smart Contract as easy as debugging a normal C++ project.

There is a short video on youtube for quick start:

Debugging

To be brief, here are the steps about debugging a C++ Smart Contract:

1. Open pyeos project in Visual Studio Code

2. Edit CMAKE_BUILD_TYPE and BUILD_DIR in eosio_build.sh

    BUILD_DIR="${PWD}/build-debug"
    CMAKE_BUILD_TYPE=Debug

3. Build pyeos in VSC terminal

./eosio_build.sh

4.Configure debug in Visual Studio Code

      {
         "name": "(lldb) Attach pyeos",
         "type": "cppdbg",
         "request": "attach",
         "program": "${workspaceFolder}/build-debug/programs/pyeos/pyeos",
         "processId": "${command:pickProcess}",
         "MIMode": "lldb"
      }

5. Launch pyeos

./pyeos/pyeos --manual-gen-block --debug -i  --contracts-console

6. Attach to pyeos

7. Create C++ Smart Contract test code.

sketch.create('hello', 'helloworld', 'cpp')

8. Set breakpoint in test code

9. Testing

from helloworld import t
t.debug()

Python Smart Contract Debugging

There is a short video on youtube about Python Smart Contract for quick start:

Debugging

The following steps show how to debug smart contract under programs/pyeos/contracts/hello

1. Launch PyEos

./pyeos/pyeos --manual-gen-block --debug -i  --contracts-console

2. Set debug contract

debug.set_debug_contract('hello', '../../programs/pyeos/contracts/hello/hello.py')

3. Start ptvsd debugger

import ptvsd
ptvsd.enable_attach("12345", address = ('127.0.0.1', 3000))
ptvsd.wait_for_attach()

4. Attach to ptvsd debugger in Visual Studio Code

Here is the debug setting:

   {
      "name": "python Attach (Remote Debug)",
      "type": "python",
      "request": "attach",
      "localRoot": "${workspaceFolder}",
      "remoteRoot": "${workspaceFolder}",
      "port": 3000,
      "secret": "12345",
      "host": "localhost"
   },

5. Set breakpoint in hello.py

6. Debugging

from hello import t
t.test()

Enjoy it!

PyEos api overview

eosapi.get_info

info = eosapi.get_info()
info
{
    "server_version": "00000000",
    "head_block_num": 127,
    "last_irreversible_block_num": 126,
    "head_block_id": "0000007fbd1ff82f29668dfa89d927a0510c657cce292c033496ccaacf04c12a",
    "head_block_time": "2018-05-06T07:57:44",
    "head_block_producer": "eosio"
}
info.head_block_time
'2017-09-23T15:16:18'
info.head_block_num
18624

wallet.create

PyEos will create a testing wallet for you the first time you start it, but you can also create other wallet wit wallet.create API

psw = wallet.create('mywallet2')
psw
'PW5JCWXaGkA15s6th6AWCabHewuGASAtrUJjTWoL1Ybx6sG9QzrSb'

You can see your wallet now. The * behind mywallet means the wallet is unlocked.

wallet.list_wallets()
['mywallet *']

wallet.open wallet.unlock

wallet.open('mywallet2')
wallet.unlock('mywallet2','PW5JCWXaGkA15s6th6AWCabHewuGASAtrUJjTWoL1Ybx6sG9QzrSb')

wallet.import_key

Let's import the private key of inita. Please refer to Setting up a wallet and importing account key for more information.

wallet.import_key('mywallet2','5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3')

eosapi.create_key

Create owner key

eosapi.create_key()
{'public': 'EOS61MgZLN7Frbc2J7giU7JdYjy2TqnfWFjZuLXvpHJoKzWAj7Nst', 'private': '5JEcwbckBCdmji5j8ZoMHLEUS8TqQiqBG1DRx1X9DN124GUok9s'}

Create active key

eosapi.create_key()
{'public': 'EOS5JuNfuZPATy8oPz9KMZV2asKf9m8fb2bSzftvhW55FKQFakzFL', 'private': '5JbDP55GXN7MLcNYKCnJtfKi9aD2HvHAdY7g8m67zFTAFkY1uBB'}

Import owner key

wallet.import_key('mywallet2','5JEcwbckBCdmji5j8ZoMHLEUS8TqQiqBG1DRx1X9DN124GUok9s')

Import active key

wallet.import_key('mywallet2','5JbDP55GXN7MLcNYKCnJtfKi9aD2HvHAdY7g8m67zFTAFkY1uBB')

eosapi.create_account

key1 = 'EOS61MgZLN7Frbc2J7giU7JdYjy2TqnfWFjZuLXvpHJoKzWAj7Nst'
key2 = 'EOS5JuNfuZPATy8oPz9KMZV2asKf9m8fb2bSzftvhW55FKQFakzFL'
eosapi.create_account('eosio', 'currency', key1, key2)

eosapi.get_account

eosapi.get_account('currency')
{
    "account_name": "currency",
    "head_block_num": 43,
    "head_block_time": "2018-07-11T09:01:00.500",
    "privileged": false,
    "last_code_update": "1970-01-01T00:00:00.000",
    "created": "2018-07-11T09:01:00.500",
    "ram_quota": 65206,
    "net_weight": 10050,
    "cpu_weight": 10050,
    "net_limit": {
        "used": 0,
        "available": 62988768000,
        "max": 62988768000
    },
    "cpu_limit": {
        "used": 0,
        "available": 12013286400,
        "max": 12013286400
    },
    "ram_usage": 3446,
    "permissions": [
        {
            "perm_name": "active",
            "parent": "owner",
            "required_auth": {
                "threshold": 1,
                "keys": [
                    {
                        "key": "EOS61MgZLN7Frbc2J7giU7JdYjy2TqnfWFjZuLXvpHJoKzWAj7Nst",
                        "weight": 1
                    }
                ],
                "accounts": [],
                "waits": []
            }
        },
        {
            "perm_name": "owner",
            "parent": "",
            "required_auth": {
                "threshold": 1,
                "keys": [
                    {
                        "key": "EOS5JuNfuZPATy8oPz9KMZV2asKf9m8fb2bSzftvhW55FKQFakzFL",
                        "weight": 1
                    }
                ],
                "accounts": [],
                "waits": []
            }
        }
    ],
    "total_resources": {
        "owner": "currency",
        "net_weight": "1.0050 EOS",
        "cpu_weight": "1.0050 EOS",
        "ram_bytes": 65206
    },
    "self_delegated_bandwidth": {
        "from": "currency",
        "to": "currency",
        "net_weight": "1.0050 EOS",
        "cpu_weight": "1.0050 EOS"
    },
    "refund_request": null,
    "voter_info": {
        "owner": "currency",
        "proxy": "",
        "producers": [],
        "staked": 20100,
        "last_vote_weight": 0.0,
        "proxied_vote_weight": 0.0,
        "is_proxy": 0
    }
}

eosapi.get_balance

eosapi.get_balance('eosio')

return:

9999999961645.494

eosapi.transfer

eosapi.transfer('eosio', 'currency', 100.6666)

return:

True

eosapi.push_action

eosapi.get_balance('eosio')
eosapi.get_balance('currency')
args = {"from":'eosio', "to":'currency', "quantity":'100.6666 EOS', "memo":'hello'}
r = eosapi.push_action('eosio.token', 'transfer', args, {'eosio':'active'})
eosapi.get_balance('eosio')
eosapi.get_balance('currency')
>>> eosapi.get_balance('eosio')
9999999961142.162
>>> eosapi.get_balance('currency')
513.9996
>>> args = {"from":'eosio', "to":'currency', "quantity":'100.6666 EOS', "memo":'hello'}
>>> r = eosapi.push_action('eosio.token', 'transfer', args, {'eosio':'active'})
>>> eosapi.get_balance('eosio')
9999999961041.496
>>> eosapi.get_balance('currency')
614.6662

the above code is what eosapi.transfer does.

eosapi.push_actions

r = get_balance('currency')
print(r)
args = {"from":'eosio', "to":'currency', "quantity":'100.6666 EOS', "memo":'hello'}
args = eosapi.pack_args('eosio.token', 'transfer', args)
act = ['eosio.token', 'transfer', {'eosio':'active'}, args]
r = eosapi.push_actions([act, act])
r = eosapi.get_balance('currency')
print(r)
>>> r = get_balance('currency')
>>> print(r)
211.9998
>>> args = {"from":'eosio', "to":'currency', "quantity":'100.6666 EOS', "memo":'hello'}
>>> args = eosapi.pack_args('eosio.token', 'transfer', args)
>>> act = ['eosio.token', 'transfer', {'eosio':'active'}, args]
>>> r = eosapi.push_actions([act, act])
>>> r = eosapi.get_balance('currency')
>>> print(r)
413.333

eosapi.get_transaction

r = eosapi.get_transaction('f6c43148dfac54105031fbaf966958d36309dd94e665c506eb2769e43febedba')
r
r.transaction.signatures
r.transaction.packed_trx

eosapi.set_contract

Publish python smart contract to the blockchain

r = eosapi.set_contract('hello','../../programs/pyeos/contracts/hello/hello.py','../../contracts/hello/hello.abi',1)
r

pyeos's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.