Skip to content

Commit

Permalink
Basic usage async example
Browse files Browse the repository at this point in the history
  • Loading branch information
michielmulders committed Jan 20, 2018
1 parent 01deae7 commit 87f3694
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/basic-usage-boilerplate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"start": "nodemon src/basic-usage.js --exec babel-node",
"query-assets": "nodemon src/query-assets.js --exec babel-node",
"seed-func": "nodemon src/seed-func.js --exec babel-node",
"basic-async": "nodemon src/basic-usage-async-await.js --exec babel-node",
"websocket": "nodemon src/websocket.js --exec babel-node"
},
"author": "BigchainDB",
Expand Down
61 changes: 61 additions & 0 deletions examples/basic-usage-boilerplate/src/basic-usage-async-await.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const driver = require('bigchaindb-driver')


// ======== Preparation ======== //
const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', {
app_id: 'c17a9968',
app_key: '0b277b94893e7b0a5b4e6afd6bccb01d'
})

const alice = new driver.Ed25519Keypair()
const bob = new driver.Ed25519Keypair()

const assetdata = {
'bicycle': {
'serial_number': 'abcd1234',
'manufacturer': 'Bicycle Inc.',
}
}

const metadata = { 'planet': 'earth' }


// Call async basic usage function
basicUsage()


async function basicUsage() {
// ======== Create Transaction Bicycle ======== //
const txCreateAliceSimple = driver.Transaction.makeCreateTransaction(
assetdata,
metadata,
[
driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(alice.publicKey))
],
alice.publicKey
)

const txCreateAliceSimpleSigned =
driver.Transaction.signTransaction(txCreateAliceSimple, alice.privateKey)


// ======== Post Transaction and Fetch Result ======== //
await conn.postTransaction(txCreateAliceSimpleSigned)
await conn.pollStatusAndFetchTransaction(txCreateAliceSimpleSigned.id)

const txTransferBob = driver.Transaction.makeTransferTransaction(
[{ tx: txCreateAliceSimpleSigned, output_index: 0 }],
[driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(bob.publicKey))],
{ price: '100 euro' }
)

const txTransferBobSigned = driver.Transaction.signTransaction(txTransferBob, alice.privateKey)

await conn.postTransaction(txTransferBobSigned)
await conn.pollStatusAndFetchTransaction(txTransferBobSigned.id)


// ======== Querying Assets ======== //
const assets = await conn.searchAssets('Bicycle Inc.')
console.log(assets) // eslint-disable-line no-console
}

0 comments on commit 87f3694

Please sign in to comment.