This module is an Ethereum Web3 connector for LoopBack.
The connector now supports two modes:
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"
},
...
}
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
},
...
}
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));
};
For an example to get you up and running, checkout the statechannels demo.