vue 使用 Xuper SDK JS 连接百度超级链开放网络

官方说明文档

百度超级链开放网络

https://xchain.baidu.com/n/console#/xuperos/contracts

image-20220215142711665

Xuper SDK JS

下载

Xuper JS SDK 代码可在 github 上下载:Xuper JS SDK,可以查看详细的 接口文档

安装

npm install –save @xuperchain/xuper-sdk
yarn add @xuperchain/xuper-sdk

使用JS SDK

首先需要使用 SDK 与节点创建一个 client。

import XuperSDK from '@xuperchain/xuper-sdk'
import {
  Endorsement
} from '@xuperchain/xuper-sdk'

const xsdk = XuperSDK.getInstance({
  node: 'https://xuper.baidu.com/nodeapi',
  chain: 'xuper',
  env: {
    node: {
      disableGRPC: true // 代表禁用 grpc,使用 http。默认 JS sdk 会与节点之间使用 gRPC,可以选择使用 http 方式。
    }
  },
  plugins: [
    Endorsement({
      transfer: {
        server: 'https://xuper.baidu.com/nodeapi', // https 地址
        fee: '400', // 背书手续费。
        endorseServiceCheckAddr: 'jknGxa6eyum1JrATWvSJKW3thJ9GKHA9n', // 可以看到这里的地址和 Go SDK 中配置文件的对应地址相同。
        endorseServiceFeeAddr: 'aB2hpHnTBDxko3UoP2BpBZRujwhdcAFoT'
      },
      makeTransaction: {
        server: 'https://xuper.baidu.com/nodeapi',
        fee: '400',
        endorseServiceCheckAddr: 'jknGxa6eyum1JrATWvSJKW3thJ9GKHA9n',
        endorseServiceFeeAddr: 'aB2hpHnTBDxko3UoP2BpBZRujwhdcAFoT'
      }
    })
  ]
})
console.log('xsdk', xsdk)

常用方法

// 检查状态
checkStatus: async() => {
  // eslint-disable-next-line no-useless-catch
  try {
    const result = await xsdk.checkStatus()
    console.log('checkStatus', result)
  } catch (err) {
    throw err
  }
},
// 导入账户
importAccount() {
  // 第一个参数为密钥的密码,第二个参数为下载的私钥文件内容。
  const yourAccount = xsdk.import("你的密码", "你的私钥文件内容", true)
  console.log('yourAccount', yourAccount)
},
// 获取区块链
getBlockChains: async() => {
  // eslint-disable-next-line no-useless-catch
  try {
    const result = await xsdk.getBlockChains()
    console.log('getBlockChains', result)
  } catch (err) {
    throw err
  }
},
// 查询余额
getBalance: async(address, node, chain) => {
  // eslint-disable-next-line no-useless-catch
  try {
    const res1 = await xsdk.getBalance()
    const res2 = await xsdk.getBalance(address)
    const res3 = await xsdk.getBalance(address, node, chain)
    console.log('getBalance', res1, res2, res3)
  } catch (err) {
    throw err
  }
},
// 查询余额详情
getBalanceDetail: async(address, node, chain) => {
  // eslint-disable-next-line no-useless-catch
  try {
    const res1 = await xsdk.getBalanceDetail()
    const res2 = await xsdk.getBalanceDetail(address)
    const res3 = await xsdk.getBalanceDetail(address, node, chain)
    console.log('getBalanceDetail', res1, res2, res3)
  } catch (err) {
    throw err
  }
},
// 获取合约账户
getContractAccounts: async() => {
  // eslint-disable-next-line no-useless-catch
  try {
    const result = await xsdk.getContractAccounts()
    console.log('getContractAccounts', result)
  } catch (err) {
    throw err
  }
},
// 根据合约账户获取合约信息
queryContractsByAccount: async(address) => {
  // eslint-disable-next-line no-useless-catch
  try {
    const result = await xsdk.getContracts(address)
    console.log('contracts', result)
  } catch (err) {
    throw err
  }
},
// 查询合约信息
queryContractInfo: async(contractAccount, contractName, methodName) => {
  const result = await xsdk.queryACL(contractAccount, {
    contractName: contractName,
    contractMethod: methodName
  })
  console.log('queryContractInfo', result)
},
// 执行 get查询 合约
invokeWasmGetContract: async(contractName, methodName, args) => {
  const { transaction, preExecutionTransaction } = await xsdk.invokeContarct(contractName, methodName, 'wasm', args)
  console.log('get invokeWasmContract: transaction, preExecutionTransaction', transaction, preExecutionTransaction)
  return { transaction, preExecutionTransaction }
},
// 执行 POST 合约
invokeWasmPostContract: async(contractName, methodName, args) => {
  const { transaction, preExecutionTransaction } = await xsdk.invokeContarct(contractName, methodName, 'wasm', args)
  console.log('post invokeWasmContract: transaction, preExecutionTransaction', transaction, preExecutionTransaction)
  const traRes = await xsdk.postTransaction(transaction)

  console.log('post transaction', traRes)
  return { transaction, preExecutionTransaction, traRes }
},
// 查询 块 信息
getBlockById: async(blockid) => {
  const result = await xsdk.getBlockById(blockid)
  console.log('getBlockById', blockid, result)
},
// 查询 块 信息
getBlockByHeight: async(blockheight) => {
  const result = await xsdk.getBlockByHeight(blockheight)
  console.log('getBlockByHeight', blockheight, result)
},
// 查询交易信息
queryTransaction: async(txid) => {
  const result = await xsdk.queryTransaction(txid)
  console.log('queryTransaction', txid, result)
}


关注微信公众号『极客技术之路

第一时间了解最新动态
关注博主不迷路~

极客技术之路:站内收集的部分资源来源于网络,若侵犯了您的合法权益,请联系我们删除!
分享到:
赞(0)

文章评论

0点赞 0评论 收藏 QQ分享 微博分享

极客技术之路

极客技术之路