YashengChen / iota-python-example

IOTA-python client 零費用交易串接使用範例

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IOTA - python

tags: 區塊鏈
主旨: Iota python 套件範例
更新日期: 2022-02-25
版本: V1
作者: Ya-Sheng Chen (Rock)

IOTA簡介

IOTA 是第三代公開無權限(permissionless) 分散式帳本, 其網路所使用的為有向無環圖( Directed Acyclic Graph, 簡稱 ) , IOTA 稱呼此 DAG 為「Tangle

開發理念

無需交易費、 速度更快、 更具擴充性的交易平台, 更在於加強物聯網( Internet of Things),紀錄和執行物聯網生態中不同設備間的資料傳輸, 提供一個人類和電腦可以交換資料的特殊網絡。

IOTA不是區塊鏈

團隊運用分散式帳本技術, 針對物聯網打造了創新的 Tangle 網路, 與區塊鏈一樣意圖創立一個去中心化的世界,TangleIOTA 開發團隊基於有向無環圖(DAG) 技術所打造的去中心化網路系統, Tangle 並不是區 塊鏈, 因為 Tangle 的世界中既沒有鏈, 沒有區塊, 也沒有礦工。

IOTA 特點

● 擴展性 (Scalability) ● 去中心化 (Decentralisation) ● 無交易手續費 (No transaction fees) ● 抵抗量子計算 (Quantum computing protection)


透過Python iota client 套件將資訊上鏈

安裝

  1. 依自己Python版本, 下載官方 python-client 套件.zip檔案,並解壓縮至您的project 資料夾
  2. 進入您需要安裝套件的python 環境
    • cd ./Document/project/iota_test
    • source ./venv/bin/activite
  3. 安裝套件
    • pip install iota_client_python-0.2.0_alpha.3-cp38-abi3-linux_x86_64.whl
  4. 完成
    • 檢視安裝套件是否完成 pip list
Package                    Version
-------------------------- -------
iota-client-python           0.2.0a3

使用

載入套件

import iota_client

init 連線

使用預設或自定義連線
# 預設
client = iota_client.Client()

# 自定義<option>
client = iota_client.Client(network='devnet',
                            nodes_name_password=[['https://api.lb-1.h.chrysalis-devnet.iota.cafe']])
  • net_type = [deafult="devnet", "mainnet"] # 主網與測試網
  • node_addr= 預設測試網址https://api.lb-1.h.chrysalis-devnet.iota.cafe 進行算力並上傳交易使用的節點地址。

取得節點資訊

client.get_info()

response

{
   "nodeinfo":{
      "name":"HORNET",
      "version":"0.6.0-alpha",
      "is_healthy":true,
      "network_id":"testnet7",
      "bech32_hrp":"atoi",
      "min_pow_score":4000.0,
      "messages_per_second":27.3,
      "referenced_messages_per_second":34.5,
      "referenced_rate":126.37362637362637,
      "latest_milestone_timestamp":1618133322,
      "latest_milestone_index":33602,
      "confirmed_milestone_index":33602,
      "pruning_index":16086,
      "features":[
         "PoW"
      ]
   },
   "url":"https://api.hornet-0.testnet.chrysalis2.com"
}

節點是否正常運作

client.get_health()
>> True

傳送zero_value_transaction 無費用交易

  • index <str> = 為你的資訊設定index名稱,之後可透過此index 於 區塊鏈瀏覽器查詢這筆資訊
  • data_str <str>= 需要上傳的資訊
idx_name = "Rock_1"
mssg     = "hello world 1"

# sending message
result   = client.message(index=idx_name, data_str=mssg)

完成

  • 可於 iota tangle explorer 查詢 透過切換 [mainnet,devnet] 查詢主網或測試網上的上鏈資訊

完整code

import iota_client

try:
    # init connect
    client = iota_client.Client()

    # check node health
    is_health = client.get_health()
    if not is_health:
        raise ValueError('node is not healthy, please check node is on, or change node address')
    
    # prepare index and mssg
    for i in range(2):
        idx_name = f'Rock' 
        mssg     = f'hello world {i}'
        
        # send message
        client.message(index=idx_name, data_str=mssg)
        
except ValueError as ve:
    print(ve)

except Exception as e:
    print(e)

reference

IOTA 介紹

develop resurce

infomation

networks intro

node


About

IOTA-python client 零費用交易串接使用範例


Languages

Language:Jupyter Notebook 100.0%