English / 中文
Version 1.0.0
这个教程教你如何使用Ontology dApp API (dAPI)开发dApp。
大纲:
请确保安装以下软件。.
创建dApp时,Ontology dAPI是与Ontology链交互的核心API之一,可以从这里下载源码。 这里. 通过 npm
安装ontology-dapi
:
$ npm install ontology-dapi
创建dAPI 实例时,要先导入库ontology-dapi,并注册客户端:
import { client } from 'ontology-dapi';
client.registerClient({});
创建dAPI 实例成功后, 就可以在你的dApp中调用 dAPI 方法.
const network = await client.api.network.getNetwork();
const height = await client.api.network.getBlockHeight();
const block = await client.api.network.getBlock({ block: 1 });
const transaction = await client.api.network.getTransaction({txHash: '314e24e5bb0bd88852b2f13e673e5dcdfd53bdab909de8b9812644d6871bc05f'});
const balance = await client.api.network.getBalance({ address: 'AcyLq3tokVpkMBMLALVMWRdVJ83TTgBUwU' });
const result = await client.api.asset.makeTransfer({ recipient, asset, amount });
const result = await client.api.smartContract.invoke({contract,method,parameters,gasPrice,gasLimit,requireIdentity});
const result = await client.api.smartContract.invokeRead({ contract, method, parameters });
const result = await client.api.smartContract.deploy({code,name,version,author,email,description,needStorage,gasPrice,gasLimit});
const message: string = values.message;
const signature: Signature = {
data,
publicKey
};
const result = await client.api.message.signMessage({ message });
const result = await client.api.message.verifyMessage({ message, signature });
所有方法列表可以在文档中找到 dAPI 规范.
拷贝 dAPI 例子 ,可以测试dAPI提供了哪些功能.
$ git clone https://github.com/ontio/ontology-dapi-demo.git
$ npm install
$ npm run start
启动成功后,在浏览器打开页面 http://localhost:3000
页面成功打开后, 点击 Provider->GetProvider.
就通过 API 调用与链交互了. 例如, 点击 Network->Get Block ,结果如下:
运行转账例子,点击 Asset->Make Transfer , Cyano Wallet会弹出确认框. 点击 Confirm
确认交易.
每笔交易都包含gas limit和gas price。gasprice 会影响到该笔交易被打包所需等待的时间,目前主网和测试网最小值都是500。
gaslimit根据合约执行复杂性而定,可以通过预执行查询该调用需要的最小gaslimit值。Native合约调用gaslimit默认是20000。部署合约根据合约而定,gaslimit一般是20000000以上。
dApp后端逻辑和存储需要使用智能合约,可以通过SmartX编辑和调试智能合约,SmartX中包含多个合约例子。SmartX,Smart Contract 教程
SmartX 使用 Cyano Wallet 帮助部署和调用合约. 请确保按照了 Cyano 并创建了测试网账号.
使用 SmartX 编译合约
写完智能合约,编译成功后,下一步是部署和调用智能合约. 如果你没有测试网的ONG,需要 申请 测试网 ONG in Discord
可以使用测试框架测试你的智能合约 smart contract automated testing framework, 如果需要部署到私链,请下载最新版Ontology并启动, Ontology release.
$ ./ontology --testmode