Git Product home page Git Product logo

js-ilp-core's Introduction

ilp-core npm circle codecov

Core ILP module — handles ledger abstraction and quoting

Installation

You need to install this module along with any ledger plugin modules you would like to use, e.g.:

npm install --save ilp-core ilp-plugin-bells

Usage

Setup

const Client = require('ilp-core').Client
const Core = require('ilp-core').Core

const core = new Core()

// options for the plugin that Client instantiates.
// '_plugin' is the plugin module.
const pluginOpts = {
  _plugin: require('ilp-plugin-bells'),
  prefix: 'ilpdemo.red.',
  account: 'https://red.ilpdemo.org/ledger/accounts/alice',
  password: 'alice'
}

// It is optional to specify clientOpts. It has one field, connectors, which
// contains http endpoints for the connectors you wish to use.
// These http addresses are used for quoting and getting ILP address information.
// If unspecified, Client will get connectors from the plugin's 'getInfo' method.
const clientOpts = {
  connectors: [
    'https://a.example:4000',
    'https://b.example:4040',
    'https://c.example:5555',
    'https://d.example:4000',
  ]
}

core.addClient('ilpdemo.red.', new Client(pluginOpts, clientOpts))

core.connect()

Sending a Payment

const payment = {
  destinationAccount: 'ilpdemo.blue.bob',
  destinationAmount: '1',
  destinationMemo: {
    myKey: 'myValue'
  },
  executionCondition: 'cc:0:3:47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU:0',
  expiresAt: (new Date(Date.now() + 10000)).toISOString()
}

const client = core.getClient('ilpdemo.red.')
client.waitForConnection().then(() => {
  return client.quote({
    destinationAddress: payment.destinationAccount,
    destinationAmount: payment.destinationAmount,
    // You can optionally specify connectors here. If left unspecified,
    // then they will be accessed from the clientOpts object in the constructor,
    // or from the 'getInfo' method of the plugin.
    connectors: [ 'https://a.example:4000', 'https://b.example:5555' ]
  })
  .then((quote) => {
    return client.sendQuotedPayment(Object.assign({}, payment, quote))
  })
  .then(() => {
    console.log('payment sent')
  })
})
.catch((err) => {
  console.log(err)
})

core.on('fulfill_execution_condition', (client, transfer, fulfillment) => {
  console.log('transfer fulfilled', fulfillment)
  core.disconnect()
})

Receiving a Transfer

Note that the receive event is fired for conditional transfers, so the event does not necessarily indicate that funds have been transferred

core.on('receive', (client, transfer) => {
  console.log(transfer)
  client.fulfillCondition(transfer.id, 'cf:0:')
})

js-ilp-core's People

Contributors

adrianhopebailie avatar emschwartz avatar justmoon avatar sentientwaffle avatar vhpoet avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.