菩提发布最新版Qtum开发工具:Qweb3

Posted 菩提预测市场

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了菩提发布最新版Qtum开发工具:Qweb3相关的知识,希望对你有一定的参考价值。

菩提发布最新版Qtum开发工具:Qweb3

菩提发布最新版Qtum开发工具:Qweb3

为了提高 Qtum 区块链的开发能力,菩提刚刚为Qtum dApp开发者发布了最新版的开发工具--Qweb3 0.6.0。Qweb3是一个围绕Qtum CLI接口的javascript包装的开发工具。 您可以在这里找到菩提开发的最新版本:

菩提发布最新版Qtum开发工具:Qweb3
https://github.com/bodhiproject/qweb3.js
菩提发布最新版Qtum开发工具:Qweb3

菩提发布最新版Qtum开发工具:Qweb3

01

它可以做什么?


Qweb3.js是一个围绕qtum-cli的Javascript包装器,它可以使开发人员可以更轻松地创建cli命令,而无需执行Qtum命令行所需的所有转换和填充。


举一个例子,智能合约功能:

function createTopic(

 address _oracle, 

 bytes32[10] _name, 

 bytes32[10] _resultNames, 

 uint256 _bettingStartBlock,

 uint256 _bettingEndBlock,

 uint256 _resultSettingStartBlock,

 uint256 _resultSettingEndBlock)

 public

 returns (TopicEvent topicEvent) { }


 用qtum-cli来做一个sendtocontract函数,看起来像这样:

$ ./qtum-cli sendtocontract 73f9bcccc2f4e9fc93c06b434a11d83f4a043e9e b7b6d82200000000000000000000000017e7888aa7412a735f336d2f6d784caefabb6fa357686f2077696c6c20626520746865204e424120416c6c2053746172204d565020696e20323031383f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004375727279000000000000000000000000000000000000000000000000000000447572616e7400000000000000000000000000000000000000000000000000004461766973000000000000000000000000000000000000000000000000000000416e7465746f6b6f756e6d706f000000000000000000000000000000000000005061756c000000000000000000000000000000000000000000000000000000005765737462726f6f6b000000000000000000000000000000000000000000000048617264656e00000000000000000000000000000000000000000000000000004a616d6573000000000000000000000000000000000000000000000000000000477265656e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111010000000000000000000000000000000000000000000000000000000000011170000000000000000000000000000000000000000000000000000000000001117a0000000000000000000000000000000000000000000000000000000000011558 0 4000000 0.0000004 qKjn4fStBaAtwGiwueJf9qFxgpbAvf1xAy


这似乎没有规律,非常难以使用。 所以我们创建了一个与本地Qtum节点交互的简单方法。 这里是Qweb3中的上述调用的等价物:

const oracleAddress = ‘qKjn4fStBaAtwGiwueJf9qFxgpbAvf1xAy’;

const eventName = ‘My event name’;

const resultNames = [‘Yes’, ’No’];

const bettingStartBlock = 68000;

const bettingEndBlock = 68100;

const resultSettingStartBlock = 68200;

const resultSettingEndBlock = 68300;

return await contract.send(‘createTopic’, {

 methodArgs: [oracleAddress, eventName, resultNames, bettingStartBlock, bettingEndBlock, resultSettingStartBlock, resultSettingEndBlock],

 gasLimit: 4000000,

 senderAddress: ‘ qKjn4fStBaAtwGiwueJf9qFxgpbAvf1xAy’,

 });

Qweb3使构建和执行这些qtum-cli命令更容易,但使用Javascript。


02

如何安装?


使用NPM,运行以下命令来将qweb3.js模块拖放到您的项目中:

$ npm install qweb3 --save


03

如何使用?


你必须启动你的qtumd本地节点,并在启动你的qtumd实例时指定rpcuser和rpcpassword。

// you can use whatever rpcuser and rpcpassword you choose

$ ./qtumd -testnet -logevents -rpcuser=bodhi -rpcpassword=bodhi


一旦你的项目中有了NPM模块,你可以像下面这样实例化一个新的Qweb3实例:

const Qweb3 = require('qweb3').default;

// Pass in the path of your local Qtum node rpc port with username/password

// In our case, username=bodhi, password=bodhi, port=13889 (default port)

const qClient = new Qweb3('http://bodhi:bodhi@localhost:13889');


一旦你有你的Qweb3实例,并且你的qtumd正在运行,你可以在Qweb3上调用命令,例如:

async function getBlockCount() {

  return await qClient.getBlockCount();

}

getBlockCount();

async function getTransactionReceipt(args) {

  const {

    transactionId, // string

  } = args;


return await qClient.getTransactionReceipt(transactionId);

}

getTransactionReceipt('1d498254fd672d6e8deaff9b92b95bcd9ab6a9591ff415b9f0b3be442fcc9392');


完整的命令列表可以在我们的Github repo自述文件中找到:

菩提发布最新版Qtum开发工具:Qweb3
https://github.com/bodhiproject/qweb3.js
菩提发布最新版Qtum开发工具:Qweb3


或者,如果您想要执行直接合同调用(如callcontract或sendtocontract),则需要实例化一个contract实例:

const Contract = require('qweb3').Contract;


// The path to your local Qtum node via RPC

const rpcAddress = 'http://bodhi:bodhi@localhost:13889';


// contractAddress = The address of your contract deployed on the blockchain

const contractAddress = 'f7b958eac2bdaca0f225b86d162f263441d23c19';


// contractAbi = The ABI of the contract

const contractAbi = [{ constant: true, inputs: [{ name: '', type: 'bytes32' }], name: 'topics', outputs: [{ name: '', type: 'address' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [{ name: '_oracle', type: 'address' }, { name: '_name', type: 'bytes32[10]' }, { name: '_resultNames', type: 'bytes32[10]' }, { name: '_bettingEndBlock', type: 'uint256' }, { name: '_resultSettingEndBlock', type: 'uint256' }], name: 'createTopic', outputs: [{ name: 'topicEvent', type: 'address' }], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [{ name: '_name', type: 'bytes32[10]' }, { name: '_resultNames', type: 'bytes32[10]' }, { name: '_bettingEndBlock', type: 'uint256' }, { name: '_resultSettingEndBlock', type: 'uint256' }], name: 'doesTopicExist', outputs: [{ name: '', type: 'bool' }], payable: false, stateMutability: 'view', type: 'function' }, { inputs: [{ name: '_addressManager', type: 'address' }], payable: false, stateMutability: 'nonpayable', type: 'constructor' }, { anonymous: false, inputs: [{ indexed: true, name: '_topicAddress', type: 'address' }, { indexed: true, name: '_creator', type: 'address' }, { indexed: true, name: '_oracle', type: 'address' }, { indexed: false, name: '_name', type: 'bytes32[10]' }, { indexed: false, name: '_resultNames', type: 'bytes32[10]' }, { indexed: false, name: '_bettingEndBlock', type: 'uint256' }, { indexed: false, name: '_resultSettingEndBlock', type: 'uint256' }], name: 'TopicCreated', type: 'event' }];


const contract = new Contract(rpcAddress, contractAddress, contractAbi);


这是一个callcontract例子:

// callcontract on a method named 'myCallFunc'

async function exampleCall(args) {

  const {

    senderAddress, // address

  } = args;


  return await contract.call('myCallFunc', {

    methodArgs: [],

    senderAddress: senderAddress,

  });

}

exampleCall('qKjn4fStBaAtwGiwueJf9qFxgpbAvf1xAy');


这是一个 sendtocontract例子:

// sendtocontract on a method named 'mySendFunc'

async function exampleSend(args) {

  const {

    resultIndex, // number

    senderAddress, // address

  } = args;


  return await contract.send('mySendFunc', {

    methodArgs: [resultIndex], // match contract function params

    gasLimit: 1000000, // setting the gas limit to 1 million

    senderAddress: senderAddress,

  });

}

exampleSend(1, 'qKjn4fStBaAtwGiwueJf9qFxgpbAvf1xAy');


04

新指令


我们最近用新的指令更新了所有的开发需求:

getBlock

getBlockchainInfo

getBlockHash

listContracts

getInfo

dumpPrivateKey

encryptWallet

getNewAddress

getUnconfirmedBalance

importAddress

importPrivateKey

importPublicKey

listLockUnspent

sendToAddress

setTxFee


05

ES6语法


我们使用ES6语法重构了我们所有的模块导出部分,而不是Babel语法。 我们觉得没有必要为了我们的工具的目的而使用Babel。


以前,要导入Qweb3,你可以这样做:

const Qweb3 = require('qweb3').default;

const Contract = require('qweb3').Contract;


现在,你可以这样做:

const { Qweb3, Contract } = require('qweb3');

// OR

const Qweb3 =require('qweb3').Qweb3;

const Contract =require('qweb3').Contract;


我们希望我们的工具对开发者们有所帮助。 我们计划的即将发布的功能之一是一个完整的命令行界面,可以用于即时进行转换。 敬请关注!


如果你有任何意见,建议或功能要求,可以在以下的Github repo中创建一个issue。谢谢!

菩提发布最新版Qtum开发工具:Qweb3
https://github.com/bodhiproject/qweb3.js
菩提发布最新版Qtum开发工具:Qweb3

菩提基金会

官方网站:www.bodhi.network 

Blog: https://medium.com/@bodhitoken  

Twitter: https://twitter.com/bodhitoken 

中文官方电报群: https://t.me/BodhiCN  

Bodhi Telegram English Group: https://t.me/BodhiEN 


可点击“阅读原文”进入菩提Bodhi官网获得更多资讯

后台回复“进群”进入菩提社区

后台回复“白皮书”查看菩提白皮书

后台回复“视频”观看菩提介绍视频

后台回复“最新”即可查看项目最新进展


菩提发布最新版Qtum开发工具:Qweb3

菩提发布最新版Qtum开发工具:Qweb3

推荐阅读

Recommended reading



点击下列标题  阅读更多专栏

菩提与 UGC Network 达成战略合作伙伴关系




以上是关于菩提发布最新版Qtum开发工具:Qweb3的主要内容,如果未能解决你的问题,请参考以下文章

量子链Qtum Blockchain(Qtum)

外媒Invest In Blockchain独家专访:Qtum量子链的优势在哪里?

区块链技术工坊 - 线下区块链技术分享

帅初&Qtum 对于BlockChain的12点认知

菩提树,禅修自在心

[机缘参悟-84]:读《心若菩提》有感