'use strict';
An example of an Oracle built with @fabric/core, a framework for building high-throughput distributed systems with Bitcoin.
Run locally with node examples/oracle.js
— use Node 12, a la
nvm use 12
if you’re using NVM, or from
nodejs.org if not!
First, let’s ensure strict mode is used to parse our code.
'use strict';
Import types from @fabric/core
:
const Oracle = require('@fabric/core/types/oracle');
An example configuration object, encoded as JSON:
{
"name": "@examples/oracle",
"description": "a simple Oracle example",
"version": "0.1.0"
}
Configuration files are most commonly stored in config.json
, but you can
also use an existing package.json
to pre-load an Oracle with some state.
const config = require('./config');
/**
* An {@link Oracle} offers a simple, self-contained {@link Service} for Fabric-
* capable agents. The `main()` function allocates necessary resources, then
* starts the service.
*/
async function main () {
Our primary objective is to create an Oracle, so we do that next by passing
the config
constant from earlier into the Fabric.Oracle
constructor.
const oracle = new Oracle(config);
The oracle
variable contains our Oracle, so now let’s define a Resource
for it to manage.
Here, we define a Request
as a resource with one field, input
, which is
both required
and restricted to a maximum size of 2048 bytes.
oracle.define('Request', {
attributes: {
input: { type: 'String', required: true, max: 2048 }
}
});
Now that a Resource has been defined, start the Oracle.
await oracle.start();
Log some output.
console.log('oracle started!');
console.log('oracle:', oracle);
}
We’ve defined our program. Start the main process!
module.exports = main();