diff --git a/.babelrc b/.babelrc index 8ae0a68..0eb4f2a 100644 --- a/.babelrc +++ b/.babelrc @@ -2,7 +2,8 @@ "presets": [ [ "@babel/preset-env", { "targets": { - "browsers": "> 1%" + "browsers": "> 1%", + "node": "8" } }] ], @@ -10,4 +11,4 @@ "@babel/plugin-transform-runtime", "@babel/plugin-proposal-class-properties" ] -} \ No newline at end of file +} diff --git a/cli.js b/cli.js index 9b0af75..c5cbc3c 100755 --- a/cli.js +++ b/cli.js @@ -33,11 +33,13 @@ function initializeContext(context) { } -function connect(autoreconnect = true) { - let node = process.argv.includes("--testnet") ? "wss://node.testnet.bitshares.eu" : undefined +function connect(autoreconnect = true) { + let + node = process.argv.includes("--node") + ? process.argv[process.argv.indexOf("--node") + 1] + : (process.argv.includes("--testnet") ? "wss://node.testnet.bitshares.eu" : BitShares.node) - BitShares.init(node, false, autoreconnect) - return BitShares.connect().then(() => console.log(`Connected to API node: ${node}`)) + return BitShares.connect(node, autoreconnect).then(() => console.log(`Connected to API node: ${node}`)) } function showError(error) { diff --git a/docs/README.md b/docs/README.md index afe4329..c9ade4a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -144,6 +144,12 @@ tx.add(operation1) tx.add(operation2) ... ``` +If you want to know the cost of the transaction: +```js +let cost = await tx.cost() +console.log(cost) // { BTS: 1.234 } +``` + After broadcast transaction: ```js await tx.broadcast() @@ -296,6 +302,11 @@ This command try autoconnect to mainnet BitShares. If you want to connect on tes $ btsdex --testnet >| ``` +or use `--node` key: +```js +$ btsdex --node wss://api.bts.blckchnd.com +>| +``` It is nodejs REPL with several variables: - `BitShares`, main class `BitShares` package diff --git a/docs/ru/README.md b/docs/ru/README.md index 0a75380..cf060cd 100644 --- a/docs/ru/README.md +++ b/docs/ru/README.md @@ -144,6 +144,11 @@ tx.add(operation1) tx.add(operation2) ... ``` +Можно узнать стоимость транзакций: +```js +let cost = await tx.cost() +console.log(cost) // { BTS: 1.234 } +``` После этого транзакцию можно отправлять: ```js await tx.broadcast() @@ -296,6 +301,11 @@ $ btsdex $ btsdex --testnet >| ``` +или воспользуйтесь ключом `--node`: +```js +$ btsdex --node wss://api.bts.blckchnd.com +>| +``` Данный режим представляет из себя простой nodejs REPL с предустановленными переменными: - `BitShares`, главный класс пакета `BitShares` diff --git a/lib/bitshares.js b/lib/bitshares.js index 6b0bae4..de784ae 100644 --- a/lib/bitshares.js +++ b/lib/bitshares.js @@ -65,6 +65,8 @@ class BitShares { this.history = Api.new('history_api'); this.network = Api.new('network_api'); //this.crypto = Api.new('crypto_api'); + + Transaction.setDB(this.db); this.newTx = Transaction.newTx; this.assets = Asset.init(this.db); diff --git a/lib/transaction.js b/lib/transaction.js index 9a8c018..80ed753 100644 --- a/lib/transaction.js +++ b/lib/transaction.js @@ -7,6 +7,10 @@ class Transaction { return new Proxy(tx, tx) } + static setDB(db) { + Transaction.db = db + } + constructor(keys) { this.tx = new TransactionBuilder() this.keys = keys @@ -34,6 +38,23 @@ class Transaction { ) return this.tx.broadcast(); } + + cost = async () => { + await this.tx.set_required_fees(); + let fees = {} + this.tx.operations.forEach(op => { + fees[op[1].fee.asset_id] = fees[op[1].fee.asset_id] || 0 + fees[op[1].fee.asset_id] += +op[1].fee.amount + }) + + let assets = await Transaction.db.get_assets(Object.keys(fees)) + + //return assets.map(asset => ({asset, amount: fees[asset.id] / 10 ** asset.precision})) + return assets.reduce((obj, asset) => { + obj[asset.symbol] = fees[asset.id] / 10 ** asset.precision + return obj + }, {}) + } } export default Transaction \ No newline at end of file diff --git a/package.json b/package.json index d880b01..334aa1e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "btsdex", - "version": "0.5.1", + "version": "0.6.0", "description": "Package for work with BitShares DEX", "main": "index.js", "bin": { diff --git a/test/Transaction.test.js b/test/Transaction.test.js new file mode 100644 index 0000000..894b684 --- /dev/null +++ b/test/Transaction.test.js @@ -0,0 +1,28 @@ +const + assert = require("assert"), + //fs = require("fs"), + BitShares = require("../index.js"); + +require("dotenv").config(); + +describe("Transaction class", () => { + before(async () => { + await BitShares.connect(process.env.BITSHARES_NODE) + }) + + describe("#cost()", () => { + it.only("get cost", async () => { + let acc = await BitShares.login(process.env.BITSHARES_ACCOUNT, process.env.BITSHARES_PASSWORD) + let tx = acc.newTx() + + let operation = await acc.transferOperation("trade-bot", "TEST", 1) + tx.add(operation) + + operation = await acc.transferOperation("trade-bot", "TEST", 1) + tx.add(operation) + + let cost = await tx.cost().catch(console.log) + console.log(JSON.stringify(cost,null,2)) + }) + }) +}) \ No newline at end of file