0.2.0 • Published 5 years ago

@mutadev/service v0.2.0

Weekly downloads
2
License
MIT
Repository
-
Last release
5 years ago

@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

RustDefinitionPayload(JSON)
u32u32Number
u64u64Number
StringBytesString
BytesStringString(hex formatted)
AddressStringString(hex formatted)
HashStringString(hex formatted)
Vec\Vec\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();