合約量化開發上線版,合約量化系統開發技術邏輯及詳細方案,合約量化原始碼

xiaofufu發表於2023-02-20

  (base)appledeMac-mini-3:Quantification apple$pip install web3


  Collecting web3


  Using cached


  Collecting eth-typing<3.0.0,>=2.0.0(from web3)


  Using cached


  Collecting eth-account<0.5.0,>=0.4.0(from web3)


  Using cached


  Requirement already satisfied:requests<3.0.0,>=2.16.0 in/Users/apple/.local/lib/python3.7/site-packages(from web3)(2.22.0)


  Collecting hexbytes<1.0.0,>=0.1.0(from web3)


  設定環境變數


  web3環境變數


  export WEB3_INFURA_PROJECT_ID=獲取到的專案ID



  使用該web3.auto.infura模組連線到Infura節點。


  (base)appledeMac-mini-3:Quantification apple$python


  Python 3.7.3(default,Mar 27 2019,16:54:48)


  [Clang 4.0.1(tags/RELEASE_401/final)]::Anaconda,Inc.on darwin


  Type"help","copyright","credits"or"license"for more information.


  from web3.auto.infura import w3


  w3.eth.blockNumber


  9913260


 


  w3,該例項現在將允許您與以太坊區塊鏈進行互動


  獲取最新塊的資訊


  w3.eth.getBlock('latest')


  AttributeDict({'difficulty':2261993248924870,'extraData':HexBytes('0x505059452d65746865726d696e652d6575312d35'),'gasLimit':9966590,'gasUsed':9954427,'hash':HexBytes('0x64a043f893abcd0a8e424c64b4104660033eba1f018371ca4d3bec72bf23cc46'),'logsBloom':HexBytes('0x08c24c5800c2201e0000a221708670036280c5012ca8409c18340ff1536800a4800c07e13210ac07060081740f5a0bc963820400099425c1282ac87120a10e2c18008d84b02080010405e66d808f6424e4132f86c1464200024302049cc430d14802a8160a2061c0d200507001a1482a8841f2011c0c1e22300405b424a6402025802b4c14642806b321a6ab8b020407200108436aa280828008088020313964a6c41062000281049c080ad64a8811918d042845c04e00a0086a8488988008d10620244693880d28840210115ee041420b04f00184080801f80000a217402306e85139035090188407600630c18122380c81185385b105e6ed2d120f389058c0'),'miner':'0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8','mixHash':HexBytes('0xbe86772ffde4f025efae9dc14063d3a906f2f1ac675c8ce8e6a35725218e1d8a'),'nonce':HexBytes('0xb18c36a40281d83a'),'number':9913288,'parentHash':HexBytes('0xaa772d2645331b7d7e44286f398de7e0d3225afadd2fd54684e9d46ef71ac576'),'receiptsRoot':HexBytes('0x7f0fa855dd4c7a9576d4ece8847dd87e82a77c25c9b733084e6f44f1940dbe4b'),'sha3Uncles':HexBytes('0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'),'size':34719,'stateRoot':HexBytes('0xbfc8504b7f8ad32e0763c18efced31c0ce9976944ea1f80322108f020621a1a2'),'timestamp':1587438028


  


  將要執行的許多典型操作都在w3.ethAPI中,web3物件通常透過連線到JSON-RPC伺服器來提供與以太坊區塊鏈進行互動的API。


  1.web3連線到區塊鏈的方式


  from web3 import Web3,HTTPProvider,IPCProvider,WebsocketProvider


  """


  HTTPProvider:用於連線基於http和https的JSON-RPC伺服器:透過完整的URI找到伺服器


  w3=Web3(HTTPProvider('))


  Web3.IPCProvider用於連線到基於ipc套接字的JSON-RPC伺服器:透過檔案系統路徑找到IPC套接字


  w3=Web3(IPCProvider(引數))


  Web3.WebsocketProvider用於連線到基於ws和wss websocket的JSON-RPC伺服器:透過完整的URI找到伺服器


  w3=Web3(WebsocketProvider('ws://127.0.0.1:8546'))


  """


  w3=Web3(HTTPProvider('))


  print(w3)#<web3.main.Web3 object at 0x105d42510


  2.型別轉化


  Web3.toHex(primary=None,hexstr=None,text=None)


  接受各種輸入並以其十六進位製表示形式返回。它遵循JSON-RPC規範


  def to_hex(


  primitive:Primitives=None,hexstr:HexStr=None,text:str=None


  )->HexStr:


  print(Web3.toHex(10))#0xa


  print(Web3.toHex(hexstr='0x00'))#0x00


  print(Web3.toHex(text='asimov'))#0x6173696d6f76


  Web3.toText(primary=None,hexstr=None,text=None)


  接受各種輸入並返回其等效字串。文字被解碼為UTF-8。


  print(Web3.toText('0x1254'))#T


  print(Web3.toText('0x6173696d6f76'))#asimov


  print(Web3.toText(b'asimx6fx76'))#asimov


  print(Web3.toText('6173696d6f76'))#asimov


  Web3.toBytes(primary=None,hexstr=None,text=None)


  接受各種輸入並返回等效的位元組數。文字被編碼為UTF-8。


  print(Web3.toBytes(0))#b'x00'


  print(Web3.toBytes(b'sasas'))#b'sasas'


  print(Web3.toBytes(hexstr='000F'))#b'x00x0f'


  print(Web3.toBytes(hexstr='0x000F'))#b'x00x0f'


  print(Web3.toBytes(text='asimov'))#b'asimov'


  Web3.toInt(primary=None,hexstr=None,text=None)


  接受各種輸入並返回其等效的整數


  print(Web3.toInt(0))#0


  print(Web3.toInt(0x00f))#15


  print(Web3.toInt(b'x00x0F'))#15


  print(Web3.toInt(hexstr='0x00F'))#15


  ValueError:invalid literal for int()with base 10:'sa'


  text:interpret as string of digits,like'12'=>12


  print(Web3.toInt(text='10'))#10


  Web3.toJSON(obj)obj:Dict[Any,Any]


  接受各種輸入並返回等效的JSON。


  print(Web3.toJSON({'asimov':'da'}))#{"asimov":"da"}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69956839/viewspace-2936035/,如需轉載,請註明出處,否則將追究法律責任。

相關文章