# Solidity 學習筆記 ###### tags: `區塊鏈` --- ### referance [精通ethereum-gitBook](https://cypherpunks-core.github.io/ethereumbook_zh/%E8%A1%93%E8%AA%9E.html) [bitcoinbook](https://github.com/bitcoinbook/bitcoinbook) [ethereumbook](https://github.com/ethereumbook/ethereumbook) [Metamask](https://github.com/MetaMask/metamask-extension) [awesome-blochchain-cn](https://github.com/chaozh/awesome-blockchain-cn) [merkle tree](https://blog.csdn.net/jiange_zh/article/details/53386250) [etherscan-api](https://etherscan.io/apis#contracts) --- ### 序 Solidity 是Ethereumn EVM虛擬機所使用的程式語言 ### 型別 & object `string` 字串 `int256` 256位元整數(可為負數) `uint256` 256位元正整數,其他更有`uint32``uint64`...等 `//` 註解符號 `contract <contractName>{}` 合約實體 `struct <objectName>` 建立實體物件 ### practice ``` // SPDX-License-Identifier: MIT pragma solidity >0.6.0 <0.9.0; contract SimpleStorage{ // this set variable defult zero uint256 favoriteNumbers; struct People{ uint256 favoriteNumbers; string name; } People[] public people; mapping(string => uint256) public nameTofavariteNumbers; function store(uint256 _favoriteNumbers) public{ favoriteNumbers = _favoriteNumbers; } function retreive() public view returns(uint256) { return favoriteNumbers; } function addPerson(string memory _name, uint256 _favoriteNumbers) public { people.push(People(_favoriteNumbers, _name)); nameTofavariteNumbers[_name] = _favoriteNumbers; } } ``` ## python Web3 透過python 與ethereumn 合約及節點進行互動,最初由Javascript.web3 ,而後被引進python社群。 ### 安裝 `pip install web3` ### 設定Provider 透過Providers 讓Web3.py 與 區塊鏈進行互動 最常見的節點連線方式: 1. IPC (使用本地檔案系統: 最安全且最快) >如果需要在某幾台本地機器運行`Web3.py`成為節點,使用IPC。 2. Websockets(遠端連線,比HTTP快) >需要透過不同電腦連線至節點,使用Websockets。 3. HTTP(支援多節點) >如果節點不支援Websockets,使用HTTP。 利用套件建立Provider: - Web3.IPCProvider - Web3.HTTPProvider - Web3.WebsocketProvider ``` >>> from web3 import Web3 # IPCProvider: >>> w3 = Web3(Web3.IPCProvider('./path/to/geth.ipc')) # HTTPProvider: >>> w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545')) # WebsocketProvider: >>> w3 = Web3(Web3.WebsocketProvider('ws://127.0.0.1:8546')) >>> w3.isConnected() True ``` 更多可參考[Provider 文件](https://web3py.readthedocs.io/en/stable/providers.html#providers)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up