Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial code in JS #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions js/examples/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env", "stage-0"]
}
1 change: 1 addition & 0 deletions js/examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
27 changes: 27 additions & 0 deletions js/examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "QED-it-asset-transfers-example",
"version": "0.1.0",
"description": "QED-it Asset_Transfers Example",
"license": "MIT",
"scripts": {
"test": "mocha"
},
"browser": {
"fs": false
},
"dependencies": {
"QED-it-asset-transfers": "../sdk",
"babel": "^6.23.0",
"bluebird": "^3.5.3"
},
"devDependencies": {
"babel-core": "6.26.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-0": "^6.24.1",
"babel-register": "^6.26.0",
"expect.js": "~0.3.1",
"mocha": "~2.3.4",
"sinon": "1.17.3"
}
}
118 changes: 118 additions & 0 deletions js/examples/test/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const Promise = require("bluebird");
const log = console.log;

const QeditAssetTransfers = require("QED-it-asset-transfers");
const apiClient = new QeditAssetTransfers.ApiClient();

// Configure API URL and key.
apiClient.basePath = process.env.QEDIT_URL || "http://localhost:12052";
apiClient.authentications.ApiKeyAuth.apiKey = process.env.QEDIT_API_KEY;

const nodeApi = new QeditAssetTransfers.NodeApi(apiClient);
const walletApi = new QeditAssetTransfers.WalletApi(apiClient);
const analyticsApi = new QeditAssetTransfers.AnalyticsApi(apiClient);
Promise.promisifyAll(nodeApi);
Promise.promisifyAll(walletApi);
Promise.promisifyAll(analyticsApi);


describe("Asset Transfers Example", () => {

it("issue and transfer", async () => {
log("")

try {
await nodeApi.nodeGenerateWalletPostAsync({
wallet_label: "Jane",
authorization: "123456",
})
} catch (err) {
log("Could not generate Jane's wallet, assuming that it already exists.")
}

try {
await nodeApi.nodeImportWalletPostAsync({
wallet_label: "bank",
encrypted_sk: "2b9a24e2eafce806cde2f03cae49a840ee4cfb408163c8d2de8264e3552b18ab5debc1def1fb446742cbec99f42ba9e2",
authorization: "123",
salt: "2829ca5659464e6a825b0ab19d1ac444878b8a3bb1093fb54f629ae4c1ef384d",
})
} catch (err) {
log("Could not import the bank's wallet, assuming that it already exists.")
}
log("")

const janeAddress = await walletApi.walletGetNewAddressPostAsync({
wallet_label: "Jane",
diversifier: "69be9d33a15535a59dd111",
})

log("Jane's address details:", janeAddress.recipient_address)

try {
await walletApi.walletIssueAssetPostAsync({
wallet_label: "bank",
authorization: "123",
recipient_address: janeAddress.recipient_address,
asset_id: 200,
amount: 1,
clear_value: true,
memo: "this is for you, Jane",
})
} catch (err) {
log("Could not issue asset. Did you configure the bank's private key?")
}
log("")

const balancesBefore = await walletApi.walletGetWalletBalancesPostAsync({
wallet_label: "Jane",
})
log("Jane's wallet assets:", balancesBefore.assets)

const transactionsBefore = await analyticsApi.analyticsGetTransactionsPostAsync({
wallet_label: "Jane",
start_index: 0,
number_of_results: 10,
})
log("Jane's transactions: ", transactionsBefore.transactions)
log("")

try {
await walletApi.walletTransferAssetPostAsync({
wallet_label: "Jane",
recipient_address: {
d: "69be9d33a15535a59dd111",
pkd: "bed104e2358a36053fb3a5dacb4ed34a2a612a2e10aa521dd166032e9b3343d5",
},
asset_id: 200,
amount: 1,
memo: "Getting rid of my asset",
})
} catch (err) {
log("Could not transfer asset. Does Jane have sufficient balance?")
}
log("")

const balancesAfter = await walletApi.walletGetWalletBalancesPostAsync({
wallet_label: "Jane",
})
log("Jane's wallet assets: ", balancesAfter.assets)

const transactionsAfter = await analyticsApi.analyticsGetTransactionsPostAsync({
wallet_label: "Jane",
start_index: 0,
number_of_results: 10,
})
log("Jane's transactions: ", transactionsAfter.transactions)
log("")
});


it("can query blocks", async () => {
const blocks = await analyticsApi.analyticsGetBlocksPostAsync({
start_index: 0,
number_of_results: 2,
});
console.log("blocks", blocks);
});
});
3 changes: 3 additions & 0 deletions js/examples/test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-r test/preload.js
--recursive
--timeout 10000
7 changes: 7 additions & 0 deletions js/examples/test/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require("babel-core/register")({
// This will override `node_modules` ignoring.
// If any filenames **do** match this regex then they
// aren't compiled.
ignore: /node_modules\/(?!QED-it-asset-transfers)/
});
require("babel-polyfill");
Loading