关于区块链:如何使用-gearjs-SDK

4次阅读

共计 3020 个字符,预计需要花费 8 分钟才能阅读完成。

gear-js 是 Gear 的 js SDK,通过这个工具咱们能够连贯节点,上传合约,发送交易,还有解析 Gear 合约等。

咱们常常应用的 https://idea.gear-tech.io/ 最外围的性能就是由 gear-js 提供反对的。

当初咱们以 erc20 合约为例子,介绍 gear-js。

如何装置 gear-js

npm install @gear-js/api
或者
yarn add @gear-js/api

开始

import {GearApi} from '@gear-js/api'

连贯到 rpc 节点,并获取节点信息

在网站左侧能够看到切换 rpc 连贯,并且能够看到最新的区块信息。通过 nodeName subscribeToNewBlocks 就能够轻松做到。

const rpc = "wss://rpc-node.gear-tech.io"

const gearApi = await GearApi.create({providerAddress: rpc})

const chain = await gearApi.chain()
const nodeName = await gearApi.nodeName()
const nodeVersion = await gearApi.nodeVersion()

console.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`)

// 获取最新块信息
await gearApi.gearEvents.subscribeToNewBlocks((header) => {console.log(`New block with number: ${header.number.toNumber()} and hash: ${header.hash.toHex()}`)
})

具体代码请参考:https://github.com/GearFans/e…

获取 wasm 合约的元数据

https://idea.gear-tech.io/ 很重要的性能是上传合约,咱们先思考如何解析合约。

import {getWasmMetadata} from '@gear-js/api'

const fileBuffer = fs.readFileSync('path/to/program.meta.wasm')
const meta = await getWasmMetadata(fileBuffer)

console.log({meta})

具体代码请参考:https://github.com/GearFans/e…

获取账户

咱们上传合约、发送信息的前提,是咱们要有 1 个账户。

// 创立一个新的 keyring
const {keyring, json} = await GearKeyring.create('keyringName')

// 从 JSON 文件失去 keyring
const jsonKeyring = fs.readFileSync('path/to/keyring.json').toString()
const keyring = GearKeyring.fromJson(jsonKeyring)

具体代码请参考:https://github.com/GearFans/e…

上传合约

有了账户,咱们上传合约,initPayload 是十分重要的参数,发送胜利后,咱们会取得 programId。

const code = fs.readFileSync('path/to/program.wasm')

const somePayload = {
  name: "GearFans Token",
  symbol: "GFT"
}

const uploadProgram = {
  code,
  gasLimit: 1_000_000,
  value: 0,
  initPayload: somePayload
};

const programId = await gearApi.program.submit(uploadProgram, meta)

await gearApi.program.signAndSend(keyring, (data) => {console.log(data)
});

具体代码请参考:https://github.com/GearFans/e…

发送音讯

mint

Gear 调用任何办法,都须要发送信息。咱们用 erc20 的 mint 办法作为例子。

const payload = {
  "mint": {
    "account": "5CyREBErNFhogptd92dtC8ybuoUczVYh2ijvdhTpS2PJGeq7",
    "amount": "1000",
  }
}

const message = {
  destination: programId, // programId
  payload: payload,
  gasLimit: 1_000_000,
  value: 0
}

await gearApi.message.submit(message, meta)

await gearApi.message.signAndSend(keyring, (data) => {console.log(data)
})

balanceOf

如何调用 balanceOf,咱们批改 payload 就能够。

const payload = {"BalanceOf": "5CyREBErNFhogptd92dtC8ybuoUczVYh2ijvdhTpS2PJGeq7"}

具体代码请参考:

https://github.com/GearFans/e…

https://github.com/GearFans/e…

获取事件

发送完事件后,咱们要承受事件,查看后果。

Gear 的事件十分多,咱们重点关注 MessageDispatchedLog

const rpc = "wss://rpc-node.gear-tech.io"

const gearApi = await GearApi.create({providerAddress: rpc})

gearApi.allEvents((events) => {
  events.forEach(async ({event: { data, method}
  }) => {if (method == "MessageDispatched") {}

    if (method == "Log") {}})

})

取得 log 后,咱们要持续解析 log 内的数据。

let decoded = CreateType.decode(type, payload, meta)

console.log(JSON.stringify(decoded))

具体代码请参考:https://github.com/GearFans/e…


对于 GearFans

Gear 是波卡生态的计算组件,GearFans 是 Gear 爱好者社区。

  • 官网:https://gear-tech.io/
  • Twitter:https://twitter.com/gear_techs
  • GitHub:https://github.com/gear-tech
  • Discord:https://discord.com/invite/7B…
  • Medium:https://medium.com/@gear_techs
  • Telegram 群:https://t.me/gear_tech
  • Telegram 中文群:https://t.me/Gear_CN
  • Telegram 中文开发群:https://t.me/gear_dev_cn
正文完
 0