scientistnik / btsdex

Package for work with BitShares DEX
MIT License
21 stars 27 forks source link

How should i check my transaction/operation is irreversible in bitshares? #9

Closed 86chenjie closed 6 years ago

86chenjie commented 6 years ago

I need a way to check my operation is irreversible or not in some case: create account / deposit / withdraw. Thanks in advance.

scientistnik commented 6 years ago

You need to wait ~21 blocks. For example:

BitShares.init("wss://bitshares.openledger.info/ws");
BitShares.subscribe("connected", start);

var tx;

async function start() {
  BitShares.subscribe("block",update)

  let bot = new BitShares.login("trade-bot", "<privateActiveKey>");
  [tx] = await bot.transfer("scientistnik", "usd", 10)

  startBlockNum = tx.block_num;
}

async function update([newBlock]) {
  if (tx && (tx.block_num + 21) < newBlock.head_block_number) {
    let blockTx = await BitShares.db.get_transaction(tx.block_num, tx.trx_num)
    if (blockTx.signatures[0] === tx.trx.signatures[0])
      console.log("tx irreversible")
  }
}