-
Notifications
You must be signed in to change notification settings - Fork 42
墨客的API使用指南
mdranger edited this page May 29, 2018
·
1 revision
https://nodejs.org/
下载nodejs安装即可, node -v查看安装的版本,npm -v 查看npm版本。 建议对nodejs进行简单的环境配置, 同时下载并安装git, 并通过git --version查看git版本。
npm install chain3 -g
注意:某些情况下,Windows下需要用“以管理员身份运行”进入cmd界面,才能使用npm install 命令
var Chain3 = require('chain3');
var chain3 = new Chain3();
chain3.setProvider(new chain3.providers.HttpProvider('http://xxx.xxx.xxx.xxx:xxxx'));
var networkId = chain3.version.network;
console.log('network id:'+ networkId);
chain3.setProvider(new chain3.providers.HttpProvider('http://localhost:8545'));
参数:string:传入的需要使用Keccak-256 SHA3算法进行哈希运算的字符串 options:可选项,如果要解析的是hex格式的十六进制字符串,需要设置为hex
var hash = chain3.sha3("the string to be hashed");
console.log(hash);
var hashOfHash = chain3.sha3(hash,{encoding:'hex'});
console.log(hashOfHash);
var peerCount = chain3.net.peerCount;
console.log(peerCount);
var listenState = chain3.net.listening;
console.log(listenState);
var nodeCoinbase = chain3.mc.coinbase;
console.log(nodeCoinbase);
var miningState = chain3.mc.mining;
console.log(miningState); //true or false
var nodeAccounts = chain3.mc.accounts;
console.log(nodeAccounts);
var nowBlockNumber = chain3.mc.blockNumber;
console.log(nowBlockNumber);
var transactionCount = chain3.mc.getBlockTransactionCount(96160);
console.log(transactionCount);
var balance = chain3.mc.getBalance("0x36eaa71d7383be53cb600743aad08a55222a4915");
console.log("getBalance1" + balance); //instanceof BigNumber
console.log("getBalance2" + balance.toString(10));
//输出结果: getBalance1:3.04527226722e+21
// getBalance2:3045272267220000000000
var defultBlock = chain3.mc.defaultBlock;
console.log("defaultBlock" + defultBlock);
//默认值是latest,即最近刚出的区块
chain3.mc.defaultBlock = 123; //传入一个值来覆盖默认配置
console.log("defaultBlock" + defultBlock);
var gasPrice = chain3.mc.gasPrice;//该值由最近几个块的gas价格的中值决定
console.log(gasPrice.toString(10));
var result = chain3.mc.estimateGas({
to :"0xf7ebc6b854a202efe08e91422a44ba2161ed50dc",
data: '0x23455654'
//gas: 11, //可选参数,设定gas
//gasPrice: 11 //可选参数,设定gasPrice
});
console.log('estimateGas :'+ result);
//输出结果:gasprice :20000000000
// estimateGas :1273
var code = chain3.mc.getCode("0x0000000000000000000000000000000000000065");//contract address
var sync = chain3.mc.syncing;
console.log('syncing :'+ sync );
//如果正在同步,返回同步开始区块号、节点当前正在同步的区块号、预估要同步到的区块号
var blockHash = "0xbeca9c6a3a2f7bde119193e802f9506cc0ae58f23aca59f7ac8bf98e4e2242b5";
var transaction = chain3.mc.getTransaction(blockHash);
console.log('get transaction:'+ JSON.stringify(transaction));
var getTheBlock = chain3.mc.getBlock(96190);
console.log('get the block :'+ JSON.stringify(getTheBlock ));
//TODO 稍后更新
Basics
DAPP Development*
- Move to MOAC
- MOAC Transaction Format
- MOAC JSON-RPC
- Chain3 JavaScript lib
- Chain3 Python lib
- Get Testnet MOAC
DAPP Examples
Information Websites
Connect to MOAC network