Most workshops at Sub0 will require that you have set up your computer to compile Substrate.
You can use this one-liner to do many of these steps for you automatically:
curl https://getsubstrate.io -sSf | bash -s -- --fast| // 引入 | |
| import { ApiPromise, WsProvider } from '@polkadot/api'; | |
| // 創建 api 對象 | |
| const wsProvider = new WsProvider('ws://localhost:9944'); | |
| const api = await ApiPromise.create({ provider: wsProvider }); | |
| // 簡單測試-讀取常量 | |
| console.log(api.consts.balances.transactionByteFee.toNumber()); |
| yarn add @polkadot/keyring |
| // 創建 api | |
| // const api = await ...; | |
| api.query.system.events(events => { | |
| events.forEach(record => { | |
| // 遍歷所有事件記錄 | |
| const { event, phase } = record; | |
| const types = event.typeDef; | |
| // 過濾掉我們不關注的事件 |
| // 訂閱著該數值 | |
| const unsub = await api.query.balances.freeBalance(ADDR, balance => { | |
| console.log(`balance of ${balance}`); | |
| }); |
| const unsub = await api.queryMulti([ | |
| // 一個 getter function | |
| api.query.timestamp.now, | |
| // 另一個 getter function,及所需參數 | |
| [api.query.balances.freeBalance, ADDR], | |
| [api.query.system.accountNonce, ADDR], | |
| ], ([now, balance, nonce]) => { // 回調函數 | |
| console.log(`${now}: balance of ${balance} and a nonce of ${nonce}`); | |
| }); |
| // 從 mnemonic 來生成,建議方法: | |
| const PHRASE = 'entire material egg meadow latin bargain dutch coral blood melt acoustic thought'; | |
| const newPair = keyring.addFromUri(PHRASE); | |
| // 只限開發時使用,即運行 Substrate 節點時加了 `--dev` 參數: | |
| const alice = keyring.addFromUri('//Alice', { name: 'Alice default' }); | |
| // 用 32 位的 16 進制數字生成 | |
| const hexPair = keyring.addFromUri('0x1234567890123456789012345678901234567890123456789012345678901234'); |
| // babe 組件內的常量 | |
| console.log(api.consts.babe.epochDuration.toNumber()); | |
| // balances 組件內的常量 | |
| console.log(api.consts.balances.creationFee.toNumber()); | |
| console.log(api.consts.balances.transferFee.toNumber()); |
| use std::str; | |
| fn main() { | |
| // -- FROM: vec of chars -- | |
| let src1: Vec<char> = vec!['j','{','"','i','m','m','y','"','}']; | |
| // to String | |
| let string1: String = src1.iter().collect::<String>(); | |
| // to str | |
| let str1: &str = &src1.iter().collect::<String>(); | |
| // to vec of byte |
openssl req -x509 -newkey rsa:4096 -sha256 -keyout example.key -out example.crt -subj "/CN=example.com" -days 3650 -passout pass:foobar
openssl x509 -x509toreq -in example.crt -out example.csr -signkey example.key -passin pass:foobar