crossbell.js
TypeScript icon, indicating that this package has built-in type declarations

0.19.18 • Public • Published

crossbell.js

A JavaScript SDK to interact with the Crossbell. It works for both browser and Node.js.

npm version npm downloads

Installation

npm install crossbell.js

Usage

Contract

Connect with Metamask

import { Contract } from 'crossbell.js'

// Create a new contract instance with metamask provider
const provider = window.ethereum
const contract = new Contract(provider)

// Example API: Create a new character for an address
try {
  const result = await contract.createCharacter(
    '0x1234567890123456789012345678901234567890',
    'Jason',
    'ipfs://xxxx/metadata.json',
  )
  console.log(result.data) // '42' (characterId)
  console.log(result.transactionHash) // '0xabcdef...'
} catch (e) {
  console.error(e.message) // e.g. "execution reverted: Web3Entry: HandleExists"
}

Connect with Private Key

You can also connect with a private key directly.

import { Contract } from 'crossbell.js'

const privateKey =
  '0xabcdef0123456789012345678901234567890123456789012345678901234'
const contract = new Contract(privateKey)

Connect with Read-Only

You can also connect with a read-only provider. Note that in this case, you can't do write operations like createCharacter.

import { Contract } from 'crossbell.js'

const contract = new Contract() // just pass nothing to use a read-only provider

For more contract api, see docs.

Indexer

You can fetch data from the crossbell indexer.

import { Indexer } from 'crossbell.js'

const indexer = new Indexer()

// get a list of characters owned by a specific address
const res = await indexer.getCharacters(
  '0x1234567890123456789012345678901234567890',
)
console.log(res.list)

For more indexer api, see docs.

Trouble Shooting

In Vue.js: TypeError: get on proxy: property provider is a read-only and non-configurable data property on the proxy target but the proxy did not return its actual value.

To fix this in Vue.js, you need to call makeRaw for the Web3Provider instance.

let web3provider = new Web3Provider(wallet.provider, 'any')
web3provider = markRaw(web3provider)

Package Sidebar

Install

npm i crossbell.js

Weekly Downloads

5

Version

0.19.18

License

MIT

Unpacked Size

7.15 MB

Total Files

10

Last publish

Collaborators

  • songkeys