OSDN Git Service

add the test case for node-sdk
[bytom/bytom-node-sdk.git] / test / testHelpers.js
1 const bytom = require('../dist/index.js')
2 const uuid = require('uuid')
3 const url = 'http://localhost:9888'
4 const accessToken = ''
5
6 const client = new bytom.Client(url, accessToken)
7
8 const balanceByAssetAlias = (balances) => {
9   let res = {}
10   return Promise.resolve(balances)
11     .then((balance) => {
12       balance.forEach((item) => {
13         res[item.sumBy.assetAlias] = item.amount
14       })
15       return res
16     })
17 }
18
19 const createAccount = (account = 'account') => {
20   return client.keys.listAll()
21     .then((keys) => {
22       return client.accounts.create({
23         alias: `${account}-${uuid.v4()}`,
24         root_xpubs: [keys[0].xpub],
25         quorum: 1
26       })
27     })
28 }
29
30 const createAccountReciever = (accountAlias = 'account') => {
31   return client.accounts.createReceiver({
32     account_alias: accountAlias
33   })
34 }
35
36 const createAsset = (asset = 'asset') => {
37   return client.keys.listAll()
38     .then((keys) => {
39       return client.assets.create({
40         alias: `${asset}-${uuid.v4()}`,
41         definition: {
42           decimals: 8,
43           description: {},
44           name: `${asset}-${uuid.v4()}`,
45           symbol: `${asset}-${uuid.v4()}`
46         },
47         root_xpubs: [keys[0].xpub],
48         quorum: 1
49       })
50     })
51 }
52
53 const buildSignSubmit = (buildFunc, optClient, password) => {
54   const c = optClient || client
55   return c.transactions.build(buildFunc)
56     .then(tpl => c.transactions.sign({
57       transaction: tpl,
58       password
59     }))
60     .then(tpl => c.transactions.submit(tpl.transaction.raw_transaction))
61 }
62
63 module.exports = {
64   // balanceByAssetAlias,
65   client,
66   createAccount,
67   createAsset,
68   createAccountReciever,
69   buildSignSubmit,
70 }