JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 14886
  • Score
    100M100P100Q139151F
  • License Apache-2.0

Flow Interaction ADT and Helpers

Package Exports

  • @onflow/interaction

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@onflow/interaction) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@onflow/interaction

This module provides an ADT (Abstract Data Type) that represents the underlying data required by the send function. It provides function in which to modify the interaction.

Status

  • Last Updated: April 21st 2020
  • Stable: Yes
  • Risk of Breaking Change: Medium

This is a keystone data structure in how the SDK works. We will strive for backwards compatibility in any changes to this, as it also makes our lives easier. Now that we have a stable encoding package for creating what needs to be signed in transactions, we will be bringing this data structure closer to what those functions need.

Known Upcoming Changes:

  • We will be changing some of the underlying fields and data so they are more inline with @onflow/encode

Install

npm install --save @onflow/interaction

Types of interactions

Currently the Access Node recognizes 7 different types of interactions.

  • Script executes a script, can be used to query Flow
  • Transaction executes a transaction
  • GetTransactionStatus requests the status of a supplied transaction
  • GetAccount requests a supplied account
  • GetEvents requests events of a supplied type
  • GetLatestBlock requests the latest block
  • Ping requests a pong

Internal Properties

The interaction is a monomorphic data structure, that merges the 7 types of interactions together. Internally it has a bunch of properties, but not everything needs to be included for each of the 7 interaction types.

  • tag (all) Int -- a marker that represents the type of the interaction
  • status (all) Int -- a marker that represents the status of the interaction
  • reason (all) String -- used to supply more information/feedback when a status is bad
  • accounts (transaction, script)
    • kind (transaction, script) Int -- denotes the kind of account, ACCOUNT or PARAM
    • tempId (transaction, script) String -- denotes the internal tempId for this account
    • addr (transaction, script) String -- denotes the address of this account
    • keyId (transaction, script) Int -- denotes the keyId in question for this account
    • sequenceNum (transaction, script) Int -- denotes the sequenceNum in question for this account
    • signature (transaction, script) String -- the signature produced by the signingFunction for this account
    • signingFunction (transaction, script) Function -- the signing function for this account
    • resolve (transaction, script) Function -- the resolver for this account
    • role (transaction, script)
      • propser (transaction, script) Boolean -- denotes if this account is a propser
      • authorizer (transaction, script) Boolean -- denotes if this account is an authorizer
      • payer (transaction, script) Boolean -- denotes if this account is a payer
      • param (transaction, script) Boolean -- denotes if this account is a param
  • params (transaction, script)
    • kind (transaction, script) Int -- denotes the kind of param, ACCOUNT or PARAM
    • tempId (transaction, script) String -- the internal tempId for this param
    • key (transaction, script) String -- the key for this param
    • value (transaction, script) Any -- the value for this param
    • xform (transaction, script) Any -- the transform for this param
    • resolve (transaction, script) Function -- a resolver for this param
  • message (script, transaction)
    • cadence (script, transaction) String -- cadence code
    • refBlock (transaction) String -- id of an existing block (used for timeout)
    • computeLimit (script) Int -- how much payer is willing to spend
    • proposer (transaction) String -- the tempId of the account proposer for a transaction
    • payer (transaction) String -- the tempId of the payer for a transaction
    • authorizations (transaction) Array<String> -- list of tempIds referencing the accounts of the authorizers for a transaction
    • params (transaction, script) Array<String> -- list of tempIds referencing the params for a transaction
  • proposer (transaction) String -- the tempId referencing the account of the proposer for a transaction
  • payer (transaction) String -- the tempId referencing the account of the payer for a transaction
  • authorizations (transaction) Array<String> -- list of tempIds referencing the accounts of the authorizers for a transaction
  • events (getEvents)
    • start (getEvents) Int -- events after this
    • end (getEvents) Int -- events before this
    • eventType (getEvents) String -- type of events to get
  • latestBlock _(getLatestBlock)
    • isSealed (getLatestBlock) Boolean Boolean -- determines if the criteria for the latest block is sealed or
  • accountAddr (getAccount) String -- the account to get
  • transactionId (getTransactionStatus) String -- the transaction to get
  • assigns (all) {[String]:Any} -- a pocket to hold things in while building and resolving

Exposed Constants

Tags

Label asInt asBin
UNKNOWN 1 0b00000001
SCRIPT 2 0b00000010
TRANSACTION 4 0b00000100
GET_TRANSACTION_STATUS 8 0b00001000
GET_ACCOUNT 16 0b00010000
GET_EVENTS 32 0b00100000
GET_LATEST_BLOCK 64 0b01000000
PING 128 0b10000000

Status

Label asInt asBin
BAD 1 0b01
OK 2 0b10

Exposed Functions

interaction/0

Constructs an empty interaction.

import {interaction} from "@onflow/interaction"

const emptyInteraction = interaction()

isInteraction/1

returns true if the value passed to it is an interaction

import {interaction, isInteraction} from "@onflow/interaction"

const ix = interaction()

isInteraction(ix) // true
isInteraction("i am a string") // false

Ok/1 and isOk/1

Sets the status of an interaction to OK

import {interaction, Ok, isOk} from "@onflow/interaction"

isOk(Ok(interaction())) // true

Bad/2, isBad/1 and why/1

Sets the status of an interaction to BAD, can also add a reason as to why its bad.

import {interaction, Bad, why} from "@onflow/interaction"

const ix = Bad(interaction, "You were supposed to do the thing")
isBad(ix) // true
why(ix) // "You were supposed to do the thing"

makeUnknown/1 and isUnknown/1

tags an interaction as Unknown

import {interaction, makeUnknown, isUnknown} from "@onflow/interaction"

isUnknown(makeUnknown(interaction())) // true

makeScript/1 and isScript/1

tags an interaction as a Script interaction

import {interaction, makeScript, isScript} from "@onflow/interaction"

isScript(makeScript(interaction())) // true

makeTransaction/1 and isTransaction/1

tags an interaction as a Transaction interaction

import {interaction, makeTransaction, isTransaction} from "@onflow/interaction"

isTransaction(makeTransaction(interaction())) // true

makeGetTransaction/1 and isGetTransaction/1

tags an interaction as a GetTransactionStatus interaction

import {
  interaction,
  makeGetTransactionStatus,
  isGetTransactionStatus,
} from "@onflow/interaction"

isGetTransactionStatus(makeGetTransactionStatus(interaction())) // true

makeGetAccount/1 and isGetAccount/1

tags an interaction as a GetAccount interaction

import {interaction, makeGetAccount, isGetAccount} from "@onflow/interaction"

isGetAccount(makeGetAccount(interaction())) // true

makeGetEvents/1 and isGetEvents/1

tags an interaction as a GetEvents interaction

import {interaction, makeGetEvents, isGetEvents} from "@onflow/interaction"

isGetEvents(makeGetEvents(interaction())) // true

makeGetLatestBlock/1 and isGetLatestBlock/1

tags an interaction as a GetLatestBlock interaction

import {
  interaction,
  makeGetLatestBlock,
  isGetLatestBlock,
} from "@onflow/interaction"

isGetLatestBlock(makeGetLatestBlock(interaction())) // true

makePing/1 and isPing/1

tags an interaction as a Ping interaction

import {interaction, makePing, isPing} from "@onflow/interaction"

isPing(makePing(interaction())) // true

get/3, put/2, update/2 and destory/1

crud operations for the assigns pocket inside the interaction. They are specifically designed to be used with pipe.

import {interaction, get, put, update, destory} from "@onflow/interaction"

let ix = interaction()
get(ix, "count", 0) // 0

ix = put("count", 0)(ix)
get(ix, "count", 0) // 0

ix = update("count", count => count + 1)(ix)
get(ix, "count", 0) // 1

ix = destory("count")(ix)
get(ix, "count", 0) // 0

makeAuthorizer/1

compose an Authorizer account, and registers a tempId for it in the interaction object accounts registry

import {makeAuthorizer, makeTransaction, pipe} from "@onflow/interaction"
const ix = pipe([
  makeTransaction``
  makeAuthorizer({ addr: "01", role: { authorizer: true } })
])

makePayer/1

compose a Payer account, and registers a tempId for it in the interaction object accounts registry

import {makePayer, makeTransaction, pipe} from "@onflow/interaction"
const ix = pipe([
  makeTransaction``
  makePayer({ addr: "01", role: { payer: true } })
])

makeProposer/1

compose a Proposer account, and registers a tempId for it in the interaction object accounts registry

import {makeProposer, makeTransaction, pipe} from "@onflow/interaction"
const ix = pipe([
  makeTransaction``
  makeProposer({ addr: "01", role: { proposer: true } })
])

makeParam/1

compose a Param, and registers a tempId for it in the interaction object params registry

import {makeParam, makeTransaction, pipe} from "@onflow/interaction"
const ix = pipe([
  makeTransaction``
  makeParam(...)
])

pipe/2

asynchronously composes transform functions and applys them to an interaction.

import {interaction, pipe, put, update} from "@onflow/interaction"

const ix = await pipe(interaction(), [
  put("a", 5),
  put("b", 6),
  update("sum", (_, ix) => get(ix, "a", 0) + get(ix, "b", 0)),
])

get(ix, "sum", 0) // 11

pipe/1

gets passed an array of transform functions, returning a function that takes an interaction to apply the transform functions to asynchronously.

import {interaction, pipe, put, update} from "@onflow/interaction"

const run = await pipe([
  put("a", 5),
  put("b", 6),
  update("sum", (_, ix) => get(ix, "a", 0) + get(ix, "b", 0)),
])

const ix = run(interaction())

get(ix, "sum", 0) // 11

// Pipes can also be composed

const p1 = pipe([put("a", 1), put("b", 2)])

const p2 = pipe([put("c", 3), put("d", 4)])

const calc = update("sum", (_, ix) =>
  ["a", "b", "c", "d"].reduce((acc, d) => acc + get(ix, d, 0), 0)
)

const ix = await pipe(interaction(), [p1, p2, calc])
get(ix, "sum", 0) // 10

// Pipes can be stoped

import {Bad, Ok, isBad, why} from "@onflow/interaction"

const countCantBeGreaterThan = value => ix =>
  get(ix, "count", 0) > value ? Bad(ix, `Was greater than ${value}`) : Ok(ix)

const setCount = count => put("count", count)

const incCountBy = amount => update("count", count => count + amount)

const ix = await pipe(interaction(), [
  setCount(5), // count: 5
  countCantBeGreaterThan(10), // Ok
  incCountBy(3), // count: 8
  countCantBeGreaterThan(10), // Ok
  incCountBy(5), // count: 13
  countCantBeGreaterThan(10), // Bad
  incCountBy(9), // never called
])

isBad(ix) // true
why(ix) // "Was greater than 10"