Skip to content

Commit

Permalink
Merge pull request #22 from Bitcoin-com/bump-bitbox-v
Browse files Browse the repository at this point in the history
v2.0.0
  • Loading branch information
cgcardona authored Feb 22, 2019
2 parents e306040 + 6272f7b commit 3f698f1
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 33 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
[SLP SDK](https://developer.bitcoin.com/slp) is powered by [BITBOX](https://developer.bitcoin.com/bitbox).

## Installation and Usage

- Ensure you have node.js v10 or higher installed.

- Clone this repository

```bash
git clone https://github.com/Bitcoin-com/slp-sdk
cd slp-sdk
Expand All @@ -26,5 +28,6 @@ cd slp-sdk
- [SLP specification unit tests](https://github.com/simpleledger/slp-unit-test-data) (Python)

### Token-Aware Wallets

- [Badger Wallet](https://badger.bitcoin.com/)
- [Electron Cash SLP](https://github.com/simpleledger/Electron-Cash-SLP)
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const repl = require("repl")
const SLP = require("./lib/SLP").default
const clone = require("git-clone")

program.version("1.3.1", "-v, --version")
program.version("2.0.0", "-v, --version")

program
.command("new <name>")
Expand Down
31 changes: 24 additions & 7 deletions lib/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,44 @@ var Utils = /** @class */ (function () {
}
Utils.prototype.list = function (id) {
return __awaiter(this, void 0, void 0, function () {
var path, response, error_1;
var path, method, response, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!id)
if (!id) {
method = "get";
path = this.restURL + "slp/list";
else
}
else if (typeof id === "string") {
method = "get";
path = this.restURL + "slp/list/" + id;
}
else if (typeof id === "object") {
method = "post";
path = this.restURL + "slp/list";
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
_a.trys.push([1, 6, , 7]);
response = void 0;
if (!(method === "get")) return [3 /*break*/, 3];
return [4 /*yield*/, axios_1.default.get(path)];
case 2:
response = _a.sent();
return [2 /*return*/, response.data];
case 3:
return [3 /*break*/, 5];
case 3: return [4 /*yield*/, axios_1.default.post(path, {
tokenIds: id
})];
case 4:
response = _a.sent();
_a.label = 5;
case 5: return [2 /*return*/, response.data];
case 6:
error_1 = _a.sent();
if (error_1.response && error_1.response.data)
throw error_1.response.data;
throw error_1;
case 4: return [2 /*return*/];
case 7: return [2 /*return*/];
}
});
});
Expand Down
38 changes: 19 additions & 19 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slp-sdk",
"version": "1.3.1",
"version": "2.0.0",
"description": "SLP SDK powered by BITBOX",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -31,7 +31,7 @@
"axios": "^0.17.1",
"babel-register": "^6.26.0",
"bignumber.js": "^7.2.1",
"bitbox-sdk": "^3.1.0",
"bitbox-sdk": "4.0.0",
"chalk": "^2.3.0",
"clear": "0.1.0",
"commander": "^2.13.0",
Expand Down
24 changes: 20 additions & 4 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,29 @@ class Utils {
this.restURL = restURL
}

async list(id: string): Promise<Object | Array<Object>> {
async list(id?: string | string[]): Promise<Object | Array<Object>> {
let path: string
if (!id) path = `${this.restURL}slp/list`
else path = `${this.restURL}slp/list/${id}`
let method: string
if (!id) {
method = "get"
path = `${this.restURL}slp/list`
} else if (typeof id === "string") {
method = "get"
path = `${this.restURL}slp/list/${id}`
} else if (typeof id === "object") {
method = "post"
path = `${this.restURL}slp/list`
}

try {
const response = await axios.get(path)
let response: any
if (method === "get") {
response = await axios.get(path)
} else {
response = await axios.post(path, {
tokenIds: id
})
}
return response.data
} catch (error) {
if (error.response && error.response.data) throw error.response.data
Expand Down
29 changes: 29 additions & 0 deletions test/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ describe("#Utils", () => {
])
assert.equal(list.id, tokenId)
})

it(`should list multople SLP tokens by array of ids`, async () => {
// Mock the call to rest.bitcoin.com
if (process.env.TEST === "unit") {
nock(SERVER)
.post(uri => uri.includes("/"))
.reply(200, mockData.mockTokens)
}

const tokenIds = [
"4276533bb702e7f8c9afd8aa61ebf016e95011dc3d54e55faa847ac1dd461e84",
"b3f4f132dc3b9c8c96316346993a8d54d729715147b7b11aa6c8cd909e955313"
]

const list = await SLP.Utils.list(tokenIds)
// console.log(`list: ${JSON.stringify(list, null, 2)}`)

assert2.hasAllKeys(list[0], [
"id",
"timestamp",
"symbol",
"name",
"documentUri",
"documentHash",
"decimals",
"initialTokenQty"
])
assert.equal(list[0].id, tokenIds[0])
})
})

describe("#balancesForAddress", () => {
Expand Down
24 changes: 24 additions & 0 deletions test/fixtures/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ const mockToken = {
initialTokenQty: 10000000000000000
}

const mockTokens = [
{
id: "4276533bb702e7f8c9afd8aa61ebf016e95011dc3d54e55faa847ac1dd461e84",
timestamp: "2018-08-25 03:54",
symbol: "USDT",
name: "US Dollar Tether",
documentUri: "",
documentHash: "",
decimals: 0,
initialTokenQty: 10000000000000000
},
{
id: "b3f4f132dc3b9c8c96316346993a8d54d729715147b7b11aa6c8cd909e955313",
timestamp: "2019-01-30 21:56",
symbol: "SLPJS",
name: "Awesome SLPJS README Token",
documentUri: "[email protected]",
documentHash: "",
decimals: 2,
initialTokenQty: 1000000
}
]

const balancesForAddress = [
{
tokenId: "df808a41672a0a0ae6475b44f272a107bc9961b90f29dc918d71301f24fe92fb",
Expand Down Expand Up @@ -95,6 +118,7 @@ const mockIsValidTxid = [
module.exports = {
mockList,
mockToken,
mockTokens,
balancesForAddress,
mockRawTx,
mockIsValidTxid
Expand Down

0 comments on commit 3f698f1

Please sign in to comment.