This is the MOAC compatible JavaScript API which implements the Generic JSON RPC spec as described in the Chain3.md. It's available on npm as a node module, for bower and component as an embeddable js and as a meteor.js package.
Some of the methods require running a local MOAC node to use this library.
npm install chain3
Bower
bower install chain3
meteor add moaclib:chain3
- Include
chain3.min.js
in your html file. (not required for the meteor package)
Use the chain3
object directly from global namespace:
var Chain3 = require('chain3');
var chain3 = new Chain3();
console.log(chain3); // {mc: .., db: ..., net: ...} // it's here!
Set a provider (HttpProvider)
if (typeof chain3 !== 'undefined') {
chain3 = new Chain3(chain3.currentProvider);
} else {
// set the provider you want from Chain3.providers
chain3 = new Chain3(new Chain3.providers.HttpProvider("http://localhost:8545"));
}
Set a provider (HttpProvider using HTTP Basic Authentication)
chain3.setProvider(new chain3.providers.HttpProvider('http://host.url', 0, BasicAuthUsername, BasicAuthPassword));
There you go, now you can use it:
var coinbase = chain3.mc.coinbase;
var balance = chain3.mc.getBalance(coinbase);
More examples are under the example directory
- Node.js
- npm
sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
sudo apt-get install nodejs-legacy
Require install gulp (https://gulpjs.com/) in the system:
npm run-script build
Test all cases. May need to install package mocha first.
mocha
Test a singe function.
mocha test/chain3.mc.coinbase.js
Example codes to send moac through signed transaction.
var rawTx = {
from: src.addr,
nonce: chain3.intToHex(txcount),
gasPrice: chain3.intToHex(2000000000),
gasLimit: chain3.intToHex(2000),
to: '0xf1f5b7a35dff6400af7ab3ea54e4e637059ef909',
value: chain3.intToHex(chain3.toSha(value, 'mc')),
data: '0x00',
chainId: chainid
}
var cmd1 = chain3.signTransaction(rawTx, src["key"]);
chain3.mc.sendRawTransaction(cmd1, function(err, hash) {
if (!err){
console.log("Succeed!: ", hash);
return hash;
}else{
console.log("Chain3 error:", err.message);
return err.message;
}
});
Deploy a contract through chain3 RPC calls. This example requires install solc
solc
build a web server to access the MOAC network using this API library.
browserify-cryptojs v0.3.1