项目作者: mile-core

项目描述 :
MILE Blockchain Common Supported Architecture API + Json-Rpc client.
高级语言: Python
项目地址: git://github.com/mile-core/mile-csa-python.git
创建时间: 2018-10-11T17:12:16Z
项目社区:https://github.com/mile-core/mile-csa-python

开源协议:MIT License

下载


Requirements

  1. Python3
  2. setuptools
  3. git
  4. gcc >= 7.0
  5. boost >= 1.66.0
  6. openssl (libssl-dev)

Tested on:

  1. Centos 7 (gcc 7.x)
  2. Ubuntu 17.04
  3. OSX 10.13

Build & Install

Global

  1. $ git clone https://github.com/mile-core/mile-csa-python
  2. $ cd mile-csa-python
  3. $ git submodule update --init --recursive --remote
  4. $ python3 ./setup.py build
  5. $ sudo python3 ./setup.py install

In virtual environment via pipenv

  1. $ pipenv install -e git+https://github.com/mile-core/mile-csa-python@master#egg=milecsa

Also you can use specific version: just replace master with target version

Boost updates (if it needs)

  1. $ wget https://dl.bintray.com/boostorg/release/1.66.0/source/boost_1_66_0.tar.gz
  2. $ tar -xzf boost_1_*
  3. $ cd boost_1_*
  4. $ ./bootstrap.sh --prefix=/usr
  5. $ ./b2 install --prefix=/usr --with=all -j4

Test

  1. $ python3 -m unittest

Examples

Global configuration

  1. from milecsa import config
  2. #
  3. # disable SSL verification
  4. #
  5. # config.sslVerification = False
  6. #
  7. # set up user defined connection timeout
  8. #
  9. config.connectionTimeout = 30 # seconds
  10. #
  11. # use MILE testnet
  12. #
  13. config.web.url = "https://wallet.testnet.mile.global"

Wallet create

github

  1. from milecsa import Wallet
  2. def main():
  3. wallet0 = Wallet(phrase="Some phrase")
  4. print(wallet0.public_key, wallet0.private_key)
  5. #
  6. # Put your address
  7. #
  8. wallet1 = Wallet(public_key="...")
  9. state = wallet1.get_state()
  10. print(state.balances, state.preferred_transaction_id, wallet1.public_key)
  11. for b in state.balances:
  12. print(b)
  13. #
  14. # Keep Secret phrase to qrcode
  15. #
  16. wqr0 = wallet0.phrase_qr()
  17. print(wqr0)
  18. wqr0.save("./img-wqr0.png")
  19. #
  20. # Generate Payment Ticket
  21. #
  22. wqr1 = wallet0.payment_qr(asset=1, amount=10)
  23. print(wqr1)
  24. wqr1.save("./img-wqr1.png")
  25. if __name__ == '__main__':
  26. main()

Transfer

github

  1. from milecsa import Transfer, Wallet
  2. import time
  3. def main():
  4. src = Wallet(phrase="Some WTF!? secret phrase")
  5. print(src.public_key, src.private_key)
  6. dst = Wallet()
  7. dst_public_key = Wallet().public_key
  8. state = src.get_state()
  9. trx0 = Transfer(src=src, dest=dst, asset_code=1, amount=0.001)
  10. trx1 = Transfer(src=src, dest=dst_public_key, asset_code=1, amount=1)
  11. print(trx0.data)
  12. print(trx1.data)
  13. #
  14. # Put your address
  15. #
  16. src = Wallet(phrase="secret-phrase")
  17. print(src.public_key, src.private_key)
  18. #
  19. # Put your address
  20. #
  21. dst = Wallet(phrase="destination-secret-phrase")
  22. result = src.transfer(dest=dst, asset_code=0, amount=0.1, description="give my money back!")
  23. print(result)
  24. time.sleep(21)
  25. state = dst.get_state()
  26. for b in state.balances:
  27. print(b)
  28. if __name__ == "__main__":
  29. main()

Do XDR Emission

github

  1. from milecsa import Wallet
  2. import time
  3. def main():
  4. #
  5. # Put your address
  6. #
  7. wallet = Wallet(private_key="...")
  8. result = wallet.emission(asset_code=1)
  9. print(result)
  10. time.sleep(42)
  11. state = wallet.get_state()
  12. for b in state.balances:
  13. print(b)

Explore blocks

github

  1. import pprint
  2. from milecsa import Chain, config
  3. #
  4. # Print block state
  5. #
  6. def print_block(chain, block):
  7. print("Block Id: ", block.block_id)
  8. print("Version: ", block.version)
  9. print("Timestamp: ", block.timestamp)
  10. print("Trx count: ", block.transaction_count)
  11. #
  12. # Get bloc transaction
  13. #
  14. pp = pprint.PrettyPrinter(indent=2)
  15. for t in block.transactions:
  16. asset = chain.asset_name(t.asset_code)
  17. pp.pprint([asset, t.__class__.__name__, t.__dict__])
  18. def main():
  19. #
  20. # Set your full node url
  21. #
  22. config.web.url = "https://wallet.testnet.mile.global"
  23. #
  24. # Enable client balancing
  25. #
  26. config.useBalancing = True
  27. #
  28. # Enable debug printing
  29. #
  30. config.rpcDebug = False
  31. # Open chain
  32. chain = Chain()
  33. # get chain state
  34. state = chain.get_state()
  35. # last block id == state.block_count-1
  36. print("Block count: ", state.block_count)
  37. #
  38. # iterate last 20 blocks
  39. #
  40. print()
  41. for bid in range(state.block_count - 20, state.block_count):
  42. block = chain.get_block(block_id=bid)
  43. print_block(chain, block)
  44. print()
  45. if __name__ == "__main__":
  46. main()