This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"PROVIDER_SOCKET": "ws://<你的 ws/wss 地址>" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -- 安装 NodeJS v12 (https://nodejs.org/en/download/), | |
# 及 yarn 工具 (https://classic.yarnpkg.com/en/docs/install) | |
# -- 下载 Substrate 前端模版并跑起来 | |
cd .. | |
git clone https://github.com/substrate-developer-hub/substrate-front-end-template front-end-template | |
cd front-end-template | |
yarn install | |
yarn start |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 引入一些幫助函數 | |
import { stringToU8a, u8aToHex } from '@polkadot/util'; | |
// 創建信息 | |
const message = stringToU8a('a testing message'); | |
// 簽署信息 | |
const signature = alice.sign(message); | |
// 覈實信息 | |
const isValid = alice.verify(message, signature); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yarn add @polkadot/types |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
// 一個模擬地址 | |
const recipient = '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE'; | |
// Sign and send a transfer from Alice to Bob | |
const txHash = await api.tx.balances | |
.transfer(recipient, 12345) | |
.signAndSend(alice); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const api = await ApiPromise.create({ | |
..., | |
types: { | |
Price: { | |
dollars: 'u32', | |
cents: 'u32', | |
currency: 'Vec<u8>', | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 引入 | |
import { Keyring } from '@polkadot/api'; | |
// 初始化 api | |
// const api = await ...; | |
// api 完成初始化後,再創建 keyring 對象。 | |
const keyring = new Keyring({ type: 'sr25519' }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 初始化 `api` 對象 | |
const api = ...; | |
// 取得鏈上的時間戳 | |
const now = await api.query.timestamp.now(); | |
// 一個模擬地址 | |
const ADDR = '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE'; | |
// 取得用戶地址中的餘額 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yarn add @polkadot/api |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 從 Alice 提交一個交易給另一用戶 | |
const unsub = await api.tx.balances | |
.transfer(recipient, 12345) | |
.signAndSend(alice, ({ eventRecords = [], status }) => { | |
console.log(`Current status is ${status.type}`); | |
if (status.isInBlock) { | |
console.log(`Transaction included at blockHash ${status.asInBlock}`); | |
} else if (status.isFinalized) { | |
console.log(`Transaction finalized at blockHash ${status.asFinalized}`); |