[关闭]
@sswsdsn 2019-03-27T10:08:56.000000Z 字数 4567 阅读 557

IOST交易所主网映射方案

本文介绍了交易所如何映射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

主网基本特性

  1. 网络每0.5s出一个块,单节点连续造6个块
  2. 区块生产委员会拥有17个席位,每10分钟换届一次。每次换届时Servi值最高的17个节点入选委员会,轮流打包区块
  3. 一个区块被2/3+1个出块节点确认后不可逆,大约40秒左右
  4. IOST token最小单位为小数点后8位
  5. 系统采用账户模型
  6. 失败的交易会包含在区块里,并扣除Gas费用

创建账户流程

IOST账号必须由已有账号创建,创建账号时至少质押10个IOST用于新账号获取Gas

目前有三种途径获得初始账号:

  1. ABC浏览器
  2. TokenPocket钱包
  3. 联系我们申请账号

创建新账号步骤:

  1. 首先交易所把已有账户导入sdk,该账户必须保证有足够的Token
  2. 生成公私钥对,公私钥链上使用字节数组存储,输出的时候使用base58编码
  3. 生成创建账户的交易,并签名
  4. 通过rpc发送交易到主网节点,根据返回判断是否创建成功

JS SDK示例代码如下:

  1. const IOST = require('../index');
  2. const bs58 = require('bs58');
  3. const KeyPair = require('../lib/crypto/key_pair');
  4. // rpc指定并连接远端区块链节点
  5. const rpc = new IOST.RPC(new IOST.HTTPProvider('http://47.244.109.92:30001'));
  6. // 首先需要导入一个已有账户,导入方式是创建一个account和一个keyPair,并调用account的addKeyPair方法导入对应的公私钥
  7. // 每个账户下面会有多个公私钥对,只需要导入一个即可,但需要保证导入的公私钥对有创建账号的权限
  8. // 该账户必须已经质押IOST,并保证当前账户的Gas和RAM足够付创建账户的费用
  9. const account = new IOST.Account("testaccount");
  10. const kp = new KeyPair(bs58.decode('4LNkrANP7tzvyy24GKZFRnUPpawLrD6nbrusbB7sJr9Kb2G9oW5dmdjENcFBkYAfKWNqKf7eywLqajxXSRc5ANVi'));
  11. account.addKeyPair(kp, "active");
  12. // 创建公私钥对,使用B58SecKey、B58PubKey方法分别获取创建的公钥、私钥
  13. const newKP = KeyPair.newKeyPair();
  14. console.log("privateKey: %s", newKP.B58SecKey());
  15. console.log("publicKey: %s", newKP.B58PubKey())
  16. /**
  17. * 生成创建账户的交易,newAccount函数的具体参数如下,更多文档细节参考iost.js文档
  18. * @param {string}name - 用户名
  19. * @param {string}creator - 帐号创建者的用户名
  20. * @param {string}ownerkey - 用户的owner key
  21. * @param {string}activekey - 用户的active key
  22. * @param {number}initialRAM - 用户初始RAM
  23. * @param {number}initialGasPledge - 用户初始IOST质押
  24. * @returns {Tx}
  25. */
  26. const newAccountTx = iost.newAccount(
  27. "myaccount",
  28. "testaccount",
  29. newKP.id,
  30. newKP.id,
  31. 0,
  32. 10
  33. );
  34. // 已有账户给交易签名
  35. account.signTx(newAccountTx);
  36. // 传入rpc、创建账户交易生成交易handler
  37. const newAccountHandler = new IOST.TxHandler(newAccountTx, rpc);
  38. /**
  39. * 交易handler的作用如下:
  40. * 1. onPending在交易发送到远端节返回后,输出当前交易的hash
  41. * 2. onSuccess在交易成功上链后,返回节点的交易回执receipt
  42. * 3. onFailed在交易没有成功上链、或者其他情况失败的时候,打印失败信息
  43. * 4. listen方法的作用是,每隔1000ms轮训一次链上,总共轮训5次,直到交易上链
  44. */
  45. newAccountHandler
  46. .onPending(function (response) {
  47. console.log("account request: %s has sent to node", response.hash)
  48. })
  49. .onSuccess(function (response) {
  50. console.log("sign up success, here is the receipt: %o", response)
  51. })
  52. .onFailed(console.log)
  53. .send()
  54. .listen(1000, 5);

转账交易流程

转账交易只能账号之间发送,不能通过公私钥对发送。

转账步骤:

  1. 导入要发送转账交易的账户
  2. 生成转账交易,并签名
  3. 通过rpc发送交易,根据返回判断是否执行成功

JS SDK示例代码如下:

  1. const bs58 = require('bs58');
  2. const {IOST, KeyPair} = require('iost');
  3. const iost = new IOST.IOST();
  4. // set iost rpc provider
  5. const rpc = new IOST.RPC(new IOST.HTTPProvider('http://13.52.105.102:30001'));
  6. iost.setRPC(rpc);
  7. // set iost default account
  8. const account = new IOST.Account("admin");
  9. const kp = new KeyPair(bs58.decode('5mY7xMTk2dz5T9wny8cEmF5hDzgbPkBT1UfMLCQYeR2BM3QMe9rDkM8ALQCGFp2fZCmLrxKzTa7nYTMirs3VQsRH'));
  10. account.addKeyPair(kp, "owner");
  11. account.addKeyPair(kp, "active");
  12. iost.setAccount(account);
  13. /**
  14. * The transfer() function takes the following parameters. See more in the iost.js documentations.
  15. * @param {string}token - token name
  16. * @param {string}to - the recipient
  17. * @param {string}amount - amount
  18. * @param {string}memo - memo message
  19. * @returns {Tx}
  20. */
  21. const transferTx = iost.callABI("exchange.iost", "transfer", ["iost", "myaccount", "10.000", "this is memo");
  22. // Sign the transaction with an existing account
  23. account.signTx(transferTx);
  24. // Take in RPC and transaction handler
  25. const transferHandler = new IOST.TxHandler(transferTxiost.currentRPC);
  26. /**
  27. * The handlers does the following things:
  28. * 1. onPending - Outputs the transaction hash when the transaction is submitted to the remote node.
  29. * 2. onSuccess - After the transaction is published on chain, return the receipt.
  30. * 3. onFailed - Prints failure message when the transaction fails to publish or fails in other ways
  31. * 4. listen method queries the transaction status every 1000ms, repeats 5 times until the transaction is published on chain, or fails definitively
  32. */
  33. transferHandler
  34. .onPending(function (response) {
  35. console.log("transfer request: %s has sent to node", response.hash)
  36. })
  37. .onSuccess(function (response) {
  38. console.log("transfer success, here is the receipt: %o", response)
  39. })
  40. .onFailed(console.log)
  41. .send()
  42. .listen(1000, 5);

交易判断

如何判断交易不可逆和执行成功,对交易所十分重要,请仔细阅读交易确认文档

映射方案

  1. 交易所基于初始账号创建自有账户,熟悉创建账号、发送交易、交易确认和质押相关流程
  2. 交易所分批将ERC20代币打入我们IOST指定的ETH地址,同时告诉我们交易所在IOST主网上的账号名
  3. ETH的ERC20到账后,根据交易所提供的IOST主网账号,发送主网币给对应的交易所主网账号

充提币方案

IOST采用账号模型,创建账号时需要消耗Token和Gas,因此不推荐交易所为每一个用户创建不同的账号,可以在冲币的时候,添加MEMO的方式实现充提币。

发送转账交易不需要购买RAM,只需要质押IOST获得Gas即可,一次交易大概消耗9000Gas。

建议充币流程:

  1. 用户提交充币请求,交易所生成跟用户相关的当前全局唯一的memo
  2. 交易所监控IOST区块链,找到对应账号和memo
  3. 交易确认

建议提币流程:

用户提供IOST主网账户,交易所发送提币交易参考上面的IOST转账交易流程文档。
借助iost.js SDK的txHandler,可以方便实现轮询链上信息,确认上链后完成提币操作。

附录

其他SDK

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

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注