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

@mutadev/service

v0.2.0

Published

This module provides a serial tools for creating bindings for [Muta service](https://github.com/nervosnetwork/muta-docs/blob/master/service_dev.md).

Downloads

29

Readme

@mutadev/service

This module provides a serial tools for creating bindings for Muta service.

example

Intro Muta Service in 3 Minute

Muta is a Blockchain framework, and the services is a layer for customizing business. There are 3 important concept for service

  • namespace: we can define many services in a system, and each service owns its state, for example, AssetService for minting tokens, DexService for exchanging tokens).
  • read: we can define many read methods in a service to get its state
  • write: we can define many write method to mutate its state. Unlike read, a successful state change must be consensus, so we need to wait for the receipt

A Quick Glance

The AssetService is a builtin-service in the Muta, this module also offered an AssetService.ts

Customizing Service Binding With TypeScript

1. Overview The ServiceMapping

impl ServiceMapping for DefaultServiceMapping {
    fn get_service<SDK: 'static + ServiceSDK>(
        &self,
        name: &str,
        sdk: SDK,
    ) -> ProtocolResult<Box<dyn Service>> {
        let service = match name {
            // service name
            "asset" => Box::new(AssetService::new(sdk)) as Box<dyn Service>
        }
        ...
}

2. Overview The Service Definition

impl AssetService {
    #[read]
    fn get_asset(&self, ctx: ServiceContext, payload: GetAssetPayload) -> ServiceResponse<Asset> {}

    #[write]
    fn create_asset(&mut self, ctx: ServiceContext, payload: CreateAssetPayload) -> ServiceResponse<Asset> {}
}

3. Mapping The Service To TypeScript

import { createServiceClass, read, write } from '@mutadev/service';

const AssetService = createServiceBindingClass('asset', {
  get_asset: read(GetAssetPayload, Asset),
  create_asset: write(CreateAssetPayload, Asset),
});

4. Overview Types Definition In Muta Service

pub struct GetAssetPayload {
    pub id: Hash,
}

pub struct Asset {
    pub id:     Hash,
    pub name:   String,
    pub symbol: String,
    pub supply: u64,
    pub issuer: Address,
}

pub struct CreateAssetPayload {
    pub name:   String,
    pub symbol: String,
    pub supply: u64,
}

5. Mapping Types From Rust To TypeScript

Follow the types mapping we can bind the Rust types on TypeScript

| Rust | Definition | Payload(JSON) | | -------------- | -------------- | --------------------- | | u32 | u32 | Number | | u64 | u64 | Number | | String | Bytes | String | | Bytes | String | String(hex formatted) | | Address | String | String(hex formatted) | | Hash | String | String(hex formatted) | | Vec<T> | Vec<T> | Array | | HashMap<K, V> | HashMap<K, V> | Object |

import { Hash, u64, Address, String } from '@mutadev/service';

const GetAssetPayload = {
  id: Hash,
};

const Asset = {
  id: Hash,
  name: String,
  symbol: String,
  supply: u64,
  issuer: Address,
};

const CreateAssetPayload = {
  name: String,
  symbol: String,
  supply: u64,
};

Finally, Try Our Binding

import { Client } from '@mutadev/client';
import { Account } from '@mutadev/account';

async function main() {
  const service = new AssetService(new Client(), new Account('0x...'));
  const createdAsset = await service.write.create_asset({
    name: 'MyCoin',
    symbol: 'MC',
    supply: 100000,
  });

  const assetId = createdAsset.response.response.succeedData.id;
  console.log(assetId);

  const asset = await service.get.get_asset({ id: assetId });
  console.log(asset);
}

main();

I Don't Want To Wait For The Receipt

await service.write.create_asset.composeTransaction();
await service.write.create_asset.sendTransaction();