npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@web3games-js/api

v0.1.3

Published

A JavaScript library that provides functionality to connect Web3Games-Chain Component APIs.

Downloads

831

Readme

GitHub Workflow Status License: GPL v3 Twitter URL Discord Medium

Description

The Web3Games-JS API provides a set of utilities, libraries and tools that enable JavaScript applications to interact with smart contracts or pallets running in the Web3Games network via queries to a Web3Games node.

Installation

npm install @web3games-js/api

or

yarn add @web3games-js/api

Getting started

Start an API connection to a running node on localhost:

import { Web3GamesApi } from '@web3games-js/api';

const Web3GamesApi = await Web3GamesApi.create();

You can also connect to a different node:

const Web3GamesApi = await Web3GamesApi.create({ providerAddress: 'wss://devnet.web3games.org/' });

Getting node info:

const chain = await Web3GamesApi.chain();
const nodeName = await Web3GamesApi.nodeName();
const nodeVersion = await Web3GamesApi.nodeVersion();
const genesis = Web3GamesApi.genesisHash.toHex();

Payloads and metadata

Encode / decode payloads

It's necessary to send only bytes to interact with programs on blockchain. For that purpose we use the scale-codec implementation from @polkadot-js

You can use static CreateType.create method to encode and decode data

import { CreateType } from '@web3games-js/api';

// If "TypeName" alredy registred
const result = CreateType.create('TypeName', somePayload);
// Otherwise need to add metadata containing TypeName and all required types
const result = CreateType.create('TypeName', somePayload, metadata);

Result of this functions is data of type Codec and it has the next methods

result.toHex(); // - returns a hex represetation of the value
result.toHuman(); // - returns human friendly object representation of the value
result.toString(); //  - returns a string represetation of the value
result.toU8a(); // - encodes the value as a Unit8Array
result.toJSON(); // - converts the value to JSON

Events

Subscribe to all events

const unsub = await Web3GamesApi.query.system.events((events) => {
  console.log(events.toHuman());
});
// Unsubscribe
unsub();

Blocks

Get block data

const data = await Web3GamesApi.blocks.get(blockNumberOrBlockHash);
console.log(data.toHuman());

Get block timestamp

const ts = await Web3GamesApi.blocks.getBlockTimestamp(blockNumberOrBlockHash);
console.log(ts.toNumber());

Get blockHash by block number

const hash = await Web3GamesApi.blocks.getBlockHash(blockNumber);
console.log(hash.toHex());

Get block number by blockhash

const hash = await Web3GamesApi.blocks.getBlockNumber(blockHash);
console.log(hash.toNumber());

Get all block's events

const events = await Web3GamesApi.blocks.getEvents(blockHash);
events.forEach((event) => {
  console.log(event.toHuman());
});

Get all block's extrinsics

const extrinsics = await Web3GamesApi.blocks.getExtrinsics(blockHash);
extrinsics.forEach((extrinsic) => {
  console.log(extrinsic.toHuman());
});

Keyring

Create keyring

To create keyring you can use static methods of Web3GamesKeyring class.

  • Creating a new keyring
import { Web3GamesKeyring } from '@web3games-js/api';
const { keyring, json } = await Web3GamesKeyring.create('keyringName', 'passphrase');
  • Getting a keyring from JSON
const jsonKeyring = fs.readFileSync('path/to/keyring.json').toString();
const keyring = Web3GamesKeyring.fromJson(jsonKeyring, 'passphrase');
  • Getting JSON for keyring
const json = Web3GamesKeyring.toJson(keyring, 'passphrase');
  • Getting a keyring from seed
const seed = '0x496f9222372eca011351630ad276c7d44768a593cecea73685299e06acef8c0a';
const keyring = await Web3GamesKeyring.fromSeed(seed, 'name');
  • Getting a keyring from mnemonic
const mnemonic = 'slim potato consider exchange shiver bitter drop carpet helmet unfair cotton eagle';
const keyring = Web3GamesKeyring.fromMnemonic(mnemonic, 'name');
  • Generate mnemonic and seed
const { mnemonic, seed } = Web3GamesKeyring.generateMnemonic();

// Getting a seed from mnemonic
const { seed } = Web3GamesKeyring.generateSeed(mnemonic);

Sign data

  1. Create signature
import { Web3GamesKeyring } from '@web3games-js/api';
const message = 'your message';
const signature = Web3GamesKeyring.sign(keyring, message);
  1. Validate signature
import { signatureIsValid } from '@web3games-js/api';
const publicKey = keyring.address;
const verified = signatureIsValid(publicKey, signature, message);

Convert public keys into ss58 format and back

Use encodeAddress and decodeAddress functions to convert the public key into ss58 format and back.

  1. Convert to raw format
import { decodeAddress } from '@web3games-js/api';
console.log(decodeAddress('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'))
  1. Convert to ss58 format
import { encodeAddress } from '@web3games-js/api';
console.log(encodeAddress('0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d'))