Skip to content

jamesyoung/loopback-connector-web3

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

loopback-connector-web3

This module is an Ethereum Web3 connector for LoopBack.

Configuration

The connector now supports two modes:

Refer to an existing solidity project

The datasource configures the solidityProject property to point to an existing solidity project. The connector automatically builds models from smart contracts discovered in the solidity project.

server/datasources.json:

{
  "web3": {
    "url": "http://localhost:8545",
    "name": "web3",
    "connector": "loopback-connector-web3",
    "solidityProject": "../channel-contracts"
  },
  ...
}

Define models with ethereum extensions

We can use lb model to create models representing Ethereum contracts.

common/models/global-click.json:

{
  "name": "GlobalClick",
  "base": "Model",
  "idInjection": true,
  "options": {
    "validateUpsert": true,
    "ethereum": {
      "contract": {}
    }
  },
  "properties": {},
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

common/models/global-click.js:

'use strict';

module.exports = function(GlobalClick) {
  // Programmatically configures the ethereum contract JSON interface
  const CONTRACT = require('channel-contracts').contracts.Demo1;
  GlobalClick.settings.ethereum.contract = CONTRACT;
};

server/model-config.json

{
  "GlobalClick": {
    "dataSource": "web3",
    "public": true
  },
  ...
}

Run migration of smart contracts

The Web3 connector is able to migrate corresponding smart contracts if the datasource refers to a solidity project. For example,

server/boot/migrate.js

'use strict';

module.exports = function(app, cb) {
  const ds = app.dataSources.web3;
  ds.on('connected', () => ds.automigrate(cb));
};

Example

For an example to get you up and running, checkout the payment channels demo.

About

ethereum web3 based loopback connector

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%