forked from mosaicdao/mosaic.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (112 loc) · 4.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"use strict";
/**
* Load openST Platform module
*/
const Web3 = require('web3')
, web3Utils = require('web3-utils')
;
const InstanceComposer = require('./instance_composer');
const version = require('./package.json').version;
require('./lib/contracts');
require('./providers/OriginWeb3');
require('./providers/AuxiliaryWeb3');
const Mosaic = function (rumNodeProvider, configurations ) {
const oThis = this;
oThis.configurations = Object.assign({}, {rumNodeProvider: rumNodeProvider}, configurations);
oThis._sanitizeConfigurations();
const _instanceComposer = new InstanceComposer(oThis.configurations);
oThis.ic = function () {
return _instanceComposer;
};
//1. Note down the rumNodeEndPoint.
oThis.rumNodeEndPoint = rumNodeProvider;
//2. Define origin
let OriginWeb3 = oThis.ic().OriginWeb3();
let _origin = new OriginWeb3();
oThis.origin = function () {
return _origin;
};
//3. Define core
let AuxiliaryWeb3 = oThis.ic().AuxiliaryWeb3();
oThis.core = function ( originCoreContractAddress ) {
return new AuxiliaryWeb3( originCoreContractAddress );
};
//4. Define contracts
oThis.contracts = oThis.ic().Contracts();
};
Mosaic.prototype = {
constructor: Mosaic,
configurations: null,
_sanitizeConfigurations: function () {
const oThis = this
, configurations = oThis.configurations
;
let areOptionsValid = true;
if ( !configurations.hasOwnProperty('origin') || typeof configurations.origin != 'object' ) {
throw "Config Missing. 'origin' configuration is missing.";
}
if ( typeof configurations.origin.provider !== 'string' ) {
throw "Invalid Origin Config. 'provider' configuration is missing.";
}
let auxiliaries = configurations.auxiliaries;
if ( !auxiliaries || !auxiliaries instanceof Array ) {
throw "Config Missing. 'auxiliaries' configuration is missing. auxiliaries should be an Array of auxiliary config";
}
let len = auxiliaries.length;
while( len-- ) {
let auxConfig = auxiliaries[ len ];
if ( !auxConfig || typeof auxConfig !== 'object' ) {
throw "Invalid Auxiliary Config. auxiliary config should be an object.";
}
if ( typeof auxConfig.provider !== 'string' ) {
throw "Invalid Auxiliary Config. 'provider' configuration is missing.";
}
if ( auxConfig.originCoreContractAddress && !web3Utils.isAddress( auxConfig.originCoreContractAddress ) ) {
throw "Invalid Auxiliary Config. 'originCoreContractAddress' should be a valid Address.";
}
}
}
}
module.exports = Mosaic;
/*
//Web3 Way:
web3 = new Web3("http://127.0.0.1:8545");
//-----------------------------------------------//
let eth = web3.eth;
eth.getBalance("0x0000000000000000000000000000000000000000",null,function () {
//This is my callback.
});
OR
web3.eth.getBalance("0x0000000000000000000000000000000000000000",null,function () {
//This is my callback.
});
//-----------------------------------------------//
//Mosaic Way
mosaic = new Mosaic("http://127.0.0.1:14545"); //All OstWeb3 params.
//-----------------------------------------------//
//Setup:
mosaic.setup.initCores( origin_core_contract_address );
mosaic.setup.initGateways( origin_core_contract_address );
//-----------------------------------------------//
//-----------------------------------------------//
//Access Value Chain web3.eth
let eth = mosaic.eth;
eth.getBalance("0x0000000000000000000000000000000000000000",null,function () {
//This is my callback.
});
OR
mosaic.eth.getBalance("0x0000000000000000000000000000000000000000",null,function () {
//This is my callback.
});
//-----------------------------------------------//
//Access Auxiliary Chain
let aux = mosaic.aux( origin_core_contract_address );
aux.getBalance("0x0000000000000000000000000000000000000000",null,function () {
//This is my callback.
});
OR
mosaic.aux( origin_core_contract_address ).getBalance("0x0000000000000000000000000000000000000000",null,function () {
//This is my callback.
});
//-----------------------------------------------//
*/