1.SDK 简介

为了方便用户接入,MOAC官方提供nodejs 版本的SDK,官方暂不考虑提供其他版本的SDK。
Node.JS SDK下载安装:
npm install moac-api
Node.JS SDK异常处理说明:
应用方根据自己业务逻辑对sdk方法进行 try catch 异常处理
示例:

var VnodeChain = require(“moac-api”).vnodeChain;
try{
 var vc = new VnodeChain(“http://47.106.69.61:8989”);
 var blockNumber = vc.getBlockNumber();
 console.log(blockNumber);
 }catch (e){
 console.log(e);
 }

2.钱包模块

2.1 注册
参数:
pwd:钱包账户密码
代码:

var account = require(“moac-api”).account;
 var wallet = account.register(pwd);

返回:

wallet:
 { address: ‘钱包地址…’,
 privateKey: ‘私钥…’,
 keyStore: ‘keyStore内容…’
 }

2.2 登录
参数:
addr:钱包地址
pwd:钱包密码
keyStore:keyStore

代码:

var account = require(“moac-api”).account;
 var status = account.login(addr, pwd, keyStore);

返回:

status:0 //登录失败
 status:1 //登录成功
 status:2 //密码错误

3.主链模块

3.1 实例化主链对象
参数:

vnodeAddress:主链访问地址 //http://47.106.69.61:8989


代码:

var VnodeChain = require(“moac-api”).vnodeChain;
 var vc = new VnodeChain(vnodeAddress);

3.2 获取主链区块高度
代码:

var blockNumber = vc.getBlockNumber();


返回:

blockNumber:主链区块高度


3.3 获取主链某一区块信息
参数:
hashOrNumber:区块hash或区块高度
代码:

var blockInfo = vc.getBlockInfo(hashOrNumber);


返回:
blockInfo:某一区块信息
3.4 获取主链交易详情
参数:
hash:交易hash
代码:

var tradeInfo = vc.getTransactionByHash(hash);


返回:
tradeInfo:交易详情
3.5 获取合约实例
参数:
microChainAddress:子链地址
versionKey:版本号(默认0.1版本)

代码:

var data = vc.getSubChainBaseInstance(microChainAddress, versionKey);


返回:
data:合约实例
3.6 获取主链账户余额
参数:
addr:钱包账户地址
代码:

var balance = vc.getBalance(addr);


返回:
balance:主链账户余额(单位为moac)
3.7 获取主链账户ERC代币余额
参数:
addr:钱包账户地址
contractAddress:合约地址

代码:

var balance = vc.getErcBalance(addr, contractAddress);


返回:
balance:账户ERC代币余额(erc20最小单位)
3.8 获取主链合约实例
参数:
abiObj:abi对象
contractAddress:合约地址
代码:

var object = vc.getContractInstance(abiObj, contractAddress);


返回:
object:主链合约实例对象
3.9 获取交易Data
参数:
method:方法 例 “issue(address,uint256)”
paramTypes:paramTypes 参数类型数组 例[‘address’,‘uint256’]
paramValues:paramValues 参数值数组 例[‘0x…’,10000](如需要传金额的入参为erc20最小单位)

代码:

var data = mc.getData(method,paramTypes,paramValues);


返回:
data:data字符串
3.10 主链加签交易
参数:
from:交易发送人
to:交易接受者(可以为个人地址,或者主链上的合约地址)
amount:交易金额
method:方法 例 “issue(address,uint256)”
paramTypes:paramTypes 参数类型数组 例[‘address’,‘uint256’]
paramValues:paramValues 参数值数组 例[‘0x…’,10000](如需要传金额的入参为erc20最小单位)
privateKey:交易发起人私钥字符串
gasPrice:gas费用(默认为0,如返回错误为gas过低,请在返回的gas基础上加上整数gas重新提交)

代码:

vc.sendRawTransaction(from, to, amount, method, paramTypes, paramValues, privateKey, gasPrice).then((hash) => {
 console.log(hash);
 });

返回:
hash:交易hash
3.11 主链MOAC转账
参数:
from:转账人地址
to:收款人地址
amount:交易金额(单位为moac)
privatekey:转账人私钥

代码:

vc.transferMoac(from, to, amount, privatekey).then((hash) => {
 console.log(hash);
 });

返回:
hash:交易hash
3.12 主链ERC代币转账
参数:
from:转账人地址
to:收款人地址
contractAddress:erc代币合约地址
amount:交易金额(单位为moac)
privateKey:转账人私钥

代码:

vc.transferErc(from, to, contractAddress, amount, privateKey).then((hash) => {
 console.log(hash);
 });

返回:
hash:交易hash
3.13 调用主链合约
参数:
method:方法 例 “issue(address,uint256)”
paramTypes:paramTypes 参数类型数组 例[‘address’,‘uint256’]
paramValues:paramValues 参数值数组 例[‘0x…’,10000](如需要传金额的入参为erc20最小单位)
contractAddress:合约地址

代码:

var callRes = vc.callContract(method, paramTypes, paramValues, contractAddress);


返回:
callRes:调用合约返回信息
3.14 ERC20充值
参数:
addr:钱包地址
privateKey:钱包私钥
microChainAddress:子链地址
method:方法 “issue(address,uint256)”
paramTypes:paramTypes 参数类型数组 [‘address’,‘uint256’]
paramValues:paramValues 参数值数组 [‘0x…’,10000](需要传金额的入参为erc20最小单位)

代码:

vc.buyErcMintToken(addr, privateKey, microChainAddress, method, paramTypes, paramValues).then((hash) => {
 console.log(hash);
 });

返回:
hash:交易hash
3.15 MOAC充值
参数:
addr:钱包地址
privateKey:钱包私钥
microChainAddress:子链地址
method:方法 “issue(address,uint256)”
paramTypes:paramTypes 参数类型数组 [‘address’,‘uint256’]
paramValues:paramValues 参数值数组 [‘0x…’,10000](金额单位为moac)

代码:

vc.buyMoacMintToken(addr, privateKey, microChainAddress, method, paramTypes, paramValues).then((hash) => {
 console.log(hash);
 });

返回:
hash:交易hash

4.子链模块
4.1 实例化子链对象
参数:
vnodeAddress:主链访问地址 //http://47.106.69.61:8989
monitorAddress:子链访问地址 //http://47.106.89.22:8546
microChainAddress:子链地址
via:子链via

代码:

var MicroChain = require(“moac-api”).microChain;
 var mc = new MicroChain(vnodeAddress, monitorAddress, microChainAddress, via);

4.2 获取子链区块高度
代码:

mc.getBlockNumber().then((blockNumber) => {
 console.log(blockNumber);
 });

返回:
blockNumber:子链区块高度
4.3获取某一区间内的多个区块信息
参数:
start:开始高度
end:结束高度

代码:

mc.getBlocks(start, end).then((blockListInfo) => {
 console.log(blockListInfo);
 });

返回:
blockListInfo:区块信息List
4.4 获取子链某一区块信息
参数:
blockNumber:区块高度
代码:

mc.getBlock(blockNumber).then((blockInfo) => {
 console.log(blockInfo);
 });

返回:
blockInfo:某一区块信息
4.5 获取子链交易详情
参数:
transactionHash:交易hash
代码:

mc.getTransactionByHash(transactionHash).then((transactionInfo) => {
 console.log(transactionInfo);
 });

返回:
transactionInfo:交易详情
4.6 获取子链账户余额
参数:
addr:钱包地址
代码:

mc.getBalance(addr).then((balance) => {
 console.log(balance);
 });

返回:
data:子链账户余额(erc20最小单位)
4.7 获取子链详细信息
代码:

mc.getMicroChainInfo().then((microChainInfo) => {
 console.log(microChainInfo);
 });

返回:
microChainInfo:子链信息
4.8 获取子链DAPP状态
代码:

mc.getDappState().then((state) => {
 console.log(state);
 });

返回:
state:1//正常
state:0//异常

4.9 获取Nonce
参数:
addr:账户钱包地址
代码:

mc.getNonce(addr).then((nonce) => {
 console.log(nonce);
 });

返回:
nonce:得到的nonce
4.10 获取子链DAPP合约实例
参数:
dappContractAddress:dapp合约地址
dappAbi:dapp合约的Abi对象

代码:

var dapp = getDappInstance(dappContractAddress, dappAbi);


返回:
dapp:dapp实例
4.11 获取交易Data
参数:
method:方法 例 “issue(address,uint256)”
paramTypes:paramTypes 参数类型数组 例[‘address’,‘uint256’]
paramValues:paramValues 参数值数组 例[‘0x…’,10000](如需要传金额的入参为erc20最小单位)

代码:

var data = mc.getData(method,paramTypes,paramValues);


返回:
data:data字符串
4.12 子链加签交易
参数:
from:发送方的钱包地址
microChainAddress:子链地址
amount:交易金额
dappAddress:dapp地址
method:方法 例 “issue(address,uint256)”
paramTypes:paramTypes 参数类型数组 例[‘address’,‘uint256’]
paramValues:paramValues 参数值数组 例[‘0x…’,10000](如需要传金额的入参为erc20最小单位)
privateKey:发送方钱包私钥

代码:

mc.sendRawTransaction(from, microChainAddress, amount, dappAddress, method, paramTypes, paramValues, privateKey).then((hash) => {
 console.log(hash);
 });

返回:
hash:交易hash
4.13 子链转账
参数:
from:发送方的钱包地址
to:接收方的钱包地址
amount:交易金额(erc20最小单位)
privateKey:钱包私钥

代码:

mc.transferCoin(from, to, amount, privateKey).then((hash) => {
 console.log(hash);
 });

返回:
hash:交易hash
4.14 调用子链合约
参数:
contractAddress:dapp合约地址
param:例如合约中存在一个无参的方法getDechatInfo,则传入[“getDechatInfo”];
存在一个有参的方法getTopicList(uint pageNum, uint pageSize), 则传入[“getTopicList”, 0, 20]

代码:

mc.callContract(contractAddress, param).then((data) => {
 console.log(data);
 });

返回:
data:调用合约返回信息
4.15 提币(MOAC)
参数:
addr:钱包地址
amount:金额(单位为moac)
privateKey:钱包私钥

代码:

mc.redeemMoacMintToken(addr, amount, privateKey).then((hash) => {
 console.log(hash);
 });

返回:
hash:交易hash
4.16 提币(ERC20)
参数:
addr:钱包地址
amount:金额(erc20最小单位)
privateKey:钱包私钥

代码:

mc.redeemErcMintToken(addr, amount,privateKey).then((hash) => {
 console.log(hash);
 });

返回:
hash:交易hash