diff --git a/README.md b/README.md index 7262f6de..1b001782 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/1-get-token-balances-bchd-local.ts b/examples/1-get-token-balances-bchd-local.ts index c30829bd..c4ccf451 100644 --- a/examples/1-get-token-balances-bchd-local.ts +++ b/examples/1-get-token-balances-bchd-local.ts @@ -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"; diff --git a/examples/1a-get-token-balances-bchd-trusted.ts b/examples/1a-get-token-balances-bchd-trusted.ts index 170a40f1..187e9dc1 100644 --- a/examples/1a-get-token-balances-bchd-trusted.ts +++ b/examples/1a-get-token-balances-bchd-trusted.ts @@ -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 diff --git a/lib/bchdnetwork.ts b/lib/bchdnetwork.ts index aa6434b0..1b06f46d 100644 --- a/lib/bchdnetwork.ts +++ b/lib/bchdnetwork.ts @@ -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; } diff --git a/lib/bitboxnetwork.ts b/lib/bitboxnetwork.ts index adff3ae0..5749c601 100644 --- a/lib/bitboxnetwork.ts +++ b/lib/bitboxnetwork.ts @@ -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; } diff --git a/package-lock.json b/package-lock.json index 34e59752..c5b4bedd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "slpjs", - "version": "0.25.5", + "version": "0.25.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 899dc452..34f6d416 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/test/bchdnetwork.test.ts b/test/bchdnetwork.test.ts new file mode 100644 index 00000000..03c41e7b --- /dev/null +++ b/test/bchdnetwork.test.ts @@ -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); } + }); + }); +}); diff --git a/test/bitboxnetwork.test.ts b/test/bitboxnetwork.test.ts index d940a072..4b151158 100644 --- a/test/bitboxnetwork.test.ts +++ b/test/bitboxnetwork.test.ts @@ -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, @@ -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); } }); }); -}); \ No newline at end of file +});