Skip to content

Commit

Permalink
add cost method for transacion, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Nozdrin-Plotnitsky committed Oct 28, 2018
1 parent 9c86abd commit a6e1853
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 7 deletions.
5 changes: 3 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"presets": [
[ "@babel/preset-env", {
"targets": {
"browsers": "> 1%"
"browsers": "> 1%",
"node": "8"
}
}]
],
"plugins": [
"@babel/plugin-transform-runtime",
"@babel/plugin-proposal-class-properties"
]
}
}
10 changes: 6 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions docs/ru/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -296,6 +301,11 @@ $ btsdex
$ btsdex --testnet
>|
```
или воспользуйтесь ключом `--node`:
```js
$ btsdex --node wss://api.bts.blckchnd.com
>|
```

Данный режим представляет из себя простой nodejs REPL с предустановленными переменными:
- `BitShares`, главный класс пакета `BitShares`
Expand Down
2 changes: 2 additions & 0 deletions lib/bitshares.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
28 changes: 28 additions & 0 deletions test/Transaction.test.js
Original file line number Diff line number Diff line change
@@ -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))
})
})
})

0 comments on commit a6e1853

Please sign in to comment.