From 45d53d589a2cbc59a8bc1e86445d06114575f021 Mon Sep 17 00:00:00 2001 From: Gabriel Cardona Date: Mon, 25 Feb 2019 12:19:41 +0900 Subject: [PATCH 1/2] Burn method. --- index.js | 2 +- lib/TokenType1.js | 44 ++++++++++++++++++++++++++++++ package-lock.json | 30 ++++++++++++++------- package.json | 6 ++--- src/TokenType1.ts | 47 ++++++++++++++++++++++++++++++++- src/interfaces/SLPInterfaces.ts | 8 ++++++ 6 files changed, 123 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 7288f0e..6da1f37 100755 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ const repl = require("repl") const SLP = require("./lib/SLP").default const clone = require("git-clone") -program.version("2.0.0", "-v, --version") +program.version("2.1.0", "-v, --version") program .command("new ") diff --git a/lib/TokenType1.js b/lib/TokenType1.js index c52fb4c..b6b9d4b 100644 --- a/lib/TokenType1.js +++ b/lib/TokenType1.js @@ -258,6 +258,50 @@ var TokenType1 = /** @class */ (function () { }); }); }; + TokenType1.prototype.burn = function (burnConfig) { + return __awaiter(this, void 0, void 0, function () { + var tmpBITBOX_2, getRawTransactions, slpValidator, bitboxNetwork, fundingAddress, bchChangeReceiverAddress, tokenInfo, tokenDecimals, balances, amount, inputUtxos, burnTxid, error_2; + var _this = this; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + _a.trys.push([0, 4, , 5]); + tmpBITBOX_2 = this.returnBITBOXInstance(burnConfig.fundingAddress); + getRawTransactions = function (txids) { return __awaiter(_this, void 0, void 0, function () { + return __generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, tmpBITBOX_2.RawTransactions.getRawTransaction(txids)]; + case 1: return [2 /*return*/, _a.sent()]; + } + }); + }); }; + slpValidator = new slpjs.LocalValidator(tmpBITBOX_2, getRawTransactions); + bitboxNetwork = new slpjs.BitboxNetwork(tmpBITBOX_2, slpValidator); + fundingAddress = addy.toSLPAddress(burnConfig.fundingAddress); + bchChangeReceiverAddress = addy.toSLPAddress(burnConfig.bchChangeReceiverAddress); + return [4 /*yield*/, bitboxNetwork.getTokenInformation(burnConfig.tokenId)]; + case 1: + tokenInfo = _a.sent(); + tokenDecimals = tokenInfo.decimals; + return [4 /*yield*/, bitboxNetwork.getAllSlpBalancesAndUtxos(fundingAddress)]; + case 2: + balances = _a.sent(); + amount = new BigNumber(burnConfig.amount).times(Math.pow(10, tokenDecimals)); + inputUtxos = balances.slpTokenUtxos[burnConfig.tokenId]; + inputUtxos = inputUtxos.concat(balances.nonSlpUtxos); + inputUtxos.forEach(function (txo) { return (txo.wif = burnConfig.fundingWif); }); + return [4 /*yield*/, bitboxNetwork.simpleTokenBurn(burnConfig.tokenId, amount, inputUtxos, bchChangeReceiverAddress)]; + case 3: + burnTxid = _a.sent(); + return [2 /*return*/, burnTxid[0]]; + case 4: + error_2 = _a.sent(); + return [2 /*return*/, error_2]; + case 5: return [2 /*return*/]; + } + }); + }); + }; TokenType1.prototype.returnNetwork = function (address) { return addy.detectAddressNetwork(address); }; diff --git a/package-lock.json b/package-lock.json index d3ad24d..b073f21 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "slp-sdk", - "version": "2.0.0", + "version": "2.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -389,6 +389,11 @@ "lodash.get": "^4.4.2" } }, + "@types/lodash": { + "version": "4.14.121", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.121.tgz", + "integrity": "sha512-ORj7IBWj13iYufXt/VXrCNMbUuCTJfhzme5kx9U/UtcIPdJYuvPDUAlHlbNhz/8lKCLy9XGIZnGrqXOtQbPGoQ==" + }, "@types/node": { "version": "10.12.26", "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.26.tgz", @@ -1709,9 +1714,9 @@ "integrity": "sha1-nGZalfiLiwj8Bc/XMfVhhZ1yWCU=" }, "bignumber.js": { - "version": "7.2.1", - "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-7.2.1.tgz", - "integrity": "sha512-S4XzBk5sMB+Rcb/LNcpzXr57VRTxgAvaAEDAl1AwRx27j00hT84O6OkteE7u8UB3NuaaygCRrEpqox4uDOrbdQ==" + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-8.1.1.tgz", + "integrity": "sha512-QD46ppGintwPGuL1KqmwhR0O+N2cZUg8JG/VzwI2e28sM9TqHjQB10lI4QAaMHVbLzwVLLAwEglpKPViWX+5NQ==" }, "binary-extensions": { "version": "1.13.0", @@ -14480,14 +14485,16 @@ } }, "slpjs": { - "version": "0.13.0", - "resolved": "https://registry.npmjs.org/slpjs/-/slpjs-0.13.0.tgz", - "integrity": "sha512-//aypDS2uId/qQPILWeWIVT5SLTzxXcha4URnuqqqSzIW/S7EFhDAD5Ea7PZ33Yct6PGkIMRuYUBFe/YtolLrw==", + "version": "0.13.2", + "resolved": "https://registry.npmjs.org/slpjs/-/slpjs-0.13.2.tgz", + "integrity": "sha512-zPrv4t3YaWpqieqWtPhk/jozSxKEhcEK9rQfTzmD1cGwTdsLOoN3ojKqDx9ACCx1cJEK1DJhiuaLPFap2+kE7Q==", "requires": { + "@types/lodash": "^4.14.120", "axios": "^0.18.0", "bchaddrjs-slp": "git://github.com/simpleledger/bchaddrjs.git#master", - "bignumber.js": "^7.2.1", - "bitcore-lib-cash": "^0.19.0" + "bignumber.js": "^8.0.2", + "bitcore-lib-cash": "^0.19.0", + "lodash": "^4.17.11" }, "dependencies": { "axios": { @@ -14498,6 +14505,11 @@ "follow-redirects": "^1.3.0", "is-buffer": "^1.1.5" } + }, + "bignumber.js": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-8.1.1.tgz", + "integrity": "sha512-QD46ppGintwPGuL1KqmwhR0O+N2cZUg8JG/VzwI2e28sM9TqHjQB10lI4QAaMHVbLzwVLLAwEglpKPViWX+5NQ==" } } }, diff --git a/package.json b/package.json index 5dde460..1fd3665 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "slp-sdk", - "version": "2.0.0", + "version": "2.1.0", "description": "SLP SDK powered by BITBOX", "main": "index.js", "scripts": { @@ -30,7 +30,7 @@ "dependencies": { "axios": "^0.17.1", "babel-register": "^6.26.0", - "bignumber.js": "^7.2.1", + "bignumber.js": "^8.0.2", "bitbox-sdk": "4.0.0", "chalk": "^2.3.0", "clear": "0.1.0", @@ -40,7 +40,7 @@ "mkdirp": "^0.5.1", "node-emoji": "^1.8.1", "repl.history": "^0.1.4", - "slpjs": "0.13.0", + "slpjs": "0.13.2", "touch": "^3.1.0" }, "devDependencies": { diff --git a/src/TokenType1.ts b/src/TokenType1.ts index 3c247ee..ed3503c 100644 --- a/src/TokenType1.ts +++ b/src/TokenType1.ts @@ -8,7 +8,8 @@ import { ICreateConfig, IMintConfig, ISendConfig, - IBurnAllConfig + IBurnAllConfig, + IBurnConfig } from "./interfaces/SLPInterfaces" // import classes @@ -278,6 +279,50 @@ class TokenType1 { } } + async burn(burnConfig: IBurnConfig) { + try { + let tmpBITBOX: any = this.returnBITBOXInstance(burnConfig.fundingAddress) + + const getRawTransactions = async (txids: any) => { + return await tmpBITBOX.RawTransactions.getRawTransaction(txids) + } + + const slpValidator: any = new slpjs.LocalValidator( + tmpBITBOX, + getRawTransactions + ) + const bitboxNetwork = new slpjs.BitboxNetwork(tmpBITBOX, slpValidator) + const fundingAddress: string = addy.toSLPAddress( + burnConfig.fundingAddress + ) + const bchChangeReceiverAddress: string = addy.toSLPAddress( + burnConfig.bchChangeReceiverAddress + ) + const tokenInfo = await bitboxNetwork.getTokenInformation( + burnConfig.tokenId + ) + const tokenDecimals = tokenInfo.decimals + const balances = await bitboxNetwork.getAllSlpBalancesAndUtxos( + fundingAddress + ) + let amount = new BigNumber(burnConfig.amount).times(10 ** tokenDecimals) + let inputUtxos = balances.slpTokenUtxos[burnConfig.tokenId] + + inputUtxos = inputUtxos.concat(balances.nonSlpUtxos) + + inputUtxos.forEach((txo: any) => (txo.wif = burnConfig.fundingWif)) + const burnTxid = await bitboxNetwork.simpleTokenBurn( + burnConfig.tokenId, + amount, + inputUtxos, + bchChangeReceiverAddress + ) + return burnTxid[0] + } catch (error) { + return error + } + } + returnNetwork(address: string): string { return addy.detectAddressNetwork(address) } diff --git a/src/interfaces/SLPInterfaces.ts b/src/interfaces/SLPInterfaces.ts index de8914f..e956050 100644 --- a/src/interfaces/SLPInterfaces.ts +++ b/src/interfaces/SLPInterfaces.ts @@ -41,3 +41,11 @@ export interface IBurnAllConfig { tokenId: string bchChangeReceiverAddress: string } + +export interface IBurnConfig { + fundingAddress: string + fundingWif: string + tokenId: string + bchChangeReceiverAddress: string + amount: number +} From da6d795437f09d2d1f292c4cc123f8ada34b3e21 Mon Sep 17 00:00:00 2001 From: Gabriel Cardona Date: Tue, 26 Feb 2019 07:45:23 +0900 Subject: [PATCH 2/2] Bump slpjs v. --- package-lock.json | 11 +++-------- package.json | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index b073f21..6cc1968 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14485,9 +14485,9 @@ } }, "slpjs": { - "version": "0.13.2", - "resolved": "https://registry.npmjs.org/slpjs/-/slpjs-0.13.2.tgz", - "integrity": "sha512-zPrv4t3YaWpqieqWtPhk/jozSxKEhcEK9rQfTzmD1cGwTdsLOoN3ojKqDx9ACCx1cJEK1DJhiuaLPFap2+kE7Q==", + "version": "0.14.0", + "resolved": "https://registry.npmjs.org/slpjs/-/slpjs-0.14.0.tgz", + "integrity": "sha512-TZP8J8LwhXeZGo+IfOck5pNoAWJItrDBRQJLk0BIyEUqUADzfq4SzbMVpR5W26r//Y21fKP5yvvgf8XjzE4gFQ==", "requires": { "@types/lodash": "^4.14.120", "axios": "^0.18.0", @@ -14505,11 +14505,6 @@ "follow-redirects": "^1.3.0", "is-buffer": "^1.1.5" } - }, - "bignumber.js": { - "version": "8.1.1", - "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-8.1.1.tgz", - "integrity": "sha512-QD46ppGintwPGuL1KqmwhR0O+N2cZUg8JG/VzwI2e28sM9TqHjQB10lI4QAaMHVbLzwVLLAwEglpKPViWX+5NQ==" } } }, diff --git a/package.json b/package.json index 1fd3665..58c0ca3 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "mkdirp": "^0.5.1", "node-emoji": "^1.8.1", "repl.history": "^0.1.4", - "slpjs": "0.13.2", + "slpjs": "0.14.0", "touch": "^3.1.0" }, "devDependencies": {