@sswsdsn
2019-03-27T10:08:56.000000Z
字数 4567
阅读 557
本文介绍了交易所如何映射IOST主网,阅读目标人群为交易所研发人员,主要内容是IOST公链的经济模型、创建账号、发送交易和交易确认。
本文demo基于IOST的JavaScript SDK编写
JavaScript SDK文档:https://developers.iost.io/docs/en/7-iost-js/IOST-class.html
JavaScript SDK代码库:https://github.com/iost-official/iost.js
IOST账号必须由已有账号创建,创建账号时至少质押10个IOST用于新账号获取Gas
JS SDK示例代码如下:
const IOST = require('../index');
const bs58 = require('bs58');
const KeyPair = require('../lib/crypto/key_pair');
// rpc指定并连接远端区块链节点
const rpc = new IOST.RPC(new IOST.HTTPProvider('http://47.244.109.92:30001'));
// 首先需要导入一个已有账户,导入方式是创建一个account和一个keyPair,并调用account的addKeyPair方法导入对应的公私钥
// 每个账户下面会有多个公私钥对,只需要导入一个即可,但需要保证导入的公私钥对有创建账号的权限
// 该账户必须已经质押IOST,并保证当前账户的Gas和RAM足够付创建账户的费用
const account = new IOST.Account("testaccount");
const kp = new KeyPair(bs58.decode('4LNkrANP7tzvyy24GKZFRnUPpawLrD6nbrusbB7sJr9Kb2G9oW5dmdjENcFBkYAfKWNqKf7eywLqajxXSRc5ANVi'));
account.addKeyPair(kp, "active");
// 创建公私钥对,使用B58SecKey、B58PubKey方法分别获取创建的公钥、私钥
const newKP = KeyPair.newKeyPair();
console.log("privateKey: %s", newKP.B58SecKey());
console.log("publicKey: %s", newKP.B58PubKey())
/**
* 生成创建账户的交易,newAccount函数的具体参数如下,更多文档细节参考iost.js文档
* @param {string}name - 用户名
* @param {string}creator - 帐号创建者的用户名
* @param {string}ownerkey - 用户的owner key
* @param {string}activekey - 用户的active key
* @param {number}initialRAM - 用户初始RAM
* @param {number}initialGasPledge - 用户初始IOST质押
* @returns {Tx}
*/
const newAccountTx = iost.newAccount(
"myaccount",
"testaccount",
newKP.id,
newKP.id,
0,
10
);
// 已有账户给交易签名
account.signTx(newAccountTx);
// 传入rpc、创建账户交易生成交易handler
const newAccountHandler = new IOST.TxHandler(newAccountTx, rpc);
/**
* 交易handler的作用如下:
* 1. onPending在交易发送到远端节返回后,输出当前交易的hash
* 2. onSuccess在交易成功上链后,返回节点的交易回执receipt
* 3. onFailed在交易没有成功上链、或者其他情况失败的时候,打印失败信息
* 4. listen方法的作用是,每隔1000ms轮训一次链上,总共轮训5次,直到交易上链
*/
newAccountHandler
.onPending(function (response) {
console.log("account request: %s has sent to node", response.hash)
})
.onSuccess(function (response) {
console.log("sign up success, here is the receipt: %o", response)
})
.onFailed(console.log)
.send()
.listen(1000, 5);
转账交易只能账号之间发送,不能通过公私钥对发送。
JS SDK示例代码如下:
const bs58 = require('bs58');
const {IOST, KeyPair} = require('iost');
const iost = new IOST.IOST();
// set iost rpc provider
const rpc = new IOST.RPC(new IOST.HTTPProvider('http://13.52.105.102:30001'));
iost.setRPC(rpc);
// set iost default account
const account = new IOST.Account("admin");
const kp = new KeyPair(bs58.decode('5mY7xMTk2dz5T9wny8cEmF5hDzgbPkBT1UfMLCQYeR2BM3QMe9rDkM8ALQCGFp2fZCmLrxKzTa7nYTMirs3VQsRH'));
account.addKeyPair(kp, "owner");
account.addKeyPair(kp, "active");
iost.setAccount(account);
/**
* The transfer() function takes the following parameters. See more in the iost.js documentations.
* @param {string}token - token name
* @param {string}to - the recipient
* @param {string}amount - amount
* @param {string}memo - memo message
* @returns {Tx}
*/
const transferTx = iost.callABI("exchange.iost", "transfer", ["iost", "myaccount", "10.000", "this is memo");
// Sign the transaction with an existing account
account.signTx(transferTx);
// Take in RPC and transaction handler
const transferHandler = new IOST.TxHandler(transferTx,iost.currentRPC);
/**
* The handlers does the following things:
* 1. onPending - Outputs the transaction hash when the transaction is submitted to the remote node.
* 2. onSuccess - After the transaction is published on chain, return the receipt.
* 3. onFailed - Prints failure message when the transaction fails to publish or fails in other ways
* 4. listen method queries the transaction status every 1000ms, repeats 5 times until the transaction is published on chain, or fails definitively
*/
transferHandler
.onPending(function (response) {
console.log("transfer request: %s has sent to node", response.hash)
})
.onSuccess(function (response) {
console.log("transfer success, here is the receipt: %o", response)
})
.onFailed(console.log)
.send()
.listen(1000, 5);
如何判断交易不可逆和执行成功,对交易所十分重要,请仔细阅读交易确认文档
IOST采用账号模型,创建账号时需要消耗Token和Gas,因此不推荐交易所为每一个用户创建不同的账号,可以在冲币的时候,添加MEMO的方式实现充提币。
发送转账交易不需要购买RAM,只需要质押IOST获得Gas即可,一次交易大概消耗9000Gas。
用户提供IOST主网账户,交易所发送提币交易参考上面的IOST转账交易流程文档。
借助iost.js SDK的txHandler,可以方便实现轮询链上信息,确认上链后完成提币操作。
JAVA SDK代码库:https://github.com/iost-official/java-sdk
PYTHON SDK代码库:https://github.com/iost-official/pyost
GO SDK代码库:https://github.com/iost-official/go-iost/tree/master/sdk