Installation
npm
npm install web3.js-thk
Yarn
yarn add web3.js-thk
As a Browser module
- Include
web3.min.js
in your html file. (not required for the meteor package)
Usage
Use the web3
object directly from the global namespace:
var Web3 = ;var web3 = ;console; // {thk: .., shh: ...} // It's here!
Set a provider (HttpProvider
using HTTP Basic Authentication):
web3;
There you go, now you can use it:
var account = web3thk;var balance = accountbalance;
You can find more examples in the example
directory.
Thinkey Web3.js SDK接口文档
1. 获取账户余额(web3.thk.getAccount)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
address | string | true | 账户地址 |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
address | string | true | 账户地址 |
nonce | int | true | 交易的发起者在之前进行过的交易数量 |
balance | bigint | true | 账户余额 |
storageRoot | string | false | 合约存储数据的hash(没有合约返回null) |
codeHash | string | false | 合约代码的hash(没有合约返回null) |
请求示例:
var response = web3thk;
response:
2. 执行一笔交易(web3.thk.SendTx)
请求参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
from | string | true | 交易发起账户地址 |
to | string | true | 交易接受账户地址 |
nonce | string | true | 交易的发起者在之前进行过的交易数量 |
value | string | true | 转账金额 |
input | string | true | 调用合约时的参数 |
fromChainId | string | true | 交易发起链 id |
toChainId | string | true | 交易接受链 id |
sig | string | true | 交易签名 |
pub | string | true | 公钥 |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
TXhash | string | true | 交易hash |
请求示例:
var response = web3thk;
response:
3. 通过交易hash获取交易详情(web3.thk.GetTxByHash)
请求参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
hash | string | true | 交易hash |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
Transaction | dict | true | 交易详情 |
root | string | true | 保存了创建该receipt对象时,“账户”的当时状态 |
status | int | true | 交易状态: 1:成功, 0:失败 |
logs | array[dict] | false | 这个交易产生的日志对象数组 |
transactionHash | string | true | 交易hash |
contractAddress | string | true | 合约账户地址 |
out | string | true | 调用返回结果数据 |
Transaction:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainID | int | true | 链id |
from | string | true | 交易发起账户地址 |
to | string | true | 交易接受账户地址 |
nonce | string | true | 交易的发起者在之前进行过的交易数量 |
val | string | true | 转账金额 |
input | string | true | 调用合约时的参数 |
请求示例:
var response = web3thk;
response:
4. 获取链信息(web3.thk.GetStats)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
currentheight | bigint | true | 交易详情 |
txcount | int | true | 总交易数 |
tps | int | true | 每秒交易数 |
tpsLastEpoch | int | true | 上一时期交易数 |
lives | int | true | 链的已存活时间 |
accountcount | int | true | 账户数 |
epochlength | int | true | 当前时期包含多少块 |
epochduration | int | true | 当前时期运行时间 |
lastepochduration | int | true | 上一时期的运行时间 |
currentcomm | array | true | 当前这条链的委员会成员 |
请求示例:
var response = web3thk
response
5. 获取指定账户在对应链上一定高度范围内的交易信息(web3.thk.GetTransactions)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
address | string | true | 链地址 |
startHeight | string | true | 查询的起始块高 |
endHeight | string | true | 查询的截止块高 |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | int | true | 链id |
from | string | true | 交易发起账户地址 |
to | string | true | 交易接受账户地址 |
nonce | int | true | 交易的发起者在之前进行过的交易数量 |
value | int | true | 转账金额 |
timestamp | int | true | 交易的时间戳 |
input | string | true | 调用合约时的参数 |
hash | string | true | 交易hash |
请求示例:
var response = web3thk;
response:
6. 调用交易(web3.thk.CallTransaction)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
fromChainId | string | true | 交易发起账户地址的链id |
toChainId | string | true | 交易接受账户地址的链id |
from | string | true | 交易发起账户地址 |
to | string | true | 交易接受账户地址 |
nonce | string | true | 交易的发起者在之前进行过的交易数量 |
value | string | true | 转账金额 |
input | string | true | 调用合约时的参数 |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
Transaction | dict | true | 交易详情 |
root | string | true | 保存了创建该receipt对象时,“账户”的当时状态 |
status | int | true | 交易状态: 1:成功, 0:失败 |
logs | array[dict] | true | 这个交易产生的日志对象数组 |
transactionHash | string | true | 交易hash |
contractAddress | string | true | 合约账户地址 |
out | string | true | 调用返回结果数据 |
Transaction:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
from | string | true | 交易发起账户地址 |
to | string | true | 交易接受账户地址 |
nonce | string | true | 交易的发起者在之前进行过的交易数量 |
val | string | true | 转账金额 |
input | string | true | 调用合约时的参数 |
请求示例:
var response = web3thk;
response:
7. 获取指定块高信息(web3.thk.GetBlockHeader)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
height | string | true | 查询块的块高 |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
hash | string | true | 此块的hash |
previoushash | string | true | 父块的hash |
chainid | int | true | 链id |
height | int | true | 查询块的块高 |
mergeroot | string | true | 合并其他链转账数据hash |
deltaroot | string | true | 跨链转账数据hash |
stateroot | string | true | 状态hash |
txcount | int | true | 交易总数 |
timestamp | int | true | 时间戳 |
请求示例:
var response = web3thk;
response:
8. 获取指定块的交易(web3.thk.GetBlockTxs)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
height | string | true | 查询块的块高 |
page | string | true | 页码 |
size | string | true | 页的大小 |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
elections | dict | true | 交易详情 |
accountchanges | array | true | 交易信息 |
accountchanges:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
height | int | true | 查询的起始块高 |
from | string | true | 交易发起账户地址 |
to | string | true | 交易接受账户地址 |
nonce | int | true | 交易的发起者在之前进行过的交易数量 |
value | int | true | 转账金额 |
timestamp | int | true | 交易的时间戳 |
请求示例:
var response = web3thk;
response:
9. 编译合约(solc.compile)
Web3.js-thk项目example目录contractTest.js 文件有示例
请求参数
var input = language: 'Solidity' sources: 'test.sol': //合约文件名称 content: contractContent //合约文件内容 settings: outputSelection: '*': '*': '*' ;
请求示例:
var solc = ;{ //合约可能有多个 var input = language: 'Solidity' sources: 'test.sol': content: contractContent settings: outputSelection: '*': '*': '*' ; var output = JSON; // for (var contractName in output.contracts['test.sol']) { // console.log( // contractName + ': ' // + output.contracts['test.sol'][contractName] // ); // } return outputcontracts'test.sol' //output.contracts里面可能有多个合约文件,key文件名称}let contents = 'pragma solidity >= 0.5.0;contract MyFirst{uint256 a = 21233;string str = "Hello";function getA() public view returns(uint256 data){return a;}function getString() public view returns(string memory data){return str;}function setString(string memory data) public{str = data;}}'var contractObj_MyFirst = 'MyFirst'; //返回合约对象里面可能有多个contract类, key为contract名称var contractAbi = contractObj_MyFirstabi;var contractByteCode = contractObj_MyFirstevmbytecodeobject === '0x' ? contractObj_MyFirstevmbytecodeobject : '0x'+contractObj_MyFirstevmbytecodeobject; //最后获取合约abi 和 byteCode 字节码
10. 部署合约(web3.thk.contract(abi).new({data: code});)
调用合约方法 (web3.thk.contract(abis,contractAddress).at(contractAddress))
Web3.js-thk项目example目录contractTest.js 文件有示例
请求参数
var contractAbi = contractObj_MyFirstabi;var contractByteCode = contractObj_MyFirstevmbytecodeobject === '0x' ? contractObj_MyFirstevmbytecodeobject : '0x'+contractObj_MyFirstevmbytecodeobject;//编译合约生成的abi 和byteCode 字节码
请求示例:
//部署合约 参数为合约abi 和 byteCode 字节码{ let contracts = web3thk; ifcontractstransactionHash var conresp = web3thk; return conrespcontractAddress return ''}//发布合约var contractAddress = console;//返回的为部署后的合约地址 //获取合约对象,调用合约方法,参数为合约abi 和 合约地址{ let contractObj = web3thk; return contractObj;}var MyContract = //调用合约内的 setString 和 getString方法MyContract //合约内的修改方法需要等待hash成功后,此处等待5秒,酌情修改等待时长console;
11. ping(web3.thk.Ping)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
address | string | true | ip+端口 |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
nodeId | string | true | 节点id |
version | string | true | 版本 |
isDataNode | bool | true | 是否是数据节点 |
dataNodeOf | int | true | 数据节点 |
lastMsgTime | int64 | true | 上一个信息时间 |
lastEventTime | int64 | true | 上一个事件时间 |
lastBlockTime | int64 | true | 上一个块时间 |
overflow | bool | true | 溢出 |
lastBlocks | map | true | 最后一个块 |
opTypes | map | true | 类型 |
请求示例:
var response = web3thk
response:
12. 生成支票的证明(web3.thk.RpcMakeVccProof)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
transaction | dict | true | 交易对象 |
transaction:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
fromChainId | string | true | 交易发起账户地址的链id |
toChainId | string | true | 交易接受账户地址的链id |
from | string | true | 交易发起账户地址 |
to | string | true | 交易接受账户地址 |
nonce | string | true | 交易的发起者在之前进行过的交易数量 |
value | string | true | 转账金额 |
ExpireHeight | int | true | 过期高度 |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
input | string | true | 生成的支票证明 |
请求示例:
let obj = chainId: '2' from: '0x2c7536e3605d9c16a7a3d7b1898e529396a65c23' to: '0x2c7536e3605d9c16a7a3d7b1898e529396a65c23' fromChainId: '2' toChainId: '3' value: '1000000000000000' expireheight: '54223' nonce: '47'var response = web3thk
response:
13. 生成取消支票的证明(web3.thk.MakeCCCExistenceProof)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
transaction | dict | true | 交易对象 |
transaction:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
fromChainId | string | true | 交易发起账户地址的链id |
toChainId | string | true | 交易接受账户地址的链id |
from | string | true | 交易发起账户地址 |
to | string | true | 交易接受账户地址 |
nonce | string | true | 交易的发起者在之前进行过的交易数量 |
value | string | true | 转账金额 |
ExpireHeight | int | true | 过期高度 |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
input | string | true | 生成的支票证明 |
existence | bool | true | 是否存过支票 |
请求示例:
let obj = chainId: '2' from: '0x2c7536e3605d9c16a7a3d7b1898e529396a65c23' to: '0x2c7536e3605d9c16a7a3d7b1898e529396a65c23' fromChainId: '2' toChainId: '3' value: '1000000000000000' expireheight: '54223' nonce: '47'var response = web3thk
response:
14. 获取链结构( web3.thk.GetChainInfo)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainIds | string | true | 链id(备注:传空代表所有) |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
[] | []chainInfo | true | 链信息数组 |
chainInfo:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | int | true | 链id |
datanodes | []dataNode | true | 数据节点群 |
mode | int | true | 模式 |
parent | int | true | 父 |
dataNode:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
dataNodeId | int | true | 数据节点id |
dataNodeIp | string | true | 数据节点ip |
dataNodePort | int | true | 数据节点端口 |
请求示例:
var response = web3thk
response:
15. 获取委员会详情(web3.thk.GetCommittee)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
chainId | string | true | 链id |
epoch | string | true | 参选轮次 |
响应参数:
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
MemberDetails | []string | true | 委员会详情 |
请求示例:
var response = web3thk
response: "0xe90a151759bf070969aae664e00502bb08568c85a73874492a3ec480c5178d5da29c790896fc62106e32d172819dec94202ff90f3b7ba3e6adf38508bc58cf43" "0x3224de0da639511fec588d2e28f4472476b1600d003a10e38e0456426337624aaecd6636e5ce7ff95fc10746471ce7b680f664ccbf17057ca18c761706afa391" "0xad88dc0c0cf7d9e4a62f97e81f33556f65abba96b3c7108a732ff20f1a23530ca7730a6885d91ac718e1bb6ebad5e18bf8b7a58b91cbf717b48b723c7ceedef6" "0x8c7872c0c96a9f5b396120a0a45706678ab7a34c34a146ce9329c894f8cb9de41ec10edbf6b9c85796fd9e91d8d651a53578f164c8ee71a2d2cbfef9d5a4c6a4" "0xdb3e5b5ea24e1d760a59cf22cfafeed5a4e57af2108fc0df3bf457a82f754264b3fdf9d77fcab306a9809ebcd76de91e382d912a90e3f37edf4eb04f3f036d0b" "0x4ce2edd98452036c804f3f2eeef157672be2ccf647369eb42eb49ab9f428821f9990efde3cf7f16e4c64616c10b673077f4278c6dd2fc6021da8ad0085a522a2" "0xd1f889690f8c75bbada89a4c8893b8bf6fe29be3b5c3d8a2d772024a340d59d375f39ed88498666a57da10af885ad63a414f8a10153fb739eb1ebfcef57cc883"
16. 新建一个合约对象(web3.thk.contract)
请求参数
参数名 | 类型 | 是否必须 | 含义 |
---|---|---|---|
abi | string | true | abi数组(合约中的response["info"]["abiDefinition"]) |
address | string | true | 合约地址 |
请求示例:
// myCon对象可以直接调用合约内方法URL : testthinkeyxyzvar myCon = web3thk;// 调用方法前手动设置账户web3thkweb3thk// 调用合约内set方法myCon//调用合约内get方法, 需调用set方法后几秒钟再调用getmyCon