{"id":26973978,"url":"https://github.com/takeshiyu/ethereum-practice","last_synced_at":"2026-05-06T10:33:49.055Z","repository":{"id":119089308,"uuid":"137179976","full_name":"takeshiyu/ethereum-practice","owner":"takeshiyu","description":null,"archived":false,"fork":false,"pushed_at":"2018-06-13T07:42:48.000Z","size":4600,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-04-24T07:23:27.333Z","etag":null,"topics":["ethereum","gin","go-eth","go-ethereum","golang","testrpc"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/takeshiyu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-06-13T07:37:10.000Z","updated_at":"2020-10-15T13:54:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"7909b66f-fa31-41df-867e-8627e26508dd","html_url":"https://github.com/takeshiyu/ethereum-practice","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/takeshiyu/ethereum-practice","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takeshiyu%2Fethereum-practice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takeshiyu%2Fethereum-practice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takeshiyu%2Fethereum-practice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takeshiyu%2Fethereum-practice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/takeshiyu","download_url":"https://codeload.github.com/takeshiyu/ethereum-practice/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takeshiyu%2Fethereum-practice/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32689177,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-06T08:33:17.875Z","status":"ssl_error","status_checked_at":"2026-05-06T08:33:17.221Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ethereum","gin","go-eth","go-ethereum","golang","testrpc"],"created_at":"2025-04-03T10:19:54.007Z","updated_at":"2026-05-06T10:33:49.033Z","avatar_url":"https://github.com/takeshiyu.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ethereum Practice\n\n專案的目的在實作與練習 Ethereum 以及 JSON RPC\n\n## 安裝\n\n專案是透過 [Gin Web Framework](https://github.com/gin-gonic/gin) 開發，並配合 [Dep](https://github.com/golang/dep) 確保使用的相關套件能夠正確的安裝。假設欲執行專案的環境裡已經有安裝了 [Dep](https://github.com/golang/dep)，請進入專案目錄後 ( $HOME/go/src/github.com/s12i/maicon-fullstack-test )，執行命令：\n\n```\n$ dep ensure\n```\n\n完成安裝後，可以在專案下發現多出一個 `vendor` 目錄，裡面放置了專案所需的相關第三方套件：\n\n```\n-rw-r--r--  1 s12i  staff   2.2K  3 12 01:21 Gopkg.lock\n-rw-r--r--  1 s12i  staff   738B  3 11 12:04 Gopkg.toml\n-rw-r--r--@ 1 s12i  staff   546B  3 12 01:20 README.md\ndrwxr-xr-x  8 s12i  staff   256B  3 11 23:50 app\n-rw-r--r--  1 s12i  staff   747B  3 12 00:55 server.go\ndrwxr-xr-x  5 s12i  staff   160B  3 12 01:21 vendor\n```\n\n爾後，執行下述命令啟動專案：\n\n```\n$ go run server.go\n```\n\n或者，直接執行預先編譯產生的執行檔 ( Mac 環境 )：\n\n```\n$ ./server\n```\n\n若執行成功，可以看見如下的訊息：\n\n```\n[GIN-debug] [WARNING] Running in \"debug\" mode. Switch to \"release\" mode in production.\n - using env:\texport GIN_MODE=release\n - using code:\tgin.SetMode(gin.ReleaseMode)\n\n[GIN-debug] GET    /node                     --\u003e github.com/s12i/maicon-fullstack-test/app.GetNodeInfo (4 handlers)\n[GIN-debug] GET    /block/:blockNumber       --\u003e github.com/s12i/maicon-fullstack-test/app.GetBlockInfo (4 handlers)\n[GIN-debug] GET    /trx/:trxHash             --\u003e github.com/s12i/maicon-fullstack-test/app.GetTrxInfo (4 handlers)\n[GIN-debug] POST   /trx                      --\u003e github.com/s12i/maicon-fullstack-test/app.SendTrx (4 handlers)\n[GIN-debug] PUT    /miner/:threads           --\u003e github.com/s12i/maicon-fullstack-test/app.StartMine (4 handlers)\n[GIN-debug] DELETE /miner                    --\u003e github.com/s12i/maicon-fullstack-test/app.StopMine (4 handlers)\n[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default\n[GIN-debug] Listening and serving HTTP on :8080\n```\n\n如訊息所顯示，專案存取位置為 `http://localhost:8080`，並提供了 6 個 API 供存取區塊鏈，各 API 的使用方式將在下方敘述：\n\n* GET /node\n* GET /block/:blockNumber\n* GET /trx/:trxHash\n* POST /trx\n* PUT /miner/:threads\n* DELETE /miner\n\n另外，為了測試與示範，專案開發過程使用 `testrpc` 來建立乙太坊測試環境，可以透過下述命令安裝所需的工具 ( 需要先安裝 NodeJS )：\n\n```\nnpm install -g ethereumjs-testrpc\n```\n\n安裝完成後，啟動 `testrpc`：\n\n```\n$ testrpc\nEthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)\n\nAvailable Accounts\n==================\n(0) 0xf9a022e734d573f92943ffd34d203dc16d6dcbcd\n(1) 0x6989ff0102d99c804b8b6459cc720b433e5c46c1\n(2) 0xd079d212d9c5cddf0e180064b625c76dd213e9ce\n(3) 0xaff2ba4ca4e02de13a8f4b71a1ddccd59bb792f9\n(4) 0x403047f4f6b05c4334af1e302a2703261e43abce\n(5) 0x9bf45892cbef1cf5c2f3fc6ebbae0b6dd3cec0d9\n(6) 0xd837e2540a1381c0cae1a8f8ad95db5f11cdb28d\n(7) 0x9c3a85c52efa119b9326c1b83ef748f994c17d2a\n(8) 0xb3d80df2975100b23135932c782fedaa64328e42\n(9) 0x6b3d042e3ac7653b0e6e926572621335182e80e2\n\nPrivate Keys\n==================\n(0) c37003d3f0d9ca886b8ebe01e400cf63927c85c772e579b2f780293ad6fd77f9\n(1) b3f98d6b254caddb1cbbc1b8791a309910555a075abdb1ca4f888612d26f6eec\n(2) 32196ead0f80fa8a8805730f01564ddba969a77d14130929a60551a9a0a3c3df\n(3) 55d5f75a5c72155007a606b836cc0e9bfd031a5b1d2d1a34e4255e64b8f5237d\n(4) 22fc6511d734e60236f5c3071e52e2ab181c0800b32a6935b526d7d8ed6ac045\n(5) e3482a6778338c2fa8a65b30b9261d3b4bd2e2284830650401e4f0ba58f8c05d\n(6) 48a70620e59747a8c9e9bd26cbf22c4df63d508c2c05354e215b2ee711b9ac7f\n(7) b04ad2976fa5c85e26af1e87f26a3ad0863ba8054aea37fcef3a3d7e365d1cf0\n(8) 7dacb0f7b983d07d664802208d469874291474fe53862ff9000832f5ea1c3190\n(9) 302c62ffbaee893cf474558586917dbfe5e2b605b675f2ca3729ae71d42a119b\n\nHD Wallet\n==================\nMnemonic:      oxygen scrap post cost virus absurd scare shuffle bar blush vote brief\nBase HD Path:  m/44'/60'/0'/0/{account_index}\n\nListening on localhost:8545\neth_sendTransaction\n```\n可以看見 `testrpc` 已預先建立了 10 組帳號 ( 並包含預設數量的乙太幣 )，稍後說明 API 的使用時，將會以 `testrpc` 所建立的測試帳號做說明。但 `testrpc` 並未包含 `admin` 模組：\n\n```\n$ geth attach http://localhost:8545\nWelcome to the Geth JavaScript console!\n\ninstance: EthereumJS TestRPC/v2.0.2/ethereum-js\ncoinbase: 0xc78270f5ff9121689a4e6205cd549efe51756fde\nat block: 0 (Mon, 12 Mar 2018 01:45:20 CST)\n modules: eth:1.0 evm:1.0 net:1.0 personal:1.0 rpc:1.0 web3:1.0\n\n$ rpc.modules\n{\n  eth: \"1.0\",\n  evm: \"1.0\",\n  net: \"1.0\",\n  personal: \"1.0\",\n  rpc: \"1.0\",\n  web3: \"1.0\"\n}\n```\n\n為了可以讓 API 順利取回節點資訊，會需要存取 `admin` 模組，因此需要實際安裝 [Go-ethereum (geth)](https://github.com/ethereum/go-ethereum/)，假設環境已經安裝了 [Go-ethereum (geth)](https://github.com/ethereum/go-ethereum/)，請執行下述命令啟動：\n\n```\n$ geth --identity maicoin-test init genesis.json --datadir privatechain\n```\n接著產生一位使用者：\n\n```\n$ geth account new\nINFO [03-12|02:06:05] Maximum peer count                       ETH=25 LES=0 total=25\nYour new account is locked with a password. Please give a password. Do not forget this password.\nPassphrase:\n```\n\n填入密碼後 ( Passphrase )，將回回傳該使用者的編號 ( 編號將與範例說明不同 )：\n\n```\nAddress: {443b2f62f649929d54ef1c7eaa11948e9901058d}\n```\n\n我們將使用該帳號來驗證開始挖礦以及停止挖礦的 API。要啟動 `geth` 可以執行如下命令，將會在專案下產生一個私有區塊資料用的目錄 `privatechain`，接著執行：\n\n```\ngeth --datadir privatechain \\\n     --networkid 1116 \\\n     --rpc --rpcapi admin,db,eth,net,web3,personal,miner \\\n     --cache=1024 --rpcport 8545 --rpcaddr 127.0.0.1 --rpccorsdomain \"*\" \\\n     --etherbase=443b2f62f649929d54ef1c7eaa11948e9901058d\n```\n\n請將上述產生的帳號加入 `etherbase`。上述命令中加入了 `admin` 以及 `miner` 模組，是為了提供對應的 `RPC` 呼叫 ( `admin` 以及 `miner` )：\n\n```\n$ geth attach http://localhost:8545\nWelcome to the Geth JavaScript console!\n\ninstance: Geth/v1.8.2-stable/darwin-amd64/go1.10\ncoinbase: 0x65fa89786c08c2019351ff5f27d92578bc6130a7\nat block: 0 (Thu, 01 Jan 1970 08:00:00 CST)\n datadir: /Users/s12i/go/src/github.com/s12i/maicon-fullstack-test/privatechain\n modules: admin:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 web3:1.0\n\n$ rpc.modules\n{\n  admin: \"1.0\",\n  eth: \"1.0\",\n  miner: \"1.0\",\n  net: \"1.0\",\n  personal: \"1.0\",\n  rpc: \"1.0\",\n  web3: \"1.0\"\n}\n```\n\n`geth` 與 `testrpc` 將只能擇一啟動，避免相關 Port ( 8545 ) 使用。\n\n## API\n\n#### GET /node\n\n取得區塊鏈節點資訊 ( 需要啟動 `geth` )\n\n#### 範例\n\n```\n// 送出請求\ncurl -X GET http://localhost:8080/node\n\n// 回傳結果 ( HTTP 200 )\n{\n    \"enode\": \"enode://78d20aea232b5a10877f9e2569e1ed72fc4243bff11fecea99f5e63529ba1e0722e16d48e764557a04df2f4f55dc782ae7c77da60d61b400f2bff7b90f0547e2@192.168.0.10:30303\",\n    \"name\": \"Geth/v1.8.2-stable/darwin-amd64/go1.10\"\n}\n```\n\n#### GET /block/:blockNumber\n\n取得區塊資訊 ( 啟動 `testrpc` 或 `geth` )\n\n#### 參數\n\n區塊編號，須為一個整數\n\n#### 範例\n\n```\n// 送出請求\ncurl -X GET http://localhost:8080/block/0\n\n// 回傳結果 ( HTTP 200 )\n{\n    \"difficulty\": \"0x0\",\n    \"gasLimit\": \"0x6691b7\",\n    \"gasUsed\": \"0x0\",\n    \"hash\": \"0x9eca50fa8b5b61151e43e13758f1caef6a05e9e65939e65fe57a7ee97113180c\",\n    \"miner\": \"0x0000000000000000000000000000000000000000\",\n    \"parentHash\": \"0x0000000000000000000000000000000000000000000000000000000000000000\",\n    \"totalDifficulty\": \"0x0\"\n}\n```\n\n若送出不為整數的區塊編號：\n\n```\n// 送出請求\ncurl -X GET http://localhost:8080/block/foo\n\n\n// 回傳結果 ( HTTP 422 )\n{\n    \"message\": \"blockNumber must be an number\"\n}\n```\n\n#### POST /trx\n\n送出交易 ( 啟動 `testrpc` )\n\n#### 參數\n\n為 `form raw data`：\n\n```\n{\n\t\"from\": \"0xf9a022e734d573f92943ffd34d203dc16d6dcbcd\",\n\t\"to\": \"0x6989ff0102d99c804b8b6459cc720b433e5c46c1\",\n\t\"value\": 100\n}\n```\n\n#### 範例\n\n```\n// 送出請求\ncurl -X POST \\\n  http://localhost:8080/trx \\\n  -H 'Content-Type: application/json' \\\n  -d '{\n\t\"from\": \"0xf9a022e734d573f92943ffd34d203dc16d6dcbcd\",\n\t\"to\": \"0x6989ff0102d99c804b8b6459cc720b433e5c46c1\",\n\t\"value\": 100\n}'\n\n\n// 回傳結果 ( HTTP 201 )\n{\n    \"trx\": \"0xb64d57709cc92445f652b32ddfbf54b6d8a78be9d5a19ea2d7ed6c0d2763075b\"\n}\n```\n\n可以在 `testrpc` console 中看見交易訊息：\n\n```\nTransaction: 0xb64d57709cc92445f652b32ddfbf54b6d8a78be9d5a19ea2d7ed6c0d2763075b\nGas usage: 21000\nBlock Number: 1\nBlock Time: Mon Mar 12 2018 02:14:10 GMT+0800 (CST)\n```\n\n#### GET /trx/:trxHash\n\n取回交易資訊 ( 啟動 `testrpc` )\n\n#### 參數\n\n為一串代表交易的編號，以上述產生交易的結果為例，編號將會為 `0xb64d57709cc92445f652b32ddfbf54b6d8a78be9d5a19ea2d7ed6c0d2763075b `\n\n#### 範例\n\n```\n// 送出請求\ncurl -X GET \\\n  http://localhost:8080/trx/0xb64d57709cc92445f652b32ddfbf54b6d8a78be9d5a19ea2d7ed6c0d2763075b\n\n// 回傳結果 ( HTTP 200 )\n{\n    \"blockHash\": \"0xd4b0a1490f308b21123ce31e20a0155f5a435c8a2c9b526ea0a85efae1cbef44\",\n    \"blockNumber\": \"0x01\",\n    \"from\": \"0xf9a022e734d573f92943ffd34d203dc16d6dcbcd\",\n    \"gas\": \"0x015f90\",\n    \"gasPrice\": \"0x01\",\n    \"hash\": \"0xb64d57709cc92445f652b32ddfbf54b6d8a78be9d5a19ea2d7ed6c0d2763075b\",\n    \"nonce\": \"0x0\",\n    \"to\": \"0x6989ff0102d99c804b8b6459cc720b433e5c46c1\",\n    \"value\": \"0x64\"\n}\n```\n\n#### PUT /miner/:threads\n\n開始挖礦\n\n#### 參數\n\n執行緒 ( threads ) 數目，須為一個整數，若小於 `1`，將預設為 `1`\n\n#### 範例\n\n```\n// 送出請求\ncurl -X PUT http://localhost:8080/miner/3\n\n// 回傳結果 ( HTTP 204 )\n\n// geth console\nINFO [03-12|02:23:36] Updated mining threads                   threads=3\nINFO [03-12|02:23:36] Transaction pool price threshold updated price=18000000000\nINFO [03-12|02:23:36] Starting mining operation\nINFO [03-12|02:23:36] Commit new mining work                   number=1 txs=0 uncles=0 elapsed=394.881µs\nINFO [03-12|02:23:44] Successfully sealed new block            number=1 hash=07949a…0dd5ae\n```\n\n```\n// 送出請求，且執行緒數目為 0\ncurl -X PUT http://localhost:8080/miner/0\n\n// 回傳結果 ( HTTP 204 )\n\n// geth console\nINFO [03-12|02:25:08] Updated mining threads                   threads=1\nINFO [03-12|02:25:08] Transaction pool price threshold updated price=18000000000\nINFO [03-12|02:25:08] Starting mining operation\nINFO [03-12|02:25:08] Commit new mining work                   number=55 txs=0 uncles=0 elapsed=429.195µs\nINFO [03-12|02:25:09] Successfully sealed new block            number=55 hash=0d55ce…1bfe6f\nINFO [03-12|02:25:09] 🔗 block reached canonical chain         number=50 hash=446621…4debc2\nINFO [03-12|02:25:09] 🔨 mined potential block                 number=55 hash=0d55ce…1bfe6f\n```\n\n#### DELETE /miner\n\n停止挖礦\n\n#### 範例\n\n```\n// 送出請求\ncurl -X DELETE http://localhost:8080/miner\n\n// 回傳結果 ( HTTP 204 )\n```\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakeshiyu%2Fethereum-practice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftakeshiyu%2Fethereum-practice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakeshiyu%2Fethereum-practice/lists"}