Skip to content

Commit

Permalink
0.25.6 updates
Browse files Browse the repository at this point in the history
- Update unit tests and examples for bchd based network
- Patch missing tokenIdHex in getTokenInformation after recent refactoring
  • Loading branch information
jcramer committed Apr 6, 2020
1 parent 313344d commit 84737c7
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 25 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,10 @@ Running the unit tests require node.js v8.15+.

# Change Log

### 0.25.6
- Update unit tests and examples for bchd based network
- Patch missing tokenIdHex in getTokenInformation after recent refactoring

### 0.25.4/5
- Typings fixes

Expand Down
7 changes: 1 addition & 6 deletions examples/1-get-token-balances-bchd-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@
*
* ************************************************************************************/

import { grpc } from "@improbable-eng/grpc-web";
import { NodeHttpTransport } from "@improbable-eng/grpc-web-node-http-transport";
// Do this first, so that we can call this library from node.js evironment.
grpc.setDefaultTransport(NodeHttpTransport());

import * as BITBOXSDK from "bitbox-sdk";
const BITBOX = new BITBOXSDK.BITBOX();
import { GrpcClient } from "grpc-bchrpc-web";
import { GrpcClient } from "grpc-bchrpc-node";
import { LocalValidator, SlpBalancesResult } from "../index";
import { BchdNetwork } from "../lib/bchdnetwork";
import { GetRawTransactionsAsync } from "../lib/localvalidator";
Expand Down
7 changes: 1 addition & 6 deletions examples/1a-get-token-balances-bchd-trusted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@
*
* ************************************************************************************/

import { grpc } from "@improbable-eng/grpc-web";
import { NodeHttpTransport } from "@improbable-eng/grpc-web-node-http-transport";
// Do this first, so that we can call this library from node.js evironment.
grpc.setDefaultTransport(NodeHttpTransport());

import * as BITBOXSDK from "bitbox-sdk";
const BITBOX = new BITBOXSDK.BITBOX();
import { GrpcClient } from "grpc-bchrpc-web";
import { GrpcClient } from "grpc-bchrpc-node";
import { BchdNetwork, SlpBalancesResult, TrustedValidator } from "../index";

// MAINNET NETWORK
Expand Down
3 changes: 3 additions & 0 deletions lib/bchdnetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export class BchdNetwork implements INetwork {
slpMsg.sendOutputs!.map((o) => o.dividedBy(10 ** slpMsg.decimals));
}
}
if (SlpTransactionType.GENESIS === slpMsg.transactionType) {
slpMsg.tokenIdHex = txid;
}
return slpMsg;
}

Expand Down
3 changes: 3 additions & 0 deletions lib/bitboxnetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export class BitboxNetwork implements INetwork {
slpMsg.sendOutputs!.map((o) => o.dividedBy(10 ** slpMsg.decimals));
}
}
if (SlpTransactionType.GENESIS === slpMsg.transactionType) {
slpMsg.tokenIdHex = txid;
}
return slpMsg;
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slpjs",
"version": "0.25.5",
"version": "0.25.6",
"description": "Simple Ledger Protocol (SLP) JavaScript Library",
"main": "index.js",
"files": [
Expand Down
54 changes: 54 additions & 0 deletions test/bchdnetwork.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as assert from "assert";
import { BigNumber } from "bignumber.js";
import { BITBOX } from "bitbox-sdk";
import { GrpcClient } from "grpc-bchrpc-node";
import { BchdNetwork } from "../lib/bchdnetwork";
import { GetRawTransactionsAsync, LocalValidator } from "../lib/localvalidator";

describe("BchdNetwork (mainnet)", () => {
const bitbox = new BITBOX();
describe("getTokenInformation()", () => {
const client = new GrpcClient();
const getRawTransactions: GetRawTransactionsAsync = async (txids: string[]) => {
const getRawTransaction = async (txid: string) => {
console.log(`Downloading: ${txid}`);
return await client.getRawTransaction({hash: txid, reversedHashOrder: true});
};
return (await Promise.all(
txids.map((txid) => getRawTransaction(txid))))
.map((res) => Buffer.from(res.getTransaction_asU8()).toString("hex"));
};

const logger = console;
const validator = new LocalValidator(bitbox, getRawTransactions, logger);
const network = new BchdNetwork({BITBOX: bitbox, client, validator, logger});
it("returns token information for a given valid tokenId", async () => {
const tokenId = "667b28d5885717e6d164c832504ae6b0c4db3c92072119ddfc5ff0db2c433456";
const tokenInfo = await network.getTokenInformation(tokenId, true);
const expectedTokenInfo = {
tokenIdHex: "667b28d5885717e6d164c832504ae6b0c4db3c92072119ddfc5ff0db2c433456",
transactionType: "GENESIS",
versionType: 1,
symbol: "BCH",
name: "Bitcoin Cash",
documentUri: "",
documentSha256: null,
decimals: 8,
containsBaton: true,
batonVout: 2,
genesisOrMintQuantity: new BigNumber("21000000"),
};
assert.deepEqual(tokenInfo, expectedTokenInfo);
});
it("throws when tokenId is not found", async () => {
const tokenId = "000028d5885717e6d164c832504ae6b0c4db3c92072119ddfc5ff0db2c433456";
let threw = false;
try {
await network.getTokenInformation(tokenId);
} catch (error) {
threw = true;
assert.equal(error.message, "5 NOT_FOUND: transaction not found");
} finally { assert.equal(threw, true); }
});
});
});
20 changes: 9 additions & 11 deletions test/bitboxnetwork.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { BitboxNetwork } from "../lib/bitboxnetwork";

import * as assert from "assert";
import { BigNumber } from "bignumber.js";
import { BITBOX } from "bitbox-sdk";
import { BitboxNetwork } from "../lib/bitboxnetwork";

describe("BitboxNetwork (mainnet)", () => {
const bitbox = new BITBOX();
describe("getTokenInformation()", () => {
//console.log(JSON.stringify(BitdbNetwork));
let net = new BitboxNetwork(bitbox);
const net = new BitboxNetwork(bitbox);
it("returns token information for a given valid tokenId", async () => {
let tokenId = "667b28d5885717e6d164c832504ae6b0c4db3c92072119ddfc5ff0db2c433456";
let tokenInfo = await net.getTokenInformation(tokenId, true);
let expectedTokenInfo = {
const tokenId = "667b28d5885717e6d164c832504ae6b0c4db3c92072119ddfc5ff0db2c433456";
const tokenInfo = await net.getTokenInformation(tokenId, true);
const expectedTokenInfo = {
tokenIdHex: "667b28d5885717e6d164c832504ae6b0c4db3c92072119ddfc5ff0db2c433456",
transactionType: "GENESIS",
versionType: 1,
Expand All @@ -24,18 +22,18 @@ describe("BitboxNetwork (mainnet)", () => {
containsBaton: true,
batonVout: 2,
genesisOrMintQuantity: new BigNumber("21000000"),
}
};
assert.deepEqual(tokenInfo, expectedTokenInfo);
});
it("throws when tokenId is not found", async () => {
let tokenId = "000028d5885717e6d164c832504ae6b0c4db3c92072119ddfc5ff0db2c433456";
const tokenId = "000028d5885717e6d164c832504ae6b0c4db3c92072119ddfc5ff0db2c433456";
let threw = false;
try {
await net.getTokenInformation(tokenId);
} catch(error) {
} catch (error) {
threw = true;
assert.equal(error.message, "No such mempool or blockchain transaction. Use gettransaction for wallet transactions.");
} finally { assert.equal(threw, true); }
});
});
});
});

0 comments on commit 84737c7

Please sign in to comment.