Spin node create spin api for node

Overview

Links

@spinfi/node

Spin node create spin api for node

How to install

yarn add @spinfi/node

How to init

import {createApi} from '@spinfi/node';
import {getPrestable} from '@spinfi/shared';

const stage = getPrestable();

const data = {
  accountId: 'some account id value',
  privateKey: 'some private key value',
};

const initApi = async () => {
  const {init} = createApi({
    contractId: stage.contractId,
    privateKey: data.privateKey,
    accountId: data.accountId,
    websocket: stage.websocket,
    near: stage.near,
  });

  const response = await init();

  if (response.type === 'ERROR') {
    console.error(apiResponse.error);
  }

  if (response.type === 'OK') {
    const api = response.data;
    console.log(api);
  }
};

initApi();

@spinfi/core

Spin core create spin api instace that work exactly in browser and node.js

Contract

Contract property provide functions to call spin contract api methods as async/await js functions

const response = await api().spin.contract.depositFt(request);

if (response.type === 'ERROR') {
  console.error(response.error);
}

if (response.type === 'OK') {
  console.log(response.data);
}

Natvive

Native property provide functions to call native near api methods used in spin contract methods

const response = await api().spin.native.transferFt(request);

if (response.type === 'ERROR') {
  console.error(response.error);
}

if (response.type === 'OK') {
  console.log(response.data);
}

Websocket

Websocket property provide functions to call spin off-chain methods by websocket

Websocket method divided in two types:

  • method (similar to async call with callback)
  • subscription

1. Method

Method require some parameters in json rpc format and return some data with onOk callback. If websocket return not expected data format - api will ignore it. If websocket return correct message without connection/js error, but with error field - data handled by onError callback. If websocket connection/js runtime rise error then method function return response.type as ERROR and no callbacks its called

How to use

const response = api().spin.websocket.ping({
  onOk: (data) => console.log(data),
  onError: (error) => console.error(error),
});

if (response.type === 'ERROR') {
  console.warn('Method not sended');
}

if (response.type === 'OK') {
  response.data.unsubscribe();
}

2. Subscription

Subscription its like method but more complex. It work like that:

  • subscription send method sub and create channel to server
  • if sub ok (channel created) then api create listners for websocket
  • if new websocket message its for channel then api call corresponding subscription callbacks
const response = api().spin.websocket.listenOrders(request, {
  onSubOk: (data) => console.log(data),
  onSubError: (error) => console.error(error),
});

Channel - its stream of websocket message that have same subscription id and have some format. Messages in channel maybe different. Some channel have first uniq message and some not. First uniq message in channel its state and other messages its notification

Websoket message match with channel by json rpc id created when subscription called and subscription id created by server

const response = api().spin.websocket.listenOrders(request, {
  onNotifyOk: (data) => console.log(data),
  onNotifyError: (error) => console.error(error),
  onStateOk: (data) => console.log(data),
  onStateError: (error) => console.error(error),
});

All subscription callbacks called when websocket get valid message and match channel. If websocket break connection before subscription call or js runtime rise error then response type will be ERROR and websocket listner from init steps will not be created (callbacks not be called)

const response = api().spin.websocket.listenOrders(request);

if (response.type === 'ERROR') {
  console.warn('Listening not established');
}

Some times its needed unsubscribe from subscription. It work like that:

  • subscription send method unsub and get response
  • if response is true then channel is closed
  • internal rxjs subscriptions its also unsubscribed
  • if response is false then channel not closed
  • onUnsubError handle false branch
const response = api().spin.websocket.listenOrders(request, {
  onSubError: (error) => console.error(error),
  onUnsubError: (error) => console.error(error),
});

if (response.type === 'ERROR') {
  console.warn('Listening not established');
}

if (response.type === 'OK') {
  response.data.unsubscribe();
}
You might also like...

A base project for Express with Typescript to create an API. Includes automatic input validation and Swagger UI generation.

A base project for Express with Typescript to create an API. Includes automatic input validation and Swagger UI generation.

(Typescript) Express API with input Validation and Swagger UI Thats a mouthful isn't it. Typescript: The language used, a superset of Javascript with

Oct 26, 2022

It is a monorepo that includes all end-to-end resolutions. You can create a website, API and mobile application.

It is a monorepo that includes all end-to-end resolutions. You can create a website, API and mobile application.

Discord Website http://localhost:3000 - vue http://localhost:5000 - vue-mobile http://localhost:4000/graphql - api Setup pnpm install docker-compose u

Dec 7, 2022

This will create a REST API using Express JS and MongoDB removing the hassle of creating everything from scratch.

rest-api-init Fastest way to create REST API with Node.js, Express.js & MongoDB. Prerequisites Node.js needs to be installed. MongoDB Compass needs to

Dec 3, 2022

A totally functional user api. It's a service where you list users, create users, update or even delete them.

USER-API 🎯 ABOUT A user api system made with TypeScript using express and prisma. It's a service where you list user, create users, update them or ev

Oct 27, 2022

Apilytics for Node.js - Easy API analytics for Node backends

apilytics-node Apilytics is a service that lets you analyze operational, performance and security metrics from your APIs without infrastructure-level

Sep 2, 2022

I'm trying to create simple program for adding the digital signature to a pdf file with self-signed certificate. I use node-signpdf and pdf-lib library.

pdf-digital-signature-with-node-signpdf-ejs I'm trying to create simple program for adding the digital signature to a pdf file with self-signed certif

Dec 25, 2022

Lucid is a library, which allows you to create Cardano transactions and off-chain code for your Plutus contracts in JavaScript and Node.js.

Lucid is a library, which allows you to create Cardano transactions and off-chain code for your Plutus contracts in JavaScript and Node.js.

Jan 8, 2023
Owner
Spin
On-chain order book solution for derivative products on NEAR Protocol
Spin
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
This is a vanilla Node.js rest API created to show that it is possible to create a rest API using only vanilla Node.js

This is a vanilla Node.js rest API created to show that it is possible to create a rest API using only vanilla Node.js. But in most cases, I would recommend you to use something like Express in a production project for productivity purposes.

Eduardo Dantas 7 Jul 19, 2022
To-do-expressJS-api - An ExpressJS API, where you can create your own To-Do's

ExpressJS to-do API What is this API about? This is an API where you can do the following: Log in. Sign up. Create task Read Task Update Task Delete T

Pértile Franco Giuliano 1 Jan 3, 2022
Create a maintainable and scalable Node.js GraphQL API with TypeScript, Express, Mongoose and Apollo Server.

Set up and build a Node.js GraphQL API using Typescript, Express, Mongoose with a maintainable and scalable structure

Adam Khomsi 7 Nov 4, 2022
A Weather API project inspired by The Ultimate API Challenge / Weather API.

Weather API Project A Weather API project inspired by The Ultimate API Challenge / Weather API. Tech Stack: React.js Tailwind Axios Inspiration The Pr

Franziska 1 Dec 29, 2021
This project is built with JavaScript, Webpack, HTML & CSS, Leaderboard api. When user clicks on Refresh button it hits the api and responds with the data, The user can also post data to the api

leaderboad Description the project. this project is about the leaderboad i did during Microverse to build a website for adding Data to the API and fet

Emmanuel Moombe 4 May 30, 2022
When a person that doesn't know how to create a programming language tries to create a programming language

Kochanowski Online Spróbuj Kochanowskiego bez konfiguracji projektu! https://mmusielik.xyz/projects/kochanowski Instalacja Stwórz nowy projekt przez n

Maciej Musielik 18 Dec 4, 2022
Benefit cards API, create and store card data and log transactions

Valex ?? Benefit cards for companies and employees! ?? Tech used Overview An API to store benefit cards from companies to employees and log transactio

Felipe Ventura 2 Apr 25, 2022
ContainerMenu is an API for BDSX that allows you to create fake interactive container menus !

ContainerMenu - A BDSX API ContainerMenu is an API for BDSX that allows you to create fake interactive container menus ! Features Multiple containers

Se7en 8 Oct 28, 2022